KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.ui.actions;
13
14 import org.eclipse.jface.action.Action;
15 import org.eclipse.jface.action.ActionContributionItem;
16 import org.eclipse.jface.action.IContributionItem;
17 import org.eclipse.jface.action.IMenuCreator;
18 import org.eclipse.jface.action.MenuManager;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Menu;
21 import org.eclipse.ui.ISharedImages;
22 import org.eclipse.ui.IWorkbenchWindow;
23 import org.eclipse.ui.internal.PerspectiveTracker;
24 import org.eclipse.ui.internal.WorkbenchMessages;
25
26 /**
27  * Action which, when run, will open the new wizard dialog.
28  * In addition, it has a drop-down showing the new wizard shortcuts
29  * associated with the current perspective.
30  * <p>
31  * This class may be instantiated; it is not intended to be subclassed.
32  * </p>
33  *
34  * @since 3.1
35  */

36 public class NewWizardDropDownAction extends Action implements
37         ActionFactory.IWorkbenchAction {
38
39     /**
40      * The workbench window; or <code>null</code> if this
41      * action has been <code>dispose</code>d.
42      */

43     private IWorkbenchWindow workbenchWindow;
44     
45     /**
46      * Tracks perspective activation, to update this action's
47      * enabled state.
48      */

49     private PerspectiveTracker tracker;
50
51     private ActionFactory.IWorkbenchAction showDlgAction;
52
53     private IContributionItem newWizardMenu;
54     
55     private IMenuCreator menuCreator = new IMenuCreator() {
56
57         private MenuManager dropDownMenuMgr;
58
59         /**
60          * Creates the menu manager for the drop-down.
61          */

62         private void createDropDownMenuMgr() {
63             if (dropDownMenuMgr == null) {
64                 dropDownMenuMgr = new MenuManager();
65                 dropDownMenuMgr.add(newWizardMenu);
66             }
67         }
68
69         /* (non-Javadoc)
70          * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Control)
71          */

72         public Menu getMenu(Control parent) {
73             createDropDownMenuMgr();
74             return dropDownMenuMgr.createContextMenu(parent);
75         }
76
77         /* (non-Javadoc)
78          * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu)
79          */

80         public Menu getMenu(Menu parent) {
81             createDropDownMenuMgr();
82             Menu menu = new Menu(parent);
83             IContributionItem[] items = dropDownMenuMgr.getItems();
84             for (int i = 0; i < items.length; i++) {
85                 IContributionItem item = items[i];
86                 IContributionItem newItem = item;
87                 if (item instanceof ActionContributionItem) {
88                     newItem = new ActionContributionItem(
89                             ((ActionContributionItem) item).getAction());
90                 }
91                 newItem.fill(menu, -1);
92             }
93             return menu;
94         }
95
96         /* (non-Javadoc)
97          * @see org.eclipse.jface.action.IMenuCreator#dispose()
98          */

99         public void dispose() {
100             if (dropDownMenuMgr != null) {
101                 dropDownMenuMgr.dispose();
102                 dropDownMenuMgr = null;
103             }
104         }
105     };
106
107     /**
108      * Create a new <code>NewWizardDropDownAction</code>, with the default
109      * action for opening the new wizard dialog, and the default contribution item
110      * for populating the drop-down menu.
111      *
112      * @param window the window in which this action appears
113      */

114     public NewWizardDropDownAction(IWorkbenchWindow window) {
115         this(window, ActionFactory.NEW.create(window), ContributionItemFactory.NEW_WIZARD_SHORTLIST.create(window));
116     }
117     
118     /**
119      * Create a new <code>NewWizardDropDownAction</code>.
120      *
121      * @param window the window in which this action appears
122      * @param showDlgAction the action to delegate to when this action is run directly,
123      * rather than being dropped down
124      * @param newWizardMenu the contribution item that adds the contents to the drop-down menu
125      */

126     public NewWizardDropDownAction(IWorkbenchWindow window,
127             ActionFactory.IWorkbenchAction showDlgAction,
128             IContributionItem newWizardMenu) {
129         super(WorkbenchMessages.NewWizardDropDown_text);
130         if (window == null) {
131             throw new IllegalArgumentException JavaDoc();
132         }
133         this.workbenchWindow = window;
134         this.showDlgAction = showDlgAction;
135         this.newWizardMenu = newWizardMenu;
136         tracker = new PerspectiveTracker(window, this);
137         
138         setToolTipText(showDlgAction.getToolTipText());
139
140         ISharedImages sharedImages = window.getWorkbench()
141                 .getSharedImages();
142         setImageDescriptor(sharedImages
143                 .getImageDescriptor(ISharedImages.IMG_TOOL_NEW_WIZARD));
144         setDisabledImageDescriptor(sharedImages
145                 .getImageDescriptor(ISharedImages.IMG_TOOL_NEW_WIZARD_DISABLED));
146
147         setMenuCreator(menuCreator);
148     }
149
150     
151     /* (non-Javadoc)
152      * @see org.eclipse.ui.actions.ActionFactory.IWorkbenchAction#dispose()
153      */

154     public void dispose() {
155         if (workbenchWindow == null) {
156             // action has already been disposed
157
return;
158         }
159         tracker.dispose();
160         showDlgAction.dispose();
161         newWizardMenu.dispose();
162         menuCreator.dispose();
163         workbenchWindow = null;
164     }
165
166     /**
167      * Runs the action, which opens the New wizard dialog.
168      */

169     public void run() {
170         if (workbenchWindow == null) {
171             // action has been disposed
172
return;
173         }
174         showDlgAction.run();
175     }
176
177 }
178
Popular Tags