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.cheatsheets.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.WorkbenchException;
020: import org.eclipse.ui.internal.cheatsheets.Messages;
021: import org.eclipse.ui.model.IWorkbenchAdapter;
022:
023: /**
024: * Category provides for hierarchical grouping of elements registered in the
025: * registry. One extension normally defines a category, and other reference it
026: * via its ID.
027: * <p>
028: * A category may specify its parent category in order to achieve hierarchy.
029: * </p>
030: */
031: public class Category implements IWorkbenchAdapter,
032: IPluginContribution, IAdaptable {
033: /**
034: * Name of the miscellaneous category
035: */
036: public final static String MISC_NAME = Messages.CATEGORY_OTHER;
037:
038: /**
039: */
040: public final static String MISC_ID = "org.eclipse.ui.cheatsheets.otherCategory"; //$NON-NLS-1$
041:
042: private String id;
043:
044: private String name;
045:
046: private String[] parentPath;
047:
048: private ArrayList elements;
049:
050: private IConfigurationElement configurationElement;
051:
052: private String pluginId;
053:
054: /**
055: * Creates an instance of <code>Category</code> as a miscellaneous
056: * category.
057: */
058: public Category() {
059: this .id = MISC_ID;
060: this .name = MISC_NAME;
061: this .pluginId = MISC_ID; // TODO: remove hack for bug 55172
062: }
063:
064: /**
065: * Creates an instance of <code>Category</code> with an ID and label.
066: *
067: * @param id
068: * the unique identifier for the category
069: * @param label
070: * the presentation label for this category
071: */
072: public Category(String id, String label) {
073: this .id = id;
074: this .name = label;
075: }
076:
077: /**
078: * Creates an instance of <code>Category</code> using the information from
079: * the specified configuration element.
080: *
081: * @param configElement
082: * the <code>IConfigurationElement<code> containing
083: * the ID, label, and optional parent category path.
084: * @throws WorkbenchException if the ID or label is <code>null</code
085: */
086: public Category(IConfigurationElement configElement)
087: throws WorkbenchException {
088: id = configElement.getAttribute("id"); //$NON-NLS-1$
089:
090: configurationElement = configElement;
091: if (id == null || getLabel() == null)
092: throw new WorkbenchException("Invalid category: " + id); //$NON-NLS-1$
093: }
094:
095: /**
096: * Add an element to this category.
097: *
098: * @param element
099: * the element to add
100: */
101: public void addElement(Object element) {
102: if (elements == null)
103: elements = new ArrayList(5);
104: elements.add(element);
105: }
106:
107: /*
108: * (non-Javadoc) Method declared on IAdaptable.
109: */
110: public Object getAdapter(Class adapter) {
111: if (adapter == IWorkbenchAdapter.class)
112: return this ;
113: else if (adapter == IConfigurationElement.class)
114: return configurationElement;
115: else
116: return null;
117: }
118:
119: /*
120: * (non-Javadoc) Method declared on IWorkbenchAdapter.
121: */
122: public Object[] getChildren(Object o) {
123: return getElements().toArray();
124: }
125:
126: /*
127: * (non-Javadoc) Method declared on IWorkbenchAdapter.
128: */
129: public ImageDescriptor getImageDescriptor(Object object) {
130: return null;
131: }
132:
133: /*
134: * (non-Javadoc) Method declared on IWorkbenchAdapter.
135: */
136: public String getLabel(Object o) {
137: return getLabel();
138: }
139:
140: /**
141: * Return the id for this category.
142: *
143: * @return the id
144: */
145: public String getId() {
146: return id;
147: }
148:
149: /**
150: * Return the label for this category.
151: *
152: * @return the label
153: */
154: public String getLabel() {
155: return configurationElement == null ? name
156: : configurationElement.getAttribute("name"); //$NON-NLS-1$
157: }
158:
159: /**
160: * Return the parent path for this category.
161: *
162: * @return the parent path
163: */
164: public String[] getParentPath() {
165: if (parentPath != null)
166: return parentPath;
167:
168: String unparsedPath = getRawParentPath();
169: if (unparsedPath != null) {
170: StringTokenizer stok = new StringTokenizer(unparsedPath,
171: "/"); //$NON-NLS-1$
172: parentPath = new String[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 getRawParentPath() {
187: return configurationElement == null ? null
188: : configurationElement.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 getRootPath() {
197: String[] 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 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 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 getParent(Object o) {
246: return null;
247: }
248:
249: /*
250: * (non-Javadoc)
251: *
252: * @see org.eclipse.ui.activities.support.IPluginContribution#getLocalId()
253: */
254: public String getLocalId() {
255: return id;
256: }
257:
258: /*
259: * (non-Javadoc)
260: *
261: * @see org.eclipse.ui.activities.support.IPluginContribution#getPluginId()
262: */
263: public String getPluginId() {
264: return configurationElement == null ? pluginId
265: : configurationElement.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: }
|