KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > registry > CapabilityRegistry


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ide.registry;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IProjectNatureDescriptor;
19 import org.eclipse.core.resources.IWorkspace;
20 import org.eclipse.core.resources.ResourcesPlugin;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IAdaptable;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Platform;
25 import org.eclipse.ui.internal.ide.Category;
26 import org.eclipse.ui.model.IWorkbenchAdapter;
27 import org.eclipse.ui.model.WorkbenchAdapter;
28
29 /**
30  * This class represents a registry of project capabilities and categories of
31  * capabilities.
32  */

33 public class CapabilityRegistry extends WorkbenchAdapter implements IAdaptable {
34     private static final String JavaDoc[] EMPTY_ID_LIST = new String JavaDoc[0];
35
36     private static final Capability[] EMPTY_CAP_LIST = new Capability[0];
37
38     private HashMap JavaDoc natureToCapability;
39
40     private ArrayList JavaDoc capabilities;
41
42     private ArrayList JavaDoc categories;
43
44     private Category miscCategory;
45
46     /**
47      * Creates a new instance of <code>CapabilityRegistry</code>
48      */

49     public CapabilityRegistry() {
50         capabilities = new ArrayList JavaDoc(30);
51         categories = new ArrayList JavaDoc(15);
52     }
53
54     /**
55      * Adds the given capability to the registry. Called by
56      * the CapabilityRegistryReader.
57      */

58     /* package */boolean addCapability(Capability capability) {
59         return capabilities.add(capability);
60     }
61
62     /**
63      * Adds the given capability category to the registry. Called
64      * by the CapabilityRegistryReader.
65      */

66     /* package */boolean addCategory(Category category) {
67         return categories.add(category);
68     }
69
70     /**
71      * Finds the capability for the given identifier, or
72      * <code>null</code> if none.
73      */

74     public Capability findCapability(String JavaDoc id) {
75         Iterator JavaDoc itr = capabilities.iterator();
76         while (itr.hasNext()) {
77             Capability cap = (Capability) itr.next();
78             if (id.equals(cap.getId())) {
79                 return cap;
80             }
81         }
82         return null;
83     }
84
85     /**
86      * Finds the category for the given identifier, or
87      * <code>null</code> if none.
88      */

89     public Category findCategory(String JavaDoc id) {
90         Iterator JavaDoc itr = categories.iterator();
91         while (itr.hasNext()) {
92             Category cat = (Category) itr.next();
93             if (id.equals(cat.getRootPath())) {
94                 return cat;
95             }
96         }
97         return null;
98     }
99
100     /**
101      * Finds the capability for each specified identifier.
102      * Any <code>null</code> entries in the resulting array
103      * are for identifiers to which no capability exist.
104      */

105     public Capability[] findCapabilities(String JavaDoc[] ids) {
106         int count = capabilities.size();
107         Capability[] results = new Capability[ids.length];
108
109         for (int i = 0; i < ids.length; i++) {
110             String JavaDoc id = ids[i];
111             for (int j = 0; j < count; j++) {
112                 Capability cap = (Capability) capabilities.get(j);
113                 if (cap.getId().equals(id)) {
114                     results[i] = cap;
115                     break;
116                 }
117             }
118         }
119
120         return results;
121     }
122
123     /**
124      * Finds the category for each specified identifier.
125      * Any <code>null</code> entries in the resulting array
126      * are for identifiers to which no category exist.
127      *
128      * @return an array of <code>ICategory</code>
129      */

130     public Category[] findCategories(String JavaDoc[] ids) {
131         int count = categories.size();
132         Category[] results = new Category[ids.length];
133
134         for (int i = 0; i < ids.length; i++) {
135             String JavaDoc id = ids[i];
136             for (int j = 0; j < count; j++) {
137                 Category cat = (Category) categories.get(j);
138                 if (cat.getId().equals(id)) {
139                     results[i] = cat;
140                     break;
141                 }
142             }
143         }
144
145         return results;
146     }
147
148     /* (non-Javadoc)
149      * Method declared on IAdaptable.
150      */

151     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
152         if (adapter == IWorkbenchAdapter.class)
153             return this;
154         else
155             return null;
156     }
157
158     /**
159      * Returns the list of categories in the registry
160      * which contain at least one capability. Does not
161      * include the misc and unknown categories.
162      */

