KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > browser > ExternalBrowserInstance


1 /*******************************************************************************
2  * Copyright (c) 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.browser;
12
13 import java.net.URL JavaDoc;
14
15 import org.eclipse.osgi.util.NLS;
16 import org.eclipse.ui.PartInitException;
17 import org.eclipse.ui.browser.AbstractWebBrowser;
18
19 /**
20  * An instance of a running Web browser. rundll32.exe
21  * url.dll,FileProtocolHandler www.ibm.com
22  */

23 public class ExternalBrowserInstance extends AbstractWebBrowser {
24     protected IBrowserDescriptor browser;
25
26     protected Process JavaDoc process;
27
28     public ExternalBrowserInstance(String JavaDoc id, IBrowserDescriptor browser) {
29         super(id);
30         this.browser = browser;
31     }
32
33     public void openURL(URL JavaDoc url) throws PartInitException {
34         String JavaDoc urlText = null;
35
36         if (url != null)
37             urlText = url.toExternalForm();
38
39         // change spaces to "%20"
40
if (urlText != null && !WebBrowserUtil.isWindows()) {
41             int index = urlText.indexOf(" "); //$NON-NLS-1$
42
while (index >= 0) {
43                 urlText = urlText.substring(0, index)
44                         + "%20" + urlText.substring(index + 1); //$NON-NLS-1$
45
index = urlText.indexOf(" "); //$NON-NLS-1$
46
}
47         }
48
49         String JavaDoc location = browser.getLocation();
50         String JavaDoc parameters = browser.getParameters();
51         Trace
52                 .trace(
53                         Trace.FINEST,
54                         "Launching external Web browser: " + location + " - " + parameters + " - " + urlText); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
55

56         String JavaDoc params = parameters;
57         if (params == null)
58             params = ""; //$NON-NLS-1$
59

60         if (urlText != null) {
61             int urlIndex = params.indexOf(IBrowserDescriptor.URL_PARAMETER);
62             if (urlIndex >= 0)
63                 params = params.substring(0, urlIndex)
64                         + " " + urlText + " " + params.substring(urlIndex + IBrowserDescriptor.URL_PARAMETER.length()); //$NON-NLS-1$ //$NON-NLS-2$
65
else {
66                 if (!params.endsWith(" ")) //$NON-NLS-1$
67
params += " "; //$NON-NLS-1$
68
params += urlText;
69             }
70         }
71
72         try {
73             Trace.trace(Trace.FINEST, "Launching " + location + " " + params); //$NON-NLS-1$//$NON-NLS-2$
74
if (params == null || params.length() == 0)
75                 process = Runtime.getRuntime().exec(location);
76             else
77                 process = Runtime.getRuntime().exec(location + " " + params); //$NON-NLS-1$
78
} catch (Exception JavaDoc e) {
79             Trace.trace(Trace.SEVERE, "Could not launch external browser", e); //$NON-NLS-1$
80
WebBrowserUtil.openError(NLS.bind(
81                     Messages.errorCouldNotLaunchWebBrowser, urlText));
82         }
83         Thread JavaDoc thread = new Thread JavaDoc() {
84             public void run() {
85                 try {
86                     process.waitFor();
87                     DefaultBrowserSupport.getInstance().removeBrowser(
88                             ExternalBrowserInstance.this);
89                 } catch (Exception JavaDoc e) {
90                     // ignore
91
}
92             }
93         };
94         thread.setDaemon(true);
95         thread.start();
96     }
97
98     public boolean close() {
99         try {
100             process.destroy();
101             return true;
102         } catch (Exception JavaDoc e) {
103             return false;
104         }
105     }
106 }
Popular Tags