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 org.eclipse.core.runtime.CoreException;
013: import org.eclipse.core.runtime.IAdaptable;
014: import org.eclipse.core.runtime.IConfigurationElement;
015: import org.eclipse.core.runtime.Platform;
016: import org.eclipse.jface.resource.ImageDescriptor;
017: import org.eclipse.jface.viewers.IStructuredSelection;
018: import org.eclipse.jface.viewers.StructuredSelection;
019: import org.eclipse.ui.IPluginContribution;
020: import org.eclipse.ui.IWorkbenchWizard;
021: import org.eclipse.ui.PlatformUI;
022: import org.eclipse.ui.SelectionEnabler;
023: import org.eclipse.ui.internal.ISelectionConversionService;
024: import org.eclipse.ui.internal.WorkbenchPlugin;
025: import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
026: import org.eclipse.ui.internal.registry.RegistryReader;
027: import org.eclipse.ui.model.IWorkbenchAdapter;
028: import org.eclipse.ui.model.IWorkbenchAdapter2;
029: import org.eclipse.ui.model.WorkbenchAdapter;
030: import org.eclipse.ui.plugin.AbstractUIPlugin;
031: import org.eclipse.ui.wizards.IWizardCategory;
032: import org.eclipse.ui.wizards.IWizardDescriptor;
033:
034: /**
035: * Instances represent registered wizards.
036: */
037: public class WorkbenchWizardElement extends WorkbenchAdapter implements
038: IAdaptable, IPluginContribution, IWizardDescriptor {
039: private String id;
040:
041: private ImageDescriptor imageDescriptor;
042:
043: private SelectionEnabler selectionEnabler;
044:
045: private IConfigurationElement configurationElement;
046:
047: private ImageDescriptor descriptionImage;
048:
049: private WizardCollectionElement parentCategory;
050:
051: /**
052: * TODO: DO we need to make this API?
053: */
054: public static final String TAG_PROJECT = "project"; //$NON-NLS-1$
055:
056: private static final String[] EMPTY_TAGS = new String[0];
057:
058: private static final String[] PROJECT_TAGS = new String[] { TAG_PROJECT };
059:
060: /**
061: * Create a new instance of this class
062: *
063: * @param configurationElement
064: * @since 3.1
065: */
066: public WorkbenchWizardElement(
067: IConfigurationElement configurationElement) {
068: this .configurationElement = configurationElement;
069: id = configurationElement
070: .getAttribute(IWorkbenchRegistryConstants.ATT_ID);
071: }
072:
073: /**
074: * Answer a boolean indicating whether the receiver is able to handle the
075: * passed selection
076: *
077: * @return boolean
078: * @param selection
079: * IStructuredSelection
080: */
081: public boolean canHandleSelection(IStructuredSelection selection) {
082: return getSelectionEnabler().isEnabledForSelection(selection);
083: }
084:
085: /**
086: * Answer the selection for the reciever based on whether the it can handle
087: * the selection. If it can return the selection. If it can handle the
088: * adapted to IResource value of the selection. If it satisfies neither of
089: * these conditions return an empty IStructuredSelection.
090: *
091: * @return IStructuredSelection
092: * @param selection
093: * IStructuredSelection
094: */
095: public IStructuredSelection adaptedSelection(
096: IStructuredSelection selection) {
097: if (canHandleSelection(selection)) {
098: return selection;
099: }
100:
101: IStructuredSelection adaptedSelection = convertToResources(selection);
102: if (canHandleSelection(adaptedSelection)) {
103: return adaptedSelection;
104: }
105:
106: //Couldn't find one that works so just return
107: return StructuredSelection.EMPTY;
108: }
109:
110: /**
111: * Create an the instance of the object described by the configuration
112: * element. That is, create the instance of the class the isv supplied in
113: * the extension point.
114: * @return the new object
115: * @throws CoreException
116: */
117: public Object createExecutableExtension() throws CoreException {
118: return WorkbenchPlugin.createExtension(configurationElement,
119: IWorkbenchRegistryConstants.ATT_CLASS);
120: }
121:
122: /**
123: * Returns an object which is an instance of the given class associated
124: * with this object. Returns <code>null</code> if no such object can be
125: * found.
126: */
127: public Object getAdapter(Class adapter) {
128: if (adapter == IWorkbenchAdapter.class
129: || adapter == IWorkbenchAdapter2.class) {
130: return this ;
131: } else if (adapter == IPluginContribution.class) {
132: return this ;
133: } else if (adapter == IConfigurationElement.class) {
134: return configurationElement;
135: }
136: return Platform.getAdapterManager().getAdapter(this , adapter);
137: }
138:
139: /**
140: * @return IConfigurationElement
141: */
142: public IConfigurationElement getConfigurationElement() {
143: return configurationElement;
144: }
145:
146: /**
147: * Answer the description parameter of this element
148: *
149: * @return java.lang.String
150: */
151: public String getDescription() {
152: return RegistryReader.getDescription(configurationElement);
153: }
154:
155: /**
156: * Answer the icon of this element.
157: */
158: public ImageDescriptor getImageDescriptor() {
159: if (imageDescriptor == null) {
160: String iconName = configurationElement
161: .getAttribute(IWorkbenchRegistryConstants.ATT_ICON);
162: if (iconName == null) {
163: return null;
164: }
165: imageDescriptor = AbstractUIPlugin
166: .imageDescriptorFromPlugin(configurationElement
167: .getNamespaceIdentifier(), iconName);
168: }
169: return imageDescriptor;
170: }
171:
172: /**
173: * Returns the name of this wizard element.
174: */
175: public ImageDescriptor getImageDescriptor(Object element) {
176: return getImageDescriptor();
177: }
178:
179: /**
180: * Returns the name of this wizard element.
181: */
182: public String getLabel(Object element) {
183: return configurationElement
184: .getAttribute(IWorkbenchRegistryConstants.ATT_NAME);
185: }
186:
187: /**
188: * Answer self's action enabler, creating it first iff necessary
189: */
190: protected SelectionEnabler getSelectionEnabler() {
191: if (selectionEnabler == null) {
192: selectionEnabler = new SelectionEnabler(
193: configurationElement);
194: }
195:
196: return selectionEnabler;
197: }
198:
199: /**
200: * Attempt to convert the elements in the passed selection into resources
201: * by asking each for its IResource property (iff it isn't already a
202: * resource). If all elements in the initial selection can be converted to
203: * resources then answer a new selection containing these resources;
204: * otherwise answer an empty selection.
205: *
206: * @param originalSelection the original selection
207: * @return the converted selection or an empty selection
208: */
209: private IStructuredSelection convertToResources(
210: IStructuredSelection originalSelection) {
211: Object selectionService = PlatformUI.getWorkbench().getService(
212: ISelectionConversionService.class);
213: if (selectionService == null || originalSelection == null) {
214: return StructuredSelection.EMPTY;
215: }
216: return ((ISelectionConversionService) selectionService)
217: .convertToResources(originalSelection);
218: }
219:
220: /*
221: * (non-Javadoc)
222: *
223: * @see org.eclipse.ui.IPluginContribution#getLocalId()
224: */
225: public String getLocalId() {
226: return getId();
227: }
228:
229: /* (non-Javadoc)
230: * @see org.eclipse.ui.IPluginContribution#getPluginId()
231: */
232: public String getPluginId() {
233: return (configurationElement != null) ? configurationElement
234: .getNamespaceIdentifier() : null;
235: }
236:
237: /* (non-Javadoc)
238: * @see org.eclipse.ui.wizards.INewWizardDescriptor#getDescriptionImage()
239: */
240: public ImageDescriptor getDescriptionImage() {
241: if (descriptionImage == null) {
242: String descImage = configurationElement
243: .getAttribute(IWorkbenchRegistryConstants.ATT_DESCRIPTION_IMAGE);
244: if (descImage == null) {
245: return null;
246: }
247: descriptionImage = AbstractUIPlugin
248: .imageDescriptorFromPlugin(configurationElement
249: .getNamespaceIdentifier(), descImage);
250: }
251: return descriptionImage;
252: }
253:
254: /* (non-Javadoc)
255: * @see org.eclipse.ui.wizards.INewWizardDescriptor#getHelpHref()
256: */
257: public String getHelpHref() {
258: return configurationElement
259: .getAttribute(IWorkbenchRegistryConstants.ATT_HELP_HREF);
260: }
261:
262: /* (non-Javadoc)
263: * @see org.eclipse.ui.wizards.INewWizardDescriptor#createWizard()
264: */
265: public IWorkbenchWizard createWizard() throws CoreException {
266: return (IWorkbenchWizard) createExecutableExtension();
267: }
268:
269: /* (non-Javadoc)
270: * @see org.eclipse.ui.IWorkbenchPartDescriptor#getId()
271: */
272: public String getId() {
273: return id;
274: }
275:
276: /* (non-Javadoc)
277: * @see org.eclipse.ui.IWorkbenchPartDescriptor#getLabel()
278: */
279: public String getLabel() {
280: return getLabel(this );
281: }
282:
283: /* (non-Javadoc)
284: * @see org.eclipse.ui.wizards.INewWizardDescriptor#getCategory()
285: */
286: public IWizardCategory getCategory() {
287: return (IWizardCategory) getParent(this );
288: }
289:
290: /**
291: * Return the collection.
292: *
293: * @return the collection
294: * @since 3.1
295: */
296: public WizardCollectionElement getCollectionElement() {
297: return (WizardCollectionElement) getParent(this );
298: }
299:
300: /* (non-Javadoc)
301: * @see org.eclipse.ui.wizards.IWizardDescriptor#getTags()
302: */
303: public String[] getTags() {
304:
305: String flag = configurationElement
306: .getAttribute(IWorkbenchRegistryConstants.ATT_PROJECT);
307: if (Boolean.valueOf(flag).booleanValue()) {
308: return PROJECT_TAGS;
309: }
310:
311: return EMPTY_TAGS;
312: }
313:
314: /* (non-Javadoc)
315: * @see org.eclipse.ui.model.IWorkbenchAdapter#getParent(java.lang.Object)
316: */
317: public Object getParent(Object object) {
318: return parentCategory;
319: }
320:
321: /**
322: * Set the parent category.
323: *
324: * @param parent the parent category
325: * @since 3.1
326: */
327: public void setParent(WizardCollectionElement parent) {
328: parentCategory = parent;
329: }
330:
331: /* (non-Javadoc)
332: * @see org.eclipse.ui.wizards.IWizardDescriptor#canFinishEarly()
333: */
334: public boolean canFinishEarly() {
335: return Boolean
336: .valueOf(
337: configurationElement
338: .getAttribute(IWorkbenchRegistryConstants.ATT_CAN_FINISH_EARLY))
339: .booleanValue();
340: }
341:
342: /* (non-Javadoc)
343: * @see org.eclipse.ui.wizards.IWizardDescriptor#hasPages()
344: */
345: public boolean hasPages() {
346: String hasPagesString = configurationElement
347: .getAttribute(IWorkbenchRegistryConstants.ATT_HAS_PAGES);
348: // default value is true
349: if (hasPagesString == null) {
350: return true;
351: }
352: return Boolean.valueOf(hasPagesString).booleanValue();
353: }
354: }
|