X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Ffunction%2Fbrowser%2FBrowserLauncher.java;fp=tim%2Fprune%2Ffunction%2Fbrowser%2FBrowserLauncher.java;h=1decbc3f586de321d7208dad537804af297d4da6;hb=54b9d8bc8f0025ccf97a67d9dd217ef1f9cf082f;hp=0000000000000000000000000000000000000000;hpb=52bf9e8686c916be37a26a0b75340393d4478b05;p=GpsPrune.git diff --git a/tim/prune/function/browser/BrowserLauncher.java b/tim/prune/function/browser/BrowserLauncher.java new file mode 100644 index 0000000..1decbc3 --- /dev/null +++ b/tim/prune/function/browser/BrowserLauncher.java @@ -0,0 +1,101 @@ +package tim.prune.function.browser; + +import javax.swing.JOptionPane; + + +/** + * Class to launch a browser window to show an external map + */ +public abstract class BrowserLauncher +{ + private static boolean _initialised = false; + private static String[] _browserCommand = null; + private static boolean _urlNeedsQuotes = false; + + + /** + * Init method to set up browser + */ + private static void init() + { + // First check if "which" command is available + if (commandExists("which")) + { + // which exists, so try browsers in turn + String[] browsersToTry = {"firefox", "iceweasel", "konqueror", "opera", "epiphany", "mozilla", "safari", "lynx"}; + String browserFound = null; + for (int i=0; i= 0; + if (isMacOsx) { + // for Mac Osx just use "open" command + _browserCommand = new String[] {"open", null}; + } + else { + // assume it's not linux or mac, so try windows method using "start" command + _browserCommand = new String[] {"cmd.exe", "/C", "start", "\"\"", null}; + _urlNeedsQuotes = true; + } + } + _initialised = true; + } + + + /** + * Check if the specified command exists on the system + * @param inCommand command to check + * @return true if the command exists + */ + private static boolean commandExists(String inCommand) + { + try + { + String[] commands = {"which", inCommand}; + if (Runtime.getRuntime().exec(commands).waitFor() == 0) + { + return true; + } + } + catch (Exception e) {} // failed + return false; + } + + + /** + * Launch a browser window to show the given url + * @param inUrl url to show + */ + public static void launchBrowser(String inUrl) + { + if (!_initialised) {init();} + if (_browserCommand == null) { + JOptionPane.showMessageDialog(null, "Cannot show url: " + inUrl); + } + else + { + try + { + // enclose url in quotes if necessary + String url = inUrl; + if (_urlNeedsQuotes) {url = "\"" + url + "\"";} + // Fill in url in last element of coommand array + _browserCommand[_browserCommand.length - 1] = url; + // execute command to launch browser + Runtime.getRuntime().exec(_browserCommand); + } + catch (Exception e) { + JOptionPane.showMessageDialog(null, "Failed to show url: " + inUrl); + } + } + } +}