KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.core.runtime.IConfigurationElement;
16 import org.eclipse.core.runtime.Platform;
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.jface.viewers.StructuredSelection;
20 import org.eclipse.ui.IPluginContribution;
21 import org.eclipse.ui.IWorkbenchWizard;
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.SelectionEnabler;
24 import org.eclipse.ui.internal.ISelectionConversionService;
25 import org.eclipse.ui.internal.WorkbenchPlugin;
26 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
27 import org.eclipse.ui.internal.registry.RegistryReader;
28 import org.eclipse.ui.model.IWorkbenchAdapter;
29 import org.eclipse.ui.model.IWorkbenchAdapter2;
30 import org.eclipse.ui.model.WorkbenchAdapter;
31 import org.eclipse.ui.plugin.AbstractUIPlugin;
32 import org.eclipse.ui.wizards.IWizardCategory;
33 import org.eclipse.ui.wizards.IWizardDescriptor;
34
35 /**
36  * Instances represent registered wizards.
37  */

38 public class WorkbenchWizardElement extends WorkbenchAdapter implements
39         IAdaptable, IPluginContribution, IWizardDescriptor {
40     private String JavaDoc id;
41     
42     private ImageDescriptor imageDescriptor;
43
44     private SelectionEnabler selectionEnabler;
45
46     private IConfigurationElement configurationElement;
47
48     private ImageDescriptor descriptionImage;
49     
50     private WizardCollectionElement parentCategory;
51     
52     /**
53      * TODO: DO we need to make this API?
54      */

55     public static final String JavaDoc TAG_PROJECT = "project"; //$NON-NLS-1$
56

57     private static final String JavaDoc [] EMPTY_TAGS = new String JavaDoc[0];
58
59     private static final String JavaDoc [] PROJECT_TAGS = new String JavaDoc[] {TAG_PROJECT};
60
61     
62     /**
63      * Create a new instance of this class
64      *
65      * @param configurationElement
66      * @since 3.1
67      */

68     public WorkbenchWizardElement(IConfigurationElement configurationElement) {
69         this.configurationElement = configurationElement;
70         id = configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
71     }
72
73     /**
74      * Answer a boolean indicating whether the receiver is able to handle the
75      * passed selection
76      *
77      * @return boolean
78      * @param selection
79      * IStructuredSelection
80      */

81     public boolean canHandleSelection(IStructuredSelection selection) {
82         return getSelectionEnabler().isEnabledForSelection(selection);
83     }
84
85     /**
86      * Answer the selection for the reciever based on whether the it can handle
87      * the selection. If it can return the selection. If it can handle the
88      * adapted to IResource value of the selection. If it satisfies neither of
89      * these conditions return an empty IStructuredSelection.
90      *
91      * @return IStructuredSelection
92      * @param selection
93      * IStructuredSelection
94      */

95     public IStructuredSelection adaptedSelection(IStructuredSelection selection) {
96         if (canHandleSelection(selection)) {
97             return selection;
98         }
99
100         IStructuredSelection adaptedSelection = convertToResources(selection);
101         if (canHandleSelection(adaptedSelection)) {
102             return adaptedSelection;
103         }
104
105         //Couldn't find one that works so just return
106
return StructuredSelection.EMPTY;
107     }
108
109     /**
110      * Create an the instance of the object described by the configuration
111      * element. That is, create the instance of the class the isv supplied in
112      * the extension point.
113      * @return the new object
114      * @throws CoreException
115      */

116     public Object JavaDoc createExecutableExtension() throws CoreException {
117         return WorkbenchPlugin.createExtension(configurationElement,
118                 IWorkbenchRegistryConstants.ATT_CLASS);
119     }
120
121     /**
122      * Returns an object which is an instance of the given class associated
123      * with this object. Returns <code>null</code> if no such object can be
124      * found.
125      */

126     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
127         if (adapter == IWorkbenchAdapter.class
128                 || adapter == IWorkbenchAdapter2.class) {
129             return this;
130         }
131         else if (adapter == IPluginContribution.class) {
132             return this;
133         }
134         else if (adapter == IConfigurationElement.class) {
135             return configurationElement;
136         }
137         return Platform.getAdapterManager().getAdapter(this, adapter);
138     }
139
140     /**
141      * @return IConfigurationElement
142      */

143     public IConfigurationElement getConfigurationElement() {
144         return configurationElement;
145     }
146
147     /**
148      * Answer the description parameter of this element
149      *
150      * @return java.lang.String
151      */

152     public String JavaDoc getDescription() {
153         return RegistryReader.getDescription(configurationElement);
154     }
155
156     /**
157      * Answer the icon of this element.
158      */

159     public ImageDescriptor getImageDescriptor() {
160         if (imageDescriptor == null) {
161             String JavaDoc iconName = configurationElement
162                     .getAttribute(IWorkbenchRegistryConstants.ATT_ICON);
163             if (iconName == null) {
164                 return null;
165             }
166             imageDescriptor = AbstractUIPlugin.imageDescriptorFromPlugin(
167                     configurationElement.getNamespaceIdentifier(), iconName);
168         }
169         return imageDescriptor;
170     }
171
172     /**
173      * Returns the name of this wizard element.
174      */

175     public ImageDescriptor getImageDescriptor(Object JavaDoc element) {
176         return getImageDescriptor();
177     }
178     
179     /**
180      * Returns the name of this wizard element.
181      */

