1 package net.sourceforge.phpeclipse.webbrowser.internal;
5 import net.sourceforge.phpeclipse.webbrowser.IExternalWebBrowser;
6 import net.sourceforge.phpeclipse.webbrowser.IExternalWebBrowserWorkingCopy;
8 import org.eclipse.swt.program.Program;
9 import org.eclipse.ui.IMemento;
14 public class ExternalWebBrowser implements IExternalWebBrowser {
15 private static final String MEMENTO_NAME = "name";
17 private static final String MEMENTO_LOCATION = "location";
19 private static final String MEMENTO_PARAMETERS = "parameters";
21 protected String name;
23 protected String location;
25 protected String parameters;
30 * @see net.sourceforge.phpeclipse.webbrowser.IWebBrowser#getName()
32 public String getName() {
39 * @see net.sourceforge.phpeclipse.webbrowser.IExternalWebBrowser#getLocation()
41 public String getLocation() {
48 * @see net.sourceforge.phpeclipse.webbrowser.IExternalWebBrowser#getParameters()
50 public String getParameters() {
54 public void delete() {
55 BrowserManager.getInstance().removeWebBrowser(this);
58 public boolean isWorkingCopy() {
62 public IExternalWebBrowserWorkingCopy getWorkingCopy() {
63 return new ExternalWebBrowserWorkingCopy(this);
66 protected void setInternal(IExternalWebBrowser browser) {
67 name = browser.getName();
68 location = browser.getLocation();
69 parameters = browser.getParameters();
75 * @see net.sourceforge.phpeclipse.webbrowser.IWebBrowser#openURL(java.net.URL)
77 public void openURL(URL url) {
78 String urlText = WebBrowserPreference.getHomePageURL();
81 urlText = url.toExternalForm();
82 else if (urlText.startsWith("file:") & urlText.length() > 6) {
83 if (urlText.charAt(5) != '/' && urlText.charAt(5) != '\\')
84 urlText = urlText.substring(0, 5) + "/" + urlText.substring(5);
87 // change spaces to "%20"
88 if (!WebBrowserUtil.isWindows()) {
89 int index = urlText.indexOf(" ");
91 urlText = urlText.substring(0, index) + "%20"
92 + urlText.substring(index + 1);
93 index = urlText.indexOf(" ");
97 Trace.trace(Trace.FINEST, "Launching external Web browser: " + location
98 + " - " + parameters + " - " + urlText);
99 if (location == null || location.length() == 0) {
101 String extension = null;
103 extension = url.getFile();
106 int index = extension.indexOf(".");
108 extension = extension.substring(index + 1);
109 Program program = Program.findProgram(extension);
110 program.execute(urlText);
111 } catch (Exception e) {
112 Trace.trace(Trace.SEVERE,
113 "Error launching default external browser", e);
114 WebBrowserUtil.openError(WebBrowserUIPlugin.getResource(
115 "%errorCouldNotLaunchWebBrowser", urlText));
120 String params = parameters;
124 int urlIndex = params.indexOf(WebBrowserPreference.URL_PARAMETER);
126 params = params.substring(0, urlIndex)
130 + params.substring(urlIndex
131 + WebBrowserPreference.URL_PARAMETER.length());
133 if (!params.endsWith(" "))
139 Trace.trace(Trace.FINEST, "Launching " + location + " " + params);
140 Runtime.getRuntime().exec(location + " " + params);
141 } catch (Exception e) {
142 Trace.trace(Trace.SEVERE, "Could not launch external browser", e);
143 WebBrowserUtil.openError(WebBrowserUIPlugin.getResource(
144 "%errorCouldNotLaunchWebBrowser", urlText));
148 protected void save(IMemento memento) {
149 memento.putString(MEMENTO_NAME, name);
150 memento.putString(MEMENTO_LOCATION, location);
151 memento.putString(MEMENTO_PARAMETERS, parameters);
154 protected void load(IMemento memento) {
155 name = memento.getString(MEMENTO_NAME);
156 location = memento.getString(MEMENTO_LOCATION);
157 parameters = memento.getString(MEMENTO_PARAMETERS);
160 public String toString() {
161 return "External Web browser: " + getName() + " / " + getLocation()
162 + " / " + getParameters();