]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/browser/BrowserLauncher.java
Version 11, August 2010
[GpsPrune.git] / tim / prune / function / browser / BrowserLauncher.java
1 package tim.prune.function.browser;
2
3 import java.net.URI;
4 import javax.swing.JOptionPane;
5
6
7 /**
8  * Class to launch a browser window to show an external map
9  * Some code and ideas taken from BareBonesBrowserLaunch at centerkey.com
10  */
11 public abstract class BrowserLauncher
12 {
13         private static boolean _initialised = false;
14         private static String[] _browserCommand = null;
15         private static boolean _urlNeedsQuotes = false;
16
17
18         /**
19          * Init method to set up browser
20          */
21         private static void init()
22         {
23                 _browserCommand = null;
24                 // First check if "which" command is available
25                 if (commandExists("which"))
26                 {
27                         // which exists, so try browsers in turn
28                         String[] browsersToTry = {"firefox", "iceweasel", "konqueror", "opera", "epiphany",
29                                 "mozilla", "safari", "google-chrome", "lynx"};
30                         String browserFound = null;
31                         for (int i=0; i<browsersToTry.length && browserFound == null; i++)
32                         {
33                                 if (commandExists(browsersToTry[i]))
34                                         browserFound = browsersToTry[i];
35                         }
36                         if (browserFound != null) {
37                                 _browserCommand = new String[] {browserFound, null};
38                         }
39                 }
40
41                 if (_browserCommand == null)
42                 {
43                         // no which command (or none of the browsers found), so check if os name looks like a mac
44                         String osName = System.getProperty("os.name").toLowerCase();
45                         boolean isMacOsx = osName.indexOf("mac os") >= 0 || osName.indexOf("darwin") >= 0;
46                         if (isMacOsx) {
47                                 // for Mac Osx just use "open" command
48                                 _browserCommand = new String[] {"open", null};
49                         }
50                         else {
51                                 // assume it's not linux or mac, so try windows method using "start" command
52                                 _browserCommand = new String[] {"cmd.exe", "/C", "start", "\"\"", null};
53                                 _urlNeedsQuotes = true;
54                         }
55                 }
56                 _initialised = true;
57         }
58
59
60         /**
61          * Check if the specified command exists on the system
62          * @param inCommand command to check
63          * @return true if the command exists
64          */
65         private static boolean commandExists(String inCommand)
66         {
67                 try
68                 {
69                         String[] commands = {"which", inCommand};
70                         if (Runtime.getRuntime().exec(commands).waitFor() == 0)
71                         {
72                                 return true;
73                         }
74                 }
75                 catch (Exception e) {} // failed
76                 return false;
77         }
78
79
80         /**
81          * Launch a browser window to show the given url
82          * @param inUrl url to show
83          */
84         public static void launchBrowser(String inUrl)
85         {
86                 // First choice is to try the Desktop library from java 6, if available
87                 try {
88                         Class<?> d = Class.forName("java.awt.Desktop");
89                         d.getDeclaredMethod("browse", new Class[] {URI.class}).invoke(
90                                 d.getDeclaredMethod("getDesktop").invoke(null), new Object[] {URI.create(inUrl)});
91                         //above code mimics: Desktop.getDesktop().browse(URI.create(inUrl));
92                 }
93                 catch (Exception ignore)
94                 {
95                         // The Desktop call failed, need to try backup methods
96                         if (!_initialised) {init();}
97                         if (_browserCommand == null) {
98                                 JOptionPane.showMessageDialog(null, "Cannot show url: " + inUrl);
99                         }
100                         else
101                         {
102                                 try
103                                 {
104                                         // enclose url in quotes if necessary
105                                         String url = inUrl;
106                                         if (_urlNeedsQuotes) {url = "\"" + url + "\"";}
107                                         // Fill in url in last element of command array
108                                         _browserCommand[_browserCommand.length - 1] = url;
109                                         // execute command to launch browser
110                                         Runtime.getRuntime().exec(_browserCommand);
111                                 }
112                                 catch (Exception e) {
113                                         JOptionPane.showMessageDialog(null, "Failed to show url: " + inUrl);
114                                 }
115                         }
116                 }
117         }
118 }