KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
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 /**
36  * Invoke the resource creation wizard selection Wizard.
37  * <p>
38  * This class may be instantiated; it is not intended to be subclassed.
39  * </p>
40  * <p>
41  * This method automatically registers listeners so that it can keep its
42  * enablement state up to date. Ordinarily, the window's references to these
43  * listeners will be dropped automatically when the window closes. However,
44  * if the client needs to get rid of an action while the window is still open,
45  * the client must call #dispose() to give the
46  * action an opportunity to deregister its listeners and to perform any other
47  * cleanup.
48  * </p>
49  */

50 public class NewWizardAction extends Action implements
51         ActionFactory.IWorkbenchAction {
52
53     /**
54      * The wizard dialog width
55      */

56     private static final int SIZING_WIZARD_WIDTH = 500;
57
58     /**
59      * The wizard dialog height
60      */

61     private static final int SIZING_WIZARD_HEIGHT = 500;
62
63     /**
64      * The id of the category to show or <code>null</code> to
65      * show all the categories.
66      */

67     private String JavaDoc categoryId = null;
68
69     /**
70      * The workbench window; or <code>null</code> if this
71      * action has been <code>dispose</code>d.
72      */

73     private IWorkbenchWindow workbenchWindow;
74
75     /**
76      * Tracks perspective activation, to update this action's
77      * enabled state.
78      */

79     private PerspectiveTracker tracker;
80     
81     /**
82      * Create a new instance of this class.
83      * @param window
84      */

85     public NewWizardAction(IWorkbenchWindow window) {
86         super(WorkbenchMessages.NewWizardAction_text);
87         if (window == null) {
88             throw new IllegalArgumentException JavaDoc();
89         }
90         this.workbenchWindow = window;
91         tracker = new PerspectiveTracker(window, this);
92         // @issues should be IDE-specific images
93
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     /**
104      * Create a new instance of this class
105      *
106      * @deprecated use the constructor <code>NewWizardAction(IWorkbenchWindow)</code>
107      */

108     public NewWizardAction() {
109         this(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
110     }
111
112     /**
113      * Returns the id of the category of wizards to show
114      * or <code>null</code> to show all categories.
115      * @return String
116      */

117     public String JavaDoc getCategoryId() {
118         return categoryId;
119     }
120
121     /**
122      * Sets the id of the category of wizards to show
123      * or <code>null</code> to show all categories.
124      * @param id
125      */

126     public void setCategoryId(String JavaDoc id) {
127         categoryId = id;
128     }
129
130     /* (non-Javadoc)
131      * Method declared on IAction.
132      */

133     public void run() {
134         if (workbenchWindow == null) {
135             // action has been disposed
136
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             // @issue the following is resource-specific legacy code
148
// Build the selection from the IFile of the editor
149
Class JavaDoc 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 JavaDoc 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"); //$NON-NLS-1$
168
if (wizardSettings == null) {
169             wizardSettings = workbenchSettings.addNewSection("NewWizardAction"); //$NON-NLS-1$
170
}
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     /* (non-Javadoc)
186      * Method declared on ActionFactory.IWorkbenchAction.
187      * @since 3.0
188      */

189     public void dispose() {
190         if (workbenchWindow == null) {
191             // action has already been disposed
192
return;
193         }
194         tracker.dispose();
195         workbenchWindow = null;
196     }
197
198 }
199
Popular Tags