]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/browser/BrowserLauncher.java
ecfd83f9b7bea07814dbcff564dcb78b63c2af09
[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                         String osName = System.getProperty("os.name").toLowerCase();
40                         boolean isMacOsx = osName.indexOf("mac os") >= 0
41                          || osName.indexOf("darwin") >= 0;
42                         if (isMacOsx) {
43                                 // for Mac Osx just use "open" command
44                                 _browserCommand = new String[] {"open", null};
45                         }
46                         else {
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;
50                         }
51                 }
52                 _initialised = true;
53         }
54
55
56         /**
57          * Check if the specified command exists on the system
58          * @param inCommand command to check
59          * @return true if the command exists
60          */
61         private static boolean commandExists(String inCommand)
62         {
63                 try
64                 {
65                         String[] commands = {"which", inCommand};
66                         if (Runtime.getRuntime().exec(commands).waitFor() == 0)
67                         {
68                                 return true;
69                         }
70                 }
71                 catch (Exception e) {} // failed
72                 return false;
73         }
74
75
76         /**
77          * Launch a browser window to show the given url
78          * @param inUrl url to show
79          */
80         public static void launchBrowser(String inUrl)
81         {
82                 if (!_initialised) {init();}
83                 if (_browserCommand == null) {
84                         JOptionPane.showMessageDialog(null, "Cannot show url: " + inUrl);
85                 }
86                 else
87                 {
88                         try
89                         {
90                                 // enclose url in quotes if necessary
91                                 String url = inUrl;
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);
97                         }
98                         catch (Exception e) {
99                                 JOptionPane.showMessageDialog(null, "Failed to show url: " + inUrl);
100                         }
101                 }
102         }
103 }