1 11 package org.eclipse.ui.internal.components.registry; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 20 import org.eclipse.core.runtime.IExtension; 21 import org.eclipse.core.runtime.IExtensionDelta; 22 import org.eclipse.core.runtime.IExtensionPoint; 23 import org.eclipse.core.runtime.IRegistryChangeEvent; 24 import org.eclipse.core.runtime.IRegistryChangeListener; 25 import org.eclipse.core.runtime.Platform; 26 import org.osgi.framework.Bundle; 27 28 31 public class ExtensionPointManager { 32 33 private String namespace; 34 private Map monitors = new HashMap (); 35 private IRegistryChangeListener listener = new IRegistryChangeListener() { 36 37 public void registryChanged(IRegistryChangeEvent event) { 38 IExtensionDelta[] deltas = event.getExtensionDeltas(); 39 40 for (int i = 0; i < deltas.length; i++) { 41 IExtensionDelta delta = deltas[i]; 42 43 IExtensionPoint point = delta.getExtensionPoint(); 45 if (!point.getNamespace().equals(namespace)) { 46 continue; 47 } 48 49 String extensionPoint = point.getSimpleIdentifier(); 50 51 List listeners = (List )monitors.get(extensionPoint); 52 if (listeners != null) { 53 for (Iterator iter = listeners.iterator(); iter.hasNext();) { 54 IExtensionPointMonitor monitor = (IExtensionPointMonitor) iter.next(); 55 56 notifyMonitor(monitor, delta.getExtension(), delta.getKind()); 57 } 58 } 59 } 60 } 61 62 }; 63 64 private static void notifyMonitor(IExtensionPointMonitor monitor, IExtension extension, int kind) { 65 if (kind == IExtensionDelta.ADDED) { 66 monitor.added(extension); 67 } else if (kind == IExtensionDelta.REMOVED) { 68 monitor.removed(extension); 69 } 70 } 71 72 public ExtensionPointManager(Bundle bundle) { 73 this.namespace = bundle.getSymbolicName(); 74 Platform.getExtensionRegistry().addRegistryChangeListener(listener); 75 } 76 77 82 public void dispose() { 83 Collection extensionPoints = monitors.keySet(); 84 for (Iterator iter = extensionPoints.iterator(); iter.hasNext();) { 85 String pointId = (String ) iter.next(); 86 87 List listeners = (List )monitors.get(pointId); 88 89 for (Iterator iter2 = listeners.iterator(); iter2.hasNext();) { 90 IExtensionPointMonitor monitor = (IExtensionPointMonitor) iter2.next(); 91 92 loadExtensionPoint(pointId, monitor, IExtensionDelta.REMOVED); 93 } 94 } 95 96 Platform.getExtensionRegistry().removeRegistryChangeListener(listener); 97 } 98 99 107 public void addMonitor(String extensionPoint, IExtensionPointMonitor newMonitor) { 108 List listeners = (List )monitors.get(extensionPoint); 109 110 if (listeners == null) { 111 listeners = new ArrayList (); 112 } 113 114 if (listeners.contains(newMonitor)) { 115 return; 116 } 117 118 listeners.add(newMonitor); 119 loadExtensionPoint(extensionPoint, newMonitor, IExtensionDelta.ADDED); 120 } 121 122 130 public void removeMonitor(String extensionPoint, IExtensionPointMonitor oldMonitor) { 131 List listeners = (List )monitors.get(extensionPoint); 132 133 if (listeners == null) { 134 return; 135 } 136 137 if (!listeners.contains(oldMonitor)) { 138 return; 139 } 140 141 listeners.remove(oldMonitor); 142 if (listeners.isEmpty()) { 143 monitors.remove(extensionPoint); 144 } 145 146 loadExtensionPoint(extensionPoint, oldMonitor, IExtensionDelta.REMOVED); 147 } 148 149 private void loadExtensionPoint(String pointId, IExtensionPointMonitor monitor, int kind) { 150 151 IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(namespace, pointId); 153 154 IExtension[] extensions = extensionPoint.getExtensions(); 155 for (int i = 0; i < extensions.length; i++) { 156 IExtension extension = extensions[i]; 157 158 notifyMonitor(monitor, extension, kind); 159 } 160 } 161 162 } 163 | Popular Tags |