001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.browser;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.core.runtime.IConfigurationElement;
014: import org.eclipse.core.runtime.IExtension;
015: import org.eclipse.core.runtime.IExtensionPoint;
016: import org.eclipse.core.runtime.Platform;
017: import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
018: import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
019: import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
020: import org.eclipse.swt.custom.BusyIndicator;
021: import org.eclipse.swt.widgets.Display;
022: import org.eclipse.ui.PartInitException;
023: import org.eclipse.ui.PlatformUI;
024: import org.eclipse.ui.browser.AbstractWorkbenchBrowserSupport;
025: import org.eclipse.ui.browser.IWebBrowser;
026: import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
027: import org.eclipse.ui.internal.WorkbenchPlugin;
028: import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
029:
030: /**
031: * Implements the support interface and delegates the calls to the active
032: * support if contributed via the extension point, or the default support
033: * otherwise.
034: *
035: * @since 3.1
036: */
037: public class WorkbenchBrowserSupport extends
038: AbstractWorkbenchBrowserSupport {
039:
040: private static WorkbenchBrowserSupport instance;
041:
042: private IWorkbenchBrowserSupport activeSupport;
043:
044: private boolean initialized;
045:
046: private String desiredBrowserSupportId;
047:
048: private IExtensionChangeHandler handler = new IExtensionChangeHandler() {
049:
050: /* (non-Javadoc)
051: * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamicHelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
052: */
053: public void addExtension(IExtensionTracker tracker,
054: IExtension extension) {
055: //Do nothing
056: }
057:
058: /* (non-Javadoc)
059: * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension, java.lang.Object[])
060: */
061: public void removeExtension(IExtension source, Object[] objects) {
062: for (int i = 0; i < objects.length; i++) {
063: if (objects[i] == activeSupport) {
064: dispose();
065: // remove ourselves - we'll be added again in initalize if
066: // needed
067: PlatformUI.getWorkbench().getExtensionTracker()
068: .unregisterHandler(handler);
069: }
070: }
071: }
072: };
073:
074: /**
075: * Cannot be instantiated from outside.
076: */
077: private WorkbenchBrowserSupport() {
078: }
079:
080: /**
081: * Returns the shared instance.
082: *
083: * @return shared instance
084: */
085: public static IWorkbenchBrowserSupport getInstance() {
086: if (instance == null) {
087: instance = new WorkbenchBrowserSupport();
088: }
089: return instance;
090: }
091:
092: /* (non-Javadoc)
093: * @see org.eclipse.ui.browser.IWorkbenchBrowserSupport#createBrowser(int, java.lang.String, java.lang.String, java.lang.String)
094: */
095: public IWebBrowser createBrowser(int style, String browserId,
096: String name, String tooltip) throws PartInitException {
097: return getActiveSupport().createBrowser(style, browserId, name,
098: tooltip);
099: }
100:
101: /* (non-Javadoc)
102: * @see org.eclipse.ui.browser.IWorkbenchBrowserSupport#createBrowser(java.lang.String)
103: */
104: public IWebBrowser createBrowser(String browserId)
105: throws PartInitException {
106: return getActiveSupport().createBrowser(browserId);
107: }
108:
109: /* (non-Javadoc)
110: * @see org.eclipse.ui.browser.IWorkbenchBrowserSupport#isInternalWebBrowserAvailable()
111: */
112: public boolean isInternalWebBrowserAvailable() {
113: return getActiveSupport().isInternalWebBrowserAvailable();
114: }
115:
116: private IWorkbenchBrowserSupport getActiveSupport() {
117: if (initialized == false) {
118: loadActiveSupport();
119: }
120: // ensure we always have an active instance
121: if (activeSupport == null) {
122: activeSupport = new DefaultWorkbenchBrowserSupport();
123: }
124: return activeSupport;
125: }
126:
127: /**
128: * Answers whether the system has a non-default browser installed.
129: *
130: * @return whether the system has a non-default browser installed
131: */
132: public boolean hasNonDefaultBrowser() {
133: return !(getActiveSupport() instanceof DefaultWorkbenchBrowserSupport);
134: }
135:
136: private void loadActiveSupport() {
137: BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {
138: /*
139: * (non-Javadoc)
140: *
141: * @see java.lang.Runnable#run()
142: */
143: public void run() {
144: IConfigurationElement[] elements = Platform
145: .getExtensionRegistry()
146: .getConfigurationElementsFor(
147: PlatformUI.PLUGIN_ID,
148: IWorkbenchRegistryConstants.PL_BROWSER_SUPPORT);
149: IConfigurationElement elementToUse = null;
150:
151: if (desiredBrowserSupportId != null) {
152: elementToUse = findDesiredElement(elements);
153: } else {
154: elementToUse = getElementToUse(elements);
155: }
156: if (elementToUse != null) {
157: initialized = initializePluggableBrowserSupport(elementToUse);
158: }
159: }
160:
161: /**
162: * Search for the element whose extension has ID equal to that of
163: * the field desiredBrowserSupport.
164: *
165: * @param elements
166: * the elements to search
167: * @return the element or <code>null</code>
168: */
169: private IConfigurationElement findDesiredElement(
170: IConfigurationElement[] elements) {
171: for (int i = 0; i < elements.length; i++) {
172: if (desiredBrowserSupportId.equals(elements[i]
173: .getDeclaringExtension()
174: .getUniqueIdentifier())) {
175: return elements[i];
176: }
177: }
178: return null;
179: }
180:
181: private IExtensionPoint getExtensionPoint() {
182: return Platform
183: .getExtensionRegistry()
184: .getExtensionPoint(
185: PlatformUI.PLUGIN_ID,
186: IWorkbenchRegistryConstants.PL_BROWSER_SUPPORT);
187: }
188:
189: private IConfigurationElement getElementToUse(
190: IConfigurationElement[] elements) {
191: if (elements.length == 0) {
192: return null;
193: }
194: IConfigurationElement defaultElement = null;
195: IConfigurationElement choice = null;
196: // find the first default element and
197: // the first non-default element. If non-default
198: // is found, pick it. Otherwise, use default.
199: for (int i = 0; i < elements.length; i++) {
200: IConfigurationElement element = elements[i];
201: if (element.getName().equals(
202: IWorkbenchRegistryConstants.TAG_SUPPORT)) {
203: String def = element
204: .getAttribute(IWorkbenchRegistryConstants.ATT_DEFAULT);
205: if (def != null
206: && Boolean.valueOf(def).booleanValue()) {
207: if (defaultElement == null) {
208: defaultElement = element;
209: }
210: } else {
211: // non-default
212: if (choice == null) {
213: choice = element;
214: }
215: }
216: }
217: }
218: if (choice == null) {
219: choice = defaultElement;
220: }
221: return choice;
222: }
223:
224: private boolean initializePluggableBrowserSupport(
225: IConfigurationElement element) {
226: // Instantiate the browser support
227: try {
228: activeSupport = (AbstractWorkbenchBrowserSupport) WorkbenchPlugin
229: .createExtension(
230: element,
231: IWorkbenchRegistryConstants.ATT_CLASS);
232: // start listening for removals
233: IExtensionTracker extensionTracker = PlatformUI
234: .getWorkbench().getExtensionTracker();
235: extensionTracker
236: .registerHandler(
237: handler,
238: ExtensionTracker
239: .createExtensionPointFilter(getExtensionPoint()));
240: // register the new browser support for removal
241: // notification
242: extensionTracker.registerObject(element
243: .getDeclaringExtension(), activeSupport,
244: IExtensionTracker.REF_WEAK);
245: return true;
246: } catch (CoreException e) {
247: WorkbenchPlugin
248: .log(
249: "Unable to instantiate browser support" + e.getStatus(), e);//$NON-NLS-1$
250: }
251: return false;
252: }
253:
254: });
255: }
256:
257: /**
258: * For debug purposes only.
259: *
260: * @param desiredBrowserSupportId the desired browser system id
261: */
262: public void setDesiredBrowserSupportId(
263: String desiredBrowserSupportId) {
264: dispose(); // prep for a new help system
265: this .desiredBrowserSupportId = desiredBrowserSupportId;
266: }
267:
268: /**
269: * Dispose of the active support.
270: */
271: protected void dispose() {
272: activeSupport = null;
273: initialized = false;
274: }
275: }
|