001: /*******************************************************************************
002: * Copyright (c) 2000, 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 Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import java.util.ArrayList;
013: import java.util.HashSet;
014: import java.util.Iterator;
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.IContributionManagerOverrides;
020: import org.eclipse.jface.action.IMenuManager;
021: import org.eclipse.jface.action.MenuManager;
022: import org.eclipse.jface.action.SubMenuManager;
023: import org.eclipse.ui.actions.RetargetAction;
024:
025: /**
026: * An <code>EditorMenuManager</code> is used to sort the contributions
027: * made by an editor so that they always appear after the action sets.
028: */
029: public class EditorMenuManager extends SubMenuManager {
030: private ArrayList wrappers;
031:
032: private boolean enabledAllowed = true;
033:
034: private class Overrides implements IContributionManagerOverrides {
035: /**
036: * Indicates that the items of this manager are allowed to enable;
037: * <code>true</code> by default.
038: */
039: public void updateEnabledAllowed() {
040: // update the items in the map
041: IContributionItem[] items = EditorMenuManager.super
042: .getItems();
043: for (int i = 0; i < items.length; i++) {
044: IContributionItem item = items[i];
045: item.update(IContributionManagerOverrides.P_ENABLED);
046: }
047: // update the wrapped menus
048: if (wrappers != null) {
049: for (int i = 0; i < wrappers.size(); i++) {
050: EditorMenuManager manager = (EditorMenuManager) wrappers
051: .get(i);
052: manager.setEnabledAllowed(enabledAllowed);
053: }
054: }
055: }
056:
057: public Boolean getEnabled(IContributionItem item) {
058: if (((item instanceof ActionContributionItem) && (((ActionContributionItem) item)
059: .getAction() instanceof RetargetAction))
060: || enabledAllowed) {
061: return null;
062: } else {
063: return Boolean.FALSE;
064: }
065: }
066:
067: public Integer getAccelerator(IContributionItem item) {
068: if (getEnabled(item) == null) {
069: return getParentMenuManager().getOverrides()
070: .getAccelerator(item);
071: } else {
072: // no acclerator if the item is disabled
073: return new Integer(0);
074: }
075: }
076:
077: public String getAcceleratorText(IContributionItem item) {
078: return getParentMenuManager().getOverrides()
079: .getAcceleratorText(item);
080: }
081:
082: public String getText(IContributionItem item) {
083: return getParentMenuManager().getOverrides().getText(item);
084: }
085: }
086:
087: private Overrides overrides = new Overrides();
088:
089: /**
090: * Constructs a new editor manager.
091: */
092: public EditorMenuManager(IMenuManager mgr) {
093: super (mgr);
094: }
095:
096: /* (non-Javadoc)
097: * Method declared on IContributionManager.
098: */
099: public IContributionItem[] getItems() {
100: return getParentMenuManager().getItems();
101: }
102:
103: /* (non-Javadoc)
104: * Method declared on IContributionManager.
105: */
106: public IContributionManagerOverrides getOverrides() {
107: return overrides;
108: }
109:
110: /* (non-Javadoc)
111: * Method declared on IContributionManager.
112: * Inserts the new item after any action set contributions which may
113: * exist within the toolbar to ensure a consistent order for actions.
114: */
115: public void insertAfter(String id, IContributionItem item) {
116: IContributionItem refItem = PluginActionSetBuilder
117: .findInsertionPoint(id, null, getParentMenuManager(),
118: false);
119: if (refItem != null) {
120: super .insertAfter(refItem.getId(), item);
121: } else {
122: WorkbenchPlugin
123: .log("Reference item " + id + " not found for action " + item.getId()); //$NON-NLS-1$ //$NON-NLS-2$
124: }
125: }
126:
127: /* (non-Javadoc)
128: * Method declared on IContributionManager.
129: * Inserts the new item after any action set contributions which may
130: * exist within the toolbar to ensure a consistent order for actions.
131: */
132: public void prependToGroup(String groupName, IContributionItem item) {
133: insertAfter(groupName, item);
134: }
135:
136: /**
137: * Sets the visibility of the manager. If the visibility is <code>true</code>
138: * then each item within the manager appears within the parent manager.
139: * Otherwise, the items are not visible.
140: * <p>
141: * If force visibility is <code>true</code>, or grayed out if force visibility is <code>false</code>
142: * <p>
143: * This is a workaround for the layout flashing when editors contribute
144: * large amounts of items.</p>
145: *
146: * @param visible the new visibility
147: * @param forceVisibility whether to change the visibility or just the
148: * enablement state.
149: */
150: public void setVisible(boolean visible, boolean forceVisibility) {
151: if (visible) {
152: if (forceVisibility) {
153: // Make the items visible
154: if (!enabledAllowed) {
155: setEnabledAllowed(true);
156: }
157: } else {
158: if (enabledAllowed) {
159: setEnabledAllowed(false);
160: }
161: }
162: if (!isVisible()) {
163: setVisible(true);
164: }
165: } else {
166: if (forceVisibility) {
167: // Remove the editor menu items
168: setVisible(false);
169: } else {
170: // Disable the editor menu items.
171: setEnabledAllowed(false);
172: }
173: }
174: }
175:
176: /**
177: * Sets the enablement ability of all the items contributed by the editor.
178: *
179: * @param enabledAllowed <code>true</code> if the items may enable
180: * @since 2.0
181: */
182: public void setEnabledAllowed(boolean enabledAllowed) {
183: if (this .enabledAllowed == enabledAllowed) {
184: return;
185: }
186: this .enabledAllowed = enabledAllowed;
187: overrides.updateEnabledAllowed();
188: }
189:
190: /* (non-Javadoc)
191: * Method declared on SubMenuManager.
192: */
193: protected SubMenuManager wrapMenu(IMenuManager menu) {
194: if (wrappers == null) {
195: wrappers = new ArrayList();
196: }
197: EditorMenuManager manager = new EditorMenuManager(menu);
198: wrappers.add(manager);
199: return manager;
200: }
201:
202: protected IAction[] getAllContributedActions() {
203: HashSet set = new HashSet();
204: getAllContributedActions(set);
205: return (IAction[]) set.toArray(new IAction[set.size()]);
206: }
207:
208: protected void getAllContributedActions(HashSet set) {
209: IContributionItem[] items = super .getItems();
210: for (int i = 0; i < items.length; i++) {
211: getAllContributedActions(set, items[i]);
212: }
213: if (wrappers == null) {
214: return;
215: }
216: for (Iterator iter = wrappers.iterator(); iter.hasNext();) {
217: EditorMenuManager element = (EditorMenuManager) iter.next();
218: element.getAllContributedActions(set);
219: }
220: }
221:
222: protected void getAllContributedActions(HashSet set,
223: IContributionItem item) {
224: if (item instanceof MenuManager) {
225: IContributionItem subItems[] = ((MenuManager) item)
226: .getItems();
227: for (int j = 0; j < subItems.length; j++) {
228: getAllContributedActions(set, subItems[j]);
229: }
230: } else if (item instanceof ActionContributionItem) {
231: set.add(((ActionContributionItem) item).getAction());
232: }
233: }
234:
235: }
|