1 11 package org.eclipse.ui.internal.actions; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.jface.action.Action; 15 import org.eclipse.jface.dialogs.ErrorDialog; 16 import org.eclipse.jface.viewers.ISelection; 17 import org.eclipse.jface.viewers.IStructuredSelection; 18 import org.eclipse.jface.viewers.StructuredSelection; 19 import org.eclipse.jface.wizard.WizardDialog; 20 import org.eclipse.swt.graphics.Point; 21 import org.eclipse.swt.widgets.Shell; 22 import org.eclipse.ui.IEditorInput; 23 import org.eclipse.ui.IEditorPart; 24 import org.eclipse.ui.INewWizard; 25 import org.eclipse.ui.IPluginContribution; 26 import org.eclipse.ui.IWorkbenchPart; 27 import org.eclipse.ui.IWorkbenchWindow; 28 import org.eclipse.ui.actions.ActionFactory; 29 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 30 import org.eclipse.ui.internal.LegacyResourceSupport; 31 import org.eclipse.ui.internal.WorkbenchMessages; 32 import org.eclipse.ui.internal.util.Util; 33 import org.eclipse.ui.wizards.IWizardDescriptor; 34 35 38 public class NewWizardShortcutAction extends Action implements 39 IPluginContribution { 40 private IWizardDescriptor wizardElement; 41 42 45 private static final int SIZING_WIZARD_WIDTH = 500; 46 47 50 private static final int SIZING_WIZARD_HEIGHT = 500; 51 52 private IWorkbenchWindow window; 53 54 60 public NewWizardShortcutAction(IWorkbenchWindow window, 61 IWizardDescriptor wizardDesc) { 62 super(wizardDesc.getLabel()); 63 setToolTipText(wizardDesc.getDescription()); 64 setImageDescriptor(wizardDesc.getImageDescriptor()); 65 setId(ActionFactory.NEW.getId()); 66 wizardElement = wizardDesc; 67 this.window = window; 68 } 69 70 75 public IWizardDescriptor getWizardDescriptor() { 76 return wizardElement; 77 } 78 79 82 public void run() { 83 85 INewWizard wizard; 86 try { 87 wizard = (INewWizard) wizardElement.createWizard(); 88 } catch (CoreException e) { 89 ErrorDialog.openError(window.getShell(), WorkbenchMessages.NewWizardShortcutAction_errorTitle, 90 WorkbenchMessages.NewWizardShortcutAction_errorMessage, 91 e.getStatus()); 92 return; 93 } 94 95 ISelection selection = window.getSelectionService().getSelection(); 96 IStructuredSelection selectionToPass = StructuredSelection.EMPTY; 97 if (selection instanceof IStructuredSelection) { 98 selectionToPass = wizardElement 99 .adaptedSelection((IStructuredSelection) selection); 100 } else { 101 IWorkbenchPart part = window.getPartService().getActivePart(); 103 if (part instanceof IEditorPart) { 104 IEditorInput input = ((IEditorPart) part).getEditorInput(); 105 Class fileClass = LegacyResourceSupport.getFileClass(); 106 if (input != null && fileClass != null) { 107 Object file = Util.getAdapter(input, fileClass); 108 if (file != null) { 109 selectionToPass = new StructuredSelection(file); 110 } 111 } 112 } 113 } 114 115 wizard.init(window.getWorkbench(), selectionToPass); 119 120 Shell parent = window.getShell(); 121 WizardDialog dialog = new WizardDialog(parent, wizard); 122 dialog.create(); 123 Point defaultSize = dialog.getShell().getSize(); 124 dialog.getShell().setSize( 125 Math.max(SIZING_WIZARD_WIDTH, defaultSize.x), 126 Math.max(SIZING_WIZARD_HEIGHT, defaultSize.y)); 127 window.getWorkbench().getHelpSystem().setHelp(dialog.getShell(), 128 IWorkbenchHelpContextIds.NEW_WIZARD_SHORTCUT); 129 130 if (wizardElement.canFinishEarly() && !wizardElement.hasPages()) { 132 wizard.performFinish(); 133 dialog.close(); 134 } else { 135 dialog.open(); 136 } 137 } 138 139 142 public String getLocalId() { 143 IPluginContribution contribution = getPluginContribution(); 144 if (contribution != null) { 145 return contribution.getLocalId(); 146 } 147 return wizardElement.getId(); 148 } 149 150 153 public String getPluginId() { 154 IPluginContribution contribution = getPluginContribution(); 155 if (contribution != null) { 156 return contribution.getPluginId(); 157 } 158 return null; 159 } 160 161 167 private IPluginContribution getPluginContribution() { 168 return (IPluginContribution) Util.getAdapter(wizardElement, 169 IPluginContribution.class); 170 } 171 } 172 | Popular Tags |