163     public ArrayList JavaDoc getUsedCategories() {
164         ArrayList JavaDoc results = new ArrayList JavaDoc(categories.size());
165         Iterator JavaDoc itr = categories.iterator();
166         while (itr.hasNext()) {
167             Category cat = (Category) itr.next();
168             if (cat.hasElements())
169                 results.add(cat);
170         }
171         return results;
172     }
173
174     /**
175      * Returns the capability for the nature id
176      */

177     public Capability getCapabilityForNature(String JavaDoc natureId) {
178         return (Capability) natureToCapability.get(natureId);
179     }
180
181     /**
182      * Returns the list of capabilities in the registry
183      */

184     public ArrayList JavaDoc getCapabilities() {
185         return capabilities;
186     }
187
188     /* (non-Javadoc)
189      * Method declared on IWorkbenchAdapter.
190      */

191     public Object JavaDoc[] getChildren(Object JavaDoc o) {
192         return capabilities.toArray();
193     }
194
195     /**
196      * Returns the membership set ids that the specified
197      * capability belongs to.
198      */

199     public String JavaDoc[] getMembershipSetIds(Capability capability) {
200         IProjectNatureDescriptor desc = capability.getNatureDescriptor();
201         if (desc == null)
202             return EMPTY_ID_LIST;
203
204         return desc.getNatureSetIds();
205     }
206
207     /**
208      * Returns the miscellaneous category, or <code>null</code>
209      * if none.
210      */

211     public Category getMiscCategory() {
212         return miscCategory;
213     }
214
215     /**
216      * Returns the capability ids that are prerequisites
217      * of the specified capability.
218      */

219     public String JavaDoc[] getPrerequisiteIds(Capability capability) {
220         IProjectNatureDescriptor desc = capability.getNatureDescriptor();
221         if (desc == null)
222             return EMPTY_ID_LIST;
223
224         String JavaDoc[] natureIds = desc.getRequiredNatureIds();
225         if (natureIds.length == 0)
226             return EMPTY_ID_LIST;
227
228         ArrayList JavaDoc results = new ArrayList JavaDoc(natureIds.length);
229         for (int i = 0; i < natureIds.length; i++) {
230             Capability cap = (Capability) natureToCapability.get(natureIds[i]);
231             if (cap != null)
232                 results.add(cap.getId());
233         }
234
235         if (results.size() == 0) {
236             return EMPTY_ID_LIST;
237         } else {
238             String JavaDoc[] ids = new String JavaDoc[results.size()];
239             results.toArray(ids);
240             return ids;
241         }
242     }
243
244     /**
245      * Returns the capabilities assigned to the specified project
246      */

247     public Capability[] getProjectCapabilities(IProject project) {
248         try {
249             String JavaDoc[] natureIds = project.getDescription().getNatureIds();
250             ArrayList JavaDoc results = new ArrayList JavaDoc(natureIds.length);
251             for (int i = 0; i < natureIds.length; i++) {
252                 Capability cap = (Capability) natureToCapability
253                         .get(natureIds[i]);
254                 if (cap == null) {
255                     cap = new Capability(natureIds[i]);
256                     mapCapability(cap);
257                 }
258                 results.add(cap);
259             }
260
261             if (results.size() == 0) {
262                 return EMPTY_CAP_LIST;
263             } else {
264                 Capability[] caps = new Capability[results.size()];
265                 results.toArray(caps);
266                 return caps;
267             }
268         } catch (CoreException e) {
269             return EMPTY_CAP_LIST;
270         }
271     }
272
273     /**
274      * Returns the capabilities assigned to the specified project
275      * that are consideed disabled by core.
276      */

277     public Capability[] getProjectDisabledCapabilities(IProject project) {
278         try {
279             String JavaDoc[] natureIds = project.getDescription().getNatureIds();
280             ArrayList JavaDoc results = new ArrayList JavaDoc(natureIds.length);
281             for (int i = 0; i < natureIds.length; i++) {
282                 if (!project.isNatureEnabled(natureIds[i])) {
283                     Capability cap = (Capability) natureToCapability
284                             .get(natureIds[i]);
285                     if (cap == null) {
286                         cap = new Capability(natureIds[i]);
287                         mapCapability(cap);
288                     }
289                     results.add(cap);
290                 }
291             }
292
293             if (results.size() == 0) {
294                 return EMPTY_CAP_LIST;
295             } else {
296                 Capability[] caps = new Capability[results.size()];
297                 results.toArray(caps);
298                 return caps;
299             }
300         } catch (CoreException e) {
301             return EMPTY_CAP_LIST;
302         }
303     }
304
305     /**
306      * Returns whether the registry contains any capabilities.
307      */

