1 11 12 package org.eclipse.ui.internal.handlers; 13 14 import org.eclipse.core.commands.ExecutionEvent; 15 import org.eclipse.core.commands.IHandler; 16 import org.eclipse.core.commands.IHandlerListener; 17 import org.eclipse.core.commands.IObjectWithState; 18 import org.eclipse.core.commands.ParameterizedCommand; 19 import org.eclipse.core.commands.State; 20 import org.eclipse.core.expressions.EvaluationResult; 21 import org.eclipse.core.expressions.Expression; 22 import org.eclipse.core.expressions.IEvaluationContext; 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IConfigurationElement; 25 import org.eclipse.core.runtime.ISafeRunnable; 26 import org.eclipse.core.runtime.IStatus; 27 import org.eclipse.core.runtime.InvalidRegistryObjectException; 28 import org.eclipse.core.runtime.ListenerList; 29 import org.eclipse.core.runtime.SafeRunner; 30 import org.eclipse.core.runtime.Status; 31 import org.eclipse.jface.action.IAction; 32 import org.eclipse.jface.util.IPropertyChangeListener; 33 import org.eclipse.jface.util.PropertyChangeEvent; 34 import org.eclipse.jface.viewers.ISelection; 35 import org.eclipse.jface.viewers.ISelectionChangedListener; 36 import org.eclipse.jface.viewers.SelectionChangedEvent; 37 import org.eclipse.jface.viewers.StructuredSelection; 38 import org.eclipse.swt.widgets.Event; 39 import org.eclipse.ui.IActionDelegate; 40 import org.eclipse.ui.IActionDelegate2; 41 import org.eclipse.ui.IActionDelegateWithEvent; 42 import org.eclipse.ui.IEditorActionDelegate; 43 import org.eclipse.ui.IEditorPart; 44 import org.eclipse.ui.INullSelectionListener; 45 import org.eclipse.ui.IObjectActionDelegate; 46 import org.eclipse.ui.ISelectionListener; 47 import org.eclipse.ui.ISources; 48 import org.eclipse.ui.IViewActionDelegate; 49 import org.eclipse.ui.IViewPart; 50 import org.eclipse.ui.IWorkbenchPage; 51 import org.eclipse.ui.IWorkbenchPart; 52 import org.eclipse.ui.IWorkbenchWindow; 53 import org.eclipse.ui.IWorkbenchWindowActionDelegate; 54 import org.eclipse.ui.handlers.IHandlerService; 55 import org.eclipse.ui.internal.WorkbenchPlugin; 56 57 69 public final class ActionDelegateHandlerProxy implements ISelectionListener, 70 ISelectionChangedListener, INullSelectionListener, IHandler, 71 IObjectWithState { 72 73 77 private CommandLegacyActionWrapper action; 78 79 83 private String actionId; 84 85 89 private ParameterizedCommand command; 90 91 94 private ISelection currentSelection; 95 96 99 private IActionDelegate delegate; 100 101 private IEditorActionDelegate editorDelegate = null; 106 private IViewActionDelegate viewDelegate = null; 107 private IObjectActionDelegate objectDelegate = null; 108 private IWorkbenchWindowActionDelegate windowDelegate = null; 109 110 114 private String delegateAttributeName; 115 116 121 private IConfigurationElement element; 122 123 128 private final Expression enabledWhenExpression; 129 130 134 private transient ListenerList listenerList = null; 135 136 141 private final String style; 142 143 148 private final String viewId; 149 150 154 private final IWorkbenchWindow window; 155 156 191 public ActionDelegateHandlerProxy(final IConfigurationElement element, 192 final String delegateAttributeName, final String actionId, 193 final ParameterizedCommand command, final IWorkbenchWindow window, 194 final String style, final Expression enabledWhenExpression, 195 final String viewId) { 196 if (element == null) { 197 throw new NullPointerException ( 198 "The configuration element backing a handler proxy cannot be null"); } 200 201 if (delegateAttributeName == null) { 202 throw new NullPointerException ( 203 "The attribute containing the action delegate must be known"); } 205 206 if (window == null) { 207 throw new NullPointerException ( 208 "The workbench window for a delegate must not be null"); } 210 211 this.element = element; 212 this.enabledWhenExpression = enabledWhenExpression; 213 this.delegateAttributeName = delegateAttributeName; 214 this.window = window; 215 this.command = command; 216 this.actionId = actionId; 217 this.style = style; 218 this.viewId = viewId; 219 } 220 221 public final void addHandlerListener(final IHandlerListener handlerListener) { 222 if (listenerList == null) { 223 listenerList = new ListenerList(ListenerList.IDENTITY); 224 } 225 226 listenerList.add(handlerListener); 227 } 228 229 public void addState(String id, State state) { 230 232 } 233 234 public final void dispose() { 235 final IActionDelegate delegate = getDelegate(); 236 if (delegate instanceof IWorkbenchWindowActionDelegate) { 237 final IWorkbenchWindowActionDelegate workbenchWindowDelegate = (IWorkbenchWindowActionDelegate) delegate; 238 workbenchWindowDelegate.dispose(); 239 } else if (delegate instanceof IActionDelegate2) { 240 final IActionDelegate2 delegate2 = (IActionDelegate2) delegate; 241 delegate2.dispose(); 242 } 243 } 244 245 public final Object execute(final ExecutionEvent event) { 246 final IAction action = getAction(); 247 if (loadDelegate() && (action != null)) { 248 final Object trigger = event.getTrigger(); 249 250 final Object applicationContext = event.getApplicationContext(); 252 if (applicationContext instanceof IEvaluationContext) { 253 final IEvaluationContext context = (IEvaluationContext) applicationContext; 254 updateDelegate(action, context); 255 } 256 257 if ((delegate instanceof IActionDelegate2) 259 && (trigger instanceof Event)) { 260 final IActionDelegate2 delegate2 = (IActionDelegate2) delegate; 262 final Event triggeringEvent = (Event) trigger; 263 delegate2.runWithEvent(action, triggeringEvent); 264 } else if ((delegate instanceof IActionDelegateWithEvent) 265 && (trigger instanceof Event)) { 266 final IActionDelegateWithEvent delegateWithEvent = (IActionDelegateWithEvent) delegate; 268 final Event triggeringEvent = (Event) trigger; 269 delegateWithEvent.runWithEvent(action, triggeringEvent); 270 } else { 271 delegate.run(action); 272 } 273 } 274 275 return null; 276 } 277 278 282 private void updateDelegate(final IAction action, 283 final IEvaluationContext context) { 284 if (action == null || delegate == null) { 285 return; 286 } 287 288 if (editorDelegate != null) { 289 final Object activeEditor = context 290 .getVariable(ISources.ACTIVE_EDITOR_NAME); 291 if (activeEditor != null) { 292 editorDelegate.setActiveEditor(action, 293 (IEditorPart) activeEditor); 294 } 295 } else if (objectDelegate != null) { 296 final Object activePart = context 297 .getVariable(ISources.ACTIVE_PART_NAME); 298 if (activePart != null) { 299 objectDelegate.setActivePart(action, 300 (IWorkbenchPart) activePart); 301 } 302 } 303 304 final Object selectionObject = getCurrentSelection(context); 305 if (selectionObject instanceof ISelection) { 306 currentSelection = (ISelection) selectionObject; 307 delegate.selectionChanged(action, currentSelection); 308 } else { 309 currentSelection = null; 310 delegate.selectionChanged(action, null); 311 } 312 } 313 314 318 private Object getCurrentSelection(final IEvaluationContext context) { 319 Object obj = context 320 .getVariable(ISources.ACTIVE_MENU_EDITOR_INPUT_NAME); 321 if (obj == null) { 322 obj = context.getVariable(ISources.ACTIVE_MENU_SELECTION_NAME); 323 if (obj == null) { 324 obj = context 325 .getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME); 326 } 327 } 328 return obj; 329 } 330 331 338 private final CommandLegacyActionWrapper getAction() { 339 if (action == null) { 340 action = new CommandLegacyActionWrapper(actionId, command, style, 341 window); 342 action.addPropertyChangeListener(new IPropertyChangeListener() { 343 public final void propertyChange(final PropertyChangeEvent event) { 344 } 346 }); 347 } 348 return action; 349 } 350 351 357 private final IActionDelegate getDelegate() { 358 return delegate; 359 } 360 361 public State getState(String stateId) { 362 return null; 364 } 365 366 public String [] getStateIds() { 367 return null; 369 } 370 371 public final void handleStateChange(final State state, final Object oldValue) { 372 } 374 375 378 private final boolean initDelegate() { 379 final IWorkbenchPage page = window.getActivePage(); 380 final IWorkbenchPart activePart; 381 final IEditorPart activeEditor; 382 if (page == null) { 383 activePart = null; 384 activeEditor = null; 385 } else { 386 activePart = page.getActivePart(); 387 activeEditor = page.getActiveEditor(); 388 } 389 final IActionDelegate delegate = getDelegate(); 390 final IAction action = getAction(); 391 392 if ((viewId != null) && (page != null) && (viewDelegate != null)) { 394 final IViewPart viewPart = page.findView(viewId); 395 if (viewPart == null) { 396 return false; 397 } 398 } 399 400 final ISafeRunnable runnable = new ISafeRunnable() { 402 public final void handleException(final Throwable exception) { 403 } 405 406 public final void run() { 407 if (delegate instanceof IActionDelegate2) { 409 final IActionDelegate2 delegate2 = (IActionDelegate2) delegate; 410 delegate2.init(action); 411 } 412 413 if ((objectDelegate != null) && (activePart != null)) { 415 objectDelegate.setActivePart(action, activePart); 416 } else if (editorDelegate != null) { 417 editorDelegate.setActiveEditor(action, activeEditor); 418 } else if ((viewId != null) && (page != null) 419 && (viewDelegate != null)) { 420 final IViewPart viewPart = page.findView(viewId); 421 viewDelegate.init(viewPart); 422 } else if (windowDelegate != null) { 423 windowDelegate.init(window); 424 } 425 } 426 }; 427 SafeRunner.run(runnable); 428 return true; 429 } 430 431 public final boolean isEnabled() { 432 final IHandlerService service = (IHandlerService) window 433 .getService(IHandlerService.class); 434 IEvaluationContext context = service.getCurrentState(); 435 return isEnabled(context); 436 } 437 438 public final boolean isEnabled(IEvaluationContext context) { 439 final CommandLegacyActionWrapper action = getAction(); 440 if (enabledWhenExpression != null) { 441 try { 442 final EvaluationResult result = enabledWhenExpression 443 .evaluate(context); 444 if (result == EvaluationResult.TRUE) { 445 updateDelegate(action, context); 446 return (action == null) 447 || action.isEnabledDisregardingCommand(); 448 } 449 } catch (final CoreException e) { 450 final StringBuffer message = new StringBuffer ( 452 "An exception occurred while evaluating the enabledWhen expression for "); if (element == null) { 454 message.append(delegate); 455 } else { 456 message.append(element.getAttribute(delegateAttributeName)); 457 } 458 message.append("' could not be loaded"); final IStatus status = new Status(IStatus.WARNING, 460 WorkbenchPlugin.PI_WORKBENCH, 0, e.getMessage(), e); 461 WorkbenchPlugin.log(message.toString(), status); 462 } 463 464 return false; 465 } 466 467 updateDelegate(action, context); 468 return (action == null) || action.isEnabledDisregardingCommand(); 469 } 470 471 public final boolean isHandled() { 472 return true; 473 } 474 475 482 private final boolean isSafeToLoadDelegate() { 483 return false; 484 } 489 490 497 private final boolean loadDelegate() { 498 if (delegate == null) { 500 504 if (viewId != null) { 505 final IWorkbenchPage activePage = window.getActivePage(); 506 if (activePage != null) { 507 final IViewPart part = activePage.findView(viewId); 508 if (part == null) { 509 return false; 510 } 511 } else { 512 return false; 513 } 514 } 515 516 try { 518 delegate = (IActionDelegate) element 519 .createExecutableExtension(delegateAttributeName); 520 String name = element.getDeclaringExtension() 521 .getExtensionPointUniqueIdentifier(); 522 if ("org.eclipse.ui.actionSets".equals(name) && delegate instanceof IWorkbenchWindowActionDelegate) { 524 windowDelegate = (IWorkbenchWindowActionDelegate) delegate; 525 } else if ("org.eclipse.ui.editorActions".equals(name) && delegate instanceof IEditorActionDelegate) { 527 editorDelegate = (IEditorActionDelegate) delegate; 528 } else if ("org.eclipse.ui.viewActions".equals(name) && delegate instanceof IViewActionDelegate) { 530 viewDelegate = (IViewActionDelegate) delegate; 531 } else if ("org.eclipse.ui.popupMenus".equals(name)) { IConfigurationElement parent = (IConfigurationElement) element 533 .getParent(); 534 if ("objectContribution".equals(parent.getName()) && delegate instanceof IObjectActionDelegate) { 536 objectDelegate = (IObjectActionDelegate) delegate; 537 } else if (viewId == null 538 && delegate instanceof IEditorActionDelegate) { 539 editorDelegate = (IEditorActionDelegate) delegate; 540 } else if (viewId != null 541 && delegate instanceof IViewActionDelegate) { 542 viewDelegate = (IViewActionDelegate) delegate; 543 } 544 } 545 if (initDelegate()) { 546 element = null; 547 delegateAttributeName = null; 548 return true; 549 } 550 551 delegate = null; 552 objectDelegate = null; 553 viewDelegate = null; 554 editorDelegate = null; 555 windowDelegate = null; 556 return false; 557 558 } catch (final ClassCastException e) { 559 final String message = "The proxied delegate was the wrong class"; final IStatus status = new Status(IStatus.ERROR, 561 WorkbenchPlugin.PI_WORKBENCH, 0, message, e); 562 WorkbenchPlugin.log(message, status); 563 return false; 564 565 } catch (final CoreException e) { 566 final String message = "The proxied delegate for '" + element.getAttribute(delegateAttributeName) 568 + "' could not be loaded"; IStatus status = new Status(IStatus.ERROR, 570 WorkbenchPlugin.PI_WORKBENCH, 0, message, e); 571 WorkbenchPlugin.log(message, status); 572 return false; 573 } 574 } 575 576 return true; 577 } 578 579 582 private final void refreshEnablement() { 583 final IActionDelegate delegate = getDelegate(); 584 final IAction action = getAction(); 585 if ((delegate != null) && (action != null)) { 586 delegate.selectionChanged(action, currentSelection); 587 } 588 } 589 590 public void removeHandlerListener(IHandlerListener handlerListener) { 591 if (listenerList != null) { 592 listenerList.remove(handlerListener); 593 594 if (listenerList.isEmpty()) { 595 listenerList = null; 596 } 597 } 598 } 599 600 public void removeState(String stateId) { 601 603 } 604 605 private final void selectionChanged(final ISelection selection) { 606 currentSelection = selection; 608 if (currentSelection == null) { 609 currentSelection = StructuredSelection.EMPTY; 610 } 611 612 616 final IActionDelegate delegate = getDelegate(); 619 if (delegate == null && isSafeToLoadDelegate()) { 620 loadDelegate(); 621 } 622 refreshEnablement(); 623 } 624 625 public final void selectionChanged(final IWorkbenchPart part, 626 final ISelection selection) { 627 selectionChanged(selection); 628 629 } 630 631 public final void selectionChanged(final SelectionChangedEvent event) { 632 final ISelection selection = event.getSelection(); 633 selectionChanged(selection); 634 } 635 636 public final String toString() { 637 final StringBuffer buffer = new StringBuffer (); 638 buffer.append("ActionDelegateHandlerProxy("); buffer.append(getDelegate()); 640 if (element != null) { 641 buffer.append(','); 642 try { 643 final String className = element 644 .getAttribute(delegateAttributeName); 645 buffer.append(className); 646 } catch (InvalidRegistryObjectException e) { 647 buffer.append(actionId); 648 } 649 } 650 buffer.append(')'); 651 return buffer.toString(); 652 } 653 } 654 | Popular Tags |