01 package org.qedeq.gui.se.util;
02
03 import java.lang.reflect.InvocationTargetException;
04
05 import javax.swing.JOptionPane;
06
07 import org.qedeq.base.utility.StringUtility;
08 import org.qedeq.base.utility.YodaUtility;
09
10 /**
11 * <b>Bare Bones Browser Launch for Java</b><br>
12 * Utility class to open a web page from a Swing application
13 * in the user's default browser.<br>
14 * Supports: Mac OS X, GNU/Linux, Unix, Windows XP/Vista/7<br>
15 * Example Usage:<code><br>
16 * String url = "http://www.google.com/";<br>
17 * BareBonesBrowserLaunch.openURL(url);<br></code>
18 * Latest Version: <a href="http://www.centerkey.com/java/browser/">www.centerkey.com/java/browser</a><br>
19 * @author: Dem Pilafian
20 * Public Domain Software -- Free to Use as You Like
21 * @version 3.1, June 6, 2010
22 */
23 public final class BareBonesBrowserLaunch {
24
25 /** Browser list. */
26 private static final String[] BROWSERS = {"google-chrome", "firefox", "opera",
27 "epiphany", "konqueror", "conkeror", "midori", "kazehakase", "mozilla" };
28
29 /** Error message. */
30 private static final String ERR_MSG = "Error attempting to launch web browser";
31
32 /**
33 * Constructor.
34 */
35 private BareBonesBrowserLaunch() {
36 // nothing to do
37 }
38
39 /**
40 * Opens the specified web page in the user's default browser.
41 *
42 * @param url A web address (URL) of a web page (ex: "http://www.google.com/")
43 */
44 public static void openURL(final String url) {
45 if (YodaUtility.existsMethod("java.awt.Desktop", "browse", new Class[] {java.net.URI.class})) {
46 try { //attempt to use Desktop library from JDK 1.6+
47 Class d = Class.forName("java.awt.Desktop");
48 d.getDeclaredMethod("browse", new Class[] {java.net.URI.class}).invoke(
49 d.getDeclaredMethod("getDesktop", new Class[] {}).invoke(null, new Class[] {}),
50 new Object[] {java.net.URI.create(url)});
51 return;
52 //above code mimicks: java.awt.Desktop.getDesktop().browse()
53 } catch (RuntimeException ignore) {
54 //library not available or failed
55 } catch (ClassNotFoundException ignore) {
56 // ignore
57 } catch (IllegalAccessException ignore) {
58 // ignore
59 } catch (InvocationTargetException ignore) {
60 // ignore
61 } catch (NoSuchMethodException ignore) {
62 // ignore
63 }
64 }
65 final String osName = System.getProperty("os.name");
66 try {
67 if (osName.startsWith("Mac OS")) {
68 Class.forName("com.apple.eio.FileManager").getDeclaredMethod(
69 "openURL", new Class[] {String.class}).invoke(null,
70 new Object[] {url});
71 } else if (osName.startsWith("Windows")) {
72 Runtime.getRuntime().exec(
73 "rundll32 url.dll,FileProtocolHandler " + url);
74 } else { //assume Unix or Linux
75 String browser = null;
76 for (int i = 0; i < BROWSERS.length; i++) {
77 String b = BROWSERS[i];
78 if (browser == null && Runtime.getRuntime().exec(new String[]
79 {"which", b}).getInputStream().read() != -1) {
80 Runtime.getRuntime().exec(new String[] {b, url});
81 browser = b;
82 }
83 }
84 if (browser == null) {
85 throw new Exception(StringUtility.toString(BROWSERS));
86 }
87 }
88 } catch (Exception e) {
89 JOptionPane.showMessageDialog(null, ERR_MSG + "\n" + e.toString());
90 }
91 }
92
93 }
|