1 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 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 desiredBrowserSupportId; 47 48 private IExtensionChangeHandler handler = new IExtensionChangeHandler() { 49 50 53 public void addExtension(IExtensionTracker tracker,IExtension extension) { 54 } 56 57 60 public void removeExtension(IExtension source, Object [] objects) { 61 for (int i = 0; i < objects.length; i++) { 62 if (objects[i] == activeSupport) { 63 dispose(); 64 PlatformUI.getWorkbench().getExtensionTracker() 67 .unregisterHandler(handler); 68 } 69 } 70 } 71 }; 72 73 76 private WorkbenchBrowserSupport() { 77 } 78 79 84 public static IWorkbenchBrowserSupport getInstance() { 85 if (instance == null) { 86 instance = new WorkbenchBrowserSupport(); 87 } 88 return instance; 89 } 90 91 94 public IWebBrowser createBrowser(int style, String browserId, String name, 95 String tooltip) throws PartInitException { 96 return getActiveSupport() 97 .createBrowser(style, browserId, name, tooltip); 98 } 99 100 103 public IWebBrowser createBrowser(String browserId) throws PartInitException { 104 return getActiveSupport().createBrowser(browserId); 105 } 106 107 110 public boolean isInternalWebBrowserAvailable() { 111 return getActiveSupport().isInternalWebBrowserAvailable(); 112 } 113 114 private IWorkbenchBrowserSupport getActiveSupport() { 115 if (initialized == false) { 116 loadActiveSupport(); 117 } 118 if (activeSupport == null) { 120 activeSupport = new DefaultWorkbenchBrowserSupport(); 121 } 122 return activeSupport; 123 } 124 125 130 public boolean hasNonDefaultBrowser() { 131 return !(getActiveSupport() instanceof DefaultWorkbenchBrowserSupport); 132 } 133 134 private void loadActiveSupport() { 135 BusyIndicator.showWhile(Display.getCurrent(), new Runnable () { 136 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 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 for (int i = 0; i < elements.length; i++) { 191 IConfigurationElement element = elements[i]; 192 if (element.getName().equals(IWorkbenchRegistryConstants.TAG_SUPPORT)) { 193 String 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 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 try { 216 activeSupport = (AbstractWorkbenchBrowserSupport) WorkbenchPlugin 217 .createExtension(element, IWorkbenchRegistryConstants.ATT_CLASS); 218 IExtensionTracker extensionTracker = PlatformUI.getWorkbench().getExtensionTracker(); 220 extensionTracker.registerHandler(handler, ExtensionTracker 221 .createExtensionPointFilter(getExtensionPoint())); 222 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); } 232 return false; 233 } 234 235 }); 236 } 237 238 243 public void setDesiredBrowserSupportId(String desiredBrowserSupportId) { 244 dispose(); this.desiredBrowserSupportId = desiredBrowserSupportId; 246 } 247 248 251 protected void dispose() { 252 activeSupport = null; 253 initialized = false; 254 } 255 } 256 | Popular Tags |