KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > 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.ide;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.StringTokenizer JavaDoc;
15
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.ui.ISharedImages;
19 import org.eclipse.ui.PlatformUI;
20 import org.eclipse.ui.WorkbenchException;
21 import org.eclipse.ui.model.IWorkbenchAdapter;
22 import org.eclipse.ui.model.WorkbenchAdapter;
23
24 /**
25  * Category provides for hierarchical grouping of elements
26  * registered in the registry. One extension normally defines
27  * a category, and other reference it via its ID.
28  * <p>
29  * A category may specify its parent category in order to
30  * achieve hierarchy.
31  * </p>
32  */

33 public class Category extends WorkbenchAdapter {
34     /**
35      * Name of the miscellaneous category
36      */

37     public final static String JavaDoc MISC_NAME = IDEWorkbenchMessages.ICategory_other;
38
39     /**
40      * Identifier of the miscellaneous category
41      */

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

44     private static final String JavaDoc ATT_ID = "id"; //$NON-NLS-1$
45

46     private static final String JavaDoc ATT_PARENT = "parentCategory"; //$NON-NLS-1$
47

48     private static final String JavaDoc ATT_NAME = "name"; //$NON-NLS-1$
49

50     private String JavaDoc id;
51
52     private String JavaDoc name;
53
54     private String JavaDoc[] parentPath;
55
56     private String JavaDoc unparsedPath;
57
58     private ArrayList JavaDoc elements;
59
60     /**
61      * Creates an instance of <code>Category</code> as a
62      * miscellaneous category.
63      */

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

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

89     public Category(IConfigurationElement configElement)
90             throws WorkbenchException {
91         id = configElement.getAttribute(ATT_ID);
92         name = configElement.getAttribute(ATT_NAME);
93         unparsedPath = configElement.getAttribute(ATT_PARENT);
94
95         if (id == null || name == null) {
96             throw new WorkbenchException("Invalid category: " + id); //$NON-NLS-1$
97
}
98     }
99
100     /* (non-Javadoc)
101      * Method declared on ICategory.
102      */

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

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

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

131     public ImageDescriptor getImageDescriptor(Object JavaDoc object) {
132         return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
133                 ISharedImages.IMG_OBJ_FOLDER);
134     }
135
136     /* (non-Javadoc)
137      * Method declared on IWorkbenchAdapter.
138      */

139     public String JavaDoc getLabel(Object JavaDoc o) {
140         return getLabel();
141     }
142
143     /* (non-Javadoc)
144      * Method declared on ICategory.
145      */

146     public String JavaDoc getId() {
147         return id;
148     }
149
150     /* (non-Javadoc)
151      * Method declared on ICategory.
152      */

153     public String JavaDoc getLabel() {
154         return name;
155     }
156
157     /* (non-Javadoc)
158      * Method declared on ICategory.
159      */

160     public String JavaDoc[] getParentPath() {
161         if (unparsedPath != null) {
162             StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(unparsedPath, "/"); //$NON-NLS-1$
163
parentPath = new String JavaDoc[stok.countTokens()];
164             for (int i = 0; stok.hasMoreTokens(); i++) {
165                 parentPath[i] = stok.nextToken();
166             }
167             unparsedPath = null;
168         }
169
170         return parentPath;
171     }
172
173     /* (non-Javadoc)
174      * Method declared on ICategory.
175      */

176     public String JavaDoc getRootPath() {
177         String JavaDoc[] path = getParentPath();
178         if (path != null && path.length > 0) {
179             return path[0];
180         } else {
181             return id;
182         }
183     }
184
185     /* (non-Javadoc)
186      * Method declared on ICategory.
187      */

188     public ArrayList JavaDoc getElements() {
189         return elements;
190     }
191
192     /* (non-Javadoc)
193      * Method declared on ICategory.
194      */

195     public boolean hasElements() {
196         if (elements != null) {
197             return !elements.isEmpty();
198         } else {
199             return false;
200         }
201     }
202 }
203
Popular Tags