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