1 11 package org.eclipse.ui; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 import java.util.WeakHashMap ; 17 18 import org.eclipse.core.commands.IHandler; 19 import org.eclipse.core.commands.common.EventManager; 20 import org.eclipse.core.expressions.EvaluationResult; 21 import org.eclipse.core.expressions.Expression; 22 import org.eclipse.core.expressions.ExpressionInfo; 23 import org.eclipse.core.expressions.IEvaluationContext; 24 import org.eclipse.jface.action.IAction; 25 import org.eclipse.jface.action.IMenuManager; 26 import org.eclipse.jface.action.IStatusLineManager; 27 import org.eclipse.jface.action.IToolBarManager; 28 import org.eclipse.jface.action.SubMenuManager; 29 import org.eclipse.jface.action.SubStatusLineManager; 30 import org.eclipse.jface.action.SubToolBarManager; 31 import org.eclipse.jface.commands.ActionHandler; 32 import org.eclipse.jface.util.IPropertyChangeListener; 33 import org.eclipse.jface.util.PropertyChangeEvent; 34 import org.eclipse.ui.handlers.IHandlerActivation; 35 import org.eclipse.ui.handlers.IHandlerService; 36 import org.eclipse.ui.internal.EditorActionBars; 37 import org.eclipse.ui.internal.WorkbenchPlugin; 38 import org.eclipse.ui.internal.actions.CommandAction; 39 import org.eclipse.ui.internal.handlers.CommandLegacyActionWrapper; 40 import org.eclipse.ui.internal.handlers.IActionCommandMappingService; 41 import org.eclipse.ui.internal.services.SourcePriorityNameMapping; 42 import org.eclipse.ui.services.IServiceLocator; 43 44 47 public class SubActionBars extends EventManager implements IActionBars { 48 49 55 private static final Expression EXPRESSION = new Expression() { 56 public final EvaluationResult evaluate(final IEvaluationContext context) { 57 return EvaluationResult.TRUE; 58 } 59 60 public final void collectExpressionInfo(final ExpressionInfo info) { 61 info 62 .addVariableNameAccess(SourcePriorityNameMapping.LEGACY_LEGACY_NAME); 63 } 64 }; 65 66 69 public static final String P_ACTION_HANDLERS = "org.eclipse.ui.internal.actionHandlers"; 71 private Map actionHandlers; 72 73 private boolean actionHandlersChanged; 74 75 80 private Map activationsByActionIdByServiceLocator; 81 82 private boolean active = false; 83 84 private SubMenuManager menuMgr; 85 86 private IActionBars parent; 87 88 93 private IServiceLocator serviceLocator; 94 95 private SubStatusLineManager statusLineMgr; 96 97 private SubToolBarManager toolBarMgr; 98 99 private Map actionIdByCommandId = new HashMap (); 100 101 108 public SubActionBars(final IActionBars parent) { 109 this(parent, null); 110 } 111 112 123 public SubActionBars(final IActionBars parent, 124 final IServiceLocator serviceLocator) { 125 if (parent == null) { 126 throw new NullPointerException ("The parent cannot be null"); } 128 129 this.parent = parent; 130 this.serviceLocator = serviceLocator; 131 } 132 133 136 public void activate() { 137 activate(true); 138 } 139 140 149 public void activate(boolean forceVisibility) { 150 setActive(true); 151 } 152 153 160 public void addPropertyChangeListener(IPropertyChangeListener listener) { 161 addListenerObject(listener); 162 } 163 164 168 protected final void basicSetActive(boolean active) { 169 this.active = active; 170 } 171 172 175 public void clearGlobalActionHandlers() { 176 if (actionHandlers != null) { 177 actionHandlers.clear(); 178 actionHandlersChanged = true; 179 } 180 181 if (activationsByActionIdByServiceLocator != null) { 182 final Iterator activationItr = activationsByActionIdByServiceLocator 184 .entrySet().iterator(); 185 while (activationItr.hasNext()) { 186 final Map.Entry value = (Map.Entry ) activationItr.next(); 187 final IServiceLocator locator = (IServiceLocator) value 188 .getKey(); 189 final IHandlerService service = (IHandlerService) locator 190 .getService(IHandlerService.class); 191 final Map activationsByActionId = (Map ) value.getValue(); 192 final Iterator iterator = activationsByActionId.values() 193 .iterator(); 194 while (iterator.hasNext()) { 195 final IHandlerActivation activation = (IHandlerActivation) iterator 196 .next(); 197 service.deactivateHandler(activation); 198 activation.getHandler().dispose(); 199 } 200 } 201 activationsByActionIdByServiceLocator.clear(); 202 } 203 } 204 205 212 protected SubMenuManager createSubMenuManager(IMenuManager parent) { 213 return new SubMenuManager(parent); 214 } 215 216 223 protected SubToolBarManager createSubToolBarManager(IToolBarManager parent) { 224 return new SubToolBarManager(parent); 225 } 226 227 230 public void deactivate() { 231 deactivate(true); 232 } 233 234 243 public void deactivate(boolean forceHide) { 244 setActive(false); 245 } 246 247 250 public void dispose() { 251 clearGlobalActionHandlers(); 252 if (menuMgr != null) { 253 menuMgr.dispose(); 254 menuMgr.disposeManager(); 255 } 256 if (statusLineMgr != null) { 257 statusLineMgr.disposeManager(); 258 } 259 if (toolBarMgr != null) { 260 toolBarMgr.disposeManager(); 261 } 262 clearListeners(); 263 } 264 265 269 protected void fireActionHandlersChanged() { 270 if (actionHandlersChanged) { 271 firePropertyChange(new PropertyChangeEvent(this, P_ACTION_HANDLERS, 273 null, null)); 274 actionHandlersChanged = false; 275 } 276 } 277 278 287 protected void firePropertyChange(PropertyChangeEvent event) { 288 Object [] listeners = getListeners(); 289 for (int i = 0; i < listeners.length; ++i) { 290 ((IPropertyChangeListener) listeners[i]).propertyChange(event); 291 } 292 } 293 294 297 protected final boolean getActive() { 298 return active; 299 } 300 301 309 public IAction getGlobalActionHandler(String actionID) { 310 if (actionHandlers == null) { 311 return null; 312 } 313 return (IAction) actionHandlers.get(actionID); 314 } 315 316 320 public Map getGlobalActionHandlers() { 321 return actionHandlers; 322 } 323 324 330 public IMenuManager getMenuManager() { 331 if (menuMgr == null) { 332 menuMgr = createSubMenuManager(parent.getMenuManager()); 333 menuMgr.setVisible(active); 334 } 335 return menuMgr; 336 } 337 338 341 protected final IActionBars getParent() { 342 return parent; 343 } 344 345 353 public final IServiceLocator getServiceLocator() { 354 if (serviceLocator != null) { 355 return serviceLocator; 356 } 357 358 return parent.getServiceLocator(); 359 } 360 361 367 public IStatusLineManager getStatusLineManager() { 368 if (statusLineMgr == null) { 369 statusLineMgr = new SubStatusLineManager(parent 370 .getStatusLineManager()); 371 statusLineMgr.setVisible(active); 372 } 373 return statusLineMgr; 374 } 375 376 382 public IToolBarManager getToolBarManager() { 383 if (toolBarMgr == null) { 384 toolBarMgr = createSubToolBarManager(parent.getToolBarManager()); 385 toolBarMgr.setVisible(active); 386 } 387 return toolBarMgr; 388 } 389 390 393 protected final boolean isSubMenuManagerCreated() { 394 return menuMgr != null; 395 } 396 397 400 protected final boolean isSubStatusLineManagerCreated() { 401 return statusLineMgr != null; 402 } 403 404 407 protected final boolean isSubToolBarManagerCreated() { 408 return toolBarMgr != null; 409 } 410 411 414 public void partChanged(IWorkbenchPart part) { 415 } 416 417 424 public void removePropertyChangeListener(IPropertyChangeListener listener) { 425 removeListenerObject(listener); 426 } 427 428 431 protected void setActive(boolean set) { 432 active = set; 433 if (menuMgr != null) { 434 menuMgr.setVisible(set); 435 } 436 437 if (statusLineMgr != null) { 438 statusLineMgr.setVisible(set); 439 } 440 441 if (toolBarMgr != null) { 442 toolBarMgr.setVisible(set); 443 } 444 } 445 446 455 public void setGlobalActionHandler(String actionID, IAction handler) { 456 if (actionID == null) { 457 462 WorkbenchPlugin 463 .log("Cannot set the global action handler for a null action id"); return; 465 } 466 467 if (handler instanceof CommandLegacyActionWrapper) { 468 WorkbenchPlugin 471 .log("Cannot feed a CommandLegacyActionWrapper back into the system"); return; 473 } 474 475 if (handler instanceof CommandAction) { 476 return; 479 } 480 481 if (handler != null) { 482 if (actionHandlers == null) { 484 actionHandlers = new HashMap (11); 485 } 486 actionHandlers.put(actionID, handler); 487 488 if (serviceLocator != null) { 490 final IActionCommandMappingService mappingService = (IActionCommandMappingService) serviceLocator 491 .getService(IActionCommandMappingService.class); 492 final String commandId = mappingService.getCommandId(actionID); 493 494 final IHandlerService service = (IHandlerService) serviceLocator 496 .getService(IHandlerService.class); 497 Map activationsByActionId = null; 498 if (activationsByActionIdByServiceLocator == null) { 499 activationsByActionIdByServiceLocator = new WeakHashMap (); 500 activationsByActionId = new HashMap (); 501 activationsByActionIdByServiceLocator.put(serviceLocator, 502 activationsByActionId); 503 } else { 504 activationsByActionId = (Map ) activationsByActionIdByServiceLocator 505 .get(serviceLocator); 506 if (activationsByActionId == null) { 507 activationsByActionId = new HashMap (); 508 activationsByActionIdByServiceLocator.put( 509 serviceLocator, activationsByActionId); 510 } else if (activationsByActionId.containsKey(actionID)) { 511 final Object value = activationsByActionId 512 .remove(actionID); 513 if (value instanceof IHandlerActivation) { 514 final IHandlerActivation activation = (IHandlerActivation) value; 515 actionIdByCommandId.remove(activation.getCommandId()); 516 service.deactivateHandler(activation); 517 activation.getHandler().dispose(); 518 } 519 } else if (commandId != null 520 && actionIdByCommandId.containsKey(commandId)) { 521 final Object value = activationsByActionId 522 .remove(actionIdByCommandId.remove(commandId)); 523 if (value instanceof IHandlerActivation) { 524 final IHandlerActivation activation = (IHandlerActivation) value; 525 service.deactivateHandler(activation); 526 activation.getHandler().dispose(); 527 } 528 } 529 } 530 531 if (commandId != null) { 532 actionIdByCommandId.put(commandId, actionID); 533 final IHandler actionHandler = new ActionHandler(handler); 537 Expression handlerExpression = EXPRESSION; 538 if (this instanceof EditorActionBars) { 540 handlerExpression = ((EditorActionBars)this).getHandlerExpression(); 541 } 542 final IHandlerActivation activation = service 543 .activateHandler(commandId, actionHandler, 544 handlerExpression); 545 activationsByActionId.put(actionID, activation); 546 } 547 } 548 549 } else { 550 if (actionHandlers != null) { 551 actionHandlers.remove(actionID); 552 } 553 554 if (serviceLocator != null) { 556 final IHandlerService service = (IHandlerService) serviceLocator 557 .getService(IHandlerService.class); 558 if (activationsByActionIdByServiceLocator != null) { 559 final Map activationsByActionId = (Map ) activationsByActionIdByServiceLocator 560 .get(serviceLocator); 561 if ((activationsByActionId != null) 562 && (activationsByActionId.containsKey(actionID))) { 563 final Object value = activationsByActionId 564 .remove(actionID); 565 if (value instanceof IHandlerActivation) { 566 final IHandlerActivation activation = (IHandlerActivation) value; 567 actionIdByCommandId.remove(activation.getCommandId()); 568 service.deactivateHandler(activation); 569 activation.getHandler().dispose(); 570 } 571 } 572 } 573 } 574 } 575 actionHandlersChanged = true; 576 } 577 578 586 protected final void setServiceLocator(final IServiceLocator locator) { 587 if (locator == null) { 588 throw new NullPointerException ("The service locator cannot be null"); } 590 this.serviceLocator = locator; 591 } 592 593 597 public void updateActionBars() { 598 parent.updateActionBars(); 599 fireActionHandlersChanged(); 600 } 601 } 602 | Popular Tags |