001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.IStatus;
014: import org.eclipse.core.runtime.SafeRunner;
015: import org.eclipse.core.runtime.Status;
016: import org.eclipse.jface.util.SafeRunnable;
017: import org.eclipse.jface.viewers.IStructuredSelection;
018: import org.eclipse.jface.wizard.IWizard;
019: import org.eclipse.jface.wizard.IWizardNode;
020: import org.eclipse.swt.custom.BusyIndicator;
021: import org.eclipse.swt.graphics.Point;
022: import org.eclipse.swt.widgets.Shell;
023: import org.eclipse.ui.IPluginContribution;
024: import org.eclipse.ui.IWorkbench;
025: import org.eclipse.ui.IWorkbenchWizard;
026: import org.eclipse.ui.internal.WorkbenchMessages;
027: import org.eclipse.ui.internal.WorkbenchPlugin;
028: import org.eclipse.ui.internal.util.Util;
029: import org.eclipse.ui.statushandlers.StatusAdapter;
030: import org.eclipse.ui.statushandlers.StatusManager;
031: import org.eclipse.ui.wizards.IWizardDescriptor;
032:
033: /**
034: * A wizard node represents a "potential" wizard. Wizard nodes
035: * are used by wizard selection pages to allow the user to pick
036: * from several available nested wizards.
037: * <p>
038: * <b>Subclasses</b> simply need to override method <code>createWizard()</code>,
039: * which is responsible for creating an instance of the wizard it represents
040: * AND ensuring that this wizard is the "right" type of wizard (e.g.-
041: * New, Import, etc.).</p>
042: */
043: public abstract class WorkbenchWizardNode implements IWizardNode,
044: IPluginContribution {
045: protected WorkbenchWizardSelectionPage parentWizardPage;
046:
047: protected IWizard wizard;
048:
049: protected IWizardDescriptor wizardElement;
050:
051: /**
052: * Creates a <code>WorkbenchWizardNode</code> that holds onto a wizard
053: * element. The wizard element provides information on how to create
054: * the wizard supplied by the ISV's extension.
055: *
056: * @param aWizardPage the wizard page
057: * @param element the wizard descriptor
058: */
059: public WorkbenchWizardNode(
060: WorkbenchWizardSelectionPage aWizardPage,
061: IWizardDescriptor element) {
062: super ();
063: this .parentWizardPage = aWizardPage;
064: this .wizardElement = element;
065: }
066:
067: /**
068: * Returns the wizard represented by this wizard node. <b>Subclasses</b>
069: * must override this method.
070: *
071: * @return the wizard object
072: * @throws CoreException
073: */
074: public abstract IWorkbenchWizard createWizard()
075: throws CoreException;
076:
077: /* (non-Javadoc)
078: * @see org.eclipse.jface.wizard.IWizardNode#dispose()
079: */
080: public void dispose() {
081: // Do nothing since the wizard wasn't created via reflection.
082: }
083:
084: /**
085: * Returns the current resource selection that is being given to the wizard.
086: */
087: protected IStructuredSelection getCurrentResourceSelection() {
088: return parentWizardPage.getCurrentResourceSelection();
089: }
090:
091: /* (non-Javadoc)
092: * @see org.eclipse.jface.wizard.IWizardNode#getExtent()
093: */
094: public Point getExtent() {
095: return new Point(-1, -1);
096: }
097:
098: /* (non-Javadoc)
099: * @see org.eclipse.ui.IPluginContribution#getLocalId()
100: */
101: public String getLocalId() {
102: IPluginContribution contribution = (IPluginContribution) Util
103: .getAdapter(wizardElement, IPluginContribution.class);
104: if (contribution != null) {
105: return contribution.getLocalId();
106: }
107: return wizardElement.getId();
108: }
109:
110: /* (non-Javadoc)
111: * @see org.eclipse.ui.IPluginContribution#getPluginId()
112: */
113: public String getPluginId() {
114: IPluginContribution contribution = (IPluginContribution) Util
115: .getAdapter(wizardElement, IPluginContribution.class);
116: if (contribution != null) {
117: return contribution.getPluginId();
118: }
119: return null;
120: }
121:
122: /* (non-Javadoc)
123: * @see org.eclipse.jface.wizard.IWizardNode#getWizard()
124: */
125: public IWizard getWizard() {
126: if (wizard != null) {
127: return wizard; // we've already created it
128: }
129:
130: final IWorkbenchWizard[] workbenchWizard = new IWorkbenchWizard[1];
131: final IStatus statuses[] = new IStatus[1];
132: // Start busy indicator.
133: BusyIndicator.showWhile(parentWizardPage.getShell()
134: .getDisplay(), new Runnable() {
135: public void run() {
136: SafeRunner.run(new SafeRunnable() {
137: /**
138: * Add the exception details to status is one happens.
139: */
140: public void handleException(Throwable e) {
141: IPluginContribution contribution = (IPluginContribution) Util
142: .getAdapter(wizardElement,
143: IPluginContribution.class);
144: statuses[0] = new Status(
145: IStatus.ERROR,
146: contribution != null ? contribution
147: .getPluginId()
148: : WorkbenchPlugin.PI_WORKBENCH,
149: IStatus.OK,
150: WorkbenchMessages.WorkbenchWizard_errorMessage,
151: e);
152: }
153:
154: public void run() {
155: try {
156: workbenchWizard[0] = createWizard();
157: // create instance of target wizard
158: } catch (CoreException e) {
159: IPluginContribution contribution = (IPluginContribution) Util
160: .getAdapter(wizardElement,
161: IPluginContribution.class);
162: statuses[0] = new Status(
163: IStatus.ERROR,
164: contribution != null ? contribution
165: .getPluginId()
166: : WorkbenchPlugin.PI_WORKBENCH,
167: IStatus.OK,
168: WorkbenchMessages.WorkbenchWizard_errorMessage,
169: e);
170: }
171: }
172: });
173: }
174: });
175:
176: if (statuses[0] != null) {
177: parentWizardPage
178: .setErrorMessage(WorkbenchMessages.WorkbenchWizard_errorMessage);
179: StatusAdapter statusAdapter = new StatusAdapter(statuses[0]);
180: statusAdapter.addAdapter(Shell.class, parentWizardPage
181: .getShell());
182: statusAdapter.setProperty(StatusAdapter.TITLE_PROPERTY,
183: WorkbenchMessages.WorkbenchWizard_errorTitle);
184: StatusManager.getManager().handle(statusAdapter,
185: StatusManager.SHOW);
186: return null;
187: }
188:
189: IStructuredSelection currentSelection = getCurrentResourceSelection();
190:
191: //Get the adapted version of the selection that works for the
192: //wizard node
193: currentSelection = wizardElement
194: .adaptedSelection(currentSelection);
195:
196: workbenchWizard[0].init(getWorkbench(), currentSelection);
197:
198: wizard = workbenchWizard[0];
199: return wizard;
200: }
201:
202: /**
203: * Returns the wizard element.
204: *
205: * @return the wizard descriptor
206: */
207: public IWizardDescriptor getWizardElement() {
208: return wizardElement;
209: }
210:
211: /**
212: * Returns the current workbench.
213: */
214: protected IWorkbench getWorkbench() {
215: return parentWizardPage.getWorkbench();
216: }
217:
218: /* (non-Javadoc)
219: * @see org.eclipse.jface.wizard.IWizardNode#isContentCreated()
220: */
221: public boolean isContentCreated() {
222: return wizard != null;
223: }
224: }
|