KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-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.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.commands.contexts.Context;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IConfigurationElement;
21 import org.eclipse.core.runtime.IExtension;
22 import org.eclipse.core.runtime.IExtensionPoint;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
25 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
26 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
27 import org.eclipse.ui.PlatformUI;
28 import org.eclipse.ui.contexts.IContextService;
29 import org.eclipse.ui.internal.WorkbenchPlugin;
30
31 /**
32  * The registry of action set extensions.
33  */

34 public class ActionSetRegistry implements IExtensionChangeHandler {
35     
36     /**
37      * @since 3.1
38      */

39     private class ActionSetPartAssociation {
40         /**
41          * @param partId
42          * @param actionSetId
43          */

44         public ActionSetPartAssociation(String JavaDoc partId, String JavaDoc actionSetId) {
45             this.partId = partId;
46             this.actionSetId = actionSetId;
47         }
48         
49         
50         String JavaDoc partId;
51         String JavaDoc actionSetId;
52     }
53     
54     private ArrayList JavaDoc children = new ArrayList JavaDoc();
55
56     private Map JavaDoc mapPartToActionSetIds = new HashMap JavaDoc();
57     
58     private Map JavaDoc mapPartToActionSets = new HashMap JavaDoc();
59
60     private IContextService contextService;
61     
62     /**
63      * Creates the action set registry.
64      */

65     public ActionSetRegistry() {
66         contextService = (IContextService) PlatformUI
67                 .getWorkbench().getService(IContextService.class);
68         PlatformUI.getWorkbench().getExtensionTracker().registerHandler(
69                 this,
70                 ExtensionTracker
71                         .createExtensionPointFilter(new IExtensionPoint[] {
72                                 getActionSetExtensionPoint(),
73                                 getActionSetPartAssociationExtensionPoint() }));
74         readFromRegistry();
75     }
76
77     /**
78      * Return the action set part association extension point.
79      *
80      * @return the action set part association extension point
81      * @since 3.1
82      */

83     private IExtensionPoint getActionSetPartAssociationExtensionPoint() {
84         return Platform
85         .getExtensionRegistry().getExtensionPoint(
86                 PlatformUI.PLUGIN_ID,
87                 IWorkbenchRegistryConstants.PL_ACTION_SET_PART_ASSOCIATIONS);
88     }
89
90     /**
91      * Return the action set extension point.
92      *
93      * @return the action set extension point
94      * @since 3.1
95      */

96     private IExtensionPoint getActionSetExtensionPoint() {
97         return Platform
98                 .getExtensionRegistry().getExtensionPoint(
99                         PlatformUI.PLUGIN_ID,
100                         IWorkbenchRegistryConstants.PL_ACTION_SETS);
101     }
102
103     /**
104      * Adds an action set.
105      * @param desc
106      */

107     private void addActionSet(ActionSetDescriptor desc) {
108         children.add(desc);
109         Context actionSetContext = contextService.getContext(desc.getId());
110         if (!actionSetContext.isDefined()) {
111             actionSetContext.define(desc.getLabel(), desc.getDescription(),
112                     "org.eclipse.ui.contexts.actionSet"); //$NON-NLS-1$
113
}
114     }
115
116     /**
117      * Remove the action set.
118      *
119      * @param desc
120      */

121     private void removeActionSet(IActionSetDescriptor desc) {
122         Context actionSetContext = contextService.getContext(desc.getId());
123         if (actionSetContext.isDefined()) {
124             actionSetContext.undefine();
125         }
126         children.remove(desc);
127     }
128
129     /**
130      * Adds an association between an action set an a part.
131      */

132     private Object JavaDoc addAssociation(String JavaDoc actionSetId, String JavaDoc partId) {
133         // get the action set ids for this part
134
ArrayList JavaDoc actionSets = (ArrayList JavaDoc) mapPartToActionSetIds.get(partId);
135         if (actionSets == null) {
136             actionSets = new ArrayList JavaDoc();
137             mapPartToActionSetIds.put(partId, actionSets);
138         }
139         actionSets.add(actionSetId);
140         
141         ActionSetPartAssociation association = new ActionSetPartAssociation(partId, actionSetId);
142         return association;
143     }
144
145     /**
146      * Finds and returns the registered action set with the given id.
147      *
148      * @param id the action set id
149      * @return the action set, or <code>null</code> if none
150      * @see IActionSetDescriptor#getId
151      */

152     public IActionSetDescriptor findActionSet(String JavaDoc id) {
153         Iterator JavaDoc i = children.iterator();
154         while (i.hasNext()) {
155             IActionSetDescriptor desc = (IActionSetDescriptor) i.next();
156             if (desc.getId().equals(id)) {
157                 return desc;
158             }
159         }
160         return null;
161     }
162
163     /**
164      * Returns a list of the action sets known to the workbench.
165      *
166      * @return a list of action sets
167      */

168     public IActionSetDescriptor[] getActionSets() {
169         return (IActionSetDescriptor []) children.toArray(new IActionSetDescriptor [children.size()]);
170     }
171
172     /**
173      * Returns a list of the action sets associated with the given part id.
174      *
175      * @param partId the part id
176      * @return a list of action sets
177      */

178     public IActionSetDescriptor[] getActionSetsFor(String JavaDoc partId) {
179         // check the resolved map first
180
ArrayList JavaDoc actionSets = (ArrayList JavaDoc) mapPartToActionSets.get(partId);
181         if (actionSets != null) {
182             return (IActionSetDescriptor[]) actionSets
183                     .toArray(new IActionSetDescriptor[actionSets.size()]);
184         }
185         
186         // get the action set ids for this part
187
ArrayList JavaDoc actionSetIds = (ArrayList JavaDoc) mapPartToActionSetIds.get(partId);
188         if (actionSetIds == null) {
189             return new IActionSetDescriptor[0];
190         }
191         
192         // resolve to action sets
193
actionSets = new ArrayList JavaDoc(actionSetIds.size());
194         for (Iterator JavaDoc i = actionSetIds.iterator(); i.hasNext();) {
195             String JavaDoc actionSetId = (String JavaDoc) i.next();
196             IActionSetDescriptor actionSet = findActionSet(actionSetId);
197             if (actionSet != null) {
198                 actionSets.add(actionSet);
199             } else {
200                WorkbenchPlugin.log("Unable to associate action set with part: " + //$NON-NLS-1$
201
partId + ". Action set " + actionSetId + " not found."); //$NON-NLS-2$ //$NON-NLS-1$
202
}
203         }
204         
205         mapPartToActionSets.put(partId, actionSets);
206         
207         return (IActionSetDescriptor[]) actionSets
208                 .toArray(new IActionSetDescriptor[actionSets.size()]);
209     }
210
211     /**
212      * Reads the registry.
213      */

214     private void readFromRegistry() {
215         IExtension[] extensions = getActionSetExtensionPoint().getExtensions();
216         for (int i = 0; i < extensions.length; i++) {
217             addActionSets(PlatformUI.getWorkbench().getExtensionTracker(),
218                     extensions[i]);
219         }
220
221         extensions = getActionSetPartAssociationExtensionPoint()
222                 .getExtensions();
223         for (int i = 0; i < extensions.length; i++) {
224             addActionSetPartAssociations(PlatformUI.getWorkbench()
225                     .getExtensionTracker(), extensions[i]);
226         }
227     }
228
229     /* (non-Javadoc)
230      * @see org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamichelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
231      */

232     public void addExtension(IExtensionTracker tracker, IExtension extension) {
233         String JavaDoc extensionPointUniqueIdentifier = extension.getExtensionPointUniqueIdentifier();
234         if (extensionPointUniqueIdentifier.equals(getActionSetExtensionPoint().getUniqueIdentifier())) {
235             addActionSets(tracker, extension);
236         }
237         else if (extensionPointUniqueIdentifier.equals(getActionSetPartAssociationExtensionPoint().getUniqueIdentifier())){
238             addActionSetPartAssociations(tracker, extension);
239         }
240     }
241
242     /**
243      * @param tracker
244      * @param extension
245      */

246     private void addActionSetPartAssociations(IExtensionTracker tracker, IExtension extension) {
247         IConfigurationElement [] elements = extension.getConfigurationElements();
248         for (int i = 0; i < elements.length; i++) {
249             IConfigurationElement element = elements[i];
250             if (element.getName().equals(IWorkbenchRegistryConstants.TAG_ACTION_SET_PART_ASSOCIATION)) {
251                 String JavaDoc actionSetId = element.getAttribute(IWorkbenchRegistryConstants.ATT_TARGET_ID);
252                 IConfigurationElement[] children = element.getChildren();
253                 for (int j = 0; j < children.length; j++) {
254                     IConfigurationElement child = children[j];
255                     if (child.getName().equals(IWorkbenchRegistryConstants.TAG_PART)) {
256                         String JavaDoc partId = child.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
257                         if (partId != null) {
258                             Object JavaDoc trackingObject = addAssociation(actionSetId, partId);
259                             if (trackingObject != null) {
260                                 tracker.registerObject(extension,
261                                         trackingObject,
262                                         IExtensionTracker.REF_STRONG);
263
264                             }
265                             
266                         }
267                     } else {
268                         WorkbenchPlugin.log("Unable to process element: " + //$NON-NLS-1$
269
child.getName() + " in action set part associations extension: " + //$NON-NLS-1$
270
extension.getUniqueIdentifier());
271                     }
272                 }
273             }
274         }
275
276         // TODO: optimize
277
mapPartToActionSets.clear();
278     }
279
280     /**
281      * @param tracker
282      * @param extension
283      */

284     private void addActionSets(IExtensionTracker tracker, IExtension extension) {
285         IConfigurationElement [] elements = extension.getConfigurationElements();
286         for (int i = 0; i < elements.length; i++) {
287             IConfigurationElement element = elements[i];
288             if (element.getName().equals(IWorkbenchRegistryConstants.TAG_ACTION_SET)) {
289                 try {
290                     ActionSetDescriptor desc = new ActionSetDescriptor(element);
291                     addActionSet(desc);
292                     tracker.registerObject(extension, desc, IExtensionTracker.REF_WEAK);
293
294                 } catch (CoreException e) {
295                     // log an error since its not safe to open a dialog here
296
WorkbenchPlugin
297                             .log(
298                                     "Unable to create action set descriptor.", e.getStatus());//$NON-NLS-1$
299
}
300             }
301         }
302
303         // TODO: optimize
304
mapPartToActionSets.clear();
305     }
306
307     /* (non-Javadoc)
308      * @see org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension, java.lang.Object[])
309      */

310     public void removeExtension(IExtension extension, Object JavaDoc[] objects) {
311         String JavaDoc extensionPointUniqueIdentifier = extension.getExtensionPointUniqueIdentifier();
312         if (extensionPointUniqueIdentifier.equals(getActionSetExtensionPoint().getUniqueIdentifier())) {
313             removeActionSets(objects);
314         }
315         else if (extensionPointUniqueIdentifier.equals(getActionSetPartAssociationExtensionPoint().getUniqueIdentifier())){
316             removeActionSetPartAssociations(objects);
317         }
318     }
319
320     /**
321      * @param objects
322      */

323     private void removeActionSetPartAssociations(Object JavaDoc[] objects) {
324         for (int i = 0; i < objects.length; i++) {
325             Object JavaDoc object = objects[i];
326             if (object instanceof ActionSetPartAssociation) {
327                 ActionSetPartAssociation association = (ActionSetPartAssociation) object;
328                 String JavaDoc actionSetId = association.actionSetId;
329                 ArrayList JavaDoc actionSets = (ArrayList JavaDoc) mapPartToActionSetIds.get(association.partId);
330                 if (actionSets == null) {
331                     return;
332                 }
333                 actionSets.remove(actionSetId);
334                 if (actionSets.isEmpty()) {
335                     mapPartToActionSetIds.remove(association.partId);
336                 }
337             }
338         }
339         // TODO: optimize
340
mapPartToActionSets.clear();
341         
342     }
343
344     /**
345      * @param objects
346      */

347     private void removeActionSets(Object JavaDoc[] objects) {
348         for (int i = 0; i < objects.length; i++) {
349             Object JavaDoc object = objects[i];
350             if (object instanceof IActionSetDescriptor) {
351                 IActionSetDescriptor desc = (IActionSetDescriptor) object;
352                 removeActionSet(desc);
353
354                 // now clean up the part associations
355
// TODO: this is expensive. We should consider another map from
356
// actionsets
357
// to parts.
358
for (Iterator JavaDoc j = mapPartToActionSetIds.values().iterator(); j
359                         .hasNext();) {
360                     ArrayList JavaDoc list = (ArrayList JavaDoc) j.next();
361                     list.remove(desc.getId());
362                     if (list.isEmpty()) {
363                         j.remove();
364                     }
365                 }
366             }
367         }
368         // TODO: optimize
369
mapPartToActionSets.clear();
370     }
371 }
372
Popular Tags