[英]Refresh menu item in view
语境
我正在为Eclipse 3.4及更高版本构建一个插件。 我有一个ID为mrp.view
的视图,其中menuContribution设置为toolbar:mrp.view
。 这个menuContribution有一些命令,而我有以下命令:
<handler
class="mrp.handlers.export"
commandId="mrp.commands.export">
</handler>
<command
commandId="mrp.commands.export"
label="My command"
style="push">
</command>
我的处理程序mrp.handlers.export
有一个动态的ìsEnabled()`方法,如下所示:
@Override
public boolean isEnabled() {
return !getMySelection().isEmpty();
}
题
数据更改后,如何刷新工具栏上的按钮? (如果我单击工具栏的另一个按钮,则刷新会自动完成,但是如果我不这样做,则会自动刷新...)
我试过了..
ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
service.refreshElements("mrp.commands.export", null);
但这似乎无能为力。
也是这个:
public class Export extends AbstractHandler implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
setBaseEnabled(!getSelection().isEmpty());
}
// ....
}
它被调用,但是我的视图菜单上的图标未刷新(在Eclipse 3.7上)。 我做错什么了吗 ?
您的处理程序在启用更改时必须触发一个事件。 如果进行更改,则使用org.eclipse.core.commands.AbstractHandler.setBaseEnabled(boolean)更新处理程序,它将触发所需的事件。
谢谢Paul Webster的回答,我知道了。
public class Export extends AbstractHandler implements PropertyChangeListener {
public Export() {
Activator.getDefault().AddListener(this);
setBaseEnabled(!getMySelection().isEmpty());
}
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
// My handler
return null;
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(Activator.EVENT_SELECTION_CHANGED)) {
boolean before = isEnabled();
boolean after = !getMySelection().isEmpty();
if (after != before) {
setBaseEnabled(after);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.