KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.ide;
12
13
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.jface.action.Action;
16 import org.eclipse.jface.dialogs.ErrorDialog;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.jface.viewers.StructuredSelection;
20 import org.eclipse.jface.wizard.WizardDialog;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.ui.IEditorInput;
23 import org.eclipse.ui.IEditorPart;
24 import org.eclipse.ui.IFileEditorInput;
25 import org.eclipse.ui.INewWizard;
26 import org.eclipse.ui.IPluginContribution;
27 import org.eclipse.ui.IWorkbenchPart;
28 import org.eclipse.ui.IWorkbenchWindow;
29 import org.eclipse.ui.actions.ActionFactory;
30 import org.eclipse.ui.help.WorkbenchHelp;
31 //@issue illegal reference to org.eclipse.ui.internal.dialogs.WorkbenchWizardElement
32
import org.eclipse.ui.internal.dialogs.WorkbenchWizardElement;
33
34 /**
35  * Instances can launch arbitrary resource creation wizards
36  * that have been marked as being available as workbench shortcut
37  * items
38  */

39 public class NewWizardShortcutAction extends Action implements IPluginContribution {
40     private WorkbenchWizardElement wizardElement;
41     private IWorkbenchWindow window;
42     /**
43      * Create an instance of this class. Use this constructor if you do
44      * not wish to pre-specify the selection that should be provided to
45      * launched shortcut wizards.
46      *
47      * @param element WorkbenchWizardElement
48      */

49     public NewWizardShortcutAction(IWorkbenchWindow window, WorkbenchWizardElement element) {
50         super(element.getLabel(element));
51         setToolTipText(element.getDescription());
52         setImageDescriptor(element.getImageDescriptor());
53         setId(ActionFactory.NEW.getId());
54         wizardElement = element;
55         this.window = window;
56     }
57     /**
58      * This action has been invoked by the user
59      */

60     public void run() {
61         // create instance of target wizard
62

63         INewWizard wizard;
64         try {
65             wizard = (INewWizard) wizardElement.createExecutableExtension();
66         } catch (CoreException e) {
67             ErrorDialog.openError(
68                 window.getShell(),
69                 IDEWorkbenchMessages.getString("NewWizardShortcutAction.errorTitle"), //$NON-NLS-1$
70
IDEWorkbenchMessages.getString("NewWizardShortcutAction.errorMessage"), //$NON-NLS-1$
71
e.getStatus());
72             return;
73         }
74
75         ISelection selection = window.getSelectionService().getSelection();
76         IStructuredSelection selectionToPass = StructuredSelection.EMPTY;
77         if (selection instanceof IStructuredSelection) {
78             selectionToPass = wizardElement.adaptedSelection((IStructuredSelection) selection);
79         } else {
80             // Build the selection from the IFile of the editor
81
IWorkbenchPart part = window.getPartService().getActivePart();
82             if (part instanceof IEditorPart) {
83                 IEditorInput input = ((IEditorPart) part).getEditorInput();
84                 if (input instanceof IFileEditorInput) {
85                     selectionToPass = new StructuredSelection(((IFileEditorInput) input).getFile());
86                 }
87             }
88         }
89
90         wizard.init(window.getWorkbench(), selectionToPass);
91
92         Shell parent = window.getShell();
93         WizardDialog dialog = new WizardDialog(parent, wizard);
94         dialog.create();
95         WorkbenchHelp.setHelp(dialog.getShell(), IHelpContextIds.NEW_WIZARD_SHORTCUT);
96         dialog.open();
97     }
98
99
100     /* (non-Javadoc)
101      * @see org.eclipse.ui.IPluginContribution#getLocalId()
102      */

103     public String JavaDoc getLocalId() {
104         return wizardElement.getLocalId();
105     }
106
107     /* (non-Javadoc)
108      * @see org.eclipse.ui.IPluginContribution#getPluginId()
109      */

110     public String JavaDoc getPluginId() {
111         return wizardElement.getPluginId();
112     }
113 }
114
Popular Tags