]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/browser/BrowserLauncher.java
7c6fa58056a19279ab7306599c711268412ab45c
[GpsPrune.git] / src / 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", "chromium", "midori", "safari", "lynx"};
30                         String browserFound = null;
31                         for (String browser : browsersToTry)
32                         {
33                                 if (commandExists(browser))
34                                 {
35                                         browserFound = browser;
36                                         break;
37                                 }
38                         }
39                         if (browserFound != null) {
40                                 _browserCommand = new String[] {browserFound, null};
41                         }
42                 }
43
44                 if (_browserCommand == null)
45                 {
46                         // no which command (or none of the browsers found), so check if os name looks like a mac
47                         String osName = System.getProperty("os.name").toLowerCase();
48                         boolean isMacOsx = osName.indexOf("mac os") >= 0 || osName.indexOf("darwin") >= 0;
49                         if (isMacOsx) {
50                                 // for Mac Osx just use "open" command
51                                 _browserCommand = new String[] {"open", null};
52                         }
53                         else {
54                                 // assume it's not linux or mac, so try windows method using "start" command
55                                 _browserCommand = new String[] {"cmd.exe", "/C", "start", "\"\"", null};
56                                 _urlNeedsQuotes = true;
57                         }
58                 }
59                 _initialised = true;
60         }
61
62
63         /**
64          * Check if the specified command exists on the system
65          * @param inCommand command to check
66          * @return true if the command exists
67          */
68         private static boolean commandExists(String inCommand)
69         {
70                 try
71                 {
72                         String[] commands = {"which", inCommand};
73                         if (Runtime.getRuntime().exec(commands).waitFor() == 0)
74                         {
75                                 return true;
76                         }
77                 }
78                 catch (Exception e) {} // failed
79                 return false;
80         }
81
82
83         /**
84          * Launch a browser window to show the given url
85          * @param inUrl url to show
86          */
87         public static void launchBrowser(String inUrl)
88         {
89                 if (inUrl == null) {return;}
90                 // First choice is to try the Desktop library from java 6, if available
91                 try {
92                         Class<?> d = Class.forName("java.awt.Desktop");
93                         d.getDeclaredMethod("browse", new Class[] {URI.class}).invoke(
94                                 d.getDeclaredMethod("getDesktop").invoke(null), new Object[] {URI.create(inUrl)});
95                         //above code mimics: Desktop.getDesktop().browse(URI.create(inUrl));
96                 }
97                 catch (Exception ignore)
98                 {
99                         // The Desktop call failed, need to try backup methods
100                         if (!_initialised) {init();}
101                         if (_browserCommand == null) {
102                                 JOptionPane.showMessageDialog(null, "Cannot show url: " + inUrl);
103                         }
104                         else
105                         {
106                                 try
107                                 {
108                                         // enclose url in quotes if necessary
109                                         String url = inUrl;
110                                         if (_urlNeedsQuotes) {url = "\"" + url + "\"";}
111                                         // Fill in url in last element of command array
112                                         _browserCommand[_browserCommand.length - 1] = url;
113                                         // execute command to launch browser
114                                         Runtime.getRuntime().exec(_browserCommand);
115                                 }
116                                 catch (Exception e) {
117                                         JOptionPane.showMessageDialog(null, "Failed to show url: " + inUrl);
118                                 }
119                         }
120                 }
121         }
122 }