intial source from htp://www.sf.net/projects/wdte
[phpeclipse.git] / net.sourceforge.phpeclipse.webbrowser / src / org / eclipse / webbrowser / internal / ExternalWebBrowser.java
1 package org.eclipse.webbrowser.internal;
2
3 import java.net.URL;
4
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;
9 /**
10  * 
11  */
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";
16
17         protected String name;
18         protected String location;
19         protected String parameters;
20         
21         /* (non-Javadoc)
22          * @see org.eclipse.webbrowser.IWebBrowser#getName()
23          */
24         public String getName() {
25                 return name;
26         }
27
28         /* (non-Javadoc)
29          * @see org.eclipse.webbrowser.IExternalWebBrowser#getLocation()
30          */
31         public String getLocation() {
32                 return location;
33         }
34
35         /* (non-Javadoc)
36          * @see org.eclipse.webbrowser.IExternalWebBrowser#getParameters()
37          */
38         public String getParameters() {
39                 return parameters;
40         }
41         
42         public void delete() {
43                 BrowserManager.getInstance().removeWebBrowser(this);
44         }
45
46         public boolean isWorkingCopy() {
47                 return false;
48         }
49
50         public IExternalWebBrowserWorkingCopy getWorkingCopy() {
51                 return new ExternalWebBrowserWorkingCopy(this);
52         }
53
54         protected void setInternal(IExternalWebBrowser browser) {
55                 name = browser.getName();
56                 location = browser.getLocation();
57                 parameters = browser.getParameters();
58         }
59
60         /* (non-Javadoc)
61          * @see org.eclipse.webbrowser.IWebBrowser#openURL(java.net.URL)
62          */
63         public void openURL(URL url) {
64                 String urlText = WebBrowserPreference.getHomePageURL();
65                 
66                 if (url != null)
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);
71                 }
72
73                 // change spaces to "%20"
74                 if (!WebBrowserUtil.isWindows()) {
75                         int index = urlText.indexOf(" ");
76                         while (index >= 0) {
77                                 urlText = urlText.substring(0, index) + "%20" + urlText.substring(index + 1);
78                                 index = urlText.indexOf(" ");
79                         }
80                 }
81
82                 Trace.trace(Trace.FINEST, "Launching external Web browser: " + location + " - " + parameters + " - " + urlText);
83                 if (location == null || location.length() == 0) {
84                         try {
85                                 String extension = null;
86                                 if (url != null)
87                                         extension = url.getFile();
88                                 else
89                                         extension = "html";
90                                 int index = extension.indexOf(".");
91                                 if (index >= 0)
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));
98                         }
99                         return;
100                 }
101                 
102                 String params = parameters;
103                 if (params == null)
104                         params = "";
105                 
106                 int urlIndex = params.indexOf(WebBrowserPreference.URL_PARAMETER);
107                 if (urlIndex >= 0)
108                         params = params.substring(0, urlIndex) + " " + urlText + " " + params.substring(urlIndex + WebBrowserPreference.URL_PARAMETER.length());
109                 else {
110                         if (!params.endsWith(" "))
111                                 params += " ";
112                         params += urlText;
113                 }
114                 
115                 try {
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));
121                 }
122         }
123
124         protected void save(IMemento memento) {
125                 memento.putString(MEMENTO_NAME, name);
126                 memento.putString(MEMENTO_LOCATION, location);
127                 memento.putString(MEMENTO_PARAMETERS, parameters);
128         }
129
130         protected void load(IMemento memento) {
131                 name = memento.getString(MEMENTO_NAME);
132                 location = memento.getString(MEMENTO_LOCATION);
133                 parameters = memento.getString(MEMENTO_PARAMETERS);
134         }
135
136         public String toString() {
137                 return "External Web browser: " + getName() + " / " + getLocation() + " / " + getParameters();
138         }
139 }