001: /*******************************************************************************
002: * Copyright (c) 2006, 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.handlers;
011:
012: import org.eclipse.core.commands.AbstractHandler;
013: import org.eclipse.core.commands.ExecutionEvent;
014: import org.eclipse.core.commands.ExecutionException;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.jface.action.IAction;
017: import org.eclipse.jface.viewers.StructuredSelection;
018: import org.eclipse.jface.wizard.WizardDialog;
019: import org.eclipse.swt.widgets.Shell;
020: import org.eclipse.ui.IWorkbenchWindow;
021: import org.eclipse.ui.IWorkbenchWizard;
022: import org.eclipse.ui.PlatformUI;
023: import org.eclipse.ui.actions.ExportResourcesAction;
024: import org.eclipse.ui.actions.ImportResourcesAction;
025: import org.eclipse.ui.actions.NewWizardAction;
026: import org.eclipse.ui.handlers.HandlerUtil;
027: import org.eclipse.ui.wizards.IWizardDescriptor;
028: import org.eclipse.ui.wizards.IWizardRegistry;
029:
030: /**
031: * Abstract handler for commands that launch the import, export and new wizards.
032: * <p>
033: * This class is only intended to be extended by the three inner classes (<code>Export</code>,
034: * <code>Import</code> and <code>New</code>) defined here.
035: * </p>
036: *
037: * @since 3.2
038: */
039: public abstract class WizardHandler extends AbstractHandler {
040:
041: /**
042: * Default handler for launching export wizards.
043: */
044: public static final class Export extends WizardHandler {
045:
046: protected IAction createWizardChooserDialogAction(
047: IWorkbenchWindow window) {
048: return new ExportResourcesAction(window);
049: }
050:
051: protected String getWizardIdParameterId() {
052: return "exportWizardId"; //$NON-NLS-1$
053: }
054:
055: protected IWizardRegistry getWizardRegistry() {
056: return PlatformUI.getWorkbench().getExportWizardRegistry();
057: }
058:
059: }
060:
061: /**
062: * Default handler for launching import wizards.
063: */
064: public static final class Import extends WizardHandler {
065:
066: protected IAction createWizardChooserDialogAction(
067: IWorkbenchWindow window) {
068: return new ImportResourcesAction(window);
069: }
070:
071: protected String getWizardIdParameterId() {
072: return "importWizardId"; //$NON-NLS-1$
073: }
074:
075: protected IWizardRegistry getWizardRegistry() {
076: return PlatformUI.getWorkbench().getImportWizardRegistry();
077: }
078:
079: }
080:
081: /**
082: * Default handler for launching new wizards.
083: */
084: public static final class New extends WizardHandler {
085:
086: protected IAction createWizardChooserDialogAction(
087: IWorkbenchWindow window) {
088: return new NewWizardAction(window);
089: }
090:
091: protected String getWizardIdParameterId() {
092: return "newWizardId"; //$NON-NLS-1$
093: }
094:
095: protected IWizardRegistry getWizardRegistry() {
096: return PlatformUI.getWorkbench().getNewWizardRegistry();
097: }
098:
099: }
100:
101: /**
102: * Returns an <code>IAction</code> that opens a dialog to allow the user
103: * to choose a wizard.
104: *
105: * @param window
106: * The workbench window to use when constructing the action.
107: * @return An <code>IAction</code> that opens a dialog to allow the user
108: * to choose a wizard.
109: */
110: protected abstract IAction createWizardChooserDialogAction(
111: IWorkbenchWindow window);
112:
113: public Object execute(ExecutionEvent event)
114: throws ExecutionException {
115:
116: String 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(
131: "unknown wizard: " + wizardId); //$NON-NLS-1$
132: }
133:
134: try {
135: IWorkbenchWizard wizard = wizardDescriptor
136: .createWizard();
137: wizard.init(PlatformUI.getWorkbench(),
138: StructuredSelection.EMPTY);
139:
140: if (wizardDescriptor.canFinishEarly()
141: && !wizardDescriptor.hasPages()) {
142: wizard.performFinish();
143: return null;
144: }
145:
146: Shell parent = activeWindow.getShell();
147: WizardDialog dialog = new WizardDialog(parent, wizard);
148: dialog.create();
149: dialog.open();
150:
151: } catch (CoreException ex) {
152: throw new ExecutionException(
153: "error creating wizard", ex); //$NON-NLS-1$
154: }
155:
156: }
157:
158: return null;
159: }
160:
161: /**
162: * Returns the id of the parameter used to indicate which wizard this
163: * command should launch.
164: *
165: * @return The id of the parameter used to indicate which wizard this
166: * command should launch.
167: */
168: protected abstract String getWizardIdParameterId();
169:
170: /**
171: * Returns the wizard registry for the concrete <code>WizardHandler</code>
172: * implementation class.
173: *
174: * @return The wizard registry for the concrete <code>WizardHandler</code>
175: * implementation class.
176: */
177: protected abstract IWizardRegistry getWizardRegistry();
178:
179: }
|