【问题标题】:Evaluation of @CanExecute for RCP e4 application评估 RCP e4 应用程序的 @CanExecute
【发布时间】:2021-11-08 13:19:02
【问题描述】:

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-rcp e4


    【解决方案1】:

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

    broker.post(UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC 的参数应该是元素 ID,UIEvents.ALL_ELEMENT_IDorg.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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-28
      • 2015-11-06
      • 2017-10-27
      • 2011-01-21
      相关资源
      最近更新 更多