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 org.eclipse.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 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;
34 * Utility class for the Web browser tooling.
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;
43 private static List defaultBrowsers2;
45 static class DefaultBrowser {
51 public DefaultBrowser(String name, String executable, String params, String[] locations) {
54 else if (name.startsWith("%"))
55 name = WebBrowserUIPlugin.getResource(name);
58 this.executable = executable;
60 this.locations = locations;
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] + ";";
76 * WebBrowserUtil constructor comment.
78 public WebBrowserUtil() {
83 * Returns true if we're running on Windows.
87 public static boolean isWindows() {
88 String os = System.getProperty("os.name");
89 if (os != null && os.toLowerCase().indexOf("win") >= 0)
96 * Returns true if we're running on linux.
100 public static boolean isLinux() {
101 String os = System.getProperty("os.name");
102 if (os != null && os.toLowerCase().indexOf("lin") >= 0)
109 * Open a dialog window.
111 * @param title java.lang.String
112 * @param message java.lang.String
114 public static void openError(String message) {
115 Display d = Display.getCurrent();
117 d = Display.getDefault();
119 Shell shell = d.getActiveShell();
120 MessageDialog.openError(shell, WebBrowserUIPlugin.getResource("%errorDialogTitle"), message);
124 * Open a dialog window.
126 * @param title java.lang.String
127 * @param message java.lang.String
129 public static void openMessage(String message) {
130 Display d = Display.getCurrent();
132 d = Display.getDefault();
134 Shell shell = d.getActiveShell();
135 MessageDialog.openInformation(shell, WebBrowserUIPlugin.getResource("%searchingTaskName"), message);
139 * Returns a List of all URL maps.
141 * @return java.util.List
143 public static List getURLMaps() {
150 * Load the url map extension point.
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");
157 int size = cf.length;
158 urlMaps = new ArrayList(size);
159 for (int i = 0; i < size; i++) {
161 IURLMap mapper = (IURLMap) cf[i].createExecutableExtension("class");
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);
169 Trace.trace(Trace.FINEST, "-<- Done loading .urlMap extension point -<-");
173 * Returns a List of all unlocked favorites.
175 * @return java.util.List
177 public static List getUnlockedFavorites() {
178 if (unlockedFavorites == null)
180 return unlockedFavorites;
184 * Returns a List of all locked favorites.
186 * @return java.util.List
188 public static List getLockedFavorites() {
189 if (lockedFavorites == null)
191 return lockedFavorites;
195 * Load the favorites extension point.
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");
202 int size = cf.length;
203 unlockedFavorites = new ArrayList(size);
204 lockedFavorites = new ArrayList(size);
205 for (int i = 0; i < size; i++) {
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);
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);
219 Trace.trace(Trace.FINEST, "-<- Done loading .favorites extension point -<-");
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.
229 public static boolean canUseInternalWebBrowser() {
231 Class clazz = Class.forName(BROWSER_PACKAGE_NAME);
234 } catch (ClassNotFoundException e) { }
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.
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();
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);
260 return isInternalBrowserOperational.booleanValue();
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());
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();
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())) {
289 File f = new File(location);
291 ExternalWebBrowser browser = new ExternalWebBrowser();
292 browser.name = browser2.name;
293 browser.location = location;
294 browser.parameters = browser2.params;
296 //Add browser here so that it get added to the table
297 BrowserManager.getInstance().addBrowser(browser);
300 } catch (Exception e) { }
308 * Create an external Web browser if the file matches the default (known) browsers.
312 public static IExternalWebBrowserWorkingCopy createExternalBrowser(File file) {
313 if (file == null || !file.isFile())
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);
332 protected static List getDefaultBrowsers() {
333 if (defaultBrowsers2 != null)
334 return defaultBrowsers2;
336 Reader reader = null;
337 defaultBrowsers2 = new ArrayList();
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();
353 IMemento[] locat = child.getChildren("location");
355 int size2 = locat.length;
356 for (int j = 0; j < size2; j++)
357 locations.add(locat[j].getTextData());
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);
367 } catch (Exception e) {
368 Trace.trace(Trace.SEVERE, "Error loading default browsers", e);
372 } catch (Exception e) { }
374 return defaultBrowsers2;