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