KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > WizardCollectionElement


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.dialogs;
12
13 import java.util.Iterator JavaDoc;
14
15 import org.eclipse.core.runtime.IAdaptable;
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.core.runtime.Platform;
20 import org.eclipse.jface.resource.ImageDescriptor;
21 import org.eclipse.ui.IPluginContribution;
22 import org.eclipse.ui.ISharedImages;
23 import org.eclipse.ui.internal.WorkbenchImages;
24 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
25 import org.eclipse.ui.model.AdaptableList;
26 import org.eclipse.ui.model.IWorkbenchAdapter;
27 import org.eclipse.ui.wizards.IWizardCategory;
28 import org.eclipse.ui.wizards.IWizardDescriptor;
29
30 /**
31  * Instances of this class are a collection of WizardCollectionElements,
32  * thereby facilitating the definition of tree structures composed of these
33  * elements. Instances also store a list of wizards.
34  */

35 public class WizardCollectionElement extends AdaptableList implements
36         IPluginContribution, IWizardCategory {
37     private String JavaDoc id;
38
39     private String JavaDoc pluginId;
40
41     private String JavaDoc name;
42
43     private WizardCollectionElement parent;
44
45     private AdaptableList wizards = new AdaptableList();
46
47     private IConfigurationElement configElement;
48
49     /**
50      * Creates a new <code>WizardCollectionElement</code>. Parent can be
51      * null.
52      * @param id the id
53      * @param pluginId the plugin
54      * @param name the name
55      * @param parent the parent
56      */

57     public WizardCollectionElement(String JavaDoc id, String JavaDoc pluginId, String JavaDoc name,
58             WizardCollectionElement parent) {
59         this.name = name;
60         this.id = id;
61         this.pluginId = pluginId;
62         this.parent = parent;
63     }
64
65     /**
66      * Creates a new <code>WizardCollectionElement</code>. Parent can be
67      * null.
68      *
69      * @param element
70      * @param parent
71      * @since 3.1
72      */

73     public WizardCollectionElement(IConfigurationElement element, WizardCollectionElement parent) {
74         configElement = element;
75         id = configElement.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
76         this.parent = parent;
77     }
78
79     /**
80      * Adds a wizard collection to this collection.
81      */

82     public AdaptableList add(IAdaptable a) {
83         if (a instanceof WorkbenchWizardElement) {
84             wizards.add(a);
85         } else {
86             super.add(a);
87         }
88         return this;
89     }
90     
91     
92     /**
93      * Remove a wizard from this collection.
94      */

95     public void remove(IAdaptable a) {
96         if (a instanceof WorkbenchWizardElement) {
97             wizards.remove(a);
98         } else {
99             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 JavaDoc[] children = getChildren(null);
114         String JavaDoc 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 JavaDoc id) {
140         Object JavaDoc[] 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.findCategory(id);
147             if (childCategory != null) {
148                 return childCategory;
149             }
150         }
151         return null;
152     }
153
154     /**
155      * Returns this collection's associated wizard object corresponding to the
156      * passed id, or <code>null</code> if such an object could not be found.
157      *
158      * @param searchId the id to search on
159      * @param recursive whether to search recursivly
160      * @return the element
161      */

162     public WorkbenchWizardElement findWizard(String JavaDoc searchId, boolean recursive) {
163         Object JavaDoc[] wizards = getWizards();
164         for (int i = 0; i < wizards.length; ++i) {
165             WorkbenchWizardElement currentWizard = (WorkbenchWizardElement) wizards[i];
166             if (currentWizard.getId().equals(searchId)) {
167                 return currentWizard;
168             }
169         }
170         if (!recursive) {
171             return null;
172         }
173         for (Iterator JavaDoc iterator = children.iterator(); iterator.hasNext();) {
174             WizardCollectionElement child = (WizardCollectionElement) iterator
175                     .next();
176             WorkbenchWizardElement result = child.findWizard(searchId, true);
177             if (result != null) {
178                 return result;
179             }
180         }
181         return null;
182     }
183
184     /**
185      * Returns an object which is an instance of the given class associated
186      * with this object. Returns <code>null</code> if no such object can be
187      * found.
188      */

189     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
190         if (adapter == IWorkbenchAdapter.class) {
191             return this;
192         }
193         return Platform.getAdapterManager().getAdapter(this, adapter);
194     }
195
196     /**
197      * Returns the unique ID of this element.
198      */

199     public String JavaDoc getId() {
200         return id;
201     }
202
203     /**
204      * Returns the label for this collection.
205      */

206     public String JavaDoc getLabel(Object JavaDoc o) {
207         return configElement != null ? configElement
208                 .getAttribute(IWorkbenchRegistryConstants.ATT_NAME) : name;
209     }
210
211     /**
212      * Returns the logical parent of the given object in its tree.
213      */

