KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > handlers > WizardHandler


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.handlers;
12
13 import org.eclipse.core.commands.AbstractHandler;
14 import org.eclipse.core.commands.ExecutionEvent;
15 import org.eclipse.core.commands.ExecutionException;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.jface.action.IAction;
18 import org.eclipse.jface.viewers.StructuredSelection;
19 import org.eclipse.jface.wizard.WizardDialog;
20 import org.eclipse.swt.widgets.Shell;
21 import org.eclipse.ui.IWorkbenchWindow;
22 import org.eclipse.ui.IWorkbenchWizard;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.actions.ExportResourcesAction;
25 import org.eclipse.ui.actions.ImportResourcesAction;
26 import org.eclipse.ui.actions.NewWizardAction;
27 import org.eclipse.ui.handlers.HandlerUtil;
28 import org.eclipse.ui.wizards.IWizardDescriptor;
29 import org.eclipse.ui.wizards.IWizardRegistry;
30
31 /**
32  * Abstract handler for commands that launch the import, export and new wizards.
33  * <p>
34  * This class is only intended to be extended by the three inner classes (<code>Export</code>,
35  * <code>Import</code> and <code>New</code>) defined here.
36  * </p>
37  *
38  * @since 3.2
39  */

40 public abstract class WizardHandler extends AbstractHandler {
41
42     /**
43      * Default handler for launching export wizards.
44      */

45     public static final class Export extends WizardHandler {
46
47         protected IAction createWizardChooserDialogAction(
48                 IWorkbenchWindow window) {
49             return new ExportResourcesAction(window);
50         }
51
52         protected String JavaDoc getWizardIdParameterId() {
53             return "exportWizardId"; //$NON-NLS-1$
54
}
55
56         protected IWizardRegistry getWizardRegistry() {
57             return PlatformUI.getWorkbench().getExportWizardRegistry();
58         }
59
60     }
61
62     /**
63      * Default handler for launching import wizards.
64      */

65     public static final class Import extends WizardHandler {
66
67         protected IAction createWizardChooserDialogAction(
68                 IWorkbenchWindow window) {
69             return new ImportResourcesAction(window);
70         }
71
72         protected String JavaDoc getWizardIdParameterId() {
73             return "importWizardId"; //$NON-NLS-1$
74
}
75
76         protected IWizardRegistry getWizardRegistry() {
77             return PlatformUI.getWorkbench().getImportWizardRegistry();
78         }
79
80     }
81
82     /**
83      * Default handler for launching new wizards.
84      */

85     public static final class New extends WizardHandler {
86
87         protected IAction createWizardChooserDialogAction(
88                 IWorkbenchWindow window) {
89             return new NewWizardAction(window);
90         }
91
92         protected String JavaDoc getWizardIdParameterId() {
93             return "newWizardId"; //$NON-NLS-1$
94
}
95
96         protected IWizardRegistry getWizardRegistry() {
97             return PlatformUI.getWorkbench().getNewWizardRegistry();
98         }
99
100     }
101
102     /**
103      * Returns an <code>IAction</code> that opens a dialog to allow the user
104      * to choose a wizard.
105      *
106      * @param window
107      * The workbench window to use when constructing the action.
108      * @return An <code>IAction</code> that opens a dialog to allow the user
109      * to choose a wizard.
110      */

111     protected abstract IAction createWizardChooserDialogAction(
112             IWorkbenchWindow window);
113
114     public Object JavaDoc execute(ExecutionEvent event) throws ExecutionException {
115
116         String JavaDoc wizardId = event.getParameter(getWizardIdParameterId());
117
118         IWorkbenchWindow activeWindow = HandlerUtil
119                 .getActiveWorkbenchWindowChecked(event);
120
121         if (wizardId == null) {
122             IAction wizardAction = createWizardChooserDialogAction(activeWindow);
123             wizardAction.run();
124         } else {
125
126             IWizardRegistry wizardRegistry = getWizardRegistry();
127             IWizardDescriptor wizardDescriptor = wizardRegistry
128                     .findWizard(wizardId);
129             if (wizardDescriptor == null) {
130                 throw new ExecutionException("unknown wizard: " + wizardId); //$NON-NLS-1$
131
}
132
133             try {
134                 IWorkbenchWizard wizard = wizardDescriptor.createWizard();
135                 wizard.init(PlatformUI.getWorkbench(),
136                         StructuredSelection.EMPTY);
137                 
138                 if (wizardDescriptor.canFinishEarly() && !wizardDescriptor.hasPages()) {
139                     wizard.performFinish();
140                     return null;
141                 }
142                 
143                 Shell parent = activeWindow.getShell();
144                 WizardDialog dialog = new WizardDialog(parent, wizard);
145                 dialog.create();
146                 dialog.open();
147
148             } catch (CoreException ex) {
149                 throw new ExecutionException("error creating wizard", ex); //$NON-NLS-1$
150
}
151
152         }
153
154         return null;
155     }
156
157     /**
158      * Returns the id of the parameter used to indicate which wizard this
159      * command should launch.
160      *
161      * @return The id of the parameter used to indicate which wizard this
162      * command should launch.
163      */

164     protected abstract String JavaDoc getWizardIdParameterId();
165
166     /**
167      * Returns the wizard registry for the concrete <code>WizardHandler</code>
168      * implementation class.
169      *
170      * @return The wizard registry for the concrete <code>WizardHandler</code>
171      * implementation class.
172      */

173     protected abstract IWizardRegistry getWizardRegistry();
174
175 }
176
Popular Tags