001: package com.projity.util;
002:
003: import java.lang.reflect.Method;
004:
005: import javax.swing.JOptionPane;
006:
007: public class BrowserControl {
008:
009: private static final String errMsg = "Error attempting to launch web browser";
010:
011: public static void displayURL(String url) {
012: String osName = System.getProperty("os.name");
013: try {
014: if (osName.startsWith("Mac OS")) {
015: Class fileMgr = Class
016: .forName("com.apple.eio.FileManager");
017: Method openURL = fileMgr.getDeclaredMethod("openURL",
018: new Class[] { String.class });
019: openURL.invoke(null, new Object[] { url });
020: } else if (osName.startsWith("Windows"))
021: Runtime.getRuntime().exec(
022: "rundll32 url.dll,FileProtocolHandler " + url);
023: else { //assume Unix or Linux
024: String[] browsers = { "firefox", "opera", "konqueror",
025: "epiphany", "mozilla", "netscape" };
026: String browser = null;
027: for (int count = 0; count < browsers.length
028: && browser == null; count++)
029: if (Runtime.getRuntime().exec(
030: new String[] { "which", browsers[count] })
031: .waitFor() == 0)
032: browser = browsers[count];
033: if (browser == null)
034: throw new Exception("Could not find web browser");
035: else
036: Runtime.getRuntime().exec(
037: new String[] { browser, url });
038: }
039: //Dedian: /etc/alternatives/x-www-browser
040: //RedHat: /usr/bin/mozilla or /usr/bin/firefox
041:
042: // /usr/share/applications/defaults.list (text/html key)
043: // /usr/share/applications/<text/html key> (Exec)
044: //info in /usr/share/mime/text/html.xml
045: //extension in /usr/share/mime/globs
046: //mimeinfo.cache
047:
048: ///usr/share/mime/packages/openproj.xml update-mime-database /usr/share/mime
049: } catch (Exception e) {
050: JOptionPane.showMessageDialog(null, errMsg + ":\n"
051: + e.getLocalizedMessage());
052: }
053: }
054:
055: }
056:
057: ///**
058: //* A simple, static class to display an URL in the system browser.
059: //*
060: //* Under Windows, this will bring up the default browser,
061: //* usually either Netscape or Microsoft IE. The default browser is
062: //* determined by the OS. This has been tested under: Windows 95/98/NT/2000.
063: //*
064: //* Under MacOS, this will bring up the default browser.
065: //* The default browser is determined by the OS.
066: //* This has been tested under: n/a
067: //*
068: //* In other cases (and under Unix),
069: //* the system browser is hard-coded to be 'netscape'.
070: //* Netscape must be in your PATH for this to work. This has been
071: //* tested with the following platforms: AIX, HP-UX and Solaris.
072: //*
073: //* Examples:
074: //* * BrowserControl.displayURL("http://www.javaworld.com")
075: //*
076: //* BrowserControl.displayURL("file://c:\\docs\\index.html")
077: //*
078: //* BrowserContorl.displayURL("file:///user/joe/index.html");
079: //*
080: //* Note - you must include the url type -- either "http://" or
081: //* "file://".
082: //*/
083: //public class BrowserControl {
084: // public static String[] getEnv(String newUrl) {
085: // String browser = System.getProperty("BROWSER");
086: // String myBrowser = "/usr/bin/" + browser + " " + newUrl;
087: // return myBrowser.split(" ");
088: // }
089: // /**
090: // * Display an URL in the system browser. If you want to display a
091: // * file, you must include the absolute path name.
092: // *
093: // * @param url the document's url (the url must start with either "http://"
094: // * or "file://").
095: // */
096: // public static boolean displayURL(String url) {
097: //
098: // // Opening a browser, even when running sandbox-restricted
099: // // in JavaWebStart.
100: // try {
101: // Class serManClass = ClassLoaderUtils.getLocalClassLoader().loadClass("javax.jnlp.ServiceManager");
102: //// Class serManClass = Class.forName("javax.jnlp.ServiceManager");
103: //// Class basSerClass = Class.forName("javax.jnlp.BasicService");
104: // Class basSerClass = ClassLoaderUtils.getLocalClassLoader().loadClass("javax.jnlp.BasicService");
105: // Class[] stringParam = {String.class};
106: // Class[] urlParam = {URL.class};
107: //
108: // Object basicService = serManClass.getMethod("lookup", stringParam)
109: // .invoke(serManClass, new Object[] {"javax.jnlp.BasicService"});
110: // basSerClass.getMethod("showDocument", urlParam)
111: // .invoke(basicService, new Object[] {new URL(url)});
112: //
113: // return true;
114: // } catch(Exception e) {
115: // // Not running in JavaWebStart or service is not supported.
116: // // We continue with the methods below ...
117: // }
118: //
119: // String[] cmd = null;
120: //
121: // switch(getPlatform()) {
122: // case(WIN_ID):
123: // return runCmdLine(replaceToken(WIN_CMDLINE, URLTOKEN, url));
124: // case(MAC_ID):
125: // return runCmdLine(replaceToken(MAC_CMDLINE, URLTOKEN, url));
126: // default:
127: // for (int i = 0; i<OTHER_CMDLINES.length; i++) {
128: // if (runCmdLine( replaceToken(OTHER_CMDLINES[i], URLTOKEN, url),
129: // replaceToken(OTHER_FALLBACKS[i], URLTOKEN, url)))
130: // return true;
131: // }
132: // }
133: //
134: // return false;
135: // }
136: //
137: // /**
138: // * Try to determine whether this application is running under Windows
139: // * or some other platform by examing the "os.name" property.
140: // *
141: // * @return the ID of the platform
142: // */
143: // private static int getPlatform()
144: // {
145: // String os = System.getProperty("os.name");
146: // if ( os != null && os.startsWith(WIN_PREFIX)) return WIN_ID;
147: // if ( os != null && os.startsWith(MAC_PREFIX)) return MAC_ID;
148: // return OTHER_ID;
149: // }
150: //
151: // private static String connectStringArray(String[] a) {
152: // if (a == null) return null;
153: //
154: // String s = "";
155: // for (int i = 0; i<a.length; i++) {
156: // if (i > 0) s += " ";
157: // s += a[i];
158: // }
159: //
160: // return s;
161: // }
162: //
163: // private static String[] replaceToken(String[] target, String token, String replacement) {
164: // if (null == target) return null;
165: // String[] result = new String[target.length];
166: //
167: // for(int i = 0; i<target.length; i++)
168: // result[i] = target[i].replaceAll(token, replacement);
169: //
170: // return result;
171: // }
172: //
173: // private static boolean runCmdLine(String[] cmdLine) {
174: // return runCmdLine(cmdLine,null);
175: // }
176: //
177: // private static boolean runCmdLine(String[] cmdLine, String[] fallBackCmdLine) {
178: // try {
179: //
180: // System.err.println(
181: // "Trying to invoke browser, cmd='" +
182: // connectStringArray(cmdLine) + "' ... ");
183: // Process p = Runtime.getRuntime().exec(cmdLine);
184: //
185: // if (null != fallBackCmdLine) {
186: // // wait for exit code -- if it's 0, command worked,
187: // // otherwise we need to start fallBackCmdLine.
188: // int exitCode = p.waitFor();
189: // if (exitCode != 0) {
190: // System.err.println(exitCode);
191: // System.err.println();
192: //
193: // System.err.println(
194: // "Trying to invoke browser, cmd='" +
195: // connectStringArray(fallBackCmdLine) + "' ...");
196: // Runtime.getRuntime().exec(fallBackCmdLine);
197: //
198: // }
199: // }
200: //
201: // System.err.println();
202: // return true;
203: //
204: // } catch(InterruptedException e) {
205: // System.err.println("Caught: " + e);
206: // } catch(IOException e) {
207: // System.err.println("Caught: " + e);
208: // }
209: //
210: // System.err.println();
211: // return false;
212: // }
213: //
214: // // This token is a placeholder for the actual URL
215: // private static final String URLTOKEN = "%URLTOKEN%";
216: //
217: //
218: // // Used to identify the windows platform.
219: // private static final int WIN_ID = 1;
220: //
221: // // Used to discover the windows platform.
222: // private static final String WIN_PREFIX = "Windows";
223: //
224: // // The default system browser under windows.
225: // // Once upon a time:
226: // // for 'Windows 9' and 'Windows M': start
227: // // for 'Windows': cmd /c start
228: // private static final String[] WIN_CMDLINE = {"rundll32", "url.dll,FileProtocolHandler", URLTOKEN};
229: //
230: //
231: // // Used to identify the mac platform.
232: // private static final int MAC_ID = 2;
233: //
234: // // Used to discover the mac platform.
235: // private static final String MAC_PREFIX = "Mac";
236: //
237: // // The default system browser under mac.
238: // private static final String[] MAC_CMDLINE = {"open", URLTOKEN};
239: //
240: //
241: // // Used to identify the mac platform.
242: // private static final int OTHER_ID = -1;
243: //
244: // private static final String[][] OTHER_CMDLINES = {
245: //
246: // // Try to invoke the browser specified in the BROWSER environment variable
247: // getEnv(URLTOKEN),
248: //
249: // // The first guess for a browser under other systems (and unix):
250: // // Remote controlling mozilla (http://www.mozilla.org/unix/remote.html)
251: // {"mozilla", "-remote", "openURL(" + URLTOKEN + ",new-window)"},
252: //
253: // // The second guess for a browser under other systems (and unix):
254: // // The RedHat skript htmlview
255: // {"htmlview", URLTOKEN},
256: //
257: // // The third guess for a browser under other systems (and unix):
258: // // Remote controlling netscape (http://wp.netscape.com/newsref/std/x-remote.html)
259: // {"netscape", "-remote", "openURL(" + URLTOKEN + ")"}
260: //
261: // };
262: //
263: // private static final String[][] OTHER_FALLBACKS = {
264: //
265: // // Fallback for remote controlling mozilla:
266: // // Starting up a new mozilla
267: // {"mozilla", URLTOKEN},
268: //
269: // // No fallback for htmlview
270: // null,
271: //
272: // // Fallback for remote controlling netscape:
273: // // Starting up a new netscape
274: // {"netscape", URLTOKEN}
275: //
276: // };
277: //
278: //}
|