001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.registry;
011:
012: import java.util.ArrayList;
013: import java.util.StringTokenizer;
014:
015: import org.eclipse.core.runtime.IAdaptable;
016: import org.eclipse.core.runtime.IConfigurationElement;
017: import org.eclipse.jface.resource.ImageDescriptor;
018: import org.eclipse.ui.IPluginContribution;
019: import org.eclipse.ui.ISharedImages;
020: import org.eclipse.ui.WorkbenchException;
021: import org.eclipse.ui.internal.WorkbenchImages;
022: import org.eclipse.ui.internal.WorkbenchMessages;
023: import org.eclipse.ui.model.IWorkbenchAdapter;
024:
025: /**
026: * Category provides for hierarchical grouping of elements
027: * registered in the registry. One extension normally defines
028: * a category, and other reference it via its ID.
029: * <p>
030: * A category may specify its parent category in order to
031: * achieve hierarchy.
032: * </p>
033: */
034: public class Category implements IWorkbenchAdapter,
035: IPluginContribution, IAdaptable {
036: /**
037: * Name of the miscellaneous category
038: */
039: public final static String MISC_NAME = WorkbenchMessages.ICategory_other;
040:
041: /**
042: * Identifier of the miscellaneous category
043: */
044: public final static String MISC_ID = "org.eclipse.ui.internal.otherCategory"; //$NON-NLS-1$
045:
046: private String id;
047:
048: private String name;
049:
050: private String[] parentPath;
051:
052: private ArrayList elements;
053:
054: private IConfigurationElement configurationElement;
055:
056: private String pluginId;
057:
058: /**
059: * Creates an instance of <code>Category</code> as a
060: * miscellaneous category.
061: */
062: public Category() {
063: this .id = MISC_ID;
064: this .name = MISC_NAME;
065: this .pluginId = MISC_ID; // TODO: remove hack for bug 55172
066: }
067:
068: /**
069: * Creates an instance of <code>Category</code> with
070: * an ID and label.
071: *
072: * @param id the unique identifier for the category
073: * @param label the presentation label for this category
074: */
075: public Category(String id, String label) {
076: this .id = id;
077: this .name = label;
078: }
079:
080: /**
081: * Creates an instance of <code>Category</code> using the
082: * information from the specified configuration element.
083: *
084: * @param configElement the <code>IConfigurationElement<code> containing
085: * the ID, label, and optional parent category path.
086: * @throws WorkbenchException if the ID or label is <code>null</code
087: */
088: public Category(IConfigurationElement configElement)
089: throws WorkbenchException {
090: id = configElement
091: .getAttribute(IWorkbenchRegistryConstants.ATT_ID);
092:
093: configurationElement = configElement;
094: if (id == null || getLabel() == null) {
095: throw new WorkbenchException("Invalid category: " + id); //$NON-NLS-1$
096: }
097: }
098:
099: /**
100: * Add an element to this category.
101: *
102: * @param element the element to add
103: */
104: public void addElement(Object element) {
105: if (elements == null) {
106: elements = new ArrayList(5);
107: }
108: elements.add(element);
109: }
110:
111: /* (non-Javadoc)
112: * Method declared on IAdaptable.
113: */
114: public Object getAdapter(Class 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[] getChildren(Object o) {
128: return getElements().toArray();
129: }
130:
131: /* (non-Javadoc)
132: * Method declared on IWorkbenchAdapter.
133: */
134: public ImageDescriptor getImageDescriptor(Object object) {
135: return WorkbenchImages
136: .getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER);
137: }
138:
139: /* (non-Javadoc)
140: * Method declared on IWorkbenchAdapter.
141: */
142: public String getLabel(Object o) {
143: return getLabel();
144: }
145:
146: /**
147: * Return the id for this category.
148: * @return the id
149: */
150: public String getId() {
151: return id;
152: }
153:
154: /**
155: * Return the label for this category.
156: *
157: * @return the label
158: */
159: public String getLabel() {
160: return configurationElement == null ? name
161: : configurationElement
162: .getAttribute(IWorkbenchRegistryConstants.ATT_NAME);
163: }
164:
165: /**
166: * Return the parent path for this category.
167: *
168: * @return the parent path
169: */
170: public String[] getParentPath() {
171: if (parentPath != null) {
172: return parentPath;
173: }
174:
175: String unparsedPath = getRawParentPath();
176: if (unparsedPath != null) {
177: StringTokenizer stok = new StringTokenizer(unparsedPath,
178: "/"); //$NON-NLS-1$
179: parentPath = new String[stok.countTokens()];
180: for (int i = 0; stok.hasMoreTokens(); i++) {
181: parentPath[i] = stok.nextToken();
182: }
183: }
184:
185: return parentPath;
186: }
187:
188: /**
189: * Return the unparsed parent path. May be <code>null</code>.
190: *
191: * @return the unparsed parent path or <code>null</code>
192: */
193: public String getRawParentPath() {
194: return configurationElement == null ? null
195: : configurationElement
196: .getAttribute(IWorkbenchRegistryConstants.ATT_PARENT_CATEGORY);
197: }
198:
199: /**
200: * Return the root path for this category.
201: *
202: * @return the root path
203: */
204: public String getRootPath() {
205: String[] path = getParentPath();
206: if (path != null && path.length > 0) {
207: return path[0];
208: }
209:
210: return id;
211: }
212:
213: /**
214: * Return the elements contained in this category.
215: *
216: * @return the elements
217: */
218: public ArrayList getElements() {
219: return elements;
220: }
221:
222: /**
223: * Return whether a given object exists in this category.
224: *
225: * @param o the object to search for
226: * @return whether the object is in this category
227: */
228: public boolean hasElement(Object o) {
229: if (elements == null) {
230: return false;
231: }
232: if (elements.isEmpty()) {
233: return false;
234: }
235: return elements.contains(o);
236: }
237:
238: /**
239: * Return whether this category has child elements.
240: *
241: * @return whether this category has child elements
242: */
243: public boolean hasElements() {
244: if (elements != null) {
245: return !elements.isEmpty();
246: }
247:
248: return false;
249: }
250:
251: /* (non-Javadoc)
252: * @see org.eclipse.ui.model.IWorkbenchAdapter#getParent(java.lang.Object)
253: */
254: public Object getParent(Object o) {
255: return null;
256: }
257:
258: /* (non-Javadoc)
259: * @see org.eclipse.ui.activities.support.IPluginContribution#getLocalId()
260: */
261: public String getLocalId() {
262: return id;
263: }
264:
265: /* (non-Javadoc)
266: * @see org.eclipse.ui.activities.support.IPluginContribution#getPluginId()
267: */
268: public String getPluginId() {
269: return configurationElement == null ? pluginId
270: : configurationElement.getNamespace();
271: }
272:
273: /**
274: * Clear all elements from this category.
275: *
276: * @since 3.1
277: */
278: public void clear() {
279: if (elements != null) {
280: elements.clear();
281: }
282: }
283: }
|