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