KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > CreateProjectAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 java.util.ArrayList JavaDoc;
14
15 import org.eclipse.jface.action.Action;
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.ui.ISharedImages;
21 import org.eclipse.ui.IWorkbenchWindow;
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.internal.ide.Category;
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 import org.eclipse.ui.internal.ide.dialogs.MultiStepWizardDialog;
28 import org.eclipse.ui.internal.ide.dialogs.NewProjectWizard;
29 import org.eclipse.ui.internal.ide.registry.Capability;
30 import org.eclipse.ui.internal.ide.registry.CapabilityRegistry;
31
32 /**
33  * Standard action for launching the new project creation
34  * wizard.
35  * <p>
36  * This class may be instantiated and subclassed by clients.
37  * </p>
38  * @deprecated This new experimental API is being temporary
39  * deprecated for release 2.0 New project creation should
40  * continue to make use of NewProjectAction.
41  */

42 public class CreateProjectAction extends Action {
43     /**
44      * The wizard dialog width
45      */

46     private static final int SIZING_WIZARD_WIDTH = 500;
47
48     /**
49      * The wizard dialog height
50      */

51     private static final int SIZING_WIZARD_HEIGHT = 500;
52
53     /**
54      * The workbench window this action will run in
55      */

56     private IWorkbenchWindow window;
57
58     /**
59      * The suggested name for the new project
60      */

61     private String JavaDoc initialProjectName;
62
63     /**
64      * The suggested capabilities for the new project
65      */

66     private Capability[] initialProjectCapabilities;
67
68     /**
69      * The suggested categories to be selected
70      */

71     private Category[] initialSelectedCategories;
72
73     /**
74      * Creates a new action for launching the new project
75      * selection wizard.
76      *
77      * @param window the workbench window to query the current
78      * selection and shell for opening the wizard.
79      */

80     public CreateProjectAction(IWorkbenchWindow window) {
81         super(IDEWorkbenchMessages.CreateProjectAction_text);
82         if (window == null) {
83             throw new IllegalArgumentException JavaDoc();
84         }
85         this.window = window;
86         ISharedImages images = PlatformUI.getWorkbench().getSharedImages();
87         setImageDescriptor(images
88                 .getImageDescriptor(ISharedImages.IMG_TOOL_NEW_WIZARD));
89         setDisabledImageDescriptor(images
90                 .getImageDescriptor(ISharedImages.IMG_TOOL_NEW_WIZARD_DISABLED));
91         setToolTipText(IDEWorkbenchMessages.CreateProjectAction_toolTip);
92         PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
93                 org.eclipse.ui.internal.IWorkbenchHelpContextIds.NEW_ACTION);
94     }
95
96     /**
97      * Returns the selection to initialized the wizard with
98      */

99     protected IStructuredSelection getInitialSelection() {
100         ISelection selection = window.getSelectionService().getSelection();
101         IStructuredSelection selectionToPass = StructuredSelection.EMPTY;
102         if (selection instanceof IStructuredSelection)
103             selectionToPass = (IStructuredSelection) selection;
104         return selectionToPass;
105     }
106
107     /**
108      * Sets the initial categories to be selected. Ignores
109      * any IDs which do not represent valid categories.
110      *
111      * @param ids initial category ids to select
112      */

113     public void setInitialSelectedCategories(String JavaDoc[] ids) {
114         if (ids == null || ids.length == 0)
115             initialSelectedCategories = null;
116         else {
117             CapabilityRegistry reg = IDEWorkbenchPlugin.getDefault()
118                     .getCapabilityRegistry();
119             ArrayList JavaDoc results = new ArrayList JavaDoc(ids.length);
120             for (int i = 0; i < ids.length; i++) {
121                 Category cat = reg.findCategory(ids[i]);
122                 if (cat != null)
123                     results.add(cat);
124             }
125             if (results.isEmpty())
126                 initialSelectedCategories = null;
127             else {
128                 initialSelectedCategories = new Category[results.size()];
129                 results.toArray(initialSelectedCategories);
130             }
131         }
132     }
133
134     /**
135      * Sets the initial project capabilities to be selected.
136      * Ignores any IDs which do not represent valid capabilities.
137      *
138      * @param ids initial project capability ids to select
139      */

140     public void setInitialProjectCapabilities(String JavaDoc[] ids) {
141         if (ids == null || ids.length == 0)
142             initialProjectCapabilities = null;
143         else {
144             CapabilityRegistry reg = IDEWorkbenchPlugin.getDefault()
145                     .getCapabilityRegistry();
146             ArrayList JavaDoc results = new ArrayList JavaDoc(ids.length);
147             for (int i = 0; i < ids.length; i++) {
148                 Capability cap = reg.findCapability(ids[i]);
149                 if (cap != null && cap.isValid())
150                     results.add(cap);
151             }
152             if (results.isEmpty())
153                 initialProjectCapabilities = null;
154             else {
155                 initialProjectCapabilities = new Capability[results.size()];
156                 results.toArray(initialProjectCapabilities);
157             }
158         }
159     }
160
161     /**
162      * Sets the initial project name. Leading and trailing
163      * spaces in the name are ignored.
164      *
165      * @param name initial project name
166      */

167     public void setInitialProjectName(String JavaDoc name) {
168         if (name == null)
169             initialProjectName = null;
170         else
171             initialProjectName = name.trim();
172     }
173
174     /* (non-Javadoc)
175      * Method declared on IAction.
176      */

177     public void run() {
178         // Create a new project wizard
179
NewProjectWizard wizard = new NewProjectWizard();
180         wizard.init(window.getWorkbench(), getInitialSelection());
181         wizard.setInitialProjectName(initialProjectName);
182         wizard.setInitialProjectCapabilities(initialProjectCapabilities);
183         wizard.setInitialSelectedCategories(initialSelectedCategories);
184
185         // Create a wizard dialog.
186
WizardDialog dialog = new MultiStepWizardDialog(window.getShell(),
187                 wizard);
188         dialog.create();
189         dialog.getShell().setSize(
190                 Math.max(SIZING_WIZARD_WIDTH, dialog.getShell().getSize().x),
191                 SIZING_WIZARD_HEIGHT);
192         PlatformUI.getWorkbench().getHelpSystem().setHelp(dialog.getShell(),
193                 IIDEHelpContextIds.NEW_PROJECT_WIZARD);
194
195         // Open the wizard.
196
dialog.open();
197     }
198 }
199
Popular Tags