KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > ui > BrowserLauncher


1 package prefuse.util.ui;
2
3 import java.io.IOException JavaDoc;
4 import java.net.URL JavaDoc;
5
6 /**
7  * <p>
8  * Browser launcher will open a URL in an external browser on your system.
9  * (e.g. Internet Explorer or Netscape). If your browser is already open,
10  * a new browser window should be created without starting any new processes.
11  * </p>
12  *
13  * <p>
14  * On Windows systems the system's default browser will be used. On UNIX and other
15  * platforms the browser defaults to Netscape. For this default behavior to work,
16  * the command 'netscape' must be on your path.
17  * </p>
18  *
19  * <p>
20  * This class was inspired by an article at www.javaworld.com by Steven Spencer.
21  * The article is available at
22  * <a HREF="http://www.javaworld.com/javaworld/javatips/jw-javatip66.html">
23  * http://www.javaworld.com/javaworld/javatips/jw-javatip66.html
24  * </a>.
25  * </p>
26  *
27  * @author Steven Spencer
28  * @author jeffrey heer (adapted original author's code)
29  */

30 public abstract class BrowserLauncher {
31
32     private static final String JavaDoc WIN_ID = "Windows";
33     private static final String JavaDoc WIN_PATH = "rundll32";
34     private static final String JavaDoc WIN_FLAG = "url.dll,FileProtocolHandler";
35     private static final String JavaDoc UNIX_PATH = "netscape";
36     private static final String JavaDoc UNIX_FLAG = "-remote openURL";
37
38     /**
39      * Display a file in the system browser.
40      * @param url the file's url
41      */

42     public static void showDocument(URL JavaDoc url) {
43         showDocument(url.toString());
44     }
45
46     /**
47      * Display a file in the system browser. If you want to display a
48      * file, you must include the absolute path name.
49      * @param url the file's url (the url must start with either
50      * "http://" or "file://").
51      */

52     public static void showDocument(String JavaDoc url) {
53         boolean windows = isWindowsPlatform();
54         String JavaDoc cmd = null;
55         try {
56             if (windows) {
57                 // cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
58
cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
59                 Runtime.getRuntime().exec(cmd);
60             } else {
61                 // Under Unix, Netscape has to be running for the "-remote"
62
// command to work. So, we try sending the command and
63
// check for an exit value. If the exit command is 0,
64
// it worked, otherwise we need to start the browser.
65
// cmd = 'netscape -remote openURL(http://www.javaworld.com)'
66
cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + url + ")";
67                 Process JavaDoc p = Runtime.getRuntime().exec(cmd);
68                 try {
69                     // wait for exit code -- if it's 0, command worked,
70
// otherwise we need to start the browser up.
71
int exitCode = p.waitFor();
72                     if (exitCode != 0) {
73                         // Command failed, start up the browser
74
// cmd = 'netscape http://www.javaworld.com'
75
cmd = UNIX_PATH + " " + url;
76                         p = Runtime.getRuntime().exec(cmd);
77                     }
78                 } catch (InterruptedException JavaDoc x) {
79                     System.err.println(
80                         "Error bringing up browser, cmd='" + cmd + "'");
81                     System.err.println("Caught: " + x);
82                 }
83             }
84         } catch (IOException JavaDoc x) {
85             // couldn't exec browser
86
System.err.println("Could not invoke browser, command=" + cmd);
87             System.err.println("Caught: " + x);
88         }
89     }
90
91     /**
92      * Try to determine whether this application is running under Windows
93      * or some other platform by examing the "os.name" property.
94      * @return true if this application is running under a Windows OS
95      */

96     public static boolean isWindowsPlatform() {
97         String JavaDoc os = System.getProperty("os.name");
98         return (os != null && os.startsWith(WIN_ID));
99     }
100
101     /**
102      * Opens the URL specified on the command line in
103      * the system browser.
104      * @param argv argument array. Only the first argument is considered.
105      */

106     public static void main(String JavaDoc[] argv) {
107         showDocument(argv[0]);
108     }
109
110 } // end of class BrowserLauncher
111
Popular Tags