首页 > 解决方案 > 评估 RCP e4 应用程序的 @CanExecute

问题描述

RCP E4 应用程序包括一个 TreeViewer,用于管理“包”集合的可见性/选择。该部件被命名为 Package Navigator。

在此处输入图像描述

当一个包裹准备好发送时,会显示 TreeViewer 图标 ,并且 应该启用在此处输入图像描述 开始发货的按钮。在此处输入图像描述

当包裹未准备好时,图标是在此处输入图像描述 ,并且要运送的按钮(处理程序)应该被禁用。

实现这种行为的代码是:

private TreeViewer viewer;
    @PostConstruct
    public void createComposite(Composite parent, IEnviosService theModel) {
        ...         
        Tree t = (Tree) viewer.getControl();
        t.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                boolean check = false;
                System.out.print("Selection listener ....");
                IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
                if (selection.getFirstElement() instanceof Package) {
                    check = ((Package)selection.getFirstElement()).isReadyToShip();
                    System.out.print("IT'S A PACKAGE....");
                    // evaluate all @CanExecute methods
                    broker.post(UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC, check);
                }
                System.out.print("\n");
            }
        });
    }

执行装运的处理程序是

public class ShipmentHandler {
    @Execute
    public void execute(Shell shell) {
        //TODO
    }
    
    @Inject
    @Optional
    @CanExecute
    public boolean canExecute(@UIEventTopic(UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC) boolean checkPackageReady) {
        System.out.println("Inside canExecute method... " + checkPackageReady);
        if (checkPackageReady)
            return true;
        return false;
    }
}

但是该按钮永远不会被禁用,即使 @canExecute 方法返回 false,例如,在单击 Packages 88 、 89 和 110 和 112 后显示以下控制台输出,该按钮始终启用:

Selection listener  PACKAGE: 88...true
Inside canExecute method... true
Selection listener  PACKAGE: 89...false
Inside canExecute method... false
Selection listener  PACKAGE: 110...false
Inside canExecute method... false
Selection listener  PACKAGE: 112...true
Inside canExecute method... true

标签: eclipse-rcpe4

解决方案


我不认为你可以混合@CanExecute@UIEventTopic喜欢那样。无论如何UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC,这是一个相当特殊的话题,不打算像这样处理。

的参数broker.post(UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC应该是元素 idUIEvents.ALL_ELEMENT_ID或 a org.eclipse.e4.ui.workbench.Selector,仅此而已。该参数选择更新哪些处理程序。

因此,您不能将“checkPackageReady”值直接传递给处理程序,您将不得不使用其他一些机制——例如视图和处理程序都可以注入的模型对象。

您还可以使用ESelectionService设置零件的当前选择,然后您可以在处理程序中访问此信息:

查看部分:

@Inject 
ESelectionService selectionService;

...

public void widgetSelected(SelectionEvent e)
{
  selectionService.setSelection(viewer.getStructuredSelection());

  broker.post(UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC, ... selector);
}

可以执行:

@CanExecute
public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, @Named(IServiceConstants.ACTIVE_PART)  MPart part)
{
  // TODO check part is your part
  // TODO check the selection
}

推荐阅读