1 package org.eclipse.webbrowser.internal;
5 import org.eclipse.swt.program.Program;
6 import org.eclipse.ui.IMemento;
7 import org.eclipse.webbrowser.IExternalWebBrowser;
8 import org.eclipse.webbrowser.IExternalWebBrowserWorkingCopy;
12 public class ExternalWebBrowser implements IExternalWebBrowser {
13 private static final String MEMENTO_NAME = "name";
14 private static final String MEMENTO_LOCATION = "location";
15 private static final String MEMENTO_PARAMETERS = "parameters";
17 protected String name;
18 protected String location;
19 protected String parameters;
22 * @see org.eclipse.webbrowser.IWebBrowser#getName()
24 public String getName() {
29 * @see org.eclipse.webbrowser.IExternalWebBrowser#getLocation()
31 public String getLocation() {
36 * @see org.eclipse.webbrowser.IExternalWebBrowser#getParameters()
38 public String getParameters() {
42 public void delete() {
43 BrowserManager.getInstance().removeWebBrowser(this);
46 public boolean isWorkingCopy() {
50 public IExternalWebBrowserWorkingCopy getWorkingCopy() {
51 return new ExternalWebBrowserWorkingCopy(this);
54 protected void setInternal(IExternalWebBrowser browser) {
55 name = browser.getName();
56 location = browser.getLocation();
57 parameters = browser.getParameters();
61 * @see org.eclipse.webbrowser.IWebBrowser#openURL(java.net.URL)
63 public void openURL(URL url) {
64 String urlText = WebBrowserPreference.getHomePageURL();
67 urlText = url.toExternalForm();
68 else if (urlText.startsWith("file:") & urlText.length() > 6) {
69 if (urlText.charAt(5) != '/' && urlText.charAt(5) != '\\')
70 urlText = urlText.substring(0, 5) + "/" + urlText.substring(5);
73 // change spaces to "%20"
74 if (!WebBrowserUtil.isWindows()) {
75 int index = urlText.indexOf(" ");
77 urlText = urlText.substring(0, index) + "%20" + urlText.substring(index + 1);
78 index = urlText.indexOf(" ");
82 Trace.trace(Trace.FINEST, "Launching external Web browser: " + location + " - " + parameters + " - " + urlText);
83 if (location == null || location.length() == 0) {
85 String extension = null;
87 extension = url.getFile();
90 int index = extension.indexOf(".");
92 extension = extension.substring(index + 1);
93 Program program = Program.findProgram(extension);
94 program.execute(urlText);
95 } catch (Exception e) {
96 Trace.trace(Trace.SEVERE, "Error launching default external browser", e);
97 WebBrowserUtil.openError(WebBrowserUIPlugin.getResource("%errorCouldNotLaunchWebBrowser", urlText));
102 String params = parameters;
106 int urlIndex = params.indexOf(WebBrowserPreference.URL_PARAMETER);
108 params = params.substring(0, urlIndex) + " " + urlText + " " + params.substring(urlIndex + WebBrowserPreference.URL_PARAMETER.length());
110 if (!params.endsWith(" "))
116 Trace.trace(Trace.FINEST, "Launching " + location + " " + params);
117 Runtime.getRuntime().exec(location + " " + params);
118 } catch (Exception e) {
119 Trace.trace(Trace.SEVERE, "Could not launch external browser", e);
120 WebBrowserUtil.openError(WebBrowserUIPlugin.getResource("%errorCouldNotLaunchWebBrowser", urlText));
124 protected void save(IMemento memento) {
125 memento.putString(MEMENTO_NAME, name);
126 memento.putString(MEMENTO_LOCATION, location);
127 memento.putString(MEMENTO_PARAMETERS, parameters);
130 protected void load(IMemento memento) {
131 name = memento.getString(MEMENTO_NAME);
132 location = memento.getString(MEMENTO_LOCATION);
133 parameters = memento.getString(MEMENTO_PARAMETERS);
136 public String toString() {
137 return "External Web browser: " + getName() + " / " + getLocation() + " / " + getParameters();