KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > registry > RegistryManager


1 /*******************************************************************************
2  * Copyright (c) 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.registry;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.ListIterator JavaDoc;
18 import java.util.Set JavaDoc;
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 JavaDoc elementId;
30     private String JavaDoc extPtId;
31     private HashMap JavaDoc 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 JavaDoc INTERNAL_REGISTRY_ADDITION = "InternalRegistryAddition"; //$NON-NLS-1$
37
private class RegistryElement {
38         private int state;
39         private ArrayList JavaDoc realObjects = null;
40         
41         public RegistryElement(Object JavaDoc obj) {
42             state = REGISTRY_CACHE_STATE_UNKNOWN;
43             if (realObjects == null) {
44                 realObjects = new ArrayList JavaDoc();
45             }
46             realObjects.add(obj);
47         }
48         
49         public void addNewObject (Object JavaDoc obj) {
50             if (realObjects == null) {
51                 realObjects = new ArrayList JavaDoc();
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 JavaDoc getRealObjects() {
63             return realObjects;
64         }
65     }
66     
67     public RegistryManager(String JavaDoc elementId, String JavaDoc extPtId) {
68         this.elementId = elementId;
69         this.extPtId = extPtId;
70         cache = new HashMap JavaDoc();
71     }
72     
73     public RegistryManager getCache() {
74         return this;
75     }
76     
77     public Object JavaDoc[] getRegistryObjects() {
78         Object JavaDoc[] regElements = cache.values().toArray();
79         if (regElements.length == 0)
80             return null;
81         ArrayList JavaDoc retList = new ArrayList JavaDoc();
82         for (int i = 0; i < regElements.length; i++) {
83             ArrayList JavaDoc listElement = ((RegistryElement)regElements[i]).getRealObjects();
84             if (listElement != null) {
85                 ListIterator JavaDoc iter = listElement.listIterator();
86                 while (iter.hasNext()) {
87                     retList.add(iter.next());
88                 }
89             }
90         }
91         retList.trimToSize();
92         Object JavaDoc[] 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             // Just retrieve any changes relating to the extension point
105
// org.eclipse.ui.perspectives
106
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                 // Only do the post-change processing if something was
114
// actually changed in the registry. If there were no
115
// deltas of relevance to this registry, there should be
116
// no need to do any extra post-change processing.
117
postChangeProcessing();
118             }
119         }
120     }
121     
122     private void doAdd(Display display, final IExtensionDelta delta) {
123         Runnable JavaDoc run = new Runnable JavaDoc() {
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         // Get the name of the plugin that is adding this extension. The
137
// name of the plugin that adds the extension point is us.
138
String JavaDoc pluginId = ext.getNamespace();
139         add(buildNewCacheObject(delta), pluginId);
140     }
141
142     public void add(Object JavaDoc element, String JavaDoc pluginId) {
143         if (element == null)
144             // Nothing to add, so just return.
145
return;
146         String JavaDoc toUsePluginId = pluginId;
147         if (pluginId == null || pluginId.length() == 0) {
148             // This element is being added to the registry but is not
149
// associated with a regular plug-in. Likely, the element
150
// was created programmatically and needs to exist in the
151
// registry. An example is the 'Other' category for views.
152
// Use the key INTERNAL_REGISTRY_ADDITION for these elements so
153
// they will not be removed from the registry with any plug-in
154
// removal.
155
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     /**
167      * This is a generic method that is expected to be over-written by the
168      * parent class. It should return a new element with all the relevant
169      * information from the delta.
170      * @param delta a delta from a listener on extension events
171      * @return a new object to be added to the registry cache
172      */

173     abstract public Object JavaDoc buildNewCacheObject (IExtensionDelta delta);
174     /**
175      * This is a generic method that is expected to be implemented by the
176      * parent class. It should do any processing necessary once deltas
177      * have been processed and the registry modified. Note that in some
178      * cases there may be no extra processing required.
179      */

180     abstract public void postChangeProcessing();
181     /**
182      * Flag a series of elements in this registry cache as 'to be removed'.
183      * This does not actually remove these elements as some processing may
184      * be required before they are removed.
185      *
186      * @param plugins a list of plug-in ids for each plug-in that is being
187      * removed. Only those elements in the registry cache that correspond
188      * to one of these plug-ins will be flagged for removal.
189      */

190     public void remove(String JavaDoc[] plugins) {
191         for(int i = 0; i < plugins.length; i++) {
192             remove(plugins[i]);
193         }
194     }
195     
196     public void remove(String JavaDoc 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 JavaDoc pluginId, Object JavaDoc object) {
203         if (pluginId == null)
204             pluginId = INTERNAL_REGISTRY_ADDITION;
205         RegistryElement element = (RegistryElement)cache.get(pluginId);
206         if (element != null) {
207             // Find the registry element that contains this object and
208
// remove only that one.
209
ArrayList JavaDoc realObjects = element.getRealObjects();
210             realObjects.remove(object);
211         }
212     }
213     /**
214      * Actually go through and remove any elements from the registry that
215      * are flagged for removal. This assumes that any processing required
216      * by the parent (child?) prior to removal of this element has been
217      * completed.
218      */

219     public void cleanRegistry() {
220         Set JavaDoc elements = cache.keySet();
221         Set JavaDoc keysToRemove = new HashSet JavaDoc();
222         Iterator JavaDoc iter = elements.iterator();
223         
224         while (iter.hasNext()) {
225             Object JavaDoc 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         // Now remove the deleted ones
233
Iterator JavaDoc removeIterator = keysToRemove.iterator();
234         while(removeIterator.hasNext()){
235             cache.remove(removeIterator.next());
236         }
237     }
238 }
Popular Tags