]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/browser/BrowserLauncher.java
Version 7, February 2009
[GpsPrune.git] / tim / prune / function / browser / BrowserLauncher.java
1 package tim.prune.function.browser;
2
3 import javax.swing.JOptionPane;
4
5
6 /**
7  * Class to launch a browser window to show an external map
8  */
9 public abstract class BrowserLauncher
10 {
11         private static boolean _initialised = false;
12         private static String[] _browserCommand = null;
13         private static boolean _urlNeedsQuotes = false;
14
15
16         /**
17          * Init method to set up browser
18          */
19         private static void init()
20         {
21                 // First check if "which" command is available
22                 if (commandExists("which"))
23                 {
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++)
28                         {
29                                 if (commandExists(browsersToTry[i]))
30                                         browserFound = browsersToTry[i];
31                         }
32                         if (browserFound != null) {
33                                 _browserCommand = new String[] {browserFound, null};
34                         }
35                 }
36                 else
37                 {
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;
40                         if (isMacOsx) {
41                                 // for Mac Osx just use "open" command
42                                 _browserCommand = new String[] {"open", null};
43                         }
44                         else {
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;
48                         }
49                 }
50                 _initialised = true;
51         }
52
53
54         /**
55          * Check if the specified command exists on the system
56          * @param inCommand command to check
57          * @return true if the command exists
58          */
59         private static boolean commandExists(String inCommand)
60         {
61                 try
62                 {
63                         String[] commands = {"which", inCommand};
64                         if (Runtime.getRuntime().exec(commands).waitFor() == 0)
65                         {
66                                 return true;
67                         }
68                 }
69                 catch (Exception e) {} // failed
70                 return false;
71         }
72
73
74         /**
75          * Launch a browser window to show the given url
76          * @param inUrl url to show
77          */
78         public static void launchBrowser(String inUrl)
79         {
80                 if (!_initialised) {init();}
81                 if (_browserCommand == null) {
82                         JOptionPane.showMessageDialog(null, "Cannot show url: " + inUrl);
83                 }
84                 else
85                 {
86                         try
87                         {
88                                 // enclose url in quotes if necessary
89                                 String url = inUrl;
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);
95                         }
96                         catch (Exception e) {
97                                 JOptionPane.showMessageDialog(null, "Failed to show url: " + inUrl);
98                         }
99                 }
100         }
101 }