001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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 java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.jface.action.ActionContributionItem;
017: import org.eclipse.jface.action.IAction;
018: import org.eclipse.jface.action.IContributionItem;
019: import org.eclipse.jface.action.IContributionManager;
020: import org.eclipse.jface.action.IMenuManager;
021: import org.eclipse.jface.action.Separator;
022: import org.eclipse.ui.IWorkbenchWindow;
023: import org.eclipse.ui.internal.actions.NewWizardShortcutAction;
024: import org.eclipse.ui.internal.dialogs.WorkbenchWizardElement;
025: import org.eclipse.ui.internal.registry.WizardsRegistryReader;
026: import org.eclipse.ui.wizards.IWizardDescriptor;
027:
028: /**
029: * A <code>NewWizardMenu</code> augments <code>BaseNewWizardMenu</code> with IDE-specific
030: * actions: New Project... (always shown) and New Example... (shown only if there are example wizards installed).
031: */
032: public class NewWizardMenu extends BaseNewWizardMenu {
033:
034: private final IAction newExampleAction;
035: private final IAction newProjectAction;
036:
037: private boolean enabled = true;
038:
039: /**
040: * Creates a new wizard shortcut menu for the IDE.
041: *
042: * @param window
043: * the window containing the menu
044: */
045: public NewWizardMenu(IWorkbenchWindow window) {
046: this (window, null);
047:
048: }
049:
050: /**
051: * Creates a new wizard shortcut menu for the IDE.
052: *
053: * @param window
054: * the window containing the menu
055: * @param id
056: * the identifier for this contribution item
057: */
058: public NewWizardMenu(IWorkbenchWindow window, String id) {
059: super (window, id);
060: newExampleAction = new NewExampleAction(window);
061: newProjectAction = new NewProjectAction(window);
062: }
063:
064: /**
065: * Create a new wizard shortcut menu.
066: * <p>
067: * If the menu will appear on a semi-permanent basis, for instance within
068: * a toolbar or menubar, the value passed for <code>register</code> should be true.
069: * If set, the menu will listen to perspective activation and update itself
070: * to suit. In this case clients are expected to call <code>deregister</code>
071: * when the menu is no longer needed. This will unhook any perspective
072: * listeners.
073: * </p>
074: *
075: * @param innerMgr the location for the shortcut menu contents
076: * @param window the window containing the menu
077: * @param register if <code>true</code> the menu listens to perspective changes in
078: * the window
079: * @deprecated use NewWizardMenu(IWorkbenchWindow) instead
080: */
081: public NewWizardMenu(IMenuManager innerMgr,
082: IWorkbenchWindow window, boolean register) {
083: this (window, null);
084: fillMenu(innerMgr);
085: // Must be done after constructor to ensure field initialization.
086: }
087:
088: /* (non-Javadoc)
089: * Fills the menu with New Wizards.
090: */
091: private void fillMenu(IContributionManager innerMgr) {
092: // Remove all.
093: innerMgr.removeAll();
094:
095: IContributionItem[] items = getContributionItems();
096: for (int i = 0; i < items.length; i++) {
097: innerMgr.add(items[i]);
098: }
099: }
100:
101: /**
102: * Removes all listeners from the containing workbench window.
103: * <p>
104: * This method should only be called if the shortcut menu is created with
105: * <code>register = true</code>.
106: * </p>
107: *
108: * @deprecated has no effect
109: */
110: public void deregisterListeners() {
111: // do nothing
112: }
113:
114: /**
115: * Return whether or not any examples are in the current install.
116: *
117: * @return boolean
118: */
119: private boolean hasExamples() {
120: return registryHasCategory(WizardsRegistryReader.FULL_EXAMPLES_WIZARD_CATEGORY);
121: }
122:
123: /* (non-Javadoc)
124: * @see org.eclipse.ui.actions.BaseNewWizardMenu#addItems(org.eclipse.jface.action.IContributionManager)
125: */
126: protected void addItems(List list) {
127: ArrayList shortCuts = new ArrayList();
128: addShortcuts(shortCuts);
129:
130: for (Iterator iterator = shortCuts.iterator(); iterator
131: .hasNext();) {
132: Object curr = iterator.next();
133: if (curr instanceof ActionContributionItem
134: && isNewProjectWizardAction(((ActionContributionItem) curr)
135: .getAction())) {
136: iterator.remove();
137: list.add(curr);
138: }
139: }
140: list.add(new ActionContributionItem(newProjectAction));
141: list.add(new Separator());
142: if (!shortCuts.isEmpty()) {
143: list.addAll(shortCuts);
144: list.add(new Separator());
145: }
146: if (hasExamples()) {
147: list.add(new ActionContributionItem(newExampleAction));
148: list.add(new Separator());
149: }
150: list.add(new ActionContributionItem(getShowDialogAction()));
151: }
152:
153: private boolean isNewProjectWizardAction(IAction action) {
154: if (action instanceof NewWizardShortcutAction) {
155: IWizardDescriptor wizardDescriptor = ((NewWizardShortcutAction) action)
156: .getWizardDescriptor();
157: String[] tags = wizardDescriptor.getTags();
158: for (int i = 0; i < tags.length; i++) {
159: if (WorkbenchWizardElement.TAG_PROJECT.equals(tags[i])) {
160: return true;
161: }
162: }
163: }
164: return false;
165: }
166:
167: /* (non-Javadoc)
168: * Method declared on IContributionItem.
169: */
170: public boolean isEnabled() {
171: return enabled;
172: }
173:
174: /**
175: * Sets the enabled state of the receiver.
176: *
177: * @param enabledValue if <code>true</code> the menu is enabled; else
178: * it is disabled
179: */
180: public void setEnabled(boolean enabledValue) {
181: this .enabled = enabledValue;
182: }
183:
184: /* (non-Javadoc)
185: * @see org.eclipse.ui.actions.BaseNewWizardMenu#getContributionItems()
186: */
187: protected IContributionItem[] getContributionItems() {
188: if (isEnabled()) {
189: return super .getContributionItems();
190: }
191: return new IContributionItem[0];
192: }
193: }
|