Enable first implementation of value change in varialbles view.
[phpeclipse.git] / net.sourceforge.phpeclipse.webbrowser / src / net / sourceforge / phpeclipse / webbrowser / internal / ExternalWebBrowser.java
1 package net.sourceforge.phpeclipse.webbrowser.internal;
2
3 import java.net.URL;
4
5 import net.sourceforge.phpeclipse.webbrowser.IExternalWebBrowser;
6 import net.sourceforge.phpeclipse.webbrowser.IExternalWebBrowserWorkingCopy;
7
8 import org.eclipse.swt.program.Program;
9 import org.eclipse.ui.IMemento;
10
11 /**
12  * 
13  */
14 public class ExternalWebBrowser implements IExternalWebBrowser {
15         private static final String MEMENTO_NAME = "name";
16
17         private static final String MEMENTO_LOCATION = "location";
18
19         private static final String MEMENTO_PARAMETERS = "parameters";
20
21         protected String name;
22
23         protected String location;
24
25         protected String parameters;
26
27         /*
28          * (non-Javadoc)
29          * 
30          * @see net.sourceforge.phpeclipse.webbrowser.IWebBrowser#getName()
31          */
32         public String getName() {
33                 return name;
34         }
35
36         /*
37          * (non-Javadoc)
38          * 
39          * @see net.sourceforge.phpeclipse.webbrowser.IExternalWebBrowser#getLocation()
40          */
41         public String getLocation() {
42                 return location;
43         }
44
45         /*
46          * (non-Javadoc)
47          * 
48          * @see net.sourceforge.phpeclipse.webbrowser.IExternalWebBrowser#getParameters()
49          */
50         public String getParameters() {
51                 return parameters;
52         }
53
54         public void delete() {
55                 BrowserManager.getInstance().removeWebBrowser(this);
56         }
57
58         public boolean isWorkingCopy() {
59                 return false;
60         }
61
62         public IExternalWebBrowserWorkingCopy getWorkingCopy() {
63                 return new ExternalWebBrowserWorkingCopy(this);
64         }
65
66         protected void setInternal(IExternalWebBrowser browser) {
67                 name = browser.getName();
68                 location = browser.getLocation();
69                 parameters = browser.getParameters();
70         }
71
72         /*
73          * (non-Javadoc)
74          * 
75          * @see net.sourceforge.phpeclipse.webbrowser.IWebBrowser#openURL(java.net.URL)
76          */
77         public void openURL(URL url) {
78                 String urlText = WebBrowserPreference.getHomePageURL();
79
80                 if (url != null)
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);
85                 }
86
87                 // change spaces to "%20"
88                 if (!WebBrowserUtil.isWindows()) {
89                         int index = urlText.indexOf(" ");
90                         while (index >= 0) {
91                                 urlText = urlText.substring(0, index) + "%20"
92                                                 + urlText.substring(index + 1);
93                                 index = urlText.indexOf(" ");
94                         }
95                 }
96
97                 Trace.trace(Trace.FINEST, "Launching external Web browser: " + location
98                                 + " - " + parameters + " - " + urlText);
99                 if (location == null || location.length() == 0) {
100                         try {
101                                 String extension = null;
102                                 if (url != null)
103                                         extension = url.getFile();
104                                 else
105                                         extension = "html";
106                                 int index = extension.indexOf(".");
107                                 if (index >= 0)
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));
116                         }
117                         return;
118                 }
119
120                 String params = parameters;
121                 if (params == null)
122                         params = "";
123
124                 int urlIndex = params.indexOf(WebBrowserPreference.URL_PARAMETER);
125                 if (urlIndex >= 0)
126                         params = params.substring(0, urlIndex)
127                                         + " "
128                                         + urlText
129                                         + " "
130                                         + params.substring(urlIndex
131                                                         + WebBrowserPreference.URL_PARAMETER.length());
132                 else {
133                         if (!params.endsWith(" "))
134                                 params += " ";
135                         params += urlText;
136                 }
137
138                 try {
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));
145                 }
146         }
147
148         protected void save(IMemento memento) {
149                 memento.putString(MEMENTO_NAME, name);
150                 memento.putString(MEMENTO_LOCATION, location);
151                 memento.putString(MEMENTO_PARAMETERS, parameters);
152         }
153
154         protected void load(IMemento memento) {
155                 name = memento.getString(MEMENTO_NAME);
156                 location = memento.getString(MEMENTO_LOCATION);
157                 parameters = memento.getString(MEMENTO_PARAMETERS);
158         }
159
160         public String toString() {
161                 return "External Web browser: " + getName() + " / " + getLocation()
162                                 + " / " + getParameters();
163         }
164 }