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.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;
40 * Utility class for the Web browser tooling.
42 public class WebBrowserUtil {
43 private static List urlMaps;
45 private static List lockedFavorites;
47 private static List unlockedFavorites;
49 private static final String BROWSER_PACKAGE_NAME = "org.eclipse.swt.browser.Browser";
51 public static Boolean isInternalBrowserOperational;
53 private static List defaultBrowsers2;
55 static class DefaultBrowser {
64 public DefaultBrowser(String name, String executable, String params,
68 else if (name.startsWith("%"))
69 name = WebBrowserUIPlugin.getResource(name);
72 this.executable = executable;
74 this.locations = locations;
77 public String toString() {
78 String s = "Browser: " + name + ", " + executable + ", " + params
80 if (locations != null) {
81 int size = locations.length;
82 for (int i = 0; i < size; i++) {
83 s += locations[i] + ";";
91 * WebBrowserUtil constructor comment.
93 public WebBrowserUtil() {
98 * Returns true if we're running on Windows.
102 public static boolean isWindows() {
103 String os = System.getProperty("os.name");
104 if (os != null && os.toLowerCase().indexOf("win") >= 0)
111 * Returns true if we're running on linux.
115 public static boolean isLinux() {
116 String os = System.getProperty("os.name");
117 if (os != null && os.toLowerCase().indexOf("lin") >= 0)
124 * Open a dialog window.
131 public static void openError(String message) {
132 Display d = Display.getCurrent();
134 d = Display.getDefault();
136 Shell shell = d.getActiveShell();
137 MessageDialog.openError(shell, WebBrowserUIPlugin
138 .getResource("%errorDialogTitle"), message);
142 * Open a dialog window.
149 public static void openMessage(String message) {
150 Display d = Display.getCurrent();
152 d = Display.getDefault();
154 Shell shell = d.getActiveShell();
155 MessageDialog.openInformation(shell, WebBrowserUIPlugin
156 .getResource("%searchingTaskName"), message);
160 * Returns a List of all URL maps.
162 * @return java.util.List
164 public static List getURLMaps() {
171 * Load the url map extension point.
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");
179 int size = cf.length;
180 urlMaps = new ArrayList(size);
181 for (int i = 0; i < size; i++) {
183 IURLMap mapper = (IURLMap) cf[i]
184 .createExecutableExtension("class");
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);
194 Trace.trace(Trace.FINEST,
195 "-<- Done loading .urlMap extension point -<-");
199 * Returns a List of all unlocked favorites.
201 * @return java.util.List
203 public static List getUnlockedFavorites() {
204 if (unlockedFavorites == null)
206 return unlockedFavorites;
210 * Returns a List of all locked favorites.
212 * @return java.util.List
214 public static List getLockedFavorites() {
215 if (lockedFavorites == null)
217 return lockedFavorites;
221 * Load the favorites extension point.
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");
229 int size = cf.length;
230 unlockedFavorites = new ArrayList(size);
231 lockedFavorites = new ArrayList(size);
232 for (int i = 0; i < size; i++) {
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);
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);
249 Trace.trace(Trace.FINEST,
250 "-<- Done loading .favorites extension point -<-");
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
262 public static boolean canUseInternalWebBrowser() {
264 Class clazz = Class.forName(BROWSER_PACKAGE_NAME);
267 } catch (ClassNotFoundException e) {
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
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();
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);
297 return isInternalBrowserOperational.booleanValue();
300 public static List getExternalBrowserPaths() {
301 List paths = new ArrayList();
302 Iterator iterator = BrowserManager.getInstance().getWebBrowsers()
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());
314 // Add any supported EXTERNAL web browsers found after an arbitrary check in
316 public static void addFoundBrowsers(List list) {
317 List paths = getExternalBrowserPaths();
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())) {
328 File f = new File(location);
330 ExternalWebBrowser browser = new ExternalWebBrowser();
331 browser.name = browser2.name;
332 browser.location = location;
333 browser.parameters = browser2.params;
335 // Add browser here so that it get added to the
337 BrowserManager.getInstance()
338 .addBrowser(browser);
341 } catch (Exception e) {
350 * Create an external Web browser if the file matches the default (known)
356 public static IExternalWebBrowserWorkingCopy createExternalBrowser(File file) {
357 if (file == null || !file.isFile())
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);
377 protected static List getDefaultBrowsers() {
378 if (defaultBrowsers2 != null)
379 return defaultBrowsers2;
381 Reader reader = null;
382 defaultBrowsers2 = new ArrayList();
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();
399 IMemento[] locat = child.getChildren("location");
401 int size2 = locat.length;
402 for (int j = 0; j < size2; j++)
403 locations.add(locat[j].getTextData());
406 String[] loc = new String[locations.size()];
407 locations.toArray(loc);
408 DefaultBrowser db = new DefaultBrowser(name, executable,
410 Trace.trace(Trace.CONFIG, "Default browser: " + db);
411 defaultBrowsers2.add(db);
414 } catch (Exception e) {
415 Trace.trace(Trace.SEVERE, "Error loading default browsers", e);
419 } catch (Exception e) {
422 return defaultBrowsers2;