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 boolean isMacOsx = System.getProperty("os.name").toLowerCase().indexOf("mac os") >= 0;
41 // for Mac Osx just use "open" command
42 _browserCommand = new String[] {"open", null};
45 // assume it's not linux or mac, so try windows method using "start" command
46 _browserCommand = new String[] {"cmd.exe", "/C", "start", "\"\"", null};
47 _urlNeedsQuotes = true;
55 * Check if the specified command exists on the system
56 * @param inCommand command to check
57 * @return true if the command exists
59 private static boolean commandExists(String inCommand)
63 String[] commands = {"which", inCommand};
64 if (Runtime.getRuntime().exec(commands).waitFor() == 0)
69 catch (Exception e) {} // failed
75 * Launch a browser window to show the given url
76 * @param inUrl url to show
78 public static void launchBrowser(String inUrl)
80 if (!_initialised) {init();}
81 if (_browserCommand == null) {
82 JOptionPane.showMessageDialog(null, "Cannot show url: " + inUrl);
88 // enclose url in quotes if necessary
90 if (_urlNeedsQuotes) {url = "\"" + url + "\"";}
91 // Fill in url in last element of coommand array
92 _browserCommand[_browserCommand.length - 1] = url;
93 // execute command to launch browser
94 Runtime.getRuntime().exec(_browserCommand);
97 JOptionPane.showMessageDialog(null, "Failed to show url: " + inUrl);