| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2000, 2008 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | package org.eclipse.ui.actions; |
| 12 | |
| 13 | import org.eclipse.jface.action.Action; |
| 14 | import org.eclipse.jface.dialogs.IDialogSettings; |
| 15 | import org.eclipse.jface.viewers.ISelection; |
| 16 | import org.eclipse.jface.viewers.IStructuredSelection; |
| 17 | import org.eclipse.jface.viewers.StructuredSelection; |
| 18 | import org.eclipse.jface.wizard.WizardDialog; |
| 19 | import org.eclipse.ui.ISharedImages; |
| 20 | import org.eclipse.ui.IWorkbench; |
| 21 | import org.eclipse.ui.IWorkbenchWindow; |
| 22 | import org.eclipse.ui.PlatformUI; |
| 23 | import org.eclipse.ui.internal.dialogs.NewWizard; |
| 24 | import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; |
| 25 | import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; |
| 26 | import org.eclipse.ui.internal.ide.IIDEHelpContextIds; |
| 27 | |
| 28 | /** |
| 29 | * Standard action for launching the create project selection |
| 30 | * wizard. |
| 31 | * <p> |
| 32 | * This class may be instantiated; it is not intended to be subclassed. |
| 33 | * </p> |
| 34 | * @noextend This class is not intended to be subclassed by clients. |
| 35 | */ |
| 36 | public class NewProjectAction extends Action { |
| 37 | |
| 38 | /** |
| 39 | * The wizard dialog width |
| 40 | */ |
| 41 | private static final int SIZING_WIZARD_WIDTH = 500; |
| 42 | |
| 43 | /** |
| 44 | * The wizard dialog height |
| 45 | */ |
| 46 | private static final int SIZING_WIZARD_HEIGHT = 500; |
| 47 | |
| 48 | /** |
| 49 | * The workbench window this action will run in |
| 50 | */ |
| 51 | private IWorkbenchWindow window; |
| 52 | |
| 53 | /** |
| 54 | * This default constructor allows the the action to be called from the welcome page. |
| 55 | */ |
| 56 | public NewProjectAction() { |
| 57 | this(PlatformUI.getWorkbench().getActiveWorkbenchWindow()); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Creates a new action for launching the new project |
| 62 | * selection wizard. |
| 63 | * |
| 64 | * @param window the workbench window to query the current |
| 65 | * selection and shell for opening the wizard. |
| 66 | */ |
| 67 | public NewProjectAction(IWorkbenchWindow window) { |
| 68 | super(IDEWorkbenchMessages.NewProjectAction_text); |
| 69 | if (window == null) { |
| 70 | throw new IllegalArgumentException(); |
| 71 | } |
| 72 | this.window = window; |
| 73 | ISharedImages images = PlatformUI.getWorkbench().getSharedImages(); |
| 74 | setImageDescriptor(images |
| 75 | .getImageDescriptor(ISharedImages.IMG_TOOL_NEW_WIZARD)); |
| 76 | setDisabledImageDescriptor(images |
| 77 | .getImageDescriptor(ISharedImages.IMG_TOOL_NEW_WIZARD_DISABLED)); |
| 78 | setToolTipText(IDEWorkbenchMessages.NewProjectAction_toolTip); |
| 79 | PlatformUI.getWorkbench().getHelpSystem().setHelp(this, |
| 80 | org.eclipse.ui.internal.IWorkbenchHelpContextIds.NEW_ACTION); |
| 81 | } |
| 82 | |
| 83 | /* (non-Javadoc) |
| 84 | * Method declared on IAction. |
| 85 | */ |
| 86 | public void run() { |
| 87 | // Create wizard selection wizard. |
| 88 | IWorkbench workbench = PlatformUI.getWorkbench(); |
| 89 | NewWizard wizard = new NewWizard(); |
| 90 | wizard.setProjectsOnly(true); |
| 91 | ISelection selection = window.getSelectionService().getSelection(); |
| 92 | IStructuredSelection selectionToPass = StructuredSelection.EMPTY; |
| 93 | if (selection instanceof IStructuredSelection) { |
| 94 | selectionToPass = (IStructuredSelection) selection; |
| 95 | } |
| 96 | wizard.init(workbench, selectionToPass); |
| 97 | IDialogSettings workbenchSettings = IDEWorkbenchPlugin.getDefault() |
| 98 | .getDialogSettings(); |
| 99 | IDialogSettings wizardSettings = workbenchSettings |
| 100 | .getSection("NewWizardAction");//$NON-NLS-1$ |
| 101 | if (wizardSettings == null) { |
| 102 | wizardSettings = workbenchSettings.addNewSection("NewWizardAction");//$NON-NLS-1$ |
| 103 | } |
| 104 | wizard.setDialogSettings(wizardSettings); |
| 105 | wizard.setForcePreviousAndNextButtons(true); |
| 106 | |
| 107 | // Create wizard dialog. |
| 108 | WizardDialog dialog = new WizardDialog(null, wizard); |
| 109 | dialog.create(); |
| 110 | dialog.getShell().setSize( |
| 111 | Math.max(SIZING_WIZARD_WIDTH, dialog.getShell().getSize().x), |
| 112 | SIZING_WIZARD_HEIGHT); |
| 113 | PlatformUI.getWorkbench().getHelpSystem().setHelp(dialog.getShell(), |
| 114 | IIDEHelpContextIds.NEW_PROJECT_WIZARD); |
| 115 | |
| 116 | // Open wizard. |
| 117 | dialog.open(); |
| 118 | } |
| 119 | } |