KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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 org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IConfigurationElement;
15 import org.eclipse.core.runtime.IExtension;
16 import org.eclipse.core.runtime.IExtensionPoint;
17 import org.eclipse.core.runtime.Platform;
18 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
19 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
20 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
21 import org.eclipse.swt.custom.BusyIndicator;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.ui.PartInitException;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.browser.AbstractWorkbenchBrowserSupport;
26 import org.eclipse.ui.browser.IWebBrowser;
27 import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
28 import org.eclipse.ui.internal.WorkbenchPlugin;
29 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
30
31 /**
32  * Implements the support interface and delegates the calls to the active
33  * support if contributed via the extension point, or the default support
34  * otherwise.
35  *
36  * @since 3.1
37  */

38 public class WorkbenchBrowserSupport extends AbstractWorkbenchBrowserSupport {
39
40     private static WorkbenchBrowserSupport instance;
41
42     private IWorkbenchBrowserSupport activeSupport;
43
44     private boolean initialized;
45     
46     private String JavaDoc desiredBrowserSupportId;
47
48     private IExtensionChangeHandler handler = new IExtensionChangeHandler() {
49         
50         /* (non-Javadoc)
51          * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamicHelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
52          */

53         public void addExtension(IExtensionTracker tracker,IExtension extension) {
54             //Do nothing
55
}
56
57         /* (non-Javadoc)
58          * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension, java.lang.Object[])
59          */

60         public void removeExtension(IExtension source, Object JavaDoc[] objects) {
61             for (int i = 0; i < objects.length; i++) {
62                 if (objects[i] == activeSupport) {
63                     dispose();
64                     // remove ourselves - we'll be added again in initalize if
65
// needed
66
PlatformUI.getWorkbench().getExtensionTracker()
67                             .unregisterHandler(handler);
68                 }
69             }
70         }
71     };
72
73     /**
74      * Cannot be instantiated from outside.
75      */

76     private WorkbenchBrowserSupport() {
77     }
78
79     /**
80      * Returns the shared instance.
81      *
82      * @return shared instance
83      */

84     public static IWorkbenchBrowserSupport getInstance() {
85         if (instance == null) {
86             instance = new WorkbenchBrowserSupport();
87         }
88         return instance;
89     }
90
91     /* (non-Javadoc)
92      * @see org.eclipse.ui.browser.IWorkbenchBrowserSupport#createBrowser(int, java.lang.String, java.lang.String, java.lang.String)
93      */

94     public IWebBrowser createBrowser(int style, String JavaDoc browserId, String JavaDoc name,
95             String JavaDoc tooltip) throws PartInitException {
96         return getActiveSupport()
97                 .createBrowser(style, browserId, name, tooltip);
98     }
99
100     /* (non-Javadoc)
101      * @see org.eclipse.ui.browser.IWorkbenchBrowserSupport#createBrowser(java.lang.String)
102      */

103     public IWebBrowser createBrowser(String JavaDoc browserId) throws PartInitException {
104         return getActiveSupport().createBrowser(browserId);
105     }
106     
107     /* (non-Javadoc)
108      * @see org.eclipse.ui.browser.IWorkbenchBrowserSupport#isInternalWebBrowserAvailable()
109      */

110     public boolean isInternalWebBrowserAvailable() {
111         return getActiveSupport().isInternalWebBrowserAvailable();
112     }
113
114     private IWorkbenchBrowserSupport getActiveSupport() {
115         if (initialized == false) {
116             loadActiveSupport();
117         }
118         // ensure we always have an active instance
119
if (activeSupport == null) {
120             activeSupport = new DefaultWorkbenchBrowserSupport();
121         }
122         return activeSupport;
123     }
124     
125     /**
126      * Answers whether the system has a non-default browser installed.
127      *
128      * @return whether the system has a non-default browser installed
129      */

130     public boolean hasNonDefaultBrowser() {
131         return !(getActiveSupport() instanceof DefaultWorkbenchBrowserSupport);
132     }
133
134     private void loadActiveSupport() {
135         BusyIndicator.showWhile(Display.getCurrent(), new Runnable JavaDoc() {
136             /*
137              * (non-Javadoc)
138              *
139              * @see java.lang.Runnable#run()
140              */

141             public void run() {
142                 IConfigurationElement[] elements = Platform
143                         .getExtensionRegistry().getConfigurationElementsFor(
144                                 PlatformUI.PLUGIN_ID,
145                                 IWorkbenchRegistryConstants.PL_BROWSER_SUPPORT);
146                 IConfigurationElement elementToUse = null;
147                 
148                 if (desiredBrowserSupportId != null) {
149                     elementToUse = findDesiredElement(elements);
150                 } else {
151                     elementToUse = getElementToUse(elements);
152                 }
153                 if (elementToUse != null) {
154                     initialized = initializePluggableBrowserSupport(elementToUse);
155                 }
156             }
157
158             /**
159              * Search for the element whose extension has ID equal to that of
160              * the field desiredBrowserSupport.
161              *
162              * @param elements
163              * the elements to search
164              * @return the element or <code>null</code>
165              */

166             private IConfigurationElement findDesiredElement(IConfigurationElement [] elements) {
167                 for (int i = 0; i < elements.length; i++) {
168                     if (desiredBrowserSupportId.equals(elements[i].getDeclaringExtension().getUniqueIdentifier())) {
169                         return elements[i];
170                     }
171                 }
172                 return null;
173             }
174
175             private IExtensionPoint getExtensionPoint() {
176                 return Platform.getExtensionRegistry()
177                         .getExtensionPoint(PlatformUI.PLUGIN_ID, IWorkbenchRegistryConstants.PL_BROWSER_SUPPORT);
178             }
179
180             private IConfigurationElement getElementToUse(
181                     IConfigurationElement[] elements) {
182                 if (elements.length == 0) {
183                     return null;
184                 }
185                 IConfigurationElement defaultElement = null;
186                 IConfigurationElement choice = null;
187                 // find the first default element and
188
// the first non-default element. If non-default
189
// is found, pick it. Otherwise, use default.
190
for (int i = 0; i < elements.length; i++) {
191                     IConfigurationElement element = elements[i];
192                     if (element.getName().equals(IWorkbenchRegistryConstants.TAG_SUPPORT)) {
193                         String JavaDoc def = element.getAttribute(IWorkbenchRegistryConstants.ATT_DEFAULT);
194                         if (def != null && Boolean.valueOf(def).booleanValue()) {
195                             if (defaultElement == null) {
196                                 defaultElement = element;
197                             }
198                         } else {
199                             // non-default
200
if (choice == null) {
201                                 choice = element;
202                             }
203                         }
204                     }
205                 }
206                 if (choice == null) {
207                     choice = defaultElement;
208                 }
209                 return choice;
210             }
211
212             private boolean initializePluggableBrowserSupport(
213                     IConfigurationElement element) {
214                 // Instantiate the browser support
215
try {
216                     activeSupport = (AbstractWorkbenchBrowserSupport) WorkbenchPlugin
217                             .createExtension(element, IWorkbenchRegistryConstants.ATT_CLASS);
218                     // start listening for removals
219
IExtensionTracker extensionTracker = PlatformUI.getWorkbench().getExtensionTracker();
220                     extensionTracker.registerHandler(handler, ExtensionTracker
221                             .createExtensionPointFilter(getExtensionPoint()));
222                     // register the new browser support for removal
223
// notification
224
extensionTracker
225                             .registerObject(element.getDeclaringExtension(),
226                                     activeSupport, IExtensionTracker.REF_WEAK);
227                     return true;
228                 } catch (CoreException e) {
229                     WorkbenchPlugin
230                             .log("Unable to instantiate browser support" + e.getStatus(), e);//$NON-NLS-1$
231
}
232                 return false;
233             }
234
235         });
236     }
237     
238     /**
239      * For debug purposes only.
240      *
241      * @param desiredBrowserSupportId the desired browser system id
242      */

243     public void setDesiredBrowserSupportId(String JavaDoc desiredBrowserSupportId) {
244         dispose(); // prep for a new help system
245
this.desiredBrowserSupportId = desiredBrowserSupportId;
246     }
247
248     /**
249      * Dispose of the active support.
250      */

251     protected void dispose() {
252         activeSupport = null;
253         initialized = false;
254     }
255 }
256
Popular Tags