Initial version from the webtools project; sligthly modified for phpeclipse
[phpeclipse.git] / net.sourceforge.phpeclipse.webbrowser / src / org / eclipse / webbrowser / internal / WebBrowserUtil.java
1 /**
2  * Copyright (c) 2003 IBM Corporation and others.
3  * All rights reserved.   This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *    IBM - Initial API and implementation
10  */
11 package org.eclipse.webbrowser.internal;
12
13 import java.io.File;
14 import java.io.InputStreamReader;
15 import java.io.Reader;
16 import java.net.URL;
17 import java.util.ArrayList;
18 import java.util.Iterator;
19 import java.util.List;
20
21 import org.eclipse.ui.IMemento;
22 import org.eclipse.ui.XMLMemento;
23 import org.eclipse.webbrowser.IExternalWebBrowser;
24 import org.eclipse.webbrowser.IExternalWebBrowserWorkingCopy;
25 import org.eclipse.webbrowser.IURLMap;
26 import org.eclipse.webbrowser.IWebBrowser;
27 import org.eclipse.swt.widgets.Display;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.core.runtime.*;
30 import org.eclipse.jface.dialogs.MessageDialog;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.browser.Browser;
33 /**
34  * Utility class for the Web browser tooling.
35  */
36 public class WebBrowserUtil {
37         private static List urlMaps;
38         private static List lockedFavorites;
39         private static List unlockedFavorites;
40         private static final String BROWSER_PACKAGE_NAME = "org.eclipse.swt.browser.Browser";
41         public static Boolean isInternalBrowserOperational;
42         
43         private static List defaultBrowsers2;
44
45         static class DefaultBrowser {
46                 String name;
47                 String params;
48                 String executable;
49                 String[] locations;
50                 
51                 public DefaultBrowser(String name, String executable, String params, String[] locations) {
52                         if (name == null)
53                                 name = "<unknown>";
54                         else if (name.startsWith("%"))
55                                 name = WebBrowserUIPlugin.getResource(name);
56
57                         this.name = name;
58                         this.executable = executable;
59                         this.params = params;
60                         this.locations = locations;
61                 }
62                 
63                 public String toString() {
64                         String s = "Browser: " + name + ", " + executable + ", " + params + ", ";
65                         if (locations != null) {
66                                 int size = locations.length;
67                                 for (int i = 0; i < size; i++) {
68                                         s += locations[i] + ";";
69                                 }
70                         }
71                         return s;
72                 }
73         }
74
75         /**
76          * WebBrowserUtil constructor comment.
77          */
78         public WebBrowserUtil() {
79                 super();
80         }
81
82         /**
83          * Returns true if we're running on Windows.
84          *
85          * @return boolean
86          */
87         public static boolean isWindows() {
88                 String os = System.getProperty("os.name");
89                 if (os != null && os.toLowerCase().indexOf("win") >= 0)
90                         return true;
91                 else
92                         return false;
93         }
94
95         /**
96          * Returns true if we're running on linux.
97          *
98          * @return boolean
99          */
100         public static boolean isLinux() {
101                 String os = System.getProperty("os.name");
102                 if (os != null && os.toLowerCase().indexOf("lin") >= 0)
103                         return true;
104                 else
105                         return false;
106         }
107
108         /**
109          * Open a dialog window.
110          *
111          * @param title java.lang.String
112          * @param message java.lang.String
113          */
114         public static void openError(String message) {
115                 Display d = Display.getCurrent();
116                 if (d == null)
117                         d = Display.getDefault();
118         
119                 Shell shell = d.getActiveShell();
120                 MessageDialog.openError(shell, WebBrowserUIPlugin.getResource("%errorDialogTitle"), message);
121         }
122         
123         /**
124          * Open a dialog window.
125          *
126          * @param title java.lang.String
127          * @param message java.lang.String
128          */
129         public static void openMessage(String message) {
130                 Display d = Display.getCurrent();
131                 if (d == null)
132                         d = Display.getDefault();
133         
134                 Shell shell = d.getActiveShell();
135                 MessageDialog.openInformation(shell, WebBrowserUIPlugin.getResource("%searchingTaskName"), message);
136         }
137         
138         /**
139          * Returns a List of all URL maps.
140          *
141          * @return java.util.List
142          */
143         public static List getURLMaps() {
144                 if (urlMaps == null)
145                         loadURLMaps();
146                 return urlMaps;
147         }
148         
149         /**
150          * Load the url map extension point.
151          */
152         private static void loadURLMaps() {
153                 Trace.trace(Trace.FINEST, "->- Loading .urlMap extension point ->-");
154                 IExtensionRegistry registry = Platform.getExtensionRegistry();
155                 IConfigurationElement[] cf = registry.getConfigurationElementsFor(WebBrowserUIPlugin.PLUGIN_ID, "urlMap");
156
157                 int size = cf.length;
158                 urlMaps = new ArrayList(size);
159                 for (int i = 0; i < size; i++) {
160                         try {
161                                 IURLMap mapper = (IURLMap) cf[i].createExecutableExtension("class");
162                                 urlMaps.add(mapper);
163                                 Trace.trace(Trace.FINEST, "  Loaded url map: " + cf[i].getAttribute("id"));
164                         } catch (Throwable t) {
165                                 Trace.trace(Trace.SEVERE, "  Could not load url map: " + cf[i].getAttribute("id"), t);
166                         }
167                 }
168                 
169                 Trace.trace(Trace.FINEST, "-<- Done loading .urlMap extension point -<-");
170         }
171         
172         /**
173          * Returns a List of all unlocked favorites.
174          *
175          * @return java.util.List
176          */
177         public static List getUnlockedFavorites() {
178                 if (unlockedFavorites == null)
179                         loadFavorites();
180                 return unlockedFavorites;
181         }
182         
183         /**
184          * Returns a List of all locked favorites.
185          *
186          * @return java.util.List
187          */
188         public static List getLockedFavorites() {
189                 if (lockedFavorites == null)
190                         loadFavorites();
191                 return lockedFavorites;
192         }
193         
194         /**
195          * Load the favorites extension point.
196          */
197         private static void loadFavorites() {
198                 Trace.trace(Trace.FINEST, "->- Loading .favorites extension point ->-");
199                 IExtensionRegistry registry = Platform.getExtensionRegistry();
200                 IConfigurationElement[] cf = registry.getConfigurationElementsFor(WebBrowserUIPlugin.PLUGIN_ID, "favorites");
201
202                 int size = cf.length;
203                 unlockedFavorites = new ArrayList(size);
204                 lockedFavorites = new ArrayList(size);
205                 for (int i = 0; i < size; i++) {
206                         try {
207                                 Favorite f = new Favorite(cf[i].getAttribute("name"), cf[i].getAttribute("url"));
208                                 String locked = cf[i].getAttribute("locked");
209                                 if (!"true".equals(locked))
210                                         unlockedFavorites.add(f);
211                                 else
212                                         lockedFavorites.add(f);
213                                 Trace.trace(Trace.FINEST, "  Loaded favorite: " + cf[i].getAttribute("id"));
214                         } catch (Throwable t) {
215                                 Trace.trace(Trace.SEVERE, "  Could not load favorite: " + cf[i].getAttribute("id"), t);
216                         }
217                 }
218                 
219                 Trace.trace(Trace.FINEST, "-<- Done loading .favorites extension point -<-");
220         }
221         
222         /**
223          * Returns whether it should be possible to use the internal browser or not, based on whether or not
224          * the org.eclipse.swt.Browser class can be found/loaded. If it can it means is is supported on the platform in which
225          * this plugin is running. If not, disable the ability to use the internal browser.
226          *
227          * @return boolean
228          */
229         public static boolean canUseInternalWebBrowser() {
230                 try {
231                         Class clazz = Class.forName(BROWSER_PACKAGE_NAME);
232                         if (clazz != null)
233                                 return true;
234                 } catch (ClassNotFoundException e) { }
235                 return false;
236         }
237
238         /**
239          * This method checks to see if it can new up a new Browser. If the SWT widget can not be bound
240          * to the particular operating system it throws an SWTException. We catch that and set a boolean
241          * flag which represents whether or not we were successfully able to create a Browser instance or not.
242          * If not, don't bother adding the Internal Web Browser that uses this widget. Designed to be attemped
243          * only once and the flag set used throughout.
244          * 
245          * @return boolean
246          */
247         protected static boolean isInternalBrowserOperational() {
248                 // if we have already figured this out, don't do it again.
249                 if (isInternalBrowserOperational != null)
250                         return isInternalBrowserOperational.booleanValue();
251                 
252                 try {
253                         new Browser(new Shell(Display.getCurrent()), SWT.NONE);
254                         isInternalBrowserOperational = new Boolean(true);                                       
255                 } catch (Throwable t) {
256                         WebBrowserUIPlugin.getInstance().getLog().log(new Status(IStatus.WARNING,
257                                 WebBrowserUIPlugin.PLUGIN_ID, 0, "Internal browser is not operational", t));
258                         isInternalBrowserOperational = new Boolean(false);
259                 }
260                 return isInternalBrowserOperational.booleanValue();
261         }
262         
263         public static List getExternalBrowserPaths() {
264                 List paths = new ArrayList();
265                 Iterator iterator = BrowserManager.getInstance().getWebBrowsers().iterator();
266                 while (iterator.hasNext()) {
267                         IWebBrowser wb = (IWebBrowser) iterator.next();
268                         if (wb instanceof IExternalWebBrowser) {
269                                 IExternalWebBrowser ext = (IExternalWebBrowser) wb;
270                                 paths.add(ext.getLocation().toLowerCase());
271                         }
272                 }
273                 return paths;
274         }
275
276         // Add any supported EXTERNAL web browsers found after an arbitrary check in specific paths
277         public static void addFoundBrowsers(List list) {
278                 List paths = getExternalBrowserPaths();
279
280                 Iterator iterator = getDefaultBrowsers().iterator();
281                 while (iterator.hasNext()) {
282                         DefaultBrowser browser2 = (DefaultBrowser) iterator.next();
283                         if (browser2.locations != null) {
284                                 int size = browser2.locations.length;
285                                 for (int j = 0; j < size; j++) {
286                                         String location = browser2.locations[j];
287                                         if (!paths.contains(location.toLowerCase())) {
288                                                 try {
289                                                         File f = new File(location);
290                                                         if (f.exists()) {
291                                                                 ExternalWebBrowser browser = new ExternalWebBrowser();
292                                                                 browser.name = browser2.name;
293                                                                 browser.location = location;
294                                                                 browser.parameters = browser2.params;
295                                                                 list.add(browser);
296                                                                 //Add browser here so that it get added to the table
297                                                                 BrowserManager.getInstance().addBrowser(browser);
298                                                                 j += size;
299                                                         }
300                                                 } catch (Exception e) { }
301                                         }
302                                 }
303                         }
304                 }
305         }
306
307         /**
308          * Create an external Web browser if the file matches the default (known) browsers.
309          * @param file
310          * @return
311          */
312         public static IExternalWebBrowserWorkingCopy createExternalBrowser(File file) {
313                 if (file == null || !file.isFile())
314                         return null;
315                 
316                 String executable = file.getName();
317                 Iterator iterator = getDefaultBrowsers().iterator();
318                 while (iterator.hasNext()) {
319                         DefaultBrowser db = (DefaultBrowser) iterator.next();
320                         if (executable.equals(db.executable)) {
321                                 IExternalWebBrowserWorkingCopy browser = BrowserManager.getInstance().createExternalWebBrowser();
322                                 browser.setName(db.name);
323                                 browser.setLocation(file.getAbsolutePath());
324                                 browser.setParameters(db.params);
325                                 return browser;
326                         }
327                 }
328                 
329                 return null;
330         }
331
332         protected static List getDefaultBrowsers() {
333                 if (defaultBrowsers2 != null)
334                         return defaultBrowsers2;
335                 
336                 Reader reader = null;
337                 defaultBrowsers2 = new ArrayList();
338                 try {
339                         URL url = WebBrowserUIPlugin.getInstance().getBundle().getEntry("defaultBrowsers.xml");
340                         URL url2 = Platform.resolve(url);
341                         reader = new InputStreamReader(url2.openStream());
342                         IMemento memento = XMLMemento.createReadRoot(reader);
343                         IMemento[] children = memento.getChildren("browser");
344                         if (children != null) {
345                                 int size = children.length;
346                                 for (int i = 0; i < size; i++) {
347                                         IMemento child = children[i];
348                                         String name = child.getString("name");
349                                         String executable = child.getString("executable");
350                                         String params = child.getString("params");
351                                         List locations = new ArrayList();
352                                         
353                                         IMemento[] locat = child.getChildren("location");
354                                         if (locat != null) {
355                                                 int size2 = locat.length;
356                                                 for (int j = 0; j < size2; j++)
357                                                         locations.add(locat[j].getTextData());
358                                         }
359                                         
360                                         String[] loc = new String[locations.size()];
361                                         locations.toArray(loc);
362                                         DefaultBrowser db = new DefaultBrowser(name, executable, params, loc);
363                                         Trace.trace(Trace.CONFIG, "Default browser: " + db);
364                                         defaultBrowsers2.add(db);
365                                 }
366                         }
367                 } catch (Exception e) {
368                         Trace.trace(Trace.SEVERE, "Error loading default browsers", e);
369                 } finally {
370                         try {
371                                 reader.close();
372                         } catch (Exception e) { }
373                 }
374                 return defaultBrowsers2;
375         }
376 }