KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.StringTokenizer JavaDoc;
15
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.core.runtime.IConfigurationElement;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.ui.IPluginContribution;
20 import org.eclipse.ui.ISharedImages;
21 import org.eclipse.ui.WorkbenchException;
22 import org.eclipse.ui.internal.WorkbenchImages;
23 import org.eclipse.ui.internal.WorkbenchMessages;
24 import org.eclipse.ui.model.IWorkbenchAdapter;
25
26 /**
27  * Category provides for hierarchical grouping of elements
28  * registered in the registry. One extension normally defines
29  * a category, and other reference it via its ID.
30  * <p>
31  * A category may specify its parent category in order to
32  * achieve hierarchy.
33  * </p>
34  */

35 public class Category implements IWorkbenchAdapter, IPluginContribution, IAdaptable {
36     /**
37      * Name of the miscellaneous category
38      */

39     public final static String JavaDoc MISC_NAME = WorkbenchMessages.ICategory_other;
40
41     /**
42      * Identifier of the miscellaneous category
43      */

44     public final static String JavaDoc MISC_ID = "org.eclipse.ui.internal.otherCategory"; //$NON-NLS-1$
45

46     private String JavaDoc id;
47
48     private String JavaDoc name;
49
50     private String JavaDoc[] parentPath;
51
52     private ArrayList JavaDoc elements;
53
54     private IConfigurationElement configurationElement;
55
56     private String JavaDoc pluginId;
57
58     /**
59      * Creates an instance of <code>Category</code> as a
60      * miscellaneous category.
61      */

62     public Category() {
63         this.id = MISC_ID;
64         this.name = MISC_NAME;
65         this.pluginId = MISC_ID; // TODO: remove hack for bug 55172
66
}
67
68     /**
69      * Creates an instance of <code>Category</code> with
70      * an ID and label.
71      *
72      * @param id the unique identifier for the category
73      * @param label the presentation label for this category
74      */

75     public Category(String JavaDoc id, String JavaDoc label) {
76         this.id = id;
77         this.name = label;
78     }
79
80     /**
81      * Creates an instance of <code>Category</code> using the
82      * information from the specified configuration element.
83      *
84      * @param configElement the <code>IConfigurationElement<code> containing
85      * the ID, label, and optional parent category path.
86      * @throws WorkbenchException if the ID or label is <code>null</code
87      */

88     public Category(IConfigurationElement configElement)
89             throws WorkbenchException {
90         id = configElement.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
91
92         configurationElement = configElement;
93         if (id == null || getLabel() == null) {
94             throw new WorkbenchException("Invalid category: " + id); //$NON-NLS-1$
95
}
96     }
97
98
99     /**
100      * Add an element to this category.
101      *
102      * @param element the element to add
103      */

104     public void addElement(Object JavaDoc element) {
105         if (elements == null) {
106             elements = new ArrayList JavaDoc(5);
107         }
108         elements.add(element);
109     }
110
111     /* (non-Javadoc)
112      * Method declared on IAdaptable.
113      */

114     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
115         if (adapter == IWorkbenchAdapter.class) {
116             return this;
117         } else if (adapter == IConfigurationElement.class) {
118             return configurationElement;
119         } else {
120             return null;
121         }
122     }
123
124     /* (non-Javadoc)
125      * Method declared on IWorkbenchAdapter.
126      */

127     public Object JavaDoc[] getChildren(Object JavaDoc o) {
128         return getElements().toArray();
129     }
130
131     /* (non-Javadoc)
132      * Method declared on IWorkbenchAdapter.
133      */

134     public ImageDescriptor getImageDescriptor(Object JavaDoc object) {
135         return WorkbenchImages.getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER);
136     }
137
138     /* (non-Javadoc)
139      * Method declared on IWorkbenchAdapter.
140      */

141     public String JavaDoc getLabel(Object JavaDoc o) {
142         return getLabel();
143     }
144
145     /**
146      * Return the id for this category.
147      * @return the id
148      */

149     public String JavaDoc getId() {
150         return id;
151     }
152
153     /**
154      * Return the label for this category.
155      *
156      * @return the label
157      */

158     public String JavaDoc getLabel() {
159         return configurationElement == null ? name : configurationElement
160                 .getAttribute(IWorkbenchRegistryConstants.ATT_NAME);
161     }
162
163     /**
164      * Return the parent path for this category.
165      *
166      * @return the parent path
167      */

168     public String JavaDoc[] getParentPath() {
169         if (parentPath != null) {
170             return parentPath;
171         }
172         
173         String JavaDoc unparsedPath = getRawParentPath();
174         if (unparsedPath != null) {
175             StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(unparsedPath, "/"); //$NON-NLS-1$
176
parentPath = new String JavaDoc[stok.countTokens()];
177             for (int i = 0; stok.hasMoreTokens(); i++) {
178                 parentPath[i] = stok.nextToken();
179             }
180         }
181
182         return parentPath;
183     }
184     
185     /**
186      * Return the unparsed parent path. May be <code>null</code>.
187      *
188      * @return the unparsed parent path or <code>null</code>
189      */

190     public String JavaDoc getRawParentPath() {
191         return configurationElement == null ? null
192                 : configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_PARENT_CATEGORY);
193     }
194
195     /**
196      * Return the root path for this category.
197      *
198      * @return the root path
199      */

200     public String JavaDoc getRootPath() {
201         String JavaDoc[] path = getParentPath();
202         if (path != null && path.length > 0) {
203             return path[0];
204         }
205         
206         return id;
207     }
208
209     /**
210      * Return the elements contained in this category.
211      *
212      * @return the elements
213      */

214     public ArrayList JavaDoc getElements() {
215         return elements;
216     }
217
218     /**
219      * Return whether a given object exists in this category.
220      *
221      * @param o the object to search for
222      * @return whether the object is in this category
223      */

224     public boolean hasElement(Object JavaDoc o) {
225         if (elements == null) {
226             return false;
227         }
228         if (elements.isEmpty()) {
229             return false;
230         }
231         return elements.contains(o);
232     }
233
234     /**
235      * Return whether this category has child elements.
236      *
237      * @return whether this category has child elements
238      */

239     public boolean hasElements() {
240         if (elements != null) {
241             return !elements.isEmpty();
242         }
243         
244         return false;
245     }
246
247     /* (non-Javadoc)
248      * @see org.eclipse.ui.model.IWorkbenchAdapter#getParent(java.lang.Object)
249      */

250     public Object JavaDoc getParent(Object JavaDoc o) {
251         return null;
252     }
253
254     /* (non-Javadoc)
255      * @see org.eclipse.ui.activities.support.IPluginContribution#getLocalId()
256      */

257     public String JavaDoc getLocalId() {
258         return id;
259     }
260
261     /* (non-Javadoc)
262      * @see org.eclipse.ui.activities.support.IPluginContribution#getPluginId()
263      */

264     public String JavaDoc getPluginId() {
265         return configurationElement == null ? pluginId : configurationElement
266                 .getNamespace();
267     }
268
269     /**
270      * Clear all elements from this category.
271      *
272      * @since 3.1
273      */

274     public void clear() {
275         if (elements != null) {
276             elements.clear();
277         }
278     }
279 }
280
Popular Tags