@CanExecute对RCP e4应用程序的评估 [英] Evaluation of @CanExecute for RCP e4 application

查看:18
本文介绍了@CanExecute对RCP e4应用程序的评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RCPE4应用程序包括一个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("
");
            }
        });
    }

执行发货的处理程序是

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,例如,在单击程序包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

推荐答案

我认为@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
}

这篇关于@CanExecute对RCP e4应用程序的评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