EMMA Coverage Report (generated Sun Apr 20 22:38:01 CEST 2008)
[all classes][net.sf.jomic.tools]

COVERAGE SUMMARY FOR SOURCE FILE [SystemTools.java]

nameclass, %method, %block, %line, %
SystemTools.java100% (1/1)71%  (10/14)30%  (132/442)34%  (29.1/86)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SystemTools100% (1/1)71%  (10/14)30%  (132/442)34%  (29.1/86)
openURL (String): void 0%   (0/1)0%   (0/35)0%   (0/9)
reveal (File): void 0%   (0/1)0%   (0/49)0%   (0/10)
revealOsx (File): void 0%   (0/1)0%   (0/119)0%   (0/21)
runScript (String, String, String, String): void 0%   (0/1)0%   (0/87)0%   (0/14)
setHelpAccelerato (JMenuItem): void 100% (1/1)68%  (17/25)75%  (4.5/6)
SystemTools (): void 100% (1/1)78%  (32/41)88%  (8.8/10)
<static initializer> 100% (1/1)80%  (12/15)80%  (0.8/1)
getJavaInfo (): String 100% (1/1)100% (3/3)100% (1/1)
getLocaleInfo (): String 100% (1/1)100% (19/19)100% (2/2)
getPlatformInfo (): String 100% (1/1)100% (18/18)100% (1/1)
instance (): SystemTools 100% (1/1)100% (8/8)100% (3/3)
isAqua (): boolean 100% (1/1)100% (17/17)100% (6/6)
isMacOSX (): boolean 100% (1/1)100% (3/3)100% (1/1)
isWindows (): boolean 100% (1/1)100% (3/3)100% (1/1)

