|
1 /** |
|
2 * $Id: BrowserLaunch.java 125 2008-04-22 08:06:08Z mbroeker $ |
|
3 * $URL: http://localhost/svn/eclipse/Schachspiel/trunk/org/homelinux/largo/utils/BrowserLaunch.java $ |
|
4 */ |
|
5 |
|
6 package org.homelinux.largo.utils; |
|
7 |
|
8 import java.lang.reflect.Method; |
|
9 |
|
10 import javax.swing.JOptionPane; |
|
11 /** |
|
12 * Cross-Platform Browserlaunch |
|
13 */ |
|
14 public class BrowserLaunch { |
|
15 private static final String errMsg = "Error attempting to launch web browser"; |
|
16 |
|
17 /** |
|
18 * This method opens the URL in a native platform browser |
|
19 */ |
|
20 public void openURL(String url) { |
|
21 String osName = System.getProperty("os.name"); |
|
22 try { |
|
23 if (osName.startsWith("Mac OS")) { |
|
24 Class<?> fileMgr = Class.forName("com.apple.eio.FileManager"); |
|
25 Method openURL = fileMgr.getDeclaredMethod("openURL", |
|
26 new Class[] { String.class }); |
|
27 openURL.invoke(null, new Object[] { url }); |
|
28 } else if (osName.startsWith("Windows")) |
|
29 Runtime.getRuntime().exec( |
|
30 "rundll32 url.dll,FileProtocolHandler " + url); |
|
31 else { |
|
32 String[] browsers = { "firefox", "opera", "konqueror", |
|
33 "epiphany", "mozilla", "netscape" }; |
|
34 String browser = null; |
|
35 for (int count = 0; count < browsers.length && browser == null; count++) |
|
36 if (Runtime.getRuntime().exec( |
|
37 new String[] { "which", browsers[count] }) |
|
38 .waitFor() == 0) |
|
39 browser = browsers[count]; |
|
40 if (browser == null) |
|
41 throw new Exception("Could not find web browser"); |
|
42 else |
|
43 Runtime.getRuntime().exec(new String[] { browser, url }); |
|
44 } |
|
45 } catch (Exception e) { |
|
46 JOptionPane.showMessageDialog(null, errMsg + ":\n" |
|
47 + e.getLocalizedMessage()); |
|
48 } |
|
49 } |
|
50 }; |