1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.ochafik.util;
20 import java.awt.event.ActionEvent;
21 import java.awt.event.ActionListener;
22 import java.awt.event.MouseAdapter;
23 import java.awt.event.MouseEvent;
24 import java.io.File;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.net.URL;
28
29 import javax.swing.Box;
30 import javax.swing.JButton;
31 import javax.swing.JComponent;
32 import javax.swing.JMenuItem;
33 import javax.swing.JOptionPane;
34 import javax.swing.JPopupMenu;
35
36 public class SystemUtils {
37 public static final void runSystemCommand(String[] cmd) throws NoSuchMethodException {
38 try {
39 Runtime.getRuntime().exec(cmd);
40 } catch (Exception ex) {
41 ex.printStackTrace();
42 throw new NoSuchMethodException(ex.toString());
43 }
44 }
45 public static final void runSystemCommand(String cmd) throws NoSuchMethodException {
46 try {
47 Runtime.getRuntime().exec(cmd);
48 } catch (Exception ex) {
49 ex.printStackTrace();
50 throw new NoSuchMethodException(ex.toString());
51 }
52 }
53 static String osName;
54 static {
55
56 osName = System.getProperty("os.name");
57 }
58
59 public static boolean isMacOSX() {
60 return osName.equals("Mac OS X");
61 }
62 public static boolean isWindows() {
63 return osName.indexOf("Windows") >= 0;
64 }
65 public static boolean isLinux() {
66 return osName.indexOf("Linux") >= 0;
67 }
68 public static boolean isSolaris() {
69 return osName.indexOf("Solaris") >= 0 || osName.indexOf("SunOS") >= 0;
70 }
71 public static boolean isUnix() {
72 return File.separatorChar == '/';
73 }
74 public static final void runSystemOpenURL(URL url) throws NoSuchMethodException, IOException {
75 if (isMacOSX()) {
76 runSystemCommand(new String[] {"open",url.toString()});
77 } else if (isWindows()) {
78 runSystemCommand(new String[] {"rundll32","url.dll,FileProtocolHandler", url.toString()});
79 } else if (isLinux()) {
80 if (hasUnixCommand("gnome-open")) {
81 runSystemCommand(new String[] {"gnome-open", url.toString()});
82 } else {
83 runSystemCommand(new String[] {"konqueror", url.toString()});
84 }
85 } else {
86 if (url.getProtocol().equals("file")) {
87 runSystemOpenFileParent(new File(url.getFile()));
88 } else {
89 runSystemCommand(new String[] {"mozilla",url.toString()});
90 }
91 }
92 }
93 public static final boolean hasUnixCommand(String name) {
94 try {
95 Process p = Runtime.getRuntime().exec(new String[] {"which",name});
96 return p.waitFor() == 0;
97 } catch (Exception ex) {
98 ex.printStackTrace();
99 return false;
100 }
101 }
102 public static final void runSystemOpenFileWith(File fileToOpen) throws NoSuchMethodException, IOException {
103 runSystemCommand(new String[] {"RUNDLL32.EXE", "SHELL32.DLL,OpenAs_RunDLL",fileToOpen.getCanonicalPath()});
104 }
105 public static final void runSystemOpenFile(File fileToOpen) throws NoSuchMethodException, IOException {
106 if (isMacOSX()) {
107 runSystemCommand(new String[] {"open",fileToOpen.getCanonicalPath()});
108 } else if (isWindows()) {
109 runSystemCommand(new String[] {"start",fileToOpen.getCanonicalPath()});
110 } else if (isLinux()) {
111 if (hasUnixCommand("gnome-open")) {
112 runSystemCommand(new String[] {"gnome-open", fileToOpen.getCanonicalPath()});
113 } else {
114 runSystemCommand(new String[] {"konqueror", fileToOpen.getCanonicalPath()});
115 }
116 } else if (isSolaris()) {
117 if (fileToOpen.isDirectory()) {
118 runSystemCommand(new String[] {"/usr/dt/bin/dtfile","-folder",fileToOpen.getCanonicalPath()});
119 }
120 }
121 }
122 public static final void runSystemOpenDirectory(File file) throws NoSuchMethodException, IOException {
123 if (isWindows()) {
124 runSystemCommand(new String[] {"explorer", file.getCanonicalPath()});
125 } else {
126 runSystemOpenFile(file);
127 }
128 }
129 public static final void runImageEditor(File imageFile) throws NoSuchMethodException, IOException {
130 if (!imageFile.exists()) throw new FileNotFoundException(imageFile.toString());
131 if (isWindows()) {
132 Runtime.getRuntime().exec(new String[] { "mspaint.exe", imageFile.getAbsolutePath()});
133 } else {
134 throw new NoSuchMethodException("Implement me ! Image editor on " + osName);
135 }
136 }
137 public static final void runSystemOpenFileParent(File fileToShow) throws NoSuchMethodException, IOException {
138 if (isMacOSX()) {
139 runSystemCommand(new String[] {"open",fileToShow.getParentFile().getAbsolutePath()});
140 } else if (isWindows()) {
141 runSystemCommand("explorer /e,/select,\""+fileToShow.getCanonicalPath()+"\"");
142 } else if (isLinux()) {
143 if (hasUnixCommand("gnome-open")) {
144 runSystemCommand(new String[] {"gnome-open", fileToShow.getParentFile().getAbsolutePath()});
145 } else {
146 runSystemCommand(new String[] {"konqueror", fileToShow.getParentFile().getAbsolutePath()});
147 }
148 } else if (isSolaris()) {
149 runSystemCommand(new String[] {"/usr/dt/bin/dtfile","-folder",fileToShow.getParentFile().getCanonicalPath()});
150 }
151 }
152 public static final JComponent createFileShowAndOpenWithPanel(final File file) {
153 String osName=System.getProperty("os.name").toLowerCase();
154 if (osName.indexOf("windows") >= 0) {
155
156 JButton bSee=new JButton("Dir.");
157 JButton bOpenWith=new JButton("Open with...");
158
159 bSee.setToolTipText("<html><body>Browse directory :<br><code>"+file.getParentFile().toString()+"</code></body></html>");
160
161
162
163
164
165
166
167
168
169
170
171
172 bSee.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) {
173 try {
174 runSystemOpenFileParent(file);
175 } catch (IOException ex) {
176 ex.printStackTrace();
177 JOptionPane.showMessageDialog(null, ex.toString(), "Error", JOptionPane.ERROR_MESSAGE);
178 } catch (NoSuchMethodException ex) {
179 ex.printStackTrace();
180 }
181 }});
182 bOpenWith.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) {
183 try {
184 runSystemOpenFileWith(file);
185 } catch (IOException ex) {
186 ex.printStackTrace();
187 JOptionPane.showMessageDialog(null, ex.toString(), "Error", JOptionPane.ERROR_MESSAGE);
188 } catch (NoSuchMethodException ex) {
189 ex.printStackTrace();
190 }
191 }});
192
193 Box box=Box.createHorizontalBox();
194
195 box.add(bSee);
196 box.add(bOpenWith);
197 return box;
198 } else {
199 System.err.println("Does not handle OS '"+osName+"'");
200 return null;
201 }
202 }
203 public static final JPopupMenu createOpenOpenWithShowDirPopupPanel(final File file) {
204 JPopupMenu menu=new JPopupMenu();
205
206
207 JMenuItem miOpen=new JMenuItem("Open");
208 JMenuItem miOpenWith=new JMenuItem("Open with...");
209 JMenuItem miShowDir=new JMenuItem("Open parent directory");
210
211 boolean isWindows = osName.indexOf("windows") >= 0;
212
213 miShowDir.setToolTipText("<html><body>Browse directory :<br><code>"+file.getParentFile().toString()+"</code></body></html>");
214
215
216
217
218
219
220
221
222
223
224
225
226 miShowDir.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) {
227 try {
228 runSystemOpenFileParent(file);
229 } catch (IOException ex) {
230 ex.printStackTrace();
231 JOptionPane.showMessageDialog(null, ex.toString(), "Error", JOptionPane.ERROR_MESSAGE);
232 } catch (NoSuchMethodException ex) {
233 ex.printStackTrace();
234 }
235 }});
236 miOpenWith.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) {
237 try {
238 runSystemOpenFileWith(file);
239 } catch (IOException ex) {
240 ex.printStackTrace();
241 JOptionPane.showMessageDialog(null, ex.toString(), "Error", JOptionPane.ERROR_MESSAGE);
242 } catch (NoSuchMethodException ex) {
243 ex.printStackTrace();
244 }
245 }});
246 miOpen.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) {
247 try {
248 runSystemOpenURL(file.toURI().toURL());
249 } catch (IOException ex) {
250 ex.printStackTrace();
251 JOptionPane.showMessageDialog(null, ex.toString(), "Error", JOptionPane.ERROR_MESSAGE);
252 } catch (NoSuchMethodException ex) {
253 ex.printStackTrace();
254 }
255 }});
256 menu.add(miOpen);
257 if (isWindows)
258 menu.add(miOpenWith);
259
260 menu.addSeparator();
261 menu.add(miShowDir);
262 return menu;
263 }
264 public static final boolean addOpenOpenWithShowDirPopupPanel(final File file, JComponent component) {
265 String osName=System.getProperty("os.name").toLowerCase();
266 if (osName.indexOf("windows") >= 0) {
267 final JPopupMenu menu = createOpenOpenWithShowDirPopupPanel(file);
268 component.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) {
269 if (evt.isPopupTrigger()||evt.getButton() > 1) {
270
271 menu.show(evt.getComponent(),evt.getX(),evt.getY());
272 }
273 }});
274 return true;
275 } else {
276 System.err.println("Does not handle OS '"+osName+"'");
277 return false;
278 }
279 }
280 public static boolean isPopupTrigger(MouseEvent e) {
281 if (e.isPopupTrigger() || e.getButton() != MouseEvent.BUTTON1)
282 return true;
283
284 if (isMacOSX())
285 return e.isControlDown();
286
287 return false;
288 }
289 }