Eclipse RCP 命令框架:我迷路了
我有两个单独的视图,它们都显示多个值列表。两个视图同时打开。
我有一个通用命令 AddNewItemInList
,我想将其添加到两个视图的工具栏中。该命令应将新项目添加到当前具有焦点的列表中。如果用户尚未选择列表,则应禁用该命令。
我目前使用以下命令来执行该命令。
public class MyViewPart extends ViewPart {
[...]
public Object getAdapter(Class clazz) {
if(clazz.equals(List.class))
return getListInFocus(); // can be null if no list in focus
return null;
}
}
public class AddNewItemInList extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
MyList list = HandlerUtil.getActivePart(event).getAdapter(MyList.class);
list.add(new Item());
return null;
}
}
到目前为止一切顺利,但我不知道如何相应地启用或禁用处理程序。我已阅读有关 Eclipse Core Commands 框架的内容。我很失落。
已回答的问题: 如何创建一个表达式来查看视图的 getAdapter() 方法是否不返回 null? ->下面回答了
如何获取配置此命令的视图(而不是当前活动视图)? ->我可以将 viewId 添加为命令参数
剩余问题: 我应该在我的plugin.xml 中配置两个单独的处理程序实例(每个视图一个)吗? -> enabledWhen 条件只能使用当前的计算上下文。看来我无法使用该命令提供的任何参数。这意味着处理程序的启用或禁用取决于整个工作台的状态。
I have two separate views that both show multiple lists of values. Both views are open at the same time.
I have a generic command AddNewItemInList
that I want to add to the Toolbar of both views. The command should add a new item to the list that currently has focus. The command should be disabled if the user has not yet selected a list.
I currently have used the following to execute the command.
public class MyViewPart extends ViewPart {
[...]
public Object getAdapter(Class clazz) {
if(clazz.equals(List.class))
return getListInFocus(); // can be null if no list in focus
return null;
}
}
public class AddNewItemInList extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
MyList list = HandlerUtil.getActivePart(event).getAdapter(MyList.class);
list.add(new Item());
return null;
}
}
So far so good, but I have no clue how to enable or disable the Handler accordingly. I have read about the Eclipse Core Commands framework. I am very lost.
Answered questions:
How can I create an expression to see if the getAdapter() method of the view does not return null? -> Answered below
How do I get the view for which this command was configured (and not the currently active view) ? -> I can add the viewId as a command parameter
Remaining questions:
Should I configure two separate Handler instances in my plugin.xml (one for each view) ? -> The enabledWhen condition can only use the current evaluation context. It seems I cannot use any parameters provided by the command. This means a handler is either enabled or disabled depending on the state of the full workbench.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用自定义属性测试器。它需要大量的样板文件并且看起来很丑,但它确实有效。
编辑:实际上,它要简单得多< /a>:
You can use custom property testers. It needs a lot of boilerplate and looks ugly, but it works.
EDIT: Actually, it's a lot simpler:
自定义属性测试仪有一些问题,而且它们真的很难看。他们承诺在 4.0 版本中进行一些改进。您可以尝试自己实现类似的东西。
Custom property testers have some issues and they are really ugly. They promise to do some improvements in version 4.0. You can try to implement something similar by yourself.