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;
39 * Utility class for the Web browser tooling.
41 public class WebBrowserUtil {
42 private static List urlMaps;
43 private static List lockedFavorites;
44 private static List unlockedFavorites;
45 private static final String BROWSER_PACKAGE_NAME = "org.eclipse.swt.browser.Browser";
46 public static Boolean isInternalBrowserOperational;
48 private static List defaultBrowsers2;
50 static class DefaultBrowser {
56 public DefaultBrowser(String name, String executable, String params, String[] locations) {
59 else if (name.startsWith("%"))
60 name = WebBrowserUIPlugin.getResource(name);
63 this.executable = executable;
65 this.locations = locations;
68 public String toString() {
69 String s = "Browser: " + name + ", " + executable + ", " + params + ", ";
70 if (locations != null) {
71 int size = locations.length;
72 for (int i = 0; i < size; i++) {
73 s += locations[i] + ";";
81 * WebBrowserUtil constructor comment.
83 public WebBrowserUtil() {
88 * Returns true if we're running on Windows.
92 public static boolean isWindows() {
93 String os = System.getProperty("os.name");
94 if (os != null && os.toLowerCase().indexOf("win") >= 0)
101 * Returns true if we're running on linux.
105 public static boolean isLinux() {
106 String os = System.getProperty("os.name");
107 if (os != null && os.toLowerCase().indexOf("lin") >= 0)
114 * Open a dialog window.
116 * @param title java.lang.String
117 * @param message java.lang.String
119 public static void openError(String message) {
120 Display d = Display.getCurrent();
122 d = Display.getDefault();
124 Shell shell = d.getActiveShell();
125 MessageDialog.openError(shell, WebBrowserUIPlugin.getResource("%errorDialogTitle"), message);
129 * Open a dialog window.
131 * @param title java.lang.String
132 * @param message java.lang.String
134 public static void openMessage(String message) {
135 Display d = Display.getCurrent();
137 d = Display.getDefault();
139 Shell shell = d.getActiveShell();
140 MessageDialog.openInformation(shell, WebBrowserUIPlugin.getResource("%searchingTaskName"), message);
144 * Returns a List of all URL maps.
146 * @return java.util.List
148 public static List getURLMaps() {
155 * Load the url map extension point.
157 private static void loadURLMaps() {
158 Trace.trace(Trace.FINEST, "->- Loading .urlMap extension point ->-");
159 IExtensionRegistry registry = Platform.getExtensionRegistry();
160 IConfigurationElement[] cf = registry.getConfigurationElementsFor(WebBrowserUIPlugin.PLUGIN_ID, "urlMap");
162 int size = cf.length;
163 urlMaps = new ArrayList(size);
164 for (int i = 0; i < size; i++) {
166 IURLMap mapper = (IURLMap) cf[i].createExecutableExtension("class");
168 Trace.trace(Trace.FINEST, " Loaded url map: " + cf[i].getAttribute("id"));
169 } catch (Throwable t) {
170 Trace.trace(Trace.SEVERE, " Could not load url map: " + cf[i].getAttribute("id"), t);
174 Trace.trace(Trace.FINEST, "-<- Done loading .urlMap extension point -<-");
178 * Returns a List of all unlocked favorites.
180 * @return java.util.List
182 public static List getUnlockedFavorites() {
183 if (unlockedFavorites == null)
185 return unlockedFavorites;
189 * Returns a List of all locked favorites.
191 * @return java.util.List
193 public static List getLockedFavorites() {
194 if (lockedFavorites == null)
196 return lockedFavorites;
200 * Load the favorites extension point.
202 private static void loadFavorites() {
203 Trace.trace(Trace.FINEST, "->- Loading .favorites extension point ->-");
204 IExtensionRegistry registry = Platform.getExtensionRegistry();
205 IConfigurationElement[] cf = registry.getConfigurationElementsFor(WebBrowserUIPlugin.PLUGIN_ID, "favorites");
207 int size = cf.length;
208 unlockedFavorites = new ArrayList(size);
209 lockedFavorites = new ArrayList(size);
210 for (int i = 0; i < size; i++) {
212 Favorite f = new Favorite(cf[i].getAttribute("name"), cf[i].getAttribute("url"));
213 String locked = cf[i].getAttribute("locked");
214 if (!"true".equals(locked))
215 unlockedFavorites.add(f);
217 lockedFavorites.add(f);
218 Trace.trace(Trace.FINEST, " Loaded favorite: " + cf[i].getAttribute("id"));
219 } catch (Throwable t) {
220 Trace.trace(Trace.SEVERE, " Could not load favorite: " + cf[i].getAttribute("id"), t);
224 Trace.trace(Trace.FINEST, "-<- Done loading .favorites extension point -<-");
228 * Returns whether it should be possible to use the internal browser or not, based on whether or not
229 * the org.eclipse.swt.Browser class can be found/loaded. If it can it means is is supported on the platform in which
230 * this plugin is running. If not, disable the ability to use the internal browser.
234 public static boolean canUseInternalWebBrowser() {
236 Class clazz = Class.forName(BROWSER_PACKAGE_NAME);
239 } catch (ClassNotFoundException e) { }
244 * This method checks to see if it can new up a new Browser. If the SWT widget can not be bound
245 * to the particular operating system it throws an SWTException. We catch that and set a boolean
246 * flag which represents whether or not we were successfully able to create a Browser instance or not.
247 * If not, don't bother adding the Internal Web Browser that uses this widget. Designed to be attemped
248 * only once and the flag set used throughout.
252 public static boolean isInternalBrowserOperational() {
253 // if we have already figured this out, don't do it again.
254 if (isInternalBrowserOperational != null)
255 return isInternalBrowserOperational.booleanValue();
258 new Browser(new Shell(Display.getCurrent()), SWT.NONE);
259 isInternalBrowserOperational = new Boolean(true);
260 } catch (Throwable t) {
261 WebBrowserUIPlugin.getInstance().getLog().log(new Status(IStatus.WARNING,
262 WebBrowserUIPlugin.PLUGIN_ID, 0, "Internal browser is not operational", t));
263 isInternalBrowserOperational = new Boolean(false);
265 return isInternalBrowserOperational.booleanValue();
268 public static List getExternalBrowserPaths() {
269 List paths = new ArrayList();
270 Iterator iterator = BrowserManager.getInstance().getWebBrowsers().iterator();
271 while (iterator.hasNext()) {
272 IWebBrowser wb = (IWebBrowser) iterator.next();
273 if (wb instanceof IExternalWebBrowser) {
274 IExternalWebBrowser ext = (IExternalWebBrowser) wb;
275 paths.add(ext.getLocation().toLowerCase());
281 // Add any supported EXTERNAL web browsers found after an arbitrary check in specific paths
282 public static void addFoundBrowsers(List list) {
283 List paths = getExternalBrowserPaths();
285 Iterator iterator = getDefaultBrowsers().iterator();
286 while (iterator.hasNext()) {
287 DefaultBrowser browser2 = (DefaultBrowser) iterator.next();
288 if (browser2.locations != null) {
289 int size = browser2.locations.length;
290 for (int j = 0; j < size; j++) {
291 String location = browser2.locations[j];
292 if (!paths.contains(location.toLowerCase())) {
294 File f = new File(location);
296 ExternalWebBrowser browser = new ExternalWebBrowser();
297 browser.name = browser2.name;
298 browser.location = location;
299 browser.parameters = browser2.params;
301 //Add browser here so that it get added to the table
302 BrowserManager.getInstance().addBrowser(browser);
305 } catch (Exception e) { }
313 * Create an external Web browser if the file matches the default (known) browsers.
317 public static IExternalWebBrowserWorkingCopy createExternalBrowser(File file) {
318 if (file == null || !file.isFile())
321 String executable = file.getName();
322 Iterator iterator = getDefaultBrowsers().iterator();
323 while (iterator.hasNext()) {
324 DefaultBrowser db = (DefaultBrowser) iterator.next();
325 if (executable.equals(db.executable)) {
326 IExternalWebBrowserWorkingCopy browser = BrowserManager.getInstance().createExternalWebBrowser();
327 browser.setName(db.name);
328 browser.setLocation(file.getAbsolutePath());
329 browser.setParameters(db.params);
337 protected static List getDefaultBrowsers() {
338 if (defaultBrowsers2 != null)
339 return defaultBrowsers2;
341 Reader reader = null;
342 defaultBrowsers2 = new ArrayList();
344 URL url = WebBrowserUIPlugin.getInstance().getBundle().getEntry("defaultBrowsers.xml");
345 URL url2 = Platform.resolve(url);
346 reader = new InputStreamReader(url2.openStream());
347 IMemento memento = XMLMemento.createReadRoot(reader);
348 IMemento[] children = memento.getChildren("browser");
349 if (children != null) {
350 int size = children.length;
351 for (int i = 0; i < size; i++) {
352 IMemento child = children[i];
353 String name = child.getString("name");
354 String executable = child.getString("executable");
355 String params = child.getString("params");
356 List locations = new ArrayList();
358 IMemento[] locat = child.getChildren("location");
360 int size2 = locat.length;
361 for (int j = 0; j < size2; j++)
362 locations.add(locat[j].getTextData());
365 String[] loc = new String[locations.size()];
366 locations.toArray(loc);
367 DefaultBrowser db = new DefaultBrowser(name, executable, params, loc);
368 Trace.trace(Trace.CONFIG, "Default browser: " + db);
369 defaultBrowsers2.add(db);
372 } catch (Exception e) {
373 Trace.trace(Trace.SEVERE, "Error loading default browsers", e);
377 } catch (Exception e) { }
379 return defaultBrowsers2;