182     public String JavaDoc getLabel(Object JavaDoc element) {
183         return configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_NAME);
184     }
185
186     /**
187      * Answer self's action enabler, creating it first iff necessary
188      */

189     protected SelectionEnabler getSelectionEnabler() {
190         if (selectionEnabler == null) {
191             selectionEnabler = new SelectionEnabler(configurationElement);
192         }
193
194         return selectionEnabler;
195     }
196
197     /**
198      * Attempt to convert the elements in the passed selection into resources
199      * by asking each for its IResource property (iff it isn't already a
200      * resource). If all elements in the initial selection can be converted to
201      * resources then answer a new selection containing these resources;
202      * otherwise answer an empty selection.
203      *
204      * @param originalSelection the original selection
205      * @return the converted selection or an empty selection
206      */

207     private IStructuredSelection convertToResources(
208             IStructuredSelection originalSelection) {
209         Object JavaDoc selectionService = PlatformUI.getWorkbench().getService(
210                 ISelectionConversionService.class);
211         if (selectionService == null || originalSelection == null) {
212             return StructuredSelection.EMPTY;
213         }
214         return ((ISelectionConversionService) selectionService)
215                 .convertToResources(originalSelection);
216     }
217
218     /*
219      * (non-Javadoc)
220      *
221      * @see org.eclipse.ui.IPluginContribution#getLocalId()
222      */

223     public String JavaDoc getLocalId() {
224         return getId();
225     }
226
227     /* (non-Javadoc)
228      * @see org.eclipse.ui.IPluginContribution#getPluginId()
229      */

230     public String JavaDoc getPluginId() {
231         return (configurationElement != null) ? configurationElement
232                 .getNamespaceIdentifier() : null;
233     }
234
235     /* (non-Javadoc)
236      * @see org.eclipse.ui.wizards.INewWizardDescriptor#getDescriptionImage()
237      */

238     public ImageDescriptor getDescriptionImage() {
239         if (descriptionImage == null) {
240             String JavaDoc descImage = configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_DESCRIPTION_IMAGE);
241             if (descImage == null) {
242                 return null;
243             }
244             descriptionImage = AbstractUIPlugin.imageDescriptorFromPlugin(
245                     configurationElement.getNamespaceIdentifier(), descImage);
246         }
247         return descriptionImage;
248     }
249
250     /* (non-Javadoc)
251      * @see org.eclipse.ui.wizards.INewWizardDescriptor#getHelpHref()
252      */

253     public String JavaDoc getHelpHref() {
254         return configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_HELP_HREF);
255     }
256     
257     /* (non-Javadoc)
258      * @see org.eclipse.ui.wizards.INewWizardDescriptor#createWizard()
259      */

260     public IWorkbenchWizard createWizard() throws CoreException {
261         return (IWorkbenchWizard) createExecutableExtension();
262     }
263
264     /* (non-Javadoc)
265      * @see org.eclipse.ui.IWorkbenchPartDescriptor#getId()
266      */

267     public String JavaDoc getId() {
268         return id;
269     }
270
271     /* (non-Javadoc)
272      * @see org.eclipse.ui.IWorkbenchPartDescriptor#getLabel()
273      */

274     public String JavaDoc getLabel() {
275         return getLabel(this);
276     }
277     
278     /* (non-Javadoc)
279      * @see org.eclipse.ui.wizards.INewWizardDescriptor#getCategory()
280      */

281     public IWizardCategory getCategory() {
282         return (IWizardCategory) getParent(this);
283     }
284     
285     /**
286      * Return the collection.
287      *
288      * @return the collection
289      * @since 3.1
290      */

291     public WizardCollectionElement getCollectionElement() {
292         return (WizardCollectionElement) getParent(this);
293     }
294
295     /* (non-Javadoc)
296      * @see org.eclipse.ui.wizards.IWizardDescriptor#getTags()
297      */

298     public String JavaDoc [] getTags() {
299  
300         String JavaDoc flag = configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_PROJECT);
301         if (Boolean.valueOf(flag).booleanValue()) {
302             return PROJECT_TAGS;
303         }
304         
305         return EMPTY_TAGS;
306     }
307     
308     /* (non-Javadoc)
309      * @see org.eclipse.ui.model.IWorkbenchAdapter#getParent(java.lang.Object)
310      */

311     public Object JavaDoc getParent(Object JavaDoc object) {
312         return parentCategory;
313     }
314
315     /**
316      * Set the parent category.
317      *
318      * @param parent the parent category
319      * @since 3.1
320      */

321     public void setParent(WizardCollectionElement parent) {
322         parentCategory = parent;
323     }
324
325     /* (non-Javadoc)
326      * @see org.eclipse.ui.wizards.IWizardDescriptor#canFinishEarly()
327      */

328     public boolean canFinishEarly() {
329         return Boolean.valueOf(configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_CAN_FINISH_EARLY)).booleanValue();
330     }
331
332     /* (non-Javadoc)
333      * @see org.eclipse.ui.wizards.IWizardDescriptor#hasPages()
334      */

335     public boolean hasPages() {
336         String JavaDoc hasPagesString = configurationElement.getAttribute(IWorkbenchRegistryConstants.ATT_HAS_PAGES);
337         // default value is true
338
if (hasPagesString == null) {
339             return true;
340         }
341         return Boolean.valueOf(hasPagesString).booleanValue();
342     }
343 }
344
Popular Tags