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.dialogs;
011:
012: import java.util.Iterator;
013:
014: import org.eclipse.core.runtime.IAdaptable;
015: import org.eclipse.core.runtime.IConfigurationElement;
016: import org.eclipse.core.runtime.IPath;
017: import org.eclipse.core.runtime.Path;
018: import org.eclipse.core.runtime.Platform;
019: import org.eclipse.jface.resource.ImageDescriptor;
020: import org.eclipse.ui.IPluginContribution;
021: import org.eclipse.ui.ISharedImages;
022: import org.eclipse.ui.internal.WorkbenchImages;
023: import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
024: import org.eclipse.ui.model.AdaptableList;
025: import org.eclipse.ui.model.IWorkbenchAdapter;
026: import org.eclipse.ui.wizards.IWizardCategory;
027: import org.eclipse.ui.wizards.IWizardDescriptor;
028:
029: /**
030: * Instances of this class are a collection of WizardCollectionElements,
031: * thereby facilitating the definition of tree structures composed of these
032: * elements. Instances also store a list of wizards.
033: */
034: public class WizardCollectionElement extends AdaptableList implements
035: IPluginContribution, IWizardCategory {
036: private String id;
037:
038: private String pluginId;
039:
040: private String name;
041:
042: private WizardCollectionElement parent;
043:
044: private AdaptableList wizards = new AdaptableList();
045:
046: private IConfigurationElement configElement;
047:
048: /**
049: * Creates a new <code>WizardCollectionElement</code>. Parent can be
050: * null.
051: * @param id the id
052: * @param pluginId the plugin
053: * @param name the name
054: * @param parent the parent
055: */
056: public WizardCollectionElement(String id, String pluginId,
057: String name, WizardCollectionElement parent) {
058: this .name = name;
059: this .id = id;
060: this .pluginId = pluginId;
061: this .parent = parent;
062: }
063:
064: /**
065: * Creates a new <code>WizardCollectionElement</code>. Parent can be
066: * null.
067: *
068: * @param element
069: * @param parent
070: * @since 3.1
071: */
072: public WizardCollectionElement(IConfigurationElement element,
073: WizardCollectionElement parent) {
074: configElement = element;
075: id = configElement
076: .getAttribute(IWorkbenchRegistryConstants.ATT_ID);
077: this .parent = parent;
078: }
079:
080: /**
081: * Adds a wizard collection to this collection.
082: */
083: public AdaptableList add(IAdaptable a) {
084: if (a instanceof WorkbenchWizardElement) {
085: wizards.add(a);
086: } else {
087: super .add(a);
088: }
089: return this ;
090: }
091:
092: /**
093: * Remove a wizard from this collection.
094: */
095: public void remove(IAdaptable a) {
096: if (a instanceof WorkbenchWizardElement) {
097: wizards.remove(a);
098: } else {
099: super .remove(a);
100: }
101: }
102:
103: /**
104: * Returns the wizard collection child object corresponding to the passed
105: * path (relative to this object), or <code>null</code> if such an object
106: * could not be found.
107: *
108: * @param searchPath
109: * org.eclipse.core.runtime.IPath
110: * @return WizardCollectionElement
111: */
112: public WizardCollectionElement findChildCollection(IPath searchPath) {
113: Object[] children = getChildren(null);
114: String searchString = searchPath.segment(0);
115: for (int i = 0; i < children.length; ++i) {
116: WizardCollectionElement currentCategory = (WizardCollectionElement) children[i];
117: if (currentCategory.getId().equals(searchString)) {
118: if (searchPath.segmentCount() == 1) {
119: return currentCategory;
120: }
121:
122: return currentCategory.findChildCollection(searchPath
123: .removeFirstSegments(1));
124: }
125: }
126:
127: return null;
128: }
129:
130: /**
131: * Returns the wizard category corresponding to the passed
132: * id, or <code>null</code> if such an object could not be found.
133: * This recurses through child categories.
134: *
135: * @param id the id for the child category
136: * @return the category, or <code>null</code> if not found
137: * @since 3.1
138: */
139: public WizardCollectionElement findCategory(String id) {
140: Object[] children = getChildren(null);
141: for (int i = 0; i < children.length; ++i) {
142: WizardCollectionElement currentCategory = (WizardCollectionElement) children[i];
143: if (id.equals(currentCategory.getId())) {
144: return currentCategory;
145: }
146: WizardCollectionElement childCategory = currentCategory
147: .findCategory(id);
148: if (childCategory != null) {
149: return childCategory;
150: }
151: }
152: return null;
153: }
154:
155: /**
156: * Returns this collection's associated wizard object corresponding to the
157: * passed id, or <code>null</code> if such an object could not be found.
158: *
159: * @param searchId the id to search on
160: * @param recursive whether to search recursivly
161: * @return the element
162: */
163: public WorkbenchWizardElement findWizard(String searchId,
164: boolean recursive) {
165: Object[] wizards = getWizards();
166: for (int i = 0; i < wizards.length; ++i) {
167: WorkbenchWizardElement currentWizard = (WorkbenchWizardElement) wizards[i];
168: if (currentWizard.getId().equals(searchId)) {
169: return currentWizard;
170: }
171: }
172: if (!recursive) {
173: return null;
174: }
175: for (Iterator iterator = children.iterator(); iterator
176: .hasNext();) {
177: WizardCollectionElement child = (WizardCollectionElement) iterator
178: .next();
179: WorkbenchWizardElement result = child.findWizard(searchId,
180: true);
181: if (result != null) {
182: return result;
183: }
184: }
185: return null;
186: }
187:
188: /**
189: * Returns an object which is an instance of the given class associated
190: * with this object. Returns <code>null</code> if no such object can be
191: * found.
192: */
193: public Object getAdapter(Class adapter) {
194: if (adapter == IWorkbenchAdapter.class) {
195: return this ;
196: }
197: return Platform.getAdapterManager().getAdapter(this , adapter);
198: }
199:
200: /**
201: * Returns the unique ID of this element.
202: */
203: public String getId() {
204: return id;
205: }
206:
207: /**
208: * Returns the label for this collection.
209: */
210: public String getLabel(Object o) {
211: return configElement != null ? configElement
212: .getAttribute(IWorkbenchRegistryConstants.ATT_NAME)
213: : name;
214: }
215:
216: /**
217: * Returns the logical parent of the given object in its tree.
218: */
219: public Object getParent(Object o) {
220: return parent;
221: }
222:
223: /* (non-Javadoc)
224: * @see org.eclipse.ui.wizards.IWizardCategory#getPath()
225: */
226: public IPath getPath() {
227: if (parent == null) {
228: return new Path(""); //$NON-NLS-1$
229: }
230:
231: return parent.getPath().append(getId());
232: }
233:
234: /* (non-Javadoc)
235: * @see org.eclipse.ui.wizards.IWizardCategory#getWizards()
236: */
237: public IWizardDescriptor[] getWizards() {
238: return (IWizardDescriptor[]) wizards
239: .getTypedChildren(IWizardDescriptor.class);
240: }
241:
242: /**
243: * Return the wizards.
244: *
245: * @return the wizards
246: * @since 3.1
247: */
248: public WorkbenchWizardElement[] getWorkbenchWizardElements() {
249: return (WorkbenchWizardElement[]) wizards
250: .getTypedChildren(WorkbenchWizardElement.class);
251: }
252:
253: /**
254: * Returns true if this element has no children and no wizards.
255: *
256: * @return whether it is empty
257: */
258: public boolean isEmpty() {
259: return size() == 0 && wizards.size() == 0;
260: }
261:
262: /**
263: * For debugging purposes.
264: */
265: public String toString() {
266: StringBuffer buf = new StringBuffer("WizardCollection, "); //$NON-NLS-1$
267: buf.append(children.size());
268: buf.append(" children, "); //$NON-NLS-1$
269: buf.append(wizards.size());
270: buf.append(" wizards"); //$NON-NLS-1$
271: return buf.toString();
272: }
273:
274: /*
275: * (non-Javadoc)
276: *
277: * @see org.eclipse.ui.model.IWorkbenchAdapter#getImageDescriptor(java.lang.Object)
278: */
279: public ImageDescriptor getImageDescriptor(Object object) {
280: return WorkbenchImages
281: .getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER);
282: }
283:
284: /*
285: * (non-Javadoc)
286: *
287: * @see org.eclipse.ui.activities.support.IPluginContribution#getLocalId()
288: */
289: public String getLocalId() {
290: return getId();
291: }
292:
293: /*
294: * (non-Javadoc)
295: *
296: * @see org.eclipse.ui.activities.support.IPluginContribution#getPluginId()
297: */
298: public String getPluginId() {
299: return configElement != null ? configElement.getNamespace()
300: : pluginId;
301: }
302:
303: /* (non-Javadoc)
304: * @see org.eclipse.ui.wizards.IWizardCategory#getParent()
305: */
306: public IWizardCategory getParent() {
307: return parent;
308: }
309:
310: /* (non-Javadoc)
311: * @see org.eclipse.ui.wizards.IWizardCategory#getCategories()
312: */
313: public IWizardCategory[] getCategories() {
314: return (IWizardCategory[]) getTypedChildren(IWizardCategory.class);
315: }
316:
317: /**
318: * Return the collection elements.
319: *
320: * @return the collection elements
321: * @since 3.1
322: */
323: public WizardCollectionElement[] getCollectionElements() {
324: return (WizardCollectionElement[]) getTypedChildren(WizardCollectionElement.class);
325: }
326:
327: /**
328: * Return the raw adapted list of wizards.
329: *
330: * @return the list of wizards
331: * @since 3.1
332: */
333: public AdaptableList getWizardAdaptableList() {
334: return wizards;
335: }
336:
337: /* (non-Javadoc)
338: * @see org.eclipse.ui.wizards.IWizardCategory#getLabel()
339: */
340: public String getLabel() {
341: return getLabel(this );
342: }
343:
344: /**
345: * Return the configuration element.
346: *
347: * @return the configuration element
348: * @since 3.1
349: */
350: public IConfigurationElement getConfigurationElement() {
351: return configElement;
352: }
353:
354: /**
355: * Return the parent collection element.
356: *
357: * @return the parent
358: * @since 3.1
359: */
360: public WizardCollectionElement getParentCollection() {
361: return parent;
362: }
363:
364: /* (non-Javadoc)
365: * @see org.eclipse.ui.wizards.IWizardCategory#findWizard(java.lang.String)
366: */
367: public IWizardDescriptor findWizard(String id) {
368: return findWizard(id, true);
369: }
370:
371: /* (non-Javadoc)
372: * @see org.eclipse.ui.wizards.IWizardCategory#findCategory(org.eclipse.core.runtime.IPath)
373: */
374: public IWizardCategory findCategory(IPath path) {
375: return findChildCollection(path);
376: }
377: }
|