]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/browser/BrowserLauncher.java
Version 5, May 2008
[GpsPrune.git] / tim / prune / browser / BrowserLauncher.java
1 package tim.prune.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 class BrowserLauncher
10 {
11         private String[] _browserCommand = null;
12         private boolean _urlNeedsQuotes = false;
13
14         /**
15          * Constructor to set up browser
16          */
17         public BrowserLauncher()
18         {
19                 // First check if "which" command is available
20                 if (commandExists("which"))
21                 {
22                         // which exists, so try browsers in turn
23                         String[] browsersToTry = {"firefox", "iceweasel", "konqueror", "opera", "epiphany", "mozilla", "safari", "lynx"};
24                         String browserFound = null;
25                         for (int i=0; i<browsersToTry.length && browserFound == null; i++)
26                         {
27                                 if (commandExists(browsersToTry[i]))
28                                         browserFound = browsersToTry[i];
29                         }
30                         if (browserFound != null) {
31                                 _browserCommand = new String[] {browserFound, null};
32                         }
33                 }
34                 else
35                 {
36                         // no which command, so check if os name looks like a mac
37                         boolean isMacOsx = System.getProperty("os.name").toLowerCase().indexOf("mac os") >= 0;
38                         if (isMacOsx) {
39                                 // for Mac Osx just use "open" command
40                                 _browserCommand = new String[] {"open", null};
41                         }
42                         else {
43                                 // assume it's not linux or mac, so try windows method using "start" command
44                                 _browserCommand = new String[] {"cmd.exe", "/C", "start", "\"\"", null};
45                                 _urlNeedsQuotes = true;
46                         }
47                 }
48         }
49
50         /**
51          * Check if the specified command exists on the system
52          * @param inCommand command to check
53          * @return true if the command exists
54          */
55         private static boolean commandExists(String inCommand)
56         {
57                 try
58                 {
59                         String[] commands = {"which", inCommand};
60                         if (Runtime.getRuntime().exec(commands).waitFor() == 0)
61                         {
62                                 return true;
63                         }
64                 }
65                 catch (Exception e) {} // failed
66                 return false;
67         }
68
69         /**
70          * Launch a browser window to show the given url
71          * @param inUrl url to show
72          */
73         public void launchBrowser(String inUrl)
74         {
75                 if (_browserCommand == null) {
76                         JOptionPane.showMessageDialog(null, "Cannot show url: " + inUrl);
77                 }
78                 else
79                 {
80                         try
81                         {
82                                 // enclose url in quotes if necessary
83                                 String url = inUrl;
84                                 if (_urlNeedsQuotes) {url = "\"" + url + "\"";}
85                                 // Fill in url in last element of coommand array
86                                 _browserCommand[_browserCommand.length - 1] = url;
87                                 // execute command to launch browser
88                                 Runtime.getRuntime().exec(_browserCommand);
89                         }
90                         catch (Exception e) {
91                                 JOptionPane.showMessageDialog(null, "Failed to show url: " + inUrl);
92                         }
93                 }
94         }
95 }