001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.actions;
011:
012: import org.eclipse.jface.action.Action;
013: import org.eclipse.jface.action.ActionContributionItem;
014: import org.eclipse.jface.action.IContributionItem;
015: import org.eclipse.jface.action.IMenuCreator;
016: import org.eclipse.jface.action.MenuManager;
017: import org.eclipse.swt.widgets.Control;
018: import org.eclipse.swt.widgets.Menu;
019: import org.eclipse.ui.ISharedImages;
020: import org.eclipse.ui.IWorkbenchWindow;
021: import org.eclipse.ui.internal.PerspectiveTracker;
022: import org.eclipse.ui.internal.WorkbenchMessages;
023:
024: /**
025: * Action which, when run, will open the new wizard dialog.
026: * In addition, it has a drop-down showing the new wizard shortcuts
027: * associated with the current perspective.
028: * <p>
029: * This class may be instantiated; it is not intended to be subclassed.
030: * </p>
031: *
032: * @since 3.1
033: */
034: public class NewWizardDropDownAction extends Action implements
035: ActionFactory.IWorkbenchAction {
036:
037: /**
038: * The workbench window; or <code>null</code> if this
039: * action has been <code>dispose</code>d.
040: */
041: private IWorkbenchWindow workbenchWindow;
042:
043: /**
044: * Tracks perspective activation, to update this action's
045: * enabled state.
046: */
047: private PerspectiveTracker tracker;
048:
049: private ActionFactory.IWorkbenchAction showDlgAction;
050:
051: private IContributionItem newWizardMenu;
052:
053: private IMenuCreator menuCreator = new IMenuCreator() {
054:
055: private MenuManager dropDownMenuMgr;
056:
057: /**
058: * Creates the menu manager for the drop-down.
059: */
060: private void createDropDownMenuMgr() {
061: if (dropDownMenuMgr == null) {
062: dropDownMenuMgr = new MenuManager();
063: dropDownMenuMgr.add(newWizardMenu);
064: }
065: }
066:
067: /* (non-Javadoc)
068: * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Control)
069: */
070: public Menu getMenu(Control parent) {
071: createDropDownMenuMgr();
072: return dropDownMenuMgr.createContextMenu(parent);
073: }
074:
075: /* (non-Javadoc)
076: * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu)
077: */
078: public Menu getMenu(Menu parent) {
079: createDropDownMenuMgr();
080: Menu menu = new Menu(parent);
081: IContributionItem[] items = dropDownMenuMgr.getItems();
082: for (int i = 0; i < items.length; i++) {
083: IContributionItem item = items[i];
084: IContributionItem newItem = item;
085: if (item instanceof ActionContributionItem) {
086: newItem = new ActionContributionItem(
087: ((ActionContributionItem) item).getAction());
088: }
089: newItem.fill(menu, -1);
090: }
091: return menu;
092: }
093:
094: /* (non-Javadoc)
095: * @see org.eclipse.jface.action.IMenuCreator#dispose()
096: */
097: public void dispose() {
098: if (dropDownMenuMgr != null) {
099: dropDownMenuMgr.dispose();
100: dropDownMenuMgr = null;
101: }
102: }
103: };
104:
105: /**
106: * Create a new <code>NewWizardDropDownAction</code>, with the default
107: * action for opening the new wizard dialog, and the default contribution item
108: * for populating the drop-down menu.
109: *
110: * @param window the window in which this action appears
111: */
112: public NewWizardDropDownAction(IWorkbenchWindow window) {
113: this (window, ActionFactory.NEW.create(window),
114: ContributionItemFactory.NEW_WIZARD_SHORTLIST
115: .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();
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: /* (non-Javadoc)
151: * @see org.eclipse.ui.actions.ActionFactory.IWorkbenchAction#dispose()
152: */
153: public void dispose() {
154: if (workbenchWindow == null) {
155: // action has already been disposed
156: return;
157: }
158: tracker.dispose();
159: showDlgAction.dispose();
160: newWizardMenu.dispose();
161: menuCreator.dispose();
162: workbenchWindow = null;
163: }
164:
165: /**
166: * Runs the action, which opens the New wizard dialog.
167: */
168: public void run() {
169: if (workbenchWindow == null) {
170: // action has been disposed
171: return;
172: }
173: showDlgAction.run();
174: }
175:
176: }
|