001 /* 002 Copyright (c) 2009 Olivier Chafik, All Rights Reserved 003 004 This file is part of JNAerator (http://jnaerator.googlecode.com/). 005 006 JNAerator is free software: you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation, either version 3 of the License, or 009 (at your option) any later version. 010 011 JNAerator is distributed in the hope that it will be useful, 012 but WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 GNU General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with JNAerator. If not, see <http://www.gnu.org/licenses/>. 018 */ 019 package com.ochafik.util; 020 import java.awt.event.ActionEvent; 021 import java.awt.event.ActionListener; 022 import java.awt.event.MouseAdapter; 023 import java.awt.event.MouseEvent; 024 import java.io.File; 025 import java.io.FileNotFoundException; 026 import java.io.IOException; 027 import java.net.URL; 028 029 import javax.swing.Box; 030 import javax.swing.JButton; 031 import javax.swing.JComponent; 032 import javax.swing.JMenuItem; 033 import javax.swing.JOptionPane; 034 import javax.swing.JPopupMenu; 035 036 public class SystemUtils { 037 public static final void runSystemCommand(String[] cmd) throws NoSuchMethodException { 038 try { 039 Runtime.getRuntime().exec(cmd); 040 } catch (Exception ex) { 041 ex.printStackTrace(); 042 throw new NoSuchMethodException(ex.toString()); 043 } 044 } 045 public static final void runSystemCommand(String cmd) throws NoSuchMethodException { 046 try { 047 Runtime.getRuntime().exec(cmd); 048 } catch (Exception ex) { 049 ex.printStackTrace(); 050 throw new NoSuchMethodException(ex.toString()); 051 } 052 } 053 static String osName; 054 static { 055 // For possible values, see http://lopica.sourceforge.net/os.html 056 osName = System.getProperty("os.name"); 057 } 058 059 public static boolean isMacOSX() { 060 return osName.equals("Mac OS X"); 061 } 062 public static boolean isWindows() { 063 return osName.indexOf("Windows") >= 0; 064 } 065 public static boolean isLinux() { 066 return osName.indexOf("Linux") >= 0; 067 } 068 public static boolean isSolaris() { 069 return osName.indexOf("Solaris") >= 0 || osName.indexOf("SunOS") >= 0; 070 } 071 public static boolean isUnix() { 072 return File.separatorChar == '/'; 073 } 074 public static final void runSystemOpenURL(URL url) throws NoSuchMethodException, IOException { 075 if (isMacOSX()) { 076 runSystemCommand(new String[] {"open",url.toString()}); 077 } else if (isWindows()) { 078 runSystemCommand(new String[] {"rundll32","url.dll,FileProtocolHandler", url.toString()}); 079 } else if (isLinux()) { 080 if (hasUnixCommand("gnome-open")) { 081 runSystemCommand(new String[] {"gnome-open", url.toString()}); 082 } else { 083 runSystemCommand(new String[] {"konqueror", url.toString()}); 084 } 085 } else { 086 if (url.getProtocol().equals("file")) { 087 runSystemOpenFileParent(new File(url.getFile())); 088 } else { 089 runSystemCommand(new String[] {"mozilla",url.toString()}); 090 } 091 } 092 } 093 public static final boolean hasUnixCommand(String name) { 094 try { 095 Process p = Runtime.getRuntime().exec(new String[] {"which",name}); 096 return p.waitFor() == 0; 097 } catch (Exception ex) { 098 ex.printStackTrace(); 099 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 //JButton bOpen=new JButton("Open"); 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 //bOpen.setToolTipText("<html><body>Open file :<br><code>"+file.toString()+"</code></body></html>"); 161 162 /*bOpen.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { 163 try { 164 runSystemOpenFile(file); 165 } catch (IOException ex) { 166 ex.printStackTrace(); 167 JOptionPane.showMessageDialog(null, ex.toString(), "Error", JOptionPane.ERROR_MESSAGE); 168 } catch (NoSuchMethodException ex) { 169 ex.printStackTrace(); 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 //box.add(bOpen); 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 //JButton bOpen=new JButton("Open"); 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 //bOpen.setToolTipText("<html><body>Open file :<br><code>"+file.toString()+"</code></body></html>"); 215 216 /*bOpen.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { 217 try { 218 runSystemOpenFile(file); 219 } catch (IOException ex) { 220 ex.printStackTrace(); 221 JOptionPane.showMessageDialog(null, ex.toString(), "Error", JOptionPane.ERROR_MESSAGE); 222 } catch (NoSuchMethodException ex) { 223 ex.printStackTrace(); 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 //menu.show((Component)evt.getSource(),evt.getX(),evt.getY()); 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 }