KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > browser > WebBrowserUIPlugin


1 /*******************************************************************************
2  * Copyright (c) 2003, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.browser;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.core.runtime.IExtensionRegistry;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Platform;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.ui.plugin.AbstractUIPlugin;
22 import org.osgi.framework.BundleContext;
23 /**
24  * The main web browser plugin class.
25  */

26 public class WebBrowserUIPlugin extends AbstractUIPlugin {
27     // Web browser plugin id
28
public static final String JavaDoc PLUGIN_ID = "org.eclipse.ui.browser"; //$NON-NLS-1$
29

30     // singleton instance of this class
31
private static WebBrowserUIPlugin singleton;
32     
33     // cached copy of all browsers
34
private static List JavaDoc browsers;
35
36     /**
37      * Create the WebBrowserUIPlugin
38      */

39     public WebBrowserUIPlugin() {
40         super();
41         singleton = this;
42     }
43
44     /**
45      * Returns the singleton instance of this plugin.
46      *
47      * @return org.eclipse.ui.internal.browser.WebBrowserPlugin
48      */

49     public static WebBrowserUIPlugin getInstance() {
50         return singleton;
51     }
52
53     /**
54      * Shuts down this plug-in and saves all plug-in state.
55      *
56      * @exception Exception
57      */

58     public void stop(BundleContext context) throws Exception JavaDoc {
59         super.stop(context);
60         BrowserManager.safeDispose();
61     }
62     
63     /**
64      * Returns an array of all known browers.
65      * <p>
66      * A new array is returned on each call, so clients may store or modify the result.
67      * </p>
68      *
69      * @return a possibly-empty array of browser instances {@link IClient}
70      */

71     public static IBrowserExt[] getBrowsers() {
72         if (browsers == null)
73             loadBrowsers();
74         IBrowserExt[] c = new IBrowserExt[browsers.size()];
75         browsers.toArray(c);
76         return c;
77     }
78     
79     public static IBrowserExt findBrowsers(String JavaDoc executable) {
80         IBrowserExt[] browsers2 = getBrowsers();
81         if (browsers2 == null || executable == null)
82             return null;
83         
84         int ind1 = executable.lastIndexOf("/"); //$NON-NLS-1$
85
int ind2 = executable.lastIndexOf("\\"); //$NON-NLS-1$
86
if (ind2 > ind1)
87             ind1 = ind2;
88         executable = executable.substring(ind1 + 1);
89         
90         String JavaDoc os = Platform.getOS();
91         int size = browsers2.length;
92         for (int i = 0; i < size; i++) {
93             if (browsers2[i].getOS().toLowerCase().indexOf(os) != -1) {
94                 if (browsers2[i].isAvailable()) {
95                     String JavaDoc executable2 = browsers2[i].getExecutable();
96                     if (executable.startsWith(executable2))
97                         return browsers2[i];
98                 }
99             }
100         }
101         return null;
102     }
103     
104     /**
105      * Load the browsers extension point.
106      */

107     private static synchronized void loadBrowsers() {
108         if (browsers != null)
109             return;
110         Trace.trace(Trace.CONFIG, "->- Loading .browsers extension point ->-"); //$NON-NLS-1$
111
IExtensionRegistry registry = Platform.getExtensionRegistry();
112         IConfigurationElement[] cf = registry.getConfigurationElementsFor(PLUGIN_ID, "browsers"); //$NON-NLS-1$
113

114         int size = cf.length;
115         browsers = new ArrayList JavaDoc(size);
116         for (int i = 0; i < size; i++) {
117             try {
118                 browsers.add(new BrowserExt(cf[i]));
119                 Trace.trace(Trace.CONFIG, " Loaded browser: " + cf[i].getAttribute("id")); //$NON-NLS-1$ //$NON-NLS-2$
120
} catch (Throwable JavaDoc t) {
121                 Trace.trace(Trace.SEVERE, " Could not load browser: " + cf[i].getAttribute("id"), t); //$NON-NLS-1$ //$NON-NLS-2$
122
}
123         }
124         Trace.trace(Trace.CONFIG, "-<- Done loading .browsers extension point -<-"); //$NON-NLS-1$
125
}
126     
127     /**
128      * Logs an Error message with an exception. Note that the message should
129      * already be localized to proper locale. ie: Resources.getString() should
130      * already have been called
131      */

132     public static synchronized void logError(String JavaDoc message, Throwable JavaDoc ex) {
133         if (message == null)
134             message = ""; //$NON-NLS-1$
135
Status errorStatus = new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK,
136                 message, ex);
137         WebBrowserUIPlugin.getInstance().getLog().log(errorStatus);
138     }
139
140     /**
141      * Logs a Warning message with an exception. Note that the message should
142      * already be localized to proper local. ie: Resources.getString() should
143      * already have been called
144      */

145     /*public static synchronized void logWarning(String message) {
146         if (WebBrowserUIPlugin.DEBUG) {
147             if (message == null)
148                 message = ""; //$NON-NLS-1$
149             Status warningStatus = new Status(IStatus.WARNING, PLUGIN_ID,
150                     IStatus.OK, message, null);
151             WebBrowserUIPlugin.getInstance().getLog().log(warningStatus);
152         }
153     }*/

154 }
Popular Tags