1// Jomic - a viewer for comic book archives.
2// Copyright (C) 2004-2008 Thomas Aglassinger
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16package net.sf.jomic.tools;
17 
18import java.awt.Toolkit;
19import java.awt.event.KeyEvent;
20import java.io.File;
21import java.io.FileWriter;
22import java.io.IOException;
23import java.io.Writer;
24import java.lang.reflect.Method;
25import java.net.URL;
26import java.net.URLClassLoader;
27import java.util.Locale;
28 
29import javax.swing.JMenuItem;
30import javax.swing.JOptionPane;
31import javax.swing.KeyStroke;
32import javax.swing.UIManager;
33 
34import net.roydesign.mac.MRJAdapter;
35import net.sf.jomic.common.PropertyConstants;
36import net.sf.wraplog.Logger;
37 
38/**
39 *  Utility methods to simplify dealing system dependent issues.
40 *
41 * @author    Thomas Aglassinger
42 */
43public final class SystemTools
44{
45    private static SystemTools instance;
46    private ConsoleTools consoleTools;
47    private boolean isMacOSX;
48    private boolean isWindows;
49    private LocaleTools localeTools;
50    private Logger logger;
51 
52    private SystemTools() {
53        String osName = System.getProperty("os.name").toLowerCase();
54 
55        if (Boolean.getBoolean(PropertyConstants.SYSTEM_PROPERTY_PREFIX + PropertyConstants.IGNORE_OSX)) {
56            isMacOSX = false;
57        } else {
58            isMacOSX = osName.startsWith("mac os x");
59        }
60        isWindows = osName.startsWith("windows");
61        consoleTools = ConsoleTools.instance();
62        localeTools = LocaleTools.instance();
63        logger = Logger.getLogger(SystemTools.class);
64    }
65 
66    /**
67     *  <p>
68     *
69     *  Set the accelerator for the "Help" menu item.</p> <p>
70     *
71     *  <b>Known bug</b> : This does not work with Mac OS X. The Help key would be Command-?, but
72     *  there is no no KeyEvent for "?". There are some hacks, but they don't work for
73     *  internaltional keyboards (like using Shift-/ to get a "?"). For example, on the German
74     *  keyboard the "?" is Shift-&szlig;.
75     */
76    public void setHelpAccelerato(JMenuItem helpMenuItem) {
77        assert helpMenuItem != null;
78        KeyStroke helpKeyStroke;
79 
80        if (isMacOSX()) {
81            // FIXME: the code below should assign Command-? to Help > Help, but does not
82            helpKeyStroke = KeyStroke.getKeyStroke('?', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
83        } else {
84            helpKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0);
85        }
86        helpMenuItem.setAccelerator(helpKeyStroke);
87    }
88 
89    public String getJavaInfo() {
90        return System.getProperty("java.version");
91    }
92 
93    public String getLocaleInfo() {
94        Locale locale = localeTools.getLocale();
95 
96        return locale.getDisplayName() + " (" + locale.toString() + ")";
97    }
98 
99    public String getPlatformInfo() {
100        return System.getProperty("os.name")
101                + "-"
102                + System.getProperty("os.version")
103                + "-"
104                + System.getProperty("os.arch");
105    }
106 
107    /**
108     *  Is Aqua the current look and feel?
109     */
110    public boolean isAqua() {
111        boolean result = false;
112 
113        if (isMacOSX()) {
114            String systemLook = UIManager.getSystemLookAndFeelClassName();
115            String currentLook = UIManager.getLookAndFeel().getClass().getName();
116 
117            result = systemLook.equals(currentLook);
118        }
119        return result;
120    }
121 
122    /**
123     *  Do we run under Mac OS X? <p>
124     *
125     *  For implementation details, see <a
126     *  href="http://developer.apple.com/technotes/tn2002/tn2110.html"> TN2110</a>
127     */
128    public boolean isMacOSX() {
129        return isMacOSX;
130    }
131 
132    /**
133     *  Do we run under some Windows derivate?
134     */
135    public boolean isWindows() {
136        return isWindows;
137    }
138 
139    /**
140     *  Get accessor to unique instance.
141     */
142    public static synchronized SystemTools instance() {
143        if (instance == null) {
144            instance = new SystemTools();
145        }
146        return instance;
147    }
148 
149    /**
150     *  Opens url in web browser.
151     */
152    public void openURL(String url) {
153        assert url != null;
154        try {
155            // TODO: check that MRJAdapter.VERSION >= 1.0.7
156            MRJAdapter.openURL(url);
157        } catch (Exception error) {
158            String message = localeTools.getMessage("warnings.cannotOpenWebPage", url);
159            String title = localeTools.getMessage("dialogs.warning.title", url);
160 
161            logger.warn(message, error);
162            JOptionPane.showMessageDialog(
163                    null, message, title, JOptionPane.WARNING_MESSAGE);
164        }
165    }
166 
167    /**
168     *  Reveals a file or directory in the Systems file browser. For Mac OS, this is the Finder. For
169     *  Windows, this is the Explorer.
170     */
171    public void reveal(File path)
172        throws IOException, InterruptedException {
173        if (isMacOSX()) {
174            revealOsx(path);
175        } else if (isWindows()) {
176            // TODO: Really reveal the file, not only the directory where it is located
177            File dir = path.getParentFile();
178 
179            consoleTools.run(new String[]{"cmd", "/C", "\"start .\""}, dir, 0);
180        } else {
181            // TODO: fix portability
182            String platform = System.getProperty("os.name");
183            String message = localeTools.getMessage("errors.revealMustBeImplemented", platform);
184 
185            throw new IllegalStateException(message);
186        }
187    }
188 
189    /**
190     *  Run script containing <code>scriptText</code>. The script is stored in a temporary file
191     *  having a name with the specified <code>prefix</code> and <code>suffix</code> (without the
192     *  dot).
193     */
194    public void runScript(String prefix, String suffix, String runner, String scriptText)
195        throws IOException, InterruptedException {
196        File scriptFile = File.createTempFile(prefix, "." + suffix);
197 
198        scriptFile.deleteOnExit();
199 
200        Writer out = new FileWriter(scriptFile);
201 
202        if (logger.isDebugEnabled()) {
203            logger.debug("run script: " + scriptFile + ":\n" + scriptText);
204        }
205 
206        out.write(scriptText);
207        out.close();
208 
209        String[] arguments;
210 
211        if (runner == null) {
212            arguments = new String[]{scriptFile.getAbsolutePath()};
213 
214        } else {
215            arguments = new String[]{runner, scriptFile.getAbsolutePath()};
216        }
217 
218        consoleTools.run(arguments, null, 0);
219        if (!scriptFile.delete()) {
220            logger.warn("cannot delete temporary script file: " + scriptFile);
221        }
222    }
223 
224    /**
225     *  Reveal <code>path</code> in Finder.
226     */
227    private void revealOsx(File path)
228        throws IOException {
229        Class nsWorkspace;
230        Exception cause = null;
231        boolean success = false;
232 
233        try {
234            // Source of this code snipplet:
235            // http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=73&t=000010
236            File nsWorkspaceClassFile = new File("/System/Library/Java/com/apple/cocoa/application/NSWorkspace.class");
237 
238            if (nsWorkspaceClassFile.exists()) {
239                // Mac OS X has NSWorkspace, but it is not in the classpath
240                File systemLibraryJavaFolder = new File("/System/Library/Java");
241                ClassLoader classLoader = new URLClassLoader(new URL[]{systemLibraryJavaFolder.toURL()});
242 
243                nsWorkspace = Class.forName("com.apple.cocoa.application.NSWorkspace", true,
244                        classLoader);
245            } else {
246                nsWorkspace = Class.forName("com.apple.cocoa.application.NSWorkspace");
247            }
248 
249            Method sharedWorkspace = nsWorkspace.getMethod("sharedWorkspace", new Class[]{});
250            Object workspace = sharedWorkspace.invoke(null, new Object[]{});
251            Method selectFile = nsWorkspace.getMethod("selectFile", new Class[]{String.class,
252                    String.class});
253 
254            success = ((Boolean) selectFile.invoke(workspace, new Object[]{
255                    path.getAbsolutePath(), path.getAbsolutePath()})).booleanValue();
256        } catch (Exception error) {
257            cause = error;
258        }
259        if (!success || (cause != null)) {
260            String message = localeTools.getMessage("errors.cannotRevealFileInFinder", path.getAbsolutePath());
261            IOException error = new IOExceptionWithCause(message, cause);
262 
263            throw error;
264        }
265    }
266}

[all classes][net.sf.jomic.tools]
EMMA 2.0.4217 (C) Vladimir Roubtsov