1 11 package org.eclipse.ui.internal; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 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 30 public class EditorMenuManager extends SubMenuManager { 31 private ArrayList wrappers; 32 33 private boolean enabledAllowed = true; 34 35 private class Overrides implements IContributionManagerOverrides { 36 40 public void updateEnabledAllowed() { 41 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 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 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 getAccelerator(IContributionItem item) { 68 if (getEnabled(item) == null) { 69 return getParentMenuManager().getOverrides().getAccelerator( 70 item); 71 } else { 72 return new Integer (0); 74 } 75 } 76 77 public String getAcceleratorText(IContributionItem item) { 78 return getParentMenuManager().getOverrides().getAcceleratorText( 79 item); 80 } 81 82 public String getText(IContributionItem item) { 83 return getParentMenuManager().getOverrides().getText(item); 84 } 85 } 86 87 private Overrides overrides = new Overrides(); 88 89 92 public EditorMenuManager(IMenuManager mgr) { 93 super(mgr); 94 } 95 96 99 public IContributionItem[] getItems() { 100 return getParentMenuManager().getItems(); 101 } 102 103 106 public IContributionManagerOverrides getOverrides() { 107 return overrides; 108 } 109 110 115 public void insertAfter(String 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()); } 124 } 125 126 131 public void prependToGroup(String groupName, IContributionItem item) { 132 insertAfter(groupName, item); 133 } 134 135 149 public void setVisible(boolean visible, boolean forceVisibility) { 150 if (visible) { 151 if (forceVisibility) { 152 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 setVisible(false); 168 } else { 169 setEnabledAllowed(false); 171 } 172 } 173 } 174 175 181 public void setEnabledAllowed(boolean enabledAllowed) { 182 if (this.enabledAllowed == enabledAllowed) { 183 return; 184 } 185 this.enabledAllowed = enabledAllowed; 186 overrides.updateEnabledAllowed(); 187 } 188 189 192 protected SubMenuManager wrapMenu(IMenuManager menu) { 193 if (wrappers == null) { 194 wrappers = new ArrayList (); 195 } 196 EditorMenuManager manager = new EditorMenuManager(menu); 197 wrappers.add(manager); 198 return manager; 199 } 200 201 protected IAction[] getAllContributedActions() { 202 HashSet set = new HashSet (); 203 getAllContributedActions(set); 204 return (IAction[]) set.toArray(new IAction[set.size()]); 205 } 206 207 protected void getAllContributedActions(HashSet 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 iter = wrappers.iterator(); iter.hasNext();) { 216 EditorMenuManager element = (EditorMenuManager) iter.next(); 217 element.getAllContributedActions(set); 218 } 219 } 220 221 protected void getAllContributedActions(HashSet 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 |