308     public boolean hasCapabilities() {
309         return !capabilities.isEmpty();
310     }
311
312     /**
313      * Returns whether the specified capability has any prerequisites.
314      */

315     public boolean hasPrerequisites(Capability capability) {
316         return getPrerequisiteIds(capability).length > 0;
317     }
318
319     /**
320      * Loads capabilities and capability categories from the platform's plugin
321      * registry.
322      */

323     public void load() {
324         CapabilityRegistryReader reader = new CapabilityRegistryReader();
325         reader.read(Platform.getExtensionRegistry(), this);
326         mapCapabilities();
327     }
328
329     /**
330      * Maps each capability in the registry to a particular category.
331      * The category is defined in xml. If the capability's category is
332      * not found, then the capability is added to the "misc" category.
333      * <p>
334      * Maps each capability in the registry to a particular nature
335      * id.
336      */

337     /* package */void mapCapabilities() {
338         natureToCapability = new HashMap JavaDoc();
339
340         Iterator JavaDoc itr = capabilities.iterator();
341         while (itr.hasNext())
342             mapCapability((Capability) itr.next());
343     }
344
345     private void mapCapability(Capability cap) {
346         // Map to category
347
if (!cap.isValid()) {
348             if (miscCategory == null)
349                 miscCategory = new Category();
350             miscCategory.addElement(cap);
351         } else {
352             Category cat = null;
353             String JavaDoc catPath = cap.getCategoryPath();
354             if (catPath != null)
355                 cat = findCategory(catPath);
356             if (cat != null) {
357                 cat.addElement(cap);
358             } else {
359                 if (miscCategory == null)
360                     miscCategory = new Category();
361                 miscCategory.addElement(cap);
362             }
363         }
364
365         // Map to nature id
366
natureToCapability.put(cap.getNatureId(), cap);
367     }
368
369     /**
370      * Removes from the capability collection all capabilities
371      * whose UI is handle by another capability in the collection.
372      * The provided collection must be in proper prerequisite order.
373      *
374      * @param capabilities the capabilities to be pruned
375      * @return a collection of capabilities pruned
376      */

377     public Capability[] pruneCapabilities(Capability[] capabilities) {
378         ArrayList JavaDoc ids = new ArrayList JavaDoc(capabilities.length);
379         for (int i = 0; i < capabilities.length; i++)
380             ids.add(capabilities[i].getId());
381
382         for (int i = 0; i < capabilities.length; i++) {
383             ArrayList JavaDoc handleIds = capabilities[i].getHandleUIs();
384             if (handleIds != null)
385                 ids.removeAll(handleIds);
386         }
387
388         String JavaDoc[] results = new String JavaDoc[ids.size()];
389         ids.toArray(results);
390         return findCapabilities(results);
391     }
392
393     /**
394      * Checks that the collection is valid. If so, the collection is
395      * ordered based on prerequisite.
396      *
397      * @param capabilities the capabilities to be checked and ordered
398      * @return a status object with code <code>IStatus.OK</code> if
399      * the given set of natures is valid, otherwise a status
400      * object indicating what is wrong with the set. Also, the
401      * collection of capabilities specified is ordered based on
402      * prerequisite.
403      */

404     public IStatus validateCapabilities(Capability[] capabilities) {
405         String JavaDoc natures[] = new String JavaDoc[capabilities.length];
406         for (int i = 0; i < capabilities.length; i++)
407             natures[i] = capabilities[i].getNatureId();
408
409         IWorkspace workspace = ResourcesPlugin.getWorkspace();
410         IStatus status = workspace.validateNatureSet(natures);
411         if (status.isOK()) {
412             natures = workspace.sortNatureSet(natures);
413             for (int i = 0; i < natures.length; i++)
414                 capabilities[i] = (Capability) natureToCapability
415                         .get(natures[i]);
416         }
417
418         return status;
419     }
420 }
421
Popular Tags