1 11 package org.eclipse.jface.action; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 17 import org.eclipse.core.runtime.ListenerList; 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.swt.widgets.Composite; 20 import org.eclipse.swt.widgets.CoolBar; 21 import org.eclipse.swt.widgets.Menu; 22 import org.eclipse.swt.widgets.ToolBar; 23 24 33 public class SubMenuManager extends SubContributionManager implements 34 IMenuManager { 35 36 41 private Map mapMenuToWrapper; 42 43 46 private ListenerList menuListeners = new ListenerList(); 47 48 52 private IMenuListener menuListener; 53 54 61 public SubMenuManager(IMenuManager mgr) { 62 super(mgr); 63 } 64 65 68 public void addMenuListener(IMenuListener listener) { 69 menuListeners.add(listener); 70 if (menuListener == null) { 71 menuListener = new IMenuListener() { 72 public void menuAboutToShow(IMenuManager manager) { 73 Object [] listeners = menuListeners.getListeners(); 74 for (int i = 0; i < listeners.length; ++i) { 75 ((IMenuListener) listeners[i]) 76 .menuAboutToShow(SubMenuManager.this); 77 } 78 } 79 }; 80 } 81 getParentMenuManager().addMenuListener(menuListener); 82 } 83 84 88 public void dispose() { 89 } 91 92 95 public void disposeManager() { 96 if (menuListener != null) { 97 getParentMenuManager().removeMenuListener(menuListener); 98 menuListener = null; 99 clearListenerList(menuListeners); 100 } 101 if (mapMenuToWrapper != null) { 107 Iterator iter = mapMenuToWrapper.values().iterator(); 108 while (iter.hasNext()) { 109 SubMenuManager wrapper = (SubMenuManager) iter.next(); 110 wrapper.disposeManager(); 111 } 112 mapMenuToWrapper.clear(); 113 mapMenuToWrapper = null; 114 } 115 super.disposeManager(); 116 } 117 118 125 private final void clearListenerList(final ListenerList list) { 126 final Object [] listeners = list.getListeners(); 127 for (int i = 0; i < listeners.length; i++) { 128 list.remove(listeners[i]); 129 } 130 } 131 132 135 public void fill(Composite parent) { 136 if (isVisible()) { 137 getParentMenuManager().fill(parent); 138 } 139 } 140 141 144 public void fill(CoolBar parent, int index) { 145 } 147 148 151 public void fill(Menu parent, int index) { 152 if (isVisible()) { 153 getParentMenuManager().fill(parent, index); 154 } 155 } 156 157 160 public void fill(ToolBar parent, int index) { 161 if (isVisible()) { 162 getParentMenuManager().fill(parent, index); 163 } 164 } 165 166 173 public IContributionItem find(String id) { 174 IContributionItem item = getParentMenuManager().find(id); 175 if (item instanceof SubContributionItem) { 176 item = unwrap(item); 178 } 179 180 if (item instanceof IMenuManager) { 181 IMenuManager menu = (IMenuManager) item; 183 item = getWrapper(menu); 184 } 185 186 return item; 187 } 188 189 196 public IMenuManager findMenuUsingPath(String path) { 197 IContributionItem item = findUsingPath(path); 198 if (item instanceof IMenuManager) { 199 return (IMenuManager) item; 200 } 201 return null; 202 } 203 204 212 public IContributionItem findUsingPath(String path) { 213 String id = path; 214 String rest = null; 215 int separator = path.indexOf('/'); 216 if (separator != -1) { 217 id = path.substring(0, separator); 218 rest = path.substring(separator + 1); 219 } 220 IContributionItem item = find(id); if (rest != null && item instanceof IMenuManager) { 222 IMenuManager menu = (IMenuManager) item; 223 item = menu.findUsingPath(rest); 224 } 225 return item; 226 } 227 228 231 public String getId() { 232 return getParentMenuManager().getId(); 233 } 234 235 238 protected final IMenuManager getParentMenuManager() { 239 return (IMenuManager) getParent(); 242 } 243 244 247 public boolean getRemoveAllWhenShown() { 248 return false; 249 } 250 251 262 protected IMenuManager getWrapper(IMenuManager mgr) { 263 if (mapMenuToWrapper == null) { 264 mapMenuToWrapper = new HashMap (4); 265 } 266 SubMenuManager wrapper = (SubMenuManager) mapMenuToWrapper.get(mgr); 267 if (wrapper == null) { 268 wrapper = wrapMenu(mgr); 269 mapMenuToWrapper.put(mgr, wrapper); 270 } 271 return wrapper; 272 } 273 274 277 public boolean isDynamic() { 278 return getParentMenuManager().isDynamic(); 279 } 280 281 284 public boolean isEnabled() { 285 return isVisible() && getParentMenuManager().isEnabled(); 286 } 287 288 291 public boolean isGroupMarker() { 292 return getParentMenuManager().isGroupMarker(); 293 } 294 295 298 public boolean isSeparator() { 299 return getParentMenuManager().isSeparator(); 300 } 301 302 305 public void removeAll() { 306 super.removeAll(); 307 if (mapMenuToWrapper != null) { 308 Iterator iter = mapMenuToWrapper.values().iterator(); 309 while (iter.hasNext()) { 310 SubMenuManager wrapper = (SubMenuManager) iter.next(); 311 wrapper.removeAll(); 312 } 313 mapMenuToWrapper.clear(); 314 mapMenuToWrapper = null; 315 } 316 } 317 318 321 public void removeMenuListener(IMenuListener listener) { 322 menuListeners.remove(listener); 323 } 324 325 328 public void saveWidgetState() { 329 } 331 332 335 public void setParent(IContributionManager parent) { 336 } 339 340 343 public void setRemoveAllWhenShown(boolean removeAll) { 344 Assert.isTrue(false, "Should not be called on submenu manager"); } 346 347 350 public void setVisible(boolean visible) { 351 super.setVisible(visible); 352 if (mapMenuToWrapper != null) { 353 Iterator iter = mapMenuToWrapper.values().iterator(); 354 while (iter.hasNext()) { 355 SubMenuManager wrapper = (SubMenuManager) iter.next(); 356 wrapper.setVisible(visible); 357 } 358 } 359 } 360 361 364 public void update() { 365 getParentMenuManager().update(); 369 } 370 371 374 public void update(boolean force) { 375 getParentMenuManager().update(force); 379 } 380 381 384 public void update(String id) { 385 getParentMenuManager().update(id); 386 } 387 388 391 public void updateAll(boolean force) { 392 getParentMenuManager().updateAll(force); 396 } 397 398 403 protected SubMenuManager wrapMenu(IMenuManager menu) { 404 SubMenuManager mgr = new SubMenuManager(menu); 405 mgr.setVisible(isVisible()); 406 return mgr; 407 } 408 } 409 | Popular Tags |