001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.browser;
011:
012: import java.io.IOException;
013: import java.net.URL;
014:
015: import org.eclipse.jface.dialogs.MessageDialog;
016: import org.eclipse.swt.SWT;
017: import org.eclipse.swt.program.Program;
018: import org.eclipse.swt.widgets.Display;
019: import org.eclipse.ui.PartInitException;
020: import org.eclipse.ui.browser.AbstractWebBrowser;
021: import org.eclipse.ui.internal.WorkbenchMessages;
022:
023: /**
024: * The default implementation of the web browser instance.
025: *
026: * @since 3.1
027: */
028: public class DefaultWebBrowser extends AbstractWebBrowser {
029: private DefaultWorkbenchBrowserSupport support;
030:
031: private String webBrowser;
032:
033: private boolean webBrowserOpened;
034:
035: /**
036: * Creates the browser instance.
037: *
038: * @param support
039: * @param id
040: */
041: public DefaultWebBrowser(DefaultWorkbenchBrowserSupport support,
042: String id) {
043: super (id);
044: this .support = support;
045: }
046:
047: /*
048: * (non-Javadoc)
049: *
050: * @see org.eclipse.ui.browser.IWebBrowser#openURL(java.net.URL)
051: */
052: public void openURL(URL url) throws PartInitException {
053: // format the href for an html file (file:///<filename.html>
054: // required for Mac only.
055: String href = url.toString();
056: if (href.startsWith("file:")) { //$NON-NLS-1$
057: href = href.substring(5);
058: while (href.startsWith("/")) { //$NON-NLS-1$
059: href = href.substring(1);
060: }
061: href = "file:///" + href; //$NON-NLS-1$
062: }
063: final String localHref = href;
064:
065: final Display d = Display.getCurrent();
066: String platform = SWT.getPlatform();
067:
068: if ("win32".equals(platform)) { //$NON-NLS-1$
069: Program.launch(localHref);
070: } else if ("carbon".equals(platform)) { //$NON-NLS-1$
071: try {
072: Runtime.getRuntime().exec("/usr/bin/open " + localHref); //$NON-NLS-1$
073: } catch (IOException e) {
074: throw new PartInitException(
075: WorkbenchMessages.ProductInfoDialog_unableToOpenWebBrowser,
076: e);
077: }
078: } else {
079: Thread launcher = new Thread("About Link Launcher") {//$NON-NLS-1$
080: public void run() {
081: try {
082: /*
083: * encoding the href as the browser does not open if
084: * there is a space in the url. Bug 77840
085: */
086: String encodedLocalHref = urlEncodeForSpaces(localHref
087: .toCharArray());
088: if (webBrowserOpened) {
089: Runtime
090: .getRuntime()
091: .exec(
092: webBrowser
093: + " -remote openURL(" + encodedLocalHref + ")"); //$NON-NLS-1$ //$NON-NLS-2$
094: } else {
095: Process p = openWebBrowser(encodedLocalHref);
096: webBrowserOpened = true;
097: try {
098: if (p != null) {
099: p.waitFor();
100: }
101: } catch (InterruptedException e) {
102: openWebBrowserError(d);
103: } finally {
104: webBrowserOpened = false;
105: }
106: }
107: } catch (IOException 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 urlEncodeForSpaces(char[] input) {
133: StringBuffer retu = new StringBuffer(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 openWebBrowser(String href) throws IOException {
146: Process 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 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 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 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() {
183: public void run() {
184: MessageDialog
185: .openError(
186: null,
187: WorkbenchMessages.ProductInfoDialog_errorTitle,
188: WorkbenchMessages.ProductInfoDialog_unableToOpenWebBrowser);
189: }
190: });
191: }
192: }
|