1 11 package org.eclipse.ui.internal.registry; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.ListIterator ; 18 import java.util.Set ; 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.swt.widgets.Display; 26 import org.eclipse.ui.PlatformUI; 27 28 public abstract class RegistryManager implements IRegistryChangeListener { 29 private String elementId; 30 private String extPtId; 31 private HashMap cache; 32 public static final int REGISTRY_CACHE_STATE_UNKNOWN = 0; 33 public static final int REGISTRY_CACHE_STATE_ACTIVE = 1; 34 public static final int REGISTRY_CACHE_STATE_DELETED = 2; 35 public static final int REGISTRY_CACHE_STATE_MAX = 2; 36 public static final String INTERNAL_REGISTRY_ADDITION = "InternalRegistryAddition"; private class RegistryElement { 38 private int state; 39 private ArrayList realObjects = null; 40 41 public RegistryElement(Object obj) { 42 state = REGISTRY_CACHE_STATE_UNKNOWN; 43 if (realObjects == null) { 44 realObjects = new ArrayList (); 45 } 46 realObjects.add(obj); 47 } 48 49 public void addNewObject (Object obj) { 50 if (realObjects == null) { 51 realObjects = new ArrayList (); 52 } 53 realObjects.add(obj); 54 } 55 56 public void changeState(int newState) { 57 if ((newState > REGISTRY_CACHE_STATE_UNKNOWN) && 58 (newState <= REGISTRY_CACHE_STATE_MAX)) 59 state = newState; 60 } 61 62 public ArrayList getRealObjects() { 63 return realObjects; 64 } 65 } 66 67 public RegistryManager(String elementId, String extPtId) { 68 this.elementId = elementId; 69 this.extPtId = extPtId; 70 cache = new HashMap (); 71 } 72 73 public RegistryManager getCache() { 74 return this; 75 } 76 77 public Object [] getRegistryObjects() { 78 Object [] regElements = cache.values().toArray(); 79 if (regElements.length == 0) 80 return null; 81 ArrayList retList = new ArrayList (); 82 for (int i = 0; i < regElements.length; i++) { 83 ArrayList listElement = ((RegistryElement)regElements[i]).getRealObjects(); 84 if (listElement != null) { 85 ListIterator iter = listElement.listIterator(); 86 while (iter.hasNext()) { 87 retList.add(iter.next()); 88 } 89 } 90 } 91 retList.trimToSize(); 92 Object [] ret = retList.toArray(); 93 return ret; 94 } 95 96 public void registryChanged(IRegistryChangeEvent event) { 97 if (!PlatformUI.isWorkbenchRunning()) 98 return; 99 int numDeltas = 0; 100 Display display = PlatformUI.getWorkbench().getDisplay(); 101 if (display == null || display.isDisposed()) 102 return; 103 try { 104 IExtensionDelta delta[] = event.getExtensionDeltas(elementId, extPtId); 107 numDeltas = delta.length; 108 for (int i = 0; i < numDeltas; i++) { 109 doAdd (display, delta[i]); 110 } 111 } finally { 112 if (numDeltas > 0) { 113 postChangeProcessing(); 118 } 119 } 120 } 121 122 private void doAdd(Display display, final IExtensionDelta delta) { 123 Runnable run = new Runnable () { 124 public void run() { 125 if (!PlatformUI.isWorkbenchRunning()) 126 return; 127 add (delta); 128 } 129 }; 130 display.syncExec(run); 131 } 132 133 public void add(IExtensionDelta delta) { 134 IExtensionPoint extPt = delta.getExtensionPoint(); 135 IExtension ext = delta.getExtension(); 136 String pluginId = ext.getNamespace(); 139 add(buildNewCacheObject(delta), pluginId); 140 } 141 142 public void add(Object element, String pluginId) { 143 if (element == null) 144 return; 146 String toUsePluginId = pluginId; 147 if (pluginId == null || pluginId.length() == 0) { 148 toUsePluginId = INTERNAL_REGISTRY_ADDITION; 156 } 157 RegistryElement regElement = (RegistryElement)cache.get(toUsePluginId); 158 if (regElement == null) { 159 regElement = new RegistryElement(element); 160 cache.put(toUsePluginId, regElement); 161 } else { 162 regElement.addNewObject(element); 163 } 164 } 165 166 173 abstract public Object buildNewCacheObject (IExtensionDelta delta); 174 180 abstract public void postChangeProcessing(); 181 190 public void remove(String [] plugins) { 191 for(int i = 0; i < plugins.length; i++) { 192 remove(plugins[i]); 193 } 194 } 195 196 public void remove(String pluginId) { 197 RegistryElement element = (RegistryElement)cache.get(pluginId); 198 if (element != null) 199 element.changeState(REGISTRY_CACHE_STATE_DELETED); 200 } 201 202 public void remove(String pluginId, Object object) { 203 if (pluginId == null) 204 pluginId = INTERNAL_REGISTRY_ADDITION; 205 RegistryElement element = (RegistryElement)cache.get(pluginId); 206 if (element != null) { 207 ArrayList realObjects = element.getRealObjects(); 210 realObjects.remove(object); 211 } 212 } 213 219 public void cleanRegistry() { 220 Set elements = cache.keySet(); 221 Set keysToRemove = new HashSet (); 222 Iterator iter = elements.iterator(); 223 224 while (iter.hasNext()) { 225 Object pluginId = iter.next(); 226 RegistryElement elem = (RegistryElement)cache.get(pluginId); 227 if (elem != null && elem.state == REGISTRY_CACHE_STATE_DELETED) { 228 keysToRemove.add(pluginId); 229 } 230 } 231 232 Iterator removeIterator = keysToRemove.iterator(); 234 while(removeIterator.hasNext()){ 235 cache.remove(removeIterator.next()); 236 } 237 } 238 } | Popular Tags |