【问题标题】:Refresh menu item in view刷新视图中的菜单项
【发布时间】:2013-06-13 12:34:58
【问题描述】:

上下文

我正在为 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 上)。 我是不是做错了什么?

【问题讨论】:

  • 好吧,这很奇怪,它在 Eclipse 4.2 上开箱即用......但在 3.4-3.7 上仍然不能工作

标签: java eclipse eclipse-plugin


【解决方案1】:

当启用更改时,您的处理程序必须触发一个事件。如果您有更改使用 org.eclipse.core.commands.AbstractHandler.setBaseEnabled(boolean) 更新您的处理程序,它将触发所需的事件。

【讨论】:

  • 感谢您的回答。但它仍然无法正常工作。 (请参阅我对问题的编辑)
【解决方案2】:

感谢 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);
            }
        }
    }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多