KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > actions > NewWizardShortcutAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.internal.actions;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.jface.action.Action;
15 import org.eclipse.jface.dialogs.ErrorDialog;
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.graphics.Point;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.ui.IEditorInput;
23 import org.eclipse.ui.IEditorPart;
24 import org.eclipse.ui.INewWizard;
25 import org.eclipse.ui.IPluginContribution;
26 import org.eclipse.ui.IWorkbenchPart;
27 import org.eclipse.ui.IWorkbenchWindow;
28 import org.eclipse.ui.actions.ActionFactory;
29 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
30 import org.eclipse.ui.internal.LegacyResourceSupport;
31 import org.eclipse.ui.internal.WorkbenchMessages;
32 import org.eclipse.ui.internal.util.Util;
33 import org.eclipse.ui.wizards.IWizardDescriptor;
34
35 /**
36  * Opens a specific new wizard.
37  */

38 public class NewWizardShortcutAction extends Action implements
39         IPluginContribution {
40     private IWizardDescriptor wizardElement;
41
42     /**
43      * The wizard dialog width
44      */

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

50     private static final int SIZING_WIZARD_HEIGHT = 500;
51     
52     private IWorkbenchWindow window;
53
54     /**
55      * Create an instance of this class.
56      *
57      * @param window the workbench window in which this action will appear
58      * @param wizardDesc a wizard element
59      */

60     public NewWizardShortcutAction(IWorkbenchWindow window,
61             IWizardDescriptor wizardDesc) {
62         super(wizardDesc.getLabel());
63         setToolTipText(wizardDesc.getDescription());
64         setImageDescriptor(wizardDesc.getImageDescriptor());
65         setId(ActionFactory.NEW.getId());
66         wizardElement = wizardDesc;
67         this.window = window;
68     }
69
70     /**
71      * Get the wizard descriptor for this action.
72      *
73      * @return the wizard descriptor
74      */

75     public IWizardDescriptor getWizardDescriptor() {
76         return wizardElement;
77     }
78    
79     /* (non-Javadoc)
80      * @see org.eclipse.jface.action.IAction#run()
81      */

82     public void run() {
83         // create instance of target wizard
84

85         INewWizard wizard;
86         try {
87             wizard = (INewWizard) wizardElement.createWizard();
88         } catch (CoreException e) {
89             ErrorDialog.openError(window.getShell(), WorkbenchMessages.NewWizardShortcutAction_errorTitle,
90                     WorkbenchMessages.NewWizardShortcutAction_errorMessage,
91                     e.getStatus());
92             return;
93         }
94
95         ISelection selection = window.getSelectionService().getSelection();
96         IStructuredSelection selectionToPass = StructuredSelection.EMPTY;
97         if (selection instanceof IStructuredSelection) {
98             selectionToPass = wizardElement
99                     .adaptedSelection((IStructuredSelection) selection);
100         } else {
101             // Build the selection from the IFile of the editor
102
IWorkbenchPart part = window.getPartService().getActivePart();
103             if (part instanceof IEditorPart) {
104                 IEditorInput input = ((IEditorPart) part).getEditorInput();
105                 Class JavaDoc fileClass = LegacyResourceSupport.getFileClass();
106                 if (input != null && fileClass != null) {
107                     Object JavaDoc file = Util.getAdapter(input, fileClass);
108                     if (file != null) {
109                         selectionToPass = new StructuredSelection(file);
110                     }
111                 }
112             }
113         }
114
115         // even tho we MAY finish early without showing a dialog, prep the
116
// wizard with a dialog and such in case it's logic requires it
117
// - yes, it wastes a dialog but they are plentiful...
118
wizard.init(window.getWorkbench(), selectionToPass);
119
120         Shell parent = window.getShell();
121         WizardDialog dialog = new WizardDialog(parent, wizard);
122         dialog.create();
123         Point defaultSize = dialog.getShell().getSize();
124         dialog.getShell().setSize(
125                 Math.max(SIZING_WIZARD_WIDTH, defaultSize.x),
126                 Math.max(SIZING_WIZARD_HEIGHT, defaultSize.y));
127         window.getWorkbench().getHelpSystem().setHelp(dialog.getShell(),
128                 IWorkbenchHelpContextIds.NEW_WIZARD_SHORTCUT);
129         
130         // if the wizard can finish early and doesn't have any pages, just finish it.
131
if (wizardElement.canFinishEarly() && !wizardElement.hasPages()) {
132             wizard.performFinish();
133             dialog.close();
134         } else {
135             dialog.open();
136         }
137     }
138
139     /* (non-Javadoc)
140      * @see org.eclipse.ui.IPluginContribution#getLocalId()
141      */

142     public String JavaDoc getLocalId() {
143         IPluginContribution contribution = getPluginContribution();
144         if (contribution != null) {
145             return contribution.getLocalId();
146         }
147         return wizardElement.getId();
148     }
149
150     /* (non-Javadoc)
151      * @see org.eclipse.ui.IPluginContribution#getPluginId()
152      */

153     public String JavaDoc getPluginId() {
154         IPluginContribution contribution = getPluginContribution();
155         if (contribution != null) {
156             return contribution.getPluginId();
157         }
158         return null;
159     }
160     
161     /**
162      * Return the plugin contribution associated with the wizard.
163      *
164      * @return the contribution or <code>null</code>
165      * @since 3.1
166      */

167     private IPluginContribution getPluginContribution() {
168         return (IPluginContribution) Util.getAdapter(wizardElement,
169                 IPluginContribution.class);
170     }
171 }
172
Popular Tags