1 11 12 package org.eclipse.ui.internal.statushandlers; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.IConfigurationElement; 19 import org.eclipse.core.runtime.IExtension; 20 import org.eclipse.core.runtime.IExtensionPoint; 21 import org.eclipse.core.runtime.Platform; 22 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker; 23 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler; 24 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; 25 import org.eclipse.ui.PlatformUI; 26 import org.eclipse.ui.internal.WorkbenchPlugin; 27 28 34 public class StatusHandlerRegistry implements IExtensionChangeHandler { 35 36 private static final String STATUSHANDLERS_POINT_NAME = "statusHandlers"; 38 private static final String TAG_STATUSHANDLER = "statusHandler"; 40 private static final String TAG_STATUSHANDLER_PRODUCTBINDING = "statusHandlerProductBinding"; 42 private ArrayList statusHandlerDescriptors = new ArrayList (); 43 44 private ArrayList productBindingDescriptors = new ArrayList (); 45 46 private StatusHandlerDescriptorsMap statusHandlerDescriptorsMap; 47 48 private StatusHandlerDescriptor defaultHandlerDescriptor; 49 50 private static StatusHandlerRegistry instance; 51 52 55 private StatusHandlerRegistry() { 56 IExtensionTracker tracker = PlatformUI.getWorkbench() 57 .getExtensionTracker(); 58 IExtensionPoint handlersPoint = Platform.getExtensionRegistry() 59 .getExtensionPoint(WorkbenchPlugin.PI_WORKBENCH, 60 STATUSHANDLERS_POINT_NAME); 61 IExtension[] extensions = handlersPoint.getExtensions(); 62 63 statusHandlerDescriptorsMap = new StatusHandlerDescriptorsMap(); 64 65 for (int i = 0; i < extensions.length; i++) { 67 addExtension(tracker, extensions[i]); 68 } 69 70 tracker.registerHandler(this, ExtensionTracker 71 .createExtensionPointFilter(handlersPoint)); 72 73 IExtensionPoint productsPoint = Platform.getExtensionRegistry() 76 .getExtensionPoint(Platform.PI_RUNTIME, Platform.PT_PRODUCT); 77 78 tracker.registerHandler(this, ExtensionTracker 79 .createExtensionPointFilter(productsPoint)); 80 } 81 82 87 public static StatusHandlerRegistry getDefault() { 88 if (instance == null) { 89 instance = new StatusHandlerRegistry(); 90 } 91 return instance; 92 } 93 94 100 public void addExtension(IExtensionTracker tracker, IExtension extension) { 101 IConfigurationElement[] configElements = extension 102 .getConfigurationElements(); 103 for (int j = 0; j < configElements.length; j++) { 104 if (configElements[j].getName().equals(TAG_STATUSHANDLER)) { 105 StatusHandlerDescriptor descriptor = new StatusHandlerDescriptor( 106 configElements[j]); 107 tracker.registerObject(extension, descriptor, 108 IExtensionTracker.REF_STRONG); 109 statusHandlerDescriptors.add(descriptor); 110 } else if (configElements[j].getName().equals( 111 TAG_STATUSHANDLER_PRODUCTBINDING)) { 112 StatusHandlerProductBindingDescriptor descriptor = new StatusHandlerProductBindingDescriptor( 113 configElements[j]); 114 tracker.registerObject(extension, descriptor, 115 IExtensionTracker.REF_STRONG); 116 productBindingDescriptors.add(descriptor); 117 } 118 } 119 buildHandlersStructure(); 120 } 121 122 128 public void removeExtension(IExtension extension, Object [] objects) { 129 for (int i = 0; i < objects.length; i++) { 130 if (objects[i] instanceof StatusHandlerDescriptor) { 131 statusHandlerDescriptors.remove(objects[i]); 132 } else if (objects[i] instanceof StatusHandlerProductBindingDescriptor) { 133 productBindingDescriptors.remove(objects[i]); 134 } 135 } 136 buildHandlersStructure(); 137 } 138 139 145 public StatusHandlerDescriptor getDefaultHandlerDescriptor() { 146 return defaultHandlerDescriptor; 147 } 148 149 156 public List getHandlerDescriptors(String pluginId) { 157 return statusHandlerDescriptorsMap.getHandlerDescriptors(pluginId); 158 } 159 160 167 public StatusHandlerDescriptor getHandlerDescriptor(String statusHandlerId) { 168 StatusHandlerDescriptor descriptor = null; 169 for (Iterator it = statusHandlerDescriptors.iterator(); it.hasNext();) { 170 descriptor = (StatusHandlerDescriptor) it.next(); 171 if (descriptor.getId().equals(statusHandlerId)) { 172 return descriptor; 173 } 174 } 175 176 if (defaultHandlerDescriptor != null 177 && defaultHandlerDescriptor.getId().equals(statusHandlerId)) { 178 return defaultHandlerDescriptor; 179 } 180 181 return null; 182 } 183 184 187 public void dispose() { 188 PlatformUI.getWorkbench().getExtensionTracker().unregisterHandler(this); 189 } 190 191 195 private void buildHandlersStructure() { 196 statusHandlerDescriptorsMap.clear(); 197 defaultHandlerDescriptor = null; 198 199 String productId = Platform.getProduct() != null ? Platform 200 .getProduct().getId() : null; 201 202 List allHandlers = new ArrayList (); 203 204 String defaultHandlerId = null; 205 206 for (Iterator it = productBindingDescriptors.iterator(); it.hasNext();) { 207 StatusHandlerProductBindingDescriptor descriptor = ((StatusHandlerProductBindingDescriptor) it 208 .next()); 209 210 if (descriptor.getProductId().equals(productId)) { 211 defaultHandlerId = descriptor.getHandlerId(); 212 } 213 } 214 215 for (Iterator it = statusHandlerDescriptors.iterator(); it.hasNext();) { 216 StatusHandlerDescriptor descriptor = ((StatusHandlerDescriptor) it 217 .next()); 218 219 allHandlers.add(descriptor); 220 } 221 222 StatusHandlerDescriptor handlerDescriptor = null; 223 224 for (Iterator it = allHandlers.iterator(); it.hasNext();) { 225 handlerDescriptor = (StatusHandlerDescriptor) it.next(); 226 227 if (handlerDescriptor.getId().equals(defaultHandlerId)) { 228 defaultHandlerDescriptor = handlerDescriptor; 229 } else { 230 statusHandlerDescriptorsMap 231 .addHandlerDescriptor(handlerDescriptor); 232 } 233 } 234 } 235 } 236 | Popular Tags |