1 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 40 public abstract class WizardHandler extends AbstractHandler { 41 42 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 getWizardIdParameterId() { 53 return "exportWizardId"; } 55 56 protected IWizardRegistry getWizardRegistry() { 57 return PlatformUI.getWorkbench().getExportWizardRegistry(); 58 } 59 60 } 61 62 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 getWizardIdParameterId() { 73 return "importWizardId"; } 75 76 protected IWizardRegistry getWizardRegistry() { 77 return PlatformUI.getWorkbench().getImportWizardRegistry(); 78 } 79 80 } 81 82 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 getWizardIdParameterId() { 93 return "newWizardId"; } 95 96 protected IWizardRegistry getWizardRegistry() { 97 return PlatformUI.getWorkbench().getNewWizardRegistry(); 98 } 99 100 } 101 102 111 protected abstract IAction createWizardChooserDialogAction( 112 IWorkbenchWindow window); 113 114 public Object execute(ExecutionEvent event) 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("unknown wizard: " + wizardId); } 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); } 151 152 } 153 154 return null; 155 } 156 157 164 protected abstract String getWizardIdParameterId(); 165 166 173 protected abstract IWizardRegistry getWizardRegistry(); 174 175 } 176 | Popular Tags |