KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > 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.cheatsheets.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.WorkbenchException;
21 import org.eclipse.ui.internal.cheatsheets.Messages;
22 import org.eclipse.ui.model.IWorkbenchAdapter;
23
24 /**
25  * Category provides for hierarchical grouping of elements registered in the
26  * registry. One extension normally defines a category, and other reference it
27  * via its ID.
28  * <p>
29  * A category may specify its parent category in order to achieve hierarchy.
30  * </p>
31  */

32 public class Category implements IWorkbenchAdapter, IPluginContribution,
33         IAdaptable {
34     /**
35      * Name of the miscellaneous category
36      */

37     public final static String JavaDoc MISC_NAME = Messages.CATEGORY_OTHER;
38
39     /**
40      */

41     public final static String JavaDoc MISC_ID = "org.eclipse.ui.cheatsheets.otherCategory"; //$NON-NLS-1$
42

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

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

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

87     public Category(IConfigurationElement configElement)
88             throws WorkbenchException {
89         id = configElement.getAttribute("id"); //$NON-NLS-1$
90

91         configurationElement = configElement;
92         if (id == null || getLabel() == null)
93             throw new WorkbenchException("Invalid category: " + id); //$NON-NLS-1$
94
}
95
96     /**
97      * Add an element to this category.
98      *
99      * @param element
100      * the element to add
101      */

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

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

123     public Object JavaDoc[] getChildren(Object JavaDoc o) {
124         return getElements().toArray();
125     }
126
127     /*
128      * (non-Javadoc) Method declared on IWorkbenchAdapter.
129      */

130     public ImageDescriptor getImageDescriptor(Object JavaDoc object) {
131         return null;
132     }
133
134     /*
135      * (non-Javadoc) Method declared on IWorkbenchAdapter.
136      */

137     public String JavaDoc getLabel(Object JavaDoc o) {
138         return getLabel();
139     }
140
141     /**
142      * Return the id for this category.
143      *
144      * @return the id
145      */

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

155     public String JavaDoc getLabel() {
156         return configurationElement == null ? name : configurationElement
157                 .getAttribute("name"); //$NON-NLS-1$
158
}
159
160     /**
161      * Return the parent path for this category.
162      *
163      * @return the parent path
164      */

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

186     public String JavaDoc getRawParentPath() {
187         return configurationElement == null ? null : configurationElement
188                 .getAttribute("parentCategory"); //$NON-NLS-1$
189
}
190
191     /**
192      * Return the root path for this category.
193      *
194      * @return the root path
195      */

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

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

220     public boolean hasElement(Object JavaDoc o) {
221         if (elements == null)
222             return false;
223         if (elements.isEmpty())
224             return false;
225         return elements.contains(o);
226     }
227
228     /**
229      * Return whether this category has child elements.
230      *
231      * @return whether this category has child elements
232      */

233     public boolean hasElements() {
234         if (elements != null)
235             return !elements.isEmpty();
236
237         return false;
238     }
239
240     /*
241      * (non-Javadoc)
242      *
243      * @see org.eclipse.ui.model.IWorkbenchAdapter#getParent(java.lang.Object)
244      */

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

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

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

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