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;
13 public class ExternalWebBrowser implements IExternalWebBrowser {
14 private static final String MEMENTO_NAME = "name";
15 private static final String MEMENTO_LOCATION = "location";
16 private static final String MEMENTO_PARAMETERS = "parameters";
18 protected String name;
19 protected String location;
20 protected String parameters;
23 * @see net.sourceforge.phpeclipse.webbrowser.IWebBrowser#getName()
25 public String getName() {
30 * @see net.sourceforge.phpeclipse.webbrowser.IExternalWebBrowser#getLocation()
32 public String getLocation() {
37 * @see net.sourceforge.phpeclipse.webbrowser.IExternalWebBrowser#getParameters()
39 public String getParameters() {
43 public void delete() {
44 BrowserManager.getInstance().removeWebBrowser(this);
47 public boolean isWorkingCopy() {
51 public IExternalWebBrowserWorkingCopy getWorkingCopy() {
52 return new ExternalWebBrowserWorkingCopy(this);
55 protected void setInternal(IExternalWebBrowser browser) {
56 name = browser.getName();
57 location = browser.getLocation();
58 parameters = browser.getParameters();
62 * @see net.sourceforge.phpeclipse.webbrowser.IWebBrowser#openURL(java.net.URL)
64 public void openURL(URL url) {
65 String urlText = WebBrowserPreference.getHomePageURL();
68 urlText = url.toExternalForm();
69 else if (urlText.startsWith("file:") & urlText.length() > 6) {
70 if (urlText.charAt(5) != '/' && urlText.charAt(5) != '\\')
71 urlText = urlText.substring(0, 5) + "/" + urlText.substring(5);
74 // change spaces to "%20"
75 if (!WebBrowserUtil.isWindows()) {
76 int index = urlText.indexOf(" ");
78 urlText = urlText.substring(0, index) + "%20" + urlText.substring(index + 1);
79 index = urlText.indexOf(" ");
83 Trace.trace(Trace.FINEST, "Launching external Web browser: " + location + " - " + parameters + " - " + urlText);
84 if (location == null || location.length() == 0) {
86 String extension = null;
88 extension = url.getFile();
91 int index = extension.indexOf(".");
93 extension = extension.substring(index + 1);
94 Program program = Program.findProgram(extension);
95 program.execute(urlText);
96 } catch (Exception e) {
97 Trace.trace(Trace.SEVERE, "Error launching default external browser", e);
98 WebBrowserUtil.openError(WebBrowserUIPlugin.getResource("%errorCouldNotLaunchWebBrowser", urlText));
103 String params = parameters;
107 int urlIndex = params.indexOf(WebBrowserPreference.URL_PARAMETER);
109 params = params.substring(0, urlIndex) + " " + urlText + " " + params.substring(urlIndex + WebBrowserPreference.URL_PARAMETER.length());
111 if (!params.endsWith(" "))
117 Trace.trace(Trace.FINEST, "Launching " + location + " " + params);
118 Runtime.getRuntime().exec(location + " " + params);
119 } catch (Exception e) {
120 Trace.trace(Trace.SEVERE, "Could not launch external browser", e);
121 WebBrowserUtil.openError(WebBrowserUIPlugin.getResource("%errorCouldNotLaunchWebBrowser", urlText));
125 protected void save(IMemento memento) {
126 memento.putString(MEMENTO_NAME, name);
127 memento.putString(MEMENTO_LOCATION, location);
128 memento.putString(MEMENTO_PARAMETERS, parameters);
131 protected void load(IMemento memento) {
132 name = memento.getString(MEMENTO_NAME);
133 location = memento.getString(MEMENTO_LOCATION);
134 parameters = memento.getString(MEMENTO_PARAMETERS);
137 public String toString() {
138 return "External Web browser: " + getName() + " / " + getLocation() + " / " + getParameters();