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