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
9 * IBM - Initial API and implementation
11 package net.sourceforge.phpeclipse.webbrowser.internal;
14 import java.io.InputStreamReader;
15 import java.io.Reader;
17 import java.util.ArrayList;
18 import java.util.Iterator;
19 import java.util.List;
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;
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;
35 * Utility class for the Web browser tooling.
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;
44 private static List defaultBrowsers2;
46 static class DefaultBrowser {
52 public DefaultBrowser(String name, String executable, String params, String[] locations) {
55 else if (name.startsWith("%"))
56 name = WebBrowserUIPlugin.getResource(name);
59 this.executable = executable;
61 this.locations = locations;
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] + ";";
77 * WebBrowserUtil constructor comment.
79 public WebBrowserUtil() {
84 * Returns true if we're running on Windows.
88 public static boolean isWindows() {
89 String os = System.getProperty("os.name");
90 if (os != null && os.toLowerCase().indexOf("win") >= 0)
97 * Returns true if we're running on linux.
101 public static boolean isLinux() {
102 String os = System.getProperty("os.name");
103 if (os != null && os.toLowerCase().indexOf("lin") >= 0)
110 * Open a dialog window.
112 * @param title java.lang.String
113 * @param message java.lang.String
115 public static void openError(String message) {
116 Display d = Display.getCurrent();
118 d = Display.getDefault();
120 Shell shell = d.getActiveShell();
121 MessageDialog.openError(shell, WebBrowserUIPlugin.getResource("%errorDialogTitle"), message);
125 * Open a dialog window.
127 * @param title java.lang.String
128 * @param message java.lang.String
130 public static void openMessage(String message) {
131 Display d = Display.getCurrent();
133 d = Display.getDefault();
135 Shell shell = d.getActiveShell();
136 MessageDialog.openInformation(shell, WebBrowserUIPlugin.getResource("%searchingTaskName"), message);
140 * Returns a List of all URL maps.
142 * @return java.util.List
144 public static List getURLMaps() {
151 * Load the url map extension point.
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");
158 int size = cf.length;
159 urlMaps = new ArrayList(size);
160 for (int i = 0; i < size; i++) {
162 IURLMap mapper = (IURLMap) cf[i].createExecutableExtension("class");
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);
170 Trace.trace(Trace.FINEST, "-<- Done loading .urlMap extension point -<-");
174 * Returns a List of all unlocked favorites.
176 * @return java.util.List
178 public static List getUnlockedFavorites() {
179 if (unlockedFavorites == null)
181 return unlockedFavorites;
185 * Returns a List of all locked favorites.
187 * @return java.util.List
189 public static List getLockedFavorites() {
190 if (lockedFavorites == null)
192 return lockedFavorites;
196 * Load the favorites extension point.
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");
203 int size = cf.length;
204 unlockedFavorites = new ArrayList(size);
205 lockedFavorites = new ArrayList(size);
206 for (int i = 0; i < size; i++) {
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);
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);
220 Trace.trace(Trace.FINEST, "-<- Done loading .favorites extension point -<-");
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.
230 public static boolean canUseInternalWebBrowser() {
232 Class clazz = Class.forName(BROWSER_PACKAGE_NAME);
235 } catch (ClassNotFoundException e) { }
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.
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();
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);
261 return isInternalBrowserOperational.booleanValue();
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());
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();
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())) {
290 File f = new File(location);
292 ExternalWebBrowser browser = new ExternalWebBrowser();
293 browser.name = browser2.name;
294 browser.location = location;
295 browser.parameters = browser2.params;
297 //Add browser here so that it get added to the table
298 BrowserManager.getInstance().addBrowser(browser);
301 } catch (Exception e) { }
309 * Create an external Web browser if the file matches the default (known) browsers.
313 public static IExternalWebBrowserWorkingCopy createExternalBrowser(File file) {
314 if (file == null || !file.isFile())
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);
333 protected static List getDefaultBrowsers() {
334 if (defaultBrowsers2 != null)
335 return defaultBrowsers2;
337 Reader reader = null;
338 defaultBrowsers2 = new ArrayList();
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();
354 IMemento[] locat = child.getChildren("location");
356 int size2 = locat.length;
357 for (int j = 0; j < size2; j++)
358 locations.add(locat[j].getTextData());
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);
368 } catch (Exception e) {
369 Trace.trace(Trace.SEVERE, "Error loading default browsers", e);
373 } catch (Exception e) { }
375 return defaultBrowsers2;