1 11 12 package org.eclipse.ui.internal.menus; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Map ; 19 import java.util.Set ; 20 21 import org.eclipse.core.commands.common.HandleObject; 22 import org.eclipse.core.commands.common.HandleObjectManager; 23 import org.eclipse.jface.util.IPropertyChangeListener; 24 import org.eclipse.jface.util.PropertyChangeEvent; 25 import org.eclipse.ui.internal.misc.Policy; 26 27 51 public final class SMenuManager extends HandleObjectManager implements 52 IPropertyChangeListener { 53 54 59 private final Map actionSetsById = new HashMap (); 60 61 65 private final Set definedActionSets = new HashSet (); 66 67 71 private final Set definedGroups = new HashSet (); 72 73 77 private final Set definedMenus = new HashSet (); 78 79 83 private final Set definedWidgets = new HashSet (); 84 85 90 private final Map groupsById = new HashMap (); 91 92 96 private SMenuLayout layout = null; 97 98 103 private final Map menusById = new HashMap (); 104 105 110 private final Map widgetsById = new HashMap (); 111 112 119 public final void addListener(final IMenuManagerListener listener) { 120 addListenerObject(listener); 121 } 122 123 129 private final void fireMenuManagerChanged(final MenuManagerEvent event) { 130 if (event == null) { 131 return; 132 } 133 134 final Object [] listeners = getListeners(); 135 for (int i = 0; i < listeners.length; i++) { 136 final IMenuManagerListener listener = (IMenuManagerListener) listeners[i]; 137 listener.menuManagerChanged(event); 138 } 139 } 140 141 153 public final SActionSet getActionSet(final String actionSetId) { 154 checkId(actionSetId); 155 156 SActionSet actionSet = (SActionSet) actionSetsById.get(actionSetId); 157 if (actionSet == null) { 158 actionSet = new SActionSet(actionSetId); 159 actionSetsById.put(actionSetId, actionSet); 160 actionSet.addListener(this); 161 } 162 163 return actionSet; 164 } 165 166 172 public final SActionSet[] getDefinedActionSets() { 173 return (SActionSet[]) definedActionSets 174 .toArray(new SActionSet[definedActionSets.size()]); 175 } 176 177 183 public final SGroup[] getDefinedGroups() { 184 return (SGroup[]) definedGroups 185 .toArray(new SGroup[definedGroups.size()]); 186 } 187 188 194 public final SItem[] getDefinedItems() { 195 return (SItem[]) definedHandleObjects 196 .toArray(new SItem[definedHandleObjects.size()]); 197 } 198 199 205 public final SMenu[] getDefinedMenus() { 206 return (SMenu[]) definedMenus.toArray(new SMenu[definedMenus.size()]); 207 } 208 209 215 public final SWidget[] getDefinedWidgets() { 216 return (SWidget[]) definedWidgets.toArray(new SWidget[definedWidgets 217 .size()]); 218 } 219 220 231 public final SGroup getGroup(final String groupId) { 232 checkId(groupId); 233 234 if (Policy.EXPERIMENTAL_MENU 235 && groupId.indexOf(LeafLocationElement.BREAKPOINT_PATH)>-1) { 236 System.err.println("getMenu: " + groupId); } 238 SGroup group = (SGroup) groupsById.get(groupId); 239 if (group == null) { 240 group = new SGroup(groupId); 241 groupsById.put(groupId, group); 242 group.addListener(this); 243 } 244 245 return group; 246 } 247 248 259 public final SItem getItem(final String itemId) { 260 checkId(itemId); 261 262 SItem item = (SItem) handleObjectsById.get(itemId); 263 if (item == null) { 264 item = new SItem(itemId); 265 handleObjectsById.put(itemId, item); 266 item.addListener(this); 267 } 268 269 return item; 270 } 271 272 290 public final SMenuLayout getLayout() { 291 final Collection menuElements = new ArrayList (definedMenus.size() 294 + definedGroups.size() + definedHandleObjects.size() 295 + definedWidgets.size()); 296 menuElements.addAll(definedMenus); 297 menuElements.addAll(definedGroups); 298 menuElements.addAll(definedHandleObjects); 299 menuElements.addAll(definedWidgets); 300 layout = SMenuLayout.computeLayout(menuElements); 301 303 return layout; 304 } 305 306 317 public final SMenu getMenu(final String menuId) { 318 checkId(menuId); 319 320 if (Policy.EXPERIMENTAL_MENU 321 && menuId.indexOf(LeafLocationElement.BREAKPOINT_PATH)>-1) { 322 System.err.println("getMenu: " + menuId); } 324 325 SMenu menu = (SMenu) menusById.get(menuId); 326 if (menu == null) { 327 menu = new SMenu(menuId); 328 menusById.put(menuId, menu); 329 menu.addListener(this); 330 } 331 332 return menu; 333 } 334 335 346 public final SWidget getWidget(final String widgetId) { 347 checkId(widgetId); 348 349 SWidget widget = (SWidget) widgetsById.get(widgetId); 350 if (widget == null) { 351 widget = new SWidget(widgetId); 352 widgetsById.put(widgetId, widget); 353 widget.addListener(this); 354 } 355 356 return widget; 357 } 358 359 public final void propertyChange(final PropertyChangeEvent event) { 360 if (MenuElement.PROPERTY_DEFINED.equals(event.getProperty())) { 361 final HandleObject handle = (HandleObject) event.getSource(); 362 final String id = handle.getId(); 363 final boolean added = ((Boolean ) event.getNewValue()) 364 .booleanValue(); 365 if (added) { 366 if (handle instanceof SActionSet) { 367 definedActionSets.add(handle); 368 if (isListenerAttached()) { 369 fireMenuManagerChanged(new MenuManagerEvent(this, null, 370 false, null, false, null, false, null, false, 371 id, added)); 372 } 373 } else if (handle instanceof SGroup) { 374 definedGroups.add(handle); 375 if (isListenerAttached()) { 376 fireMenuManagerChanged(new MenuManagerEvent(this, id, 377 added, null, false, null, false, null, false, 378 null, false)); 379 } 380 } else if (handle instanceof SItem) { 381 definedHandleObjects.add(handle); 382 if (isListenerAttached()) { 383 fireMenuManagerChanged(new MenuManagerEvent(this, null, 384 false, id, added, null, false, null, false, 385 null, false)); 386 } 387 } else if (handle instanceof SMenu) { 388 definedMenus.add(handle); 389 if (isListenerAttached()) { 390 fireMenuManagerChanged(new MenuManagerEvent(this, null, 391 false, null, false, id, added, null, false, 392 null, false)); 393 } 394 } else if (handle instanceof SWidget) { 395 definedWidgets.add(handle); 396 if (isListenerAttached()) { 397 fireMenuManagerChanged(new MenuManagerEvent(this, null, 398 false, null, false, null, false, id, added, 399 null, false)); 400 } 401 } 402 } else { 403 if (handle instanceof SActionSet) { 404 definedActionSets.remove(handle); 405 if (isListenerAttached()) { 406 fireMenuManagerChanged(new MenuManagerEvent(this, null, 407 false, null, false, null, false, null, false, 408 id, added)); 409 } 410 } else if (handle instanceof SGroup) { 411 definedGroups.remove(handle); 412 if (isListenerAttached()) { 413 fireMenuManagerChanged(new MenuManagerEvent(this, id, 414 added, null, false, null, false, null, false, 415 null, false)); 416 } 417 } else if (handle instanceof SItem) { 418 definedHandleObjects.remove(handle); 419 if (isListenerAttached()) { 420 fireMenuManagerChanged(new MenuManagerEvent(this, null, 421 false, id, added, null, false, null, false, 422 null, false)); 423 } 424 } else if (handle instanceof SMenu) { 425 definedMenus.remove(handle); 426 if (isListenerAttached()) { 427 fireMenuManagerChanged(new MenuManagerEvent(this, null, 428 false, null, false, id, added, null, false, 429 null, false)); 430 } 431 } else if (handle instanceof SWidget) { 432 definedWidgets.remove(handle); 433 if (isListenerAttached()) { 434 fireMenuManagerChanged(new MenuManagerEvent(this, null, 435 false, null, false, null, false, id, added, 436 null, false)); 437 } 438 } 439 } 440 } 441 } 442 443 449 public final void removeListener(final IMenuManagerListener listener) { 450 removeListenerObject(listener); 451 } 452 } 453 | Popular Tags |