KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > EditorMenuManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.ui.internal;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.eclipse.jface.action.ActionContributionItem;
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.action.IContributionItem;
20 import org.eclipse.jface.action.IContributionManagerOverrides;
21 import org.eclipse.jface.action.IMenuManager;
22 import org.eclipse.jface.action.MenuManager;
23 import org.eclipse.jface.action.SubMenuManager;
24 import org.eclipse.ui.actions.RetargetAction;
25
26 /**
27  * An <code>EditorMenuManager</code> is used to sort the contributions
28  * made by an editor so that they always appear after the action sets.
29  */

30 public class EditorMenuManager extends SubMenuManager {
31     private ArrayList JavaDoc wrappers;
32
33     private boolean enabledAllowed = true;
34
35     private class Overrides implements IContributionManagerOverrides {
36         /**
37          * Indicates that the items of this manager are allowed to enable;
38          * <code>true</code> by default.
39          */

40         public void updateEnabledAllowed() {
41             // update the items in the map
42
IContributionItem[] items = EditorMenuManager.super.getItems();
43             for (int i = 0; i < items.length; i++) {
44                 IContributionItem item = items[i];
45                 item.update(IContributionManagerOverrides.P_ENABLED);
46             }
47             // update the wrapped menus
48
if (wrappers != null) {
49                 for (int i = 0; i < wrappers.size(); i++) {
50                     EditorMenuManager manager = (EditorMenuManager) wrappers
51                             .get(i);
52                     manager.setEnabledAllowed(enabledAllowed);
53                 }
54             }
55         }
56
57         public Boolean JavaDoc getEnabled(IContributionItem item) {
58             if (((item instanceof ActionContributionItem) && (((ActionContributionItem) item)
59                     .getAction() instanceof RetargetAction))
60                     || enabledAllowed) {
61                 return null;
62             } else {
63                 return Boolean.FALSE;
64             }
65         }
66
67         public Integer JavaDoc getAccelerator(IContributionItem item) {
68             if (getEnabled(item) == null) {
69                 return getParentMenuManager().getOverrides().getAccelerator(
70                         item);
71             } else {
72                 // no acclerator if the item is disabled
73
return new Integer JavaDoc(0);
74             }
75         }
76
77         public String JavaDoc getAcceleratorText(IContributionItem item) {
78             return getParentMenuManager().getOverrides().getAcceleratorText(
79                     item);
80         }
81
82         public String JavaDoc getText(IContributionItem item) {
83             return getParentMenuManager().getOverrides().getText(item);
84         }
85     }
86
87     private Overrides overrides = new Overrides();
88
89     /**
90      * Constructs a new editor manager.
91      */

92     public EditorMenuManager(IMenuManager mgr) {
93         super(mgr);
94     }
95
96     /* (non-Javadoc)
97      * Method declared on IContributionManager.
98      */

99     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 JavaDoc id, IContributionItem item) {
116         IContributionItem refItem = PluginActionSetBuilder.findInsertionPoint(
117                 id, null, getParentMenuManager(), false);
118         if (refItem != null) {
119             super.insertAfter(refItem.getId(), item);
120         } else {
121             WorkbenchPlugin
122                     .log("Reference item " + id + " not found for action " + item.getId()); //$NON-NLS-1$ //$NON-NLS-2$
123
}
124     }
125
126     /* (non-Javadoc)
127      * Method declared on IContributionManager.
128      * Inserts the new item after any action set contributions which may
129      * exist within the toolbar to ensure a consistent order for actions.
130      */

131     public void prependToGroup(String JavaDoc groupName, IContributionItem item) {
132         insertAfter(groupName, item);
133     }
134
135     /**
136      * Sets the visibility of the manager. If the visibility is <code>true</code>
137      * then each item within the manager appears within the parent manager.
138      * Otherwise, the items are not visible.
139      * <p>
140      * If force visibility is <code>true</code>, or grayed out if force visibility is <code>false</code>
141      * <p>
142      * This is a workaround for the layout flashing when editors contribute
143      * large amounts of items.</p>
144      *
145      * @param visible the new visibility
146      * @param forceVisibility whether to change the visibility or just the
147      * enablement state.
148      */

149     public void setVisible(boolean visible, boolean forceVisibility) {
150         if (visible) {
151             if (forceVisibility) {
152                 // Make the items visible
153
if (!enabledAllowed) {
154                     setEnabledAllowed(true);
155                 }
156             } else {
157                 if (enabledAllowed) {
158                     setEnabledAllowed(false);
159                 }
160             }
161             if (!isVisible()) {
162                 setVisible(true);
163             }
164         } else {
165             if (forceVisibility) {
166                 // Remove the editor menu items
167
setVisible(false);
168             } else {
169                 // Disable the editor menu items.
170
setEnabledAllowed(false);
171             }
172         }
173     }
174
175     /**
176      * Sets the enablement ability of all the items contributed by the editor.
177      *
178      * @param enabledAllowed <code>true</code> if the items may enable
179      * @since 2.0
180      */

181     public void setEnabledAllowed(boolean enabledAllowed) {
182         if (this.enabledAllowed == enabledAllowed) {
183             return;
184         }
185         this.enabledAllowed = enabledAllowed;
186         overrides.updateEnabledAllowed();
187     }
188
189     /* (non-Javadoc)
190      * Method declared on SubMenuManager.
191      */

192     protected SubMenuManager wrapMenu(IMenuManager menu) {
193         if (wrappers == null) {
194             wrappers = new ArrayList JavaDoc();
195         }
196         EditorMenuManager manager = new EditorMenuManager(menu);
197         wrappers.add(manager);
198         return manager;
199     }
200
201     protected IAction[] getAllContributedActions() {
202         HashSet JavaDoc set = new HashSet JavaDoc();
203         getAllContributedActions(set);
204         return (IAction[]) set.toArray(new IAction[set.size()]);
205     }
206
207     protected void getAllContributedActions(HashSet JavaDoc set) {
208         IContributionItem[] items = super.getItems();
209         for (int i = 0; i < items.length; i++) {
210             getAllContributedActions(set, items[i]);
211         }
212         if (wrappers == null) {
213             return;
214         }
215         for (Iterator JavaDoc iter = wrappers.iterator(); iter.hasNext();) {
216             EditorMenuManager element = (EditorMenuManager) iter.next();
217             element.getAllContributedActions(set);
218         }
219     }
220
221     protected void getAllContributedActions(HashSet JavaDoc set, IContributionItem item) {
222         if (item instanceof MenuManager) {
223             IContributionItem subItems[] = ((MenuManager) item).getItems();
224             for (int j = 0; j < subItems.length; j++) {
225                 getAllContributedActions(set, subItems[j]);
226             }
227         } else if (item instanceof ActionContributionItem) {
228             set.add(((ActionContributionItem) item).getAction());
229         }
230     }
231
232 }
233
Popular Tags