001: package hero.client.grapheditor;
002:
003: import java.io.IOException;
004:
005: /**
006: * A simple, static class to display a URL in the system browser.
007:
008:
009: *
010: * Under Unix, the system browser is hard-coded to be 'netscape'.
011: * Netscape must be in your PATH for this to work. This has been
012: * tested with the following platforms: AIX, HP-UX and Solaris.
013:
014:
015: *
016: * Under Windows, this will bring up the default browser under windows,
017: * usually either Netscape or Microsoft IE. The default browser is
018: * determined by the OS. This has been tested under Windows 95/98/NT.
019:
020:
021: *
022: * Examples:
023:
024:
025: *
026: BrowserControl.displayURL("http://www.javaworld.com")
027: *
028: BrowserControl.displayURL("file://c:\\docs\\index.html")
029: *
030: BrowserContorl.displayURL("file:///user/joe/index.html");
031: *
032:
033: * Note - you must include the url type -- either "http://" or
034: * "file://".
035: */
036: public class BrowserControl {
037: static java.util.ResourceBundle resource = java.util.ResourceBundle
038: .getBundle("resources.Traduction")/*#BundleType=List*/;
039:
040: /**
041: * Display a file in the system browser. If you want to display a
042: * file, you must include the absolute path name.
043: *
044: * @param url the file's url (the url must start with either "http://"
045: or
046: * "file://").
047: */
048: public static void displayURL(String url) {
049: boolean windows = isWindowsPlatform();
050: String cmd = null;
051: try {
052: if (windows) {
053: // cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
054: cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
055: Process p = Runtime.getRuntime().exec(cmd);
056: } else {
057: // Under Unix, Netscape has to be running for the "-remote"
058: // command to work. So, we try sending the command and
059: // check for an exit value. If the exit command is 0,
060: // it worked, otherwise we need to start the browser.
061: // cmd = 'netscape -remote openURL(http://www.javaworld.com)'
062: cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + url + ")";
063: Process p = Runtime.getRuntime().exec(cmd);
064: try {
065: // wait for exit code -- if it's 0, command worked,
066: // otherwise we need to start the browser up.
067: int exitCode = p.waitFor();
068: if (exitCode != 0) {
069: // Command failed, start up the browser
070: // cmd = 'netscape http://www.javaworld.com'
071: cmd = UNIX_PATH + " " + url;
072: p = Runtime.getRuntime().exec(cmd);
073: }
074: } catch (InterruptedException x) {
075: System.err.println(resource
076: .getString("browsercontrol.error")
077: + cmd + "'");
078: System.err.println(resource
079: .getString("browsercontrol.caught")
080: + x);
081: }
082: }
083: } catch (IOException x) {
084: // couldn't exec browser
085: System.err.println(resource
086: .getString("browsercontrol.notinvoke")
087: + cmd);
088: System.err.println(resource
089: .getString("browsercontrol.caught")
090: + x);
091: }
092: }
093:
094: /**
095: * Try to determine whether this application is running under Windows
096: * or some other platform by examing the "os.name" property.
097: *
098: * @return true if this application is running under a Windows OS
099: */
100: public static boolean isWindowsPlatform() {
101: String os = System.getProperty("os.name");
102: if (os != null && os.startsWith(WIN_ID))
103: return true;
104: else
105: return false;
106:
107: }
108:
109: /**
110: * Simple example.
111: */
112: public static void main(String[] args) {
113: displayURL("http://www.javaworld.com");
114: }
115:
116: // Used to identify the windows platform.
117: private static final String WIN_ID = "Windows";
118: // The default system browser under windows.
119: private static final String WIN_PATH = "rundll32";
120: // The flag to display a url.
121: private static final String WIN_FLAG = "url.dll,FileProtocolHandler";
122: // The default browser under unix.
123: private static final String UNIX_PATH = "netscape";
124: // The flag to display a url.
125: private static final String UNIX_FLAG = "-remote openURL";
126: }
|