eclipse-rcp - 评估 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
解决方案
我不认为你可以混合@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
}
推荐阅读
- ansible - 列出提供给剧本的额外变量
- r - min() 函数不适用于 R 中的日期对象
- hive - 如何在检查条件时使用 double 和 float 类型修复 Hive 查询问题,(在转换时遇到问题)
- publish - 谷歌禁止华为。我应该如何处理 google 依赖项?
- c# - 如何从 2 个单独的文本文件创建组合列表?
- vlc - 使用 vlc cli 将 mp4 从渐进式转换为隔行式
- c - 如何用断言实现 C 宏
- serverless-framework - 设置 serverless.yml 文件以使用特定的 aws 配置文件
- entity-framework - Telerik:生成的 SQL 与示例项目不同
- python - Yagmail (Python) - 如何使用密件抄送/密件发送电子邮件