KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.IStatus;
15 import org.eclipse.core.runtime.SafeRunner;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.jface.util.SafeRunnable;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.jface.wizard.IWizard;
20 import org.eclipse.jface.wizard.IWizardNode;
21 import org.eclipse.swt.custom.BusyIndicator;
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.ui.IPluginContribution;
25 import org.eclipse.ui.IWorkbench;
26 import org.eclipse.ui.IWorkbenchWizard;
27 import org.eclipse.ui.internal.WorkbenchMessages;
28 import org.eclipse.ui.internal.WorkbenchPlugin;
29 import org.eclipse.ui.internal.util.Util;
30 import org.eclipse.ui.statushandlers.StatusAdapter;
31 import org.eclipse.ui.statushandlers.StatusManager;
32 import org.eclipse.ui.wizards.IWizardDescriptor;
33
34 /**
35  * A wizard node represents a "potential" wizard. Wizard nodes
36  * are used by wizard selection pages to allow the user to pick
37  * from several available nested wizards.
38  * <p>
39  * <b>Subclasses</b> simply need to override method <code>createWizard()</code>,
40  * which is responsible for creating an instance of the wizard it represents
41  * AND ensuring that this wizard is the "right" type of wizard (e.g.-
42  * New, Import, etc.).</p>
43  */

44 public abstract class WorkbenchWizardNode implements IWizardNode,
45         IPluginContribution {
46     protected WorkbenchWizardSelectionPage parentWizardPage;
47
48     protected IWizard wizard;
49
50     protected IWizardDescriptor wizardElement;
51
52     /**
53      * Creates a <code>WorkbenchWizardNode</code> that holds onto a wizard
54      * element. The wizard element provides information on how to create
55      * the wizard supplied by the ISV's extension.
56      *
57      * @param aWizardPage the wizard page
58      * @param element the wizard descriptor
59      */

60     public WorkbenchWizardNode(WorkbenchWizardSelectionPage aWizardPage,
61             IWizardDescriptor element) {
62         super();
63         this.parentWizardPage = aWizardPage;
64         this.wizardElement = element;
65     }
66
67     /**
68      * Returns the wizard represented by this wizard node. <b>Subclasses</b>
69      * must override this method.
70      *
71      * @return the wizard object
72      * @throws CoreException
73      */

74     public abstract IWorkbenchWizard createWizard() throws CoreException;
75
76     /* (non-Javadoc)
77      * @see org.eclipse.jface.wizard.IWizardNode#dispose()
78      */

79     public void dispose() {
80         // Do nothing since the wizard wasn't created via reflection.
81
}
82
83     /**
84      * Returns the current resource selection that is being given to the wizard.
85      */

86     protected IStructuredSelection getCurrentResourceSelection() {
87         return parentWizardPage.getCurrentResourceSelection();
88     }
89
90     /* (non-Javadoc)
91      * @see org.eclipse.jface.wizard.IWizardNode#getExtent()
92      */

93     public Point getExtent() {
94         return new Point(-1, -1);
95     }
96
97     /* (non-Javadoc)
98      * @see org.eclipse.ui.IPluginContribution#getLocalId()
99      */

100     public String JavaDoc getLocalId() {
101         IPluginContribution contribution = (IPluginContribution) Util.getAdapter(wizardElement,
102                 IPluginContribution.class);
103         if (contribution != null) {
104             return contribution.getLocalId();
105         }
106         return wizardElement.getId();
107     }
108
109     /* (non-Javadoc)
110      * @see org.eclipse.ui.IPluginContribution#getPluginId()
111      */

112     public String JavaDoc getPluginId() {
113         IPluginContribution contribution = (IPluginContribution) Util.getAdapter(wizardElement,
114                 IPluginContribution.class);
115         if (contribution != null) {
116             return contribution.getPluginId();
117         }
118         return null;
119     }
120
121     /* (non-Javadoc)
122      * @see org.eclipse.jface.wizard.IWizardNode#getWizard()
123      */

124     public IWizard getWizard() {
125         if (wizard != null) {
126             return wizard; // we've already created it
127
}
128
129         final IWorkbenchWizard[] workbenchWizard = new IWorkbenchWizard[1];
130         final IStatus statuses[] = new IStatus[1];
131         // Start busy indicator.
132
BusyIndicator.showWhile(parentWizardPage.getShell().getDisplay(),
133                 new Runnable JavaDoc() {
134                     public void run() {
135                         SafeRunner.run(new SafeRunnable() {
136                             /**
137                              * Add the exception details to status is one happens.
138                              */

139                             public void handleException(Throwable JavaDoc e) {
140                                 IPluginContribution contribution = (IPluginContribution) Util.getAdapter(wizardElement, IPluginContribution.class);
141                                 statuses[0] = new Status(
142                                         IStatus.ERROR,
143                                         contribution != null ? contribution.getPluginId() : WorkbenchPlugin.PI_WORKBENCH,
144                                         IStatus.OK,
145                                         WorkbenchMessages.WorkbenchWizard_errorMessage,
146                                         e);
147                             }
148
149                             public void run() {
150                                 try {
151                                     workbenchWizard[0] = createWizard();
152                                     // create instance of target wizard
153
} catch (CoreException e) {
154                                     IPluginContribution contribution = (IPluginContribution) Util.getAdapter(wizardElement, IPluginContribution.class);
155                                     statuses[0] = new Status(
156                                             IStatus.ERROR,
157                                             contribution != null ? contribution.getPluginId() : WorkbenchPlugin.PI_WORKBENCH,
158                                             IStatus.OK,
159                                             WorkbenchMessages.WorkbenchWizard_errorMessage,
160                                             e);
161                                 }
162                             }
163                         });
164                     }
165                 });
166
167         if (statuses[0] != null) {
168             parentWizardPage
169                     .setErrorMessage(WorkbenchMessages.WorkbenchWizard_errorMessage);
170             StatusAdapter statusAdapter = new StatusAdapter(statuses[0]);
171             statusAdapter.addAdapter(Shell.class, parentWizardPage.getShell());
172             statusAdapter.setProperty(StatusAdapter.TITLE_PROPERTY,
173                     WorkbenchMessages.WorkbenchWizard_errorTitle);
174             StatusManager.getManager()
175                     .handle(statusAdapter, StatusManager.SHOW);
176             return null;
177         }
178
179         IStructuredSelection currentSelection = getCurrentResourceSelection();
180
181         //Get the adapted version of the selection that works for the
182
//wizard node
183
currentSelection = wizardElement.adaptedSelection(currentSelection);
184
185         workbenchWizard[0].init(getWorkbench(), currentSelection);
186
187         wizard = workbenchWizard[0];
188         return wizard;
189     }
190
191     /**
192      * Returns the wizard element.
193      *
194      * @return the wizard descriptor
195      */

196     public IWizardDescriptor getWizardElement() {
197         return wizardElement;
198     }
199
200     /**
201      * Returns the current workbench.
202      */

203     protected IWorkbench getWorkbench() {
204         return parentWizardPage.getWorkbench();
205     }
206
207     /* (non-Javadoc)
208      * @see org.eclipse.jface.wizard.IWizardNode#isContentCreated()
209      */

210     public boolean isContentCreated() {
211         return wizard != null;
212     }
213 }
214
Popular Tags