214     public Object JavaDoc getParent(Object JavaDoc o) {
215         return parent;
216     }
217
218     /* (non-Javadoc)
219      * @see org.eclipse.ui.wizards.IWizardCategory#getPath()
220      */

221     public IPath getPath() {
222         if (parent == null) {
223             return new Path(""); //$NON-NLS-1$
224
}
225
226         return parent.getPath().append(getId());
227     }
228
229
230     /* (non-Javadoc)
231      * @see org.eclipse.ui.wizards.IWizardCategory#getWizards()
232      */

233     public IWizardDescriptor [] getWizards() {
234         return (IWizardDescriptor[]) wizards
235                 .getTypedChildren(IWizardDescriptor.class);
236     }
237     
238     /**
239      * Return the wizards.
240      *
241      * @return the wizards
242      * @since 3.1
243      */

244     public WorkbenchWizardElement [] getWorkbenchWizardElements() {
245         return (WorkbenchWizardElement[]) wizards
246                 .getTypedChildren(WorkbenchWizardElement.class);
247     }
248
249     /**
250      * Returns true if this element has no children and no wizards.
251      *
252      * @return whether it is empty
253      */

254     public boolean isEmpty() {
255         return size() == 0 && wizards.size() == 0;
256     }
257
258     /**
259      * For debugging purposes.
260      */

261     public String JavaDoc toString() {
262         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("WizardCollection, "); //$NON-NLS-1$
263
buf.append(children.size());
264         buf.append(" children, "); //$NON-NLS-1$
265
buf.append(wizards.size());
266         buf.append(" wizards"); //$NON-NLS-1$
267
return buf.toString();
268     }
269
270     /*
271      * (non-Javadoc)
272      *
273      * @see org.eclipse.ui.model.IWorkbenchAdapter#getImageDescriptor(java.lang.Object)
274      */

275     public ImageDescriptor getImageDescriptor(Object JavaDoc object) {
276         return WorkbenchImages.getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER);
277     }
278
279     /*
280      * (non-Javadoc)
281      *
282      * @see org.eclipse.ui.activities.support.IPluginContribution#getLocalId()
283      */

284     public String JavaDoc getLocalId() {
285         return getId();
286     }
287
288     /*
289      * (non-Javadoc)
290      *
291      * @see org.eclipse.ui.activities.support.IPluginContribution#getPluginId()
292      */

293     public String JavaDoc getPluginId() {
294         return configElement != null ? configElement.getNamespace() : pluginId;
295     }
296     
297     
298     /* (non-Javadoc)
299      * @see org.eclipse.ui.wizards.IWizardCategory#getParent()
300      */

301     public IWizardCategory getParent() {
302         return parent;
303     }
304     
305     /* (non-Javadoc)
306      * @see org.eclipse.ui.wizards.IWizardCategory#getCategories()
307      */

308     public IWizardCategory[] getCategories() {
309         return (IWizardCategory []) getTypedChildren(IWizardCategory.class);
310     }
311     
312     /**
313      * Return the collection elements.
314      *
315      * @return the collection elements
316      * @since 3.1
317      */

318     public WizardCollectionElement [] getCollectionElements() {
319         return (WizardCollectionElement[]) getTypedChildren(WizardCollectionElement.class);
320     }
321     
322     /**
323      * Return the raw adapted list of wizards.
324      *
325      * @return the list of wizards
326      * @since 3.1
327      */

328     public AdaptableList getWizardAdaptableList() {
329         return wizards;
330     }
331     
332     /* (non-Javadoc)
333      * @see org.eclipse.ui.wizards.IWizardCategory#getLabel()
334      */

335     public String JavaDoc getLabel() {
336         return getLabel(this);
337     }
338     
339     /**
340      * Return the configuration element.
341      *
342      * @return the configuration element
343      * @since 3.1
344      */

345     public IConfigurationElement getConfigurationElement() {
346         return configElement;
347     }
348
349     /**
350      * Return the parent collection element.
351      *
352      * @return the parent
353      * @since 3.1
354      */

355     public WizardCollectionElement getParentCollection() {
356         return parent;
357     }
358
359     /* (non-Javadoc)
360      * @see org.eclipse.ui.wizards.IWizardCategory#findWizard(java.lang.String)
361      */

362     public IWizardDescriptor findWizard(String JavaDoc id) {
363         return findWizard(id, true);
364     }
365     
366     /* (non-Javadoc)
367      * @see org.eclipse.ui.wizards.IWizardCategory#findCategory(org.eclipse.core.runtime.IPath)
368      */

369     public IWizardCategory findCategory(IPath path) {
370         return findChildCollection(path);
371     }
372 }
373
Popular Tags