1 11 package org.eclipse.ui.internal.ide; 12 13 14 import org.eclipse.core.runtime.CoreException; 15 import org.eclipse.jface.action.Action; 16 import org.eclipse.jface.dialogs.ErrorDialog; 17 import org.eclipse.jface.viewers.ISelection; 18 import org.eclipse.jface.viewers.IStructuredSelection; 19 import org.eclipse.jface.viewers.StructuredSelection; 20 import org.eclipse.jface.wizard.WizardDialog; 21 import org.eclipse.swt.widgets.Shell; 22 import org.eclipse.ui.IEditorInput; 23 import org.eclipse.ui.IEditorPart; 24 import org.eclipse.ui.IFileEditorInput; 25 import org.eclipse.ui.INewWizard; 26 import org.eclipse.ui.IPluginContribution; 27 import org.eclipse.ui.IWorkbenchPart; 28 import org.eclipse.ui.IWorkbenchWindow; 29 import org.eclipse.ui.actions.ActionFactory; 30 import org.eclipse.ui.help.WorkbenchHelp; 31 import org.eclipse.ui.internal.dialogs.WorkbenchWizardElement; 33 34 39 public class NewWizardShortcutAction extends Action implements IPluginContribution { 40 private WorkbenchWizardElement wizardElement; 41 private IWorkbenchWindow window; 42 49 public NewWizardShortcutAction(IWorkbenchWindow window, WorkbenchWizardElement element) { 50 super(element.getLabel(element)); 51 setToolTipText(element.getDescription()); 52 setImageDescriptor(element.getImageDescriptor()); 53 setId(ActionFactory.NEW.getId()); 54 wizardElement = element; 55 this.window = window; 56 } 57 60 public void run() { 61 63 INewWizard wizard; 64 try { 65 wizard = (INewWizard) wizardElement.createExecutableExtension(); 66 } catch (CoreException e) { 67 ErrorDialog.openError( 68 window.getShell(), 69 IDEWorkbenchMessages.getString("NewWizardShortcutAction.errorTitle"), IDEWorkbenchMessages.getString("NewWizardShortcutAction.errorMessage"), e.getStatus()); 72 return; 73 } 74 75 ISelection selection = window.getSelectionService().getSelection(); 76 IStructuredSelection selectionToPass = StructuredSelection.EMPTY; 77 if (selection instanceof IStructuredSelection) { 78 selectionToPass = wizardElement.adaptedSelection((IStructuredSelection) selection); 79 } else { 80 IWorkbenchPart part = window.getPartService().getActivePart(); 82 if (part instanceof IEditorPart) { 83 IEditorInput input = ((IEditorPart) part).getEditorInput(); 84 if (input instanceof IFileEditorInput) { 85 selectionToPass = new StructuredSelection(((IFileEditorInput) input).getFile()); 86 } 87 } 88 } 89 90 wizard.init(window.getWorkbench(), selectionToPass); 91 92 Shell parent = window.getShell(); 93 WizardDialog dialog = new WizardDialog(parent, wizard); 94 dialog.create(); 95 WorkbenchHelp.setHelp(dialog.getShell(), IHelpContextIds.NEW_WIZARD_SHORTCUT); 96 dialog.open(); 97 } 98 99 100 103 public String getLocalId() { 104 return wizardElement.getLocalId(); 105 } 106 107 110 public String getPluginId() { 111 return wizardElement.getPluginId(); 112 } 113 } 114 | Popular Tags |