KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hero > client > grapheditor > BrowserControl


1 package hero.client.grapheditor;
2
3 import java.io.IOException JavaDoc;
4
5 /**
6 * A simple, static class to display a URL in the system browser.
7
8
9 *
10 * Under Unix, the system browser is hard-coded to be 'netscape'.
11 * Netscape must be in your PATH for this to work. This has been
12 * tested with the following platforms: AIX, HP-UX and Solaris.
13
14
15 *
16 * Under Windows, this will bring up the default browser under windows,
17 * usually either Netscape or Microsoft IE. The default browser is
18 * determined by the OS. This has been tested under Windows 95/98/NT.
19
20
21 *
22 * Examples:
23
24
25 *
26 BrowserControl.displayURL("http://www.javaworld.com")
27 *
28 BrowserControl.displayURL("file://c:\\docs\\index.html")
29 *
30 BrowserContorl.displayURL("file:///user/joe/index.html");
31 *
32
33 * Note - you must include the url type -- either "http://" or
34 * "file://".
35 */

36 public class BrowserControl
37 {
38 static java.util.ResourceBundle JavaDoc resource = java.util.ResourceBundle.getBundle("resources.Traduction")/*#BundleType=List*/;
39     /**
40      * Display a file in the system browser. If you want to display a
41      * file, you must include the absolute path name.
42      *
43      * @param url the file's url (the url must start with either "http://"
44 or
45      * "file://").
46      */

47     public static void displayURL(String JavaDoc url)
48     {
49         boolean windows = isWindowsPlatform();
50         String JavaDoc cmd = null;
51         try
52         {
53             if (windows)
54             {
55                 // cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
56
cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
57                 Process JavaDoc p = Runtime.getRuntime().exec(cmd);
58             }
59             else
60             {
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                 {
70                     // wait for exit code -- if it's 0, command worked,
71
// otherwise we need to start the browser up.
72
int exitCode = p.waitFor();
73                     if (exitCode != 0)
74                     {
75                         // Command failed, start up the browser
76
// cmd = 'netscape http://www.javaworld.com'
77
cmd = UNIX_PATH + " " + url;
78                         p = Runtime.getRuntime().exec(cmd);
79                     }
80                 }
81                 catch(InterruptedException JavaDoc x)
82                 {
83                     System.err.println(resource.getString("browsercontrol.error") +
84                                        cmd + "'");
85                     System.err.println(resource.getString("browsercontrol.caught") + x);
86                 }
87             }
88         }
89         catch(IOException JavaDoc x)
90         {
91             // couldn't exec browser
92
System.err.println(resource.getString("browsercontrol.notinvoke") + cmd);
93             System.err.println(resource.getString("browsercontrol.caught") + x);
94         }
95     }
96     /**
97      * Try to determine whether this application is running under Windows
98      * or some other platform by examing the "os.name" property.
99      *
100      * @return true if this application is running under a Windows OS
101      */

102     public static boolean isWindowsPlatform()
103     {
104         String JavaDoc os = System.getProperty("os.name");
105         if ( os != null && os.startsWith(WIN_ID))
106             return true;
107         else
108             return false;
109
110     }
111     /**
112      * Simple example.
113      */

114     public static void main(String JavaDoc[] args)
115     {
116         displayURL("http://www.javaworld.com");
117     }
118     // Used to identify the windows platform.
119
private static final String JavaDoc WIN_ID = "Windows";
120     // The default system browser under windows.
121
private static final String JavaDoc WIN_PATH = "rundll32";
122     // The flag to display a url.
123
private static final String JavaDoc WIN_FLAG = "url.dll,FileProtocolHandler";
124     // The default browser under unix.
125
private static final String JavaDoc UNIX_PATH = "netscape";
126     // The flag to display a url.
127
private static final String JavaDoc UNIX_FLAG = "-remote openURL";
128 }
129
Popular Tags