1 11 12 package org.eclipse.ui.actions; 13 14 import org.eclipse.jface.action.Action; 15 import org.eclipse.jface.dialogs.IDialogSettings; 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.widgets.Shell; 21 import org.eclipse.ui.IEditorInput; 22 import org.eclipse.ui.IEditorPart; 23 import org.eclipse.ui.ISharedImages; 24 import org.eclipse.ui.IWorkbenchPart; 25 import org.eclipse.ui.IWorkbenchWindow; 26 import org.eclipse.ui.PlatformUI; 27 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 28 import org.eclipse.ui.internal.LegacyResourceSupport; 29 import org.eclipse.ui.internal.PerspectiveTracker; 30 import org.eclipse.ui.internal.WorkbenchMessages; 31 import org.eclipse.ui.internal.WorkbenchPlugin; 32 import org.eclipse.ui.internal.dialogs.NewWizard; 33 import org.eclipse.ui.internal.util.Util; 34 35 50 public class NewWizardAction extends Action implements 51 ActionFactory.IWorkbenchAction { 52 53 56 private static final int SIZING_WIZARD_WIDTH = 500; 57 58 61 private static final int SIZING_WIZARD_HEIGHT = 500; 62 63 67 private String categoryId = null; 68 69 73 private IWorkbenchWindow workbenchWindow; 74 75 79 private PerspectiveTracker tracker; 80 81 85 public NewWizardAction(IWorkbenchWindow window) { 86 super(WorkbenchMessages.NewWizardAction_text); 87 if (window == null) { 88 throw new IllegalArgumentException (); 89 } 90 this.workbenchWindow = window; 91 tracker = new PerspectiveTracker(window, this); 92 ISharedImages images = PlatformUI.getWorkbench().getSharedImages(); 94 setImageDescriptor(images 95 .getImageDescriptor(ISharedImages.IMG_TOOL_NEW_WIZARD)); 96 setDisabledImageDescriptor(images 97 .getImageDescriptor(ISharedImages.IMG_TOOL_NEW_WIZARD_DISABLED)); 98 setToolTipText(WorkbenchMessages.NewWizardAction_toolTip); 99 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 100 IWorkbenchHelpContextIds.NEW_ACTION); 101 } 102 103 108 public NewWizardAction() { 109 this(PlatformUI.getWorkbench().getActiveWorkbenchWindow()); 110 } 111 112 117 public String getCategoryId() { 118 return categoryId; 119 } 120 121 126 public void setCategoryId(String id) { 127 categoryId = id; 128 } 129 130 133 public void run() { 134 if (workbenchWindow == null) { 135 return; 137 } 138 NewWizard wizard = new NewWizard(); 139 wizard.setCategoryId(categoryId); 140 141 ISelection selection = workbenchWindow.getSelectionService() 142 .getSelection(); 143 IStructuredSelection selectionToPass = StructuredSelection.EMPTY; 144 if (selection instanceof IStructuredSelection) { 145 selectionToPass = (IStructuredSelection) selection; 146 } else { 147 Class resourceClass = LegacyResourceSupport.getResourceClass(); 150 if (resourceClass != null) { 151 IWorkbenchPart part = workbenchWindow.getPartService() 152 .getActivePart(); 153 if (part instanceof IEditorPart) { 154 IEditorInput input = ((IEditorPart) part).getEditorInput(); 155 Object resource = Util.getAdapter(input, resourceClass); 156 if (resource != null) { 157 selectionToPass = new StructuredSelection(resource); 158 } 159 } 160 } 161 } 162 163 wizard.init(workbenchWindow.getWorkbench(), selectionToPass); 164 IDialogSettings workbenchSettings = WorkbenchPlugin.getDefault() 165 .getDialogSettings(); 166 IDialogSettings wizardSettings = workbenchSettings 167 .getSection("NewWizardAction"); if (wizardSettings == null) { 169 wizardSettings = workbenchSettings.addNewSection("NewWizardAction"); } 171 wizard.setDialogSettings(wizardSettings); 172 wizard.setForcePreviousAndNextButtons(true); 173 174 Shell parent = workbenchWindow.getShell(); 175 WizardDialog dialog = new WizardDialog(parent, wizard); 176 dialog.create(); 177 dialog.getShell().setSize( 178 Math.max(SIZING_WIZARD_WIDTH, dialog.getShell().getSize().x), 179 SIZING_WIZARD_HEIGHT); 180 PlatformUI.getWorkbench().getHelpSystem().setHelp(dialog.getShell(), 181 IWorkbenchHelpContextIds.NEW_WIZARD); 182 dialog.open(); 183 } 184 185 189 public void dispose() { 190 if (workbenchWindow == null) { 191 return; 193 } 194 tracker.dispose(); 195 workbenchWindow = null; 196 } 197 198 } 199 | Popular Tags |