1 package tim.prune.function.browser;
3 import javax.swing.JOptionPane;
7 * Class to launch a browser window to show an external map
9 public abstract class BrowserLauncher
11 private static boolean _initialised = false;
12 private static String[] _browserCommand = null;
13 private static boolean _urlNeedsQuotes = false;
17 * Init method to set up browser
19 private static void init()
21 // First check if "which" command is available
22 if (commandExists("which"))
24 // which exists, so try browsers in turn
25 String[] browsersToTry = {"firefox", "iceweasel", "konqueror", "opera", "epiphany", "mozilla", "safari", "lynx"};
26 String browserFound = null;
27 for (int i=0; i<browsersToTry.length && browserFound == null; i++)
29 if (commandExists(browsersToTry[i]))
30 browserFound = browsersToTry[i];
32 if (browserFound != null) {
33 _browserCommand = new String[] {browserFound, null};
38 // no which command, so check if os name looks like a mac
39 String osName = System.getProperty("os.name").toLowerCase();
40 boolean isMacOsx = osName.indexOf("mac os") >= 0
41 || osName.indexOf("darwin") >= 0;
43 // for Mac Osx just use "open" command
44 _browserCommand = new String[] {"open", null};
47 // assume it's not linux or mac, so try windows method using "start" command
48 _browserCommand = new String[] {"cmd.exe", "/C", "start", "\"\"", null};
49 _urlNeedsQuotes = true;
57 * Check if the specified command exists on the system
58 * @param inCommand command to check
59 * @return true if the command exists
61 private static boolean commandExists(String inCommand)
65 String[] commands = {"which", inCommand};
66 if (Runtime.getRuntime().exec(commands).waitFor() == 0)
71 catch (Exception e) {} // failed
77 * Launch a browser window to show the given url
78 * @param inUrl url to show
80 public static void launchBrowser(String inUrl)
82 if (!_initialised) {init();}
83 if (_browserCommand == null) {
84 JOptionPane.showMessageDialog(null, "Cannot show url: " + inUrl);
90 // enclose url in quotes if necessary
92 if (_urlNeedsQuotes) {url = "\"" + url + "\"";}
93 // Fill in url in last element of coommand array
94 _browserCommand[_browserCommand.length - 1] = url;
95 // execute command to launch browser
96 Runtime.getRuntime().exec(_browserCommand);
99 JOptionPane.showMessageDialog(null, "Failed to show url: " + inUrl);