File: BrowserLauncher.java

package info (click to toggle)
gpsprune 10-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,220 kB
  • ctags: 3,013
  • sloc: java: 22,662; sh: 23; makefile: 16; python: 15
file content (103 lines) | stat: -rw-r--r-- 2,643 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package tim.prune.function.browser;

import javax.swing.JOptionPane;


/**
 * Class to launch a browser window to show an external map
 */
public abstract class BrowserLauncher
{
	private static boolean _initialised = false;
	private static String[] _browserCommand = null;
	private static boolean _urlNeedsQuotes = false;


	/**
	 * Init method to set up browser
	 */
	private static void init()
	{
		// First check if "which" command is available
		if (commandExists("which"))
		{
			// which exists, so try browsers in turn
			String[] browsersToTry = {"firefox", "iceweasel", "konqueror", "opera", "epiphany", "mozilla", "safari", "lynx"};
			String browserFound = null;
			for (int i=0; i<browsersToTry.length && browserFound == null; i++)
			{
				if (commandExists(browsersToTry[i]))
					browserFound = browsersToTry[i];
			}
			if (browserFound != null) {
				_browserCommand = new String[] {browserFound, null};
			}
		}
		else
		{
			// no which command, so check if os name looks like a mac
			String osName = System.getProperty("os.name").toLowerCase();
			boolean isMacOsx = osName.indexOf("mac os") >= 0
			 || osName.indexOf("darwin") >= 0;
			if (isMacOsx) {
				// for Mac Osx just use "open" command
				_browserCommand = new String[] {"open", null};
			}
			else {
				// assume it's not linux or mac, so try windows method using "start" command
				_browserCommand = new String[] {"cmd.exe", "/C", "start", "\"\"", null};
				_urlNeedsQuotes = true;
			}
		}
		_initialised = true;
	}


	/**
	 * Check if the specified command exists on the system
	 * @param inCommand command to check
	 * @return true if the command exists
	 */
	private static boolean commandExists(String inCommand)
	{
		try
		{
			String[] commands = {"which", inCommand};
			if (Runtime.getRuntime().exec(commands).waitFor() == 0)
			{
				return true;
			}
		}
		catch (Exception e) {} // failed
		return false;
	}


	/**
	 * Launch a browser window to show the given url
	 * @param inUrl url to show
	 */
	public static void launchBrowser(String inUrl)
	{
		if (!_initialised) {init();}
		if (_browserCommand == null) {
			JOptionPane.showMessageDialog(null, "Cannot show url: " + inUrl);
		}
		else
		{
			try
			{
				// enclose url in quotes if necessary
				String url = inUrl;
				if (_urlNeedsQuotes) {url = "\"" + url + "\"";}
				// Fill in url in last element of coommand array
				_browserCommand[_browserCommand.length - 1] = url;
				// execute command to launch browser
				Runtime.getRuntime().exec(_browserCommand);
			}
			catch (Exception e) {
				JOptionPane.showMessageDialog(null, "Failed to show url: " + inUrl);
			}
		}
	}
}