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