KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.io.IOException JavaDoc;
14 import java.net.URL JavaDoc;
15
16 import org.eclipse.jface.dialogs.MessageDialog;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.program.Program;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.ui.PartInitException;
21 import org.eclipse.ui.browser.AbstractWebBrowser;
22 import org.eclipse.ui.internal.WorkbenchMessages;
23
24 /**
25  * The default implementation of the web browser instance.
26  *
27  * @since 3.1
28  */

29 public class DefaultWebBrowser extends AbstractWebBrowser {
30     private DefaultWorkbenchBrowserSupport support;
31
32     private String JavaDoc webBrowser;
33
34     private boolean webBrowserOpened;
35
36     /**
37      * Creates the browser instance.
38      *
39      * @param support
40      * @param id
41      */

42     public DefaultWebBrowser(DefaultWorkbenchBrowserSupport support, String JavaDoc id) {
43         super(id);
44         this.support = support;
45     }
46
47     /*
48      * (non-Javadoc)
49      *
50      * @see org.eclipse.ui.browser.IWebBrowser#openURL(java.net.URL)
51      */

52     public void openURL(URL JavaDoc url) throws PartInitException {
53         // format the href for an html file (file:///<filename.html>
54
// required for Mac only.
55
String JavaDoc href = url.toString();
56         if (href.startsWith("file:")) { //$NON-NLS-1$
57
href = href.substring(5);
58             while (href.startsWith("/")) { //$NON-NLS-1$
59
href = href.substring(1);
60             }
61             href = "file:///" + href; //$NON-NLS-1$
62
}
63         final String JavaDoc localHref = href;
64
65         final Display d = Display.getCurrent();
66         String JavaDoc platform = SWT.getPlatform();
67
68         if ("win32".equals(platform)) { //$NON-NLS-1$
69
Program.launch(localHref);
70         } else if ("carbon".equals(platform)) { //$NON-NLS-1$
71
try {
72                 Runtime.getRuntime().exec("/usr/bin/open " + localHref); //$NON-NLS-1$
73
} catch (IOException JavaDoc e) {
74                 throw new PartInitException(
75                         WorkbenchMessages.ProductInfoDialog_unableToOpenWebBrowser,
76                         e);
77             }
78         } else {
79             Thread JavaDoc launcher = new Thread JavaDoc("About Link Launcher") {//$NON-NLS-1$
80
public void run() {
81                     try {
82                         /*
83                          * encoding the href as the browser does not open if
84                          * there is a space in the url. Bug 77840
85                          */

86                         String JavaDoc encodedLocalHref = urlEncodeForSpaces(localHref
87                                 .toCharArray());
88                         if (webBrowserOpened) {
89                             Runtime
90                                     .getRuntime()
91                                     .exec(
92                                             webBrowser
93                                                     + " -remote openURL(" + encodedLocalHref + ")"); //$NON-NLS-1$ //$NON-NLS-2$
94
} else {
95                             Process JavaDoc p = openWebBrowser(encodedLocalHref);
96                             webBrowserOpened = true;
97                             try {
98                                 if (p != null) {
99                                     p.waitFor();
100                                 }
101                             } catch (InterruptedException JavaDoc e) {
102                                 openWebBrowserError(d);
103                             } finally {
104                                 webBrowserOpened = false;
105                             }
106                         }
107                     } catch (IOException JavaDoc e) {
108                         openWebBrowserError(d);
109                     }
110                 }
111             };
112             launcher.start();
113         }
114     }
115
116     /*
117      * (non-Javadoc)
118      *
119      * @see org.eclipse.ui.browser.IWebBrowser#close()
120      */

121     public boolean close() {
122         support.unregisterBrowser(this);
123         return super.close();
124     }
125
126     /**
127      * This method encodes the url, removes the spaces from the url and replaces
128      * the same with <code>"%20"</code>. This method is required to fix Bug
129      * 77840.
130      *
131      */

132     private String JavaDoc urlEncodeForSpaces(char[] input) {
133         StringBuffer JavaDoc retu = new StringBuffer JavaDoc(input.length);
134         for (int i = 0; i < input.length; i++) {
135             if (input[i] == ' ') {
136                 retu.append("%20"); //$NON-NLS-1$
137
} else {
138                 retu.append(input[i]);
139             }
140         }
141         return retu.toString();
142     }
143
144     // TODO: Move browser support from Help system, remove this method
145
private Process JavaDoc openWebBrowser(String JavaDoc href) throws IOException JavaDoc {
146         Process JavaDoc p = null;
147         if (webBrowser == null) {
148             try {
149                 webBrowser = "firefox"; //$NON-NLS-1$
150
p = Runtime.getRuntime().exec(webBrowser + " " + href); //$NON-NLS-1$;
151
} catch (IOException JavaDoc e) {
152                 p = null;
153                 webBrowser = "mozilla"; //$NON-NLS-1$
154
}
155         }
156
157         if (p == null) {
158             try {
159                 p = Runtime.getRuntime().exec(webBrowser + " " + href); //$NON-NLS-1$;
160
} catch (IOException JavaDoc e) {
161                 p = null;
162                 webBrowser = "netscape"; //$NON-NLS-1$
163
}
164         }
165         
166         if (p == null) {
167             try {
168                 p = Runtime.getRuntime().exec(webBrowser + " " + href); //$NON-NLS-1$;
169
} catch (IOException JavaDoc e) {
170                 p = null;
171                 throw e;
172             }
173         }
174         
175         return p;
176     }
177
178     /**
179      * display an error message
180      */

181     private void openWebBrowserError(Display display) {
182         display.asyncExec(new Runnable JavaDoc() {
183             public void run() {
184                 MessageDialog
185                         .openError(
186                                 null,
187                                 WorkbenchMessages.ProductInfoDialog_errorTitle,
188                                 WorkbenchMessages.ProductInfoDialog_unableToOpenWebBrowser);
189             }
190         });
191     }
192 }
193
Popular Tags