001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 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 - Initial API and implementation
010: * hzhou@actuate.com - Fix for Bug 71695 -
011: * [WorkingSets]Removed Working Set is still shown under the menu item
012: * when it is the recently used working set
013: *******************************************************************************/package org.eclipse.ui.internal.ide.actions;
014:
015: import org.eclipse.core.resources.ResourcesPlugin;
016: import org.eclipse.jface.action.ActionContributionItem;
017: import org.eclipse.jface.action.ContributionItem;
018: import org.eclipse.jface.action.IAction;
019: import org.eclipse.jface.action.IMenuListener;
020: import org.eclipse.jface.action.IMenuManager;
021: import org.eclipse.jface.action.MenuManager;
022: import org.eclipse.jface.action.Separator;
023: import org.eclipse.swt.widgets.Menu;
024: import org.eclipse.ui.IWorkbenchWindow;
025: import org.eclipse.ui.IWorkingSet;
026: import org.eclipse.ui.application.IActionBarConfigurer;
027:
028: /**
029: * Sub-menu off project menu for showing MRU list of working set builds.
030: *
031: * @since 3.0
032: */
033: public class BuildSetMenu extends ContributionItem {
034: private IActionBarConfigurer actionBars;
035:
036: boolean dirty = true;
037:
038: private IMenuListener menuListener = new IMenuListener() {
039: public void menuAboutToShow(IMenuManager manager) {
040: manager.markDirty();
041: dirty = true;
042: }
043: };
044:
045: private IAction selectBuildWorkingSetAction;
046:
047: private IWorkbenchWindow window;
048:
049: /**
050: * Create a new instance of the receiver.
051: * @param window
052: * @param actionBars
053: */
054: public BuildSetMenu(IWorkbenchWindow window,
055: IActionBarConfigurer actionBars) {
056: this .window = window;
057: this .actionBars = actionBars;
058: selectBuildWorkingSetAction = new SelectBuildWorkingSetAction(
059: window, actionBars);
060: }
061:
062: /**
063: * Adds a mnemonic accelerator to actions in the MRU list of
064: * recently built working sets.
065: * @param action the action to add
066: * @param index the index to add it at
067: */
068: private void addMnemonic(BuildSetAction action, int index) {
069: StringBuffer label = new StringBuffer();
070: //add the numerical accelerator
071: if (index < 9) {
072: label.append('&');
073: label.append(index);
074: label.append(' ');
075: }
076: label.append(action.getWorkingSet().getLabel());
077: action.setText(label.toString());
078: }
079:
080: /* (non-Javadoc)
081: * @see org.eclipse.jface.action.IContributionItem#fill(org.eclipse.swt.widgets.Menu, int)
082: */
083: public void fill(Menu menu, int index) {
084: if (getParent() instanceof MenuManager) {
085: ((MenuManager) getParent()).addMenuListener(menuListener);
086: }
087: if (!dirty) {
088: return;
089: }
090: fillMenu(menu);
091: dirty = false;
092: }
093:
094: /**
095: * Fills the menu with Show View actions.
096: * @param menu The menu being filled.
097: */
098: private void fillMenu(Menu menu) {
099: boolean isAutoBuilding = ResourcesPlugin.getWorkspace()
100: .isAutoBuilding();
101:
102: //build MRU list of recently built working sets:
103: IWorkingSet[] sets = window.getWorkbench()
104: .getWorkingSetManager().getRecentWorkingSets();
105: BuildSetAction last = BuildSetAction.lastBuilt;
106: IWorkingSet lastSet = null;
107: //add build action for the last working set that was built
108: int accel = 1;
109: if (last != null) {
110: // add it only if it has not been removed
111: boolean found = false;
112: for (int i = 0; i < sets.length; i++) {
113: if (sets[i].equals(last.getWorkingSet())) {
114: found = true;
115: break;
116: }
117: }
118: if (found) {
119: last.setChecked(true);
120: last.setEnabled(!isAutoBuilding);
121: last
122: .setActionDefinitionId("org.eclipse.ui.project.buildLast"); //$NON-NLS-1$
123: addMnemonic(last, accel++);
124: new ActionContributionItem(last).fill(menu, -1);
125: lastSet = last.getWorkingSet();
126: } else {
127: BuildSetAction.lastBuilt = null;
128: }
129: }
130: //add build actions for the most recently used working sets
131: for (int i = 0; i < sets.length; i++) {
132: if (lastSet != null && lastSet.equals(sets[i])) {
133: continue;
134: }
135: BuildSetAction action = new BuildSetAction(sets[i], window,
136: actionBars);
137: addMnemonic(action, accel++);
138: action.setEnabled(!isAutoBuilding);
139: new ActionContributionItem(action).fill(menu, -1);
140: }
141: //add the action to select a different working set
142: if (sets.length > 0) {
143: new Separator().fill(menu, -1);
144: }
145: selectBuildWorkingSetAction.setEnabled(!isAutoBuilding);
146: new ActionContributionItem(selectBuildWorkingSetAction).fill(
147: menu, -1);
148: }
149:
150: public boolean isDirty() {
151: return dirty;
152: }
153:
154: /**
155: * Overridden to always return true and force dynamic menu building.
156: */
157: public boolean isDynamic() {
158: return true;
159: }
160: }
|