Eclipse e4 RCP: Core Expressions - Something less XML-hell?

吃可爱长大的小学妹 提交于 2019-12-19 09:03:25

问题


I'm working on an E4 RCP application, and have a context menu which has menu items visible or not depending on the selection. The way I've found to do this is with core expressions defined in the plugin.xml like so:

<extension
     point="org.eclipse.core.expressions.definitions">
     <definition
        id="com.foo.bar.test.core.expression">
      <with variable="org.eclipse.ui.selection">
        <iterate ifEmpty="false">
            <or>
          <instanceof value="com.foo.bar.Class1">
          </instanceof>
          <instanceof value="com.foo.bar.Class2">
          </instanceof>
            </or>
        </iterate>
      </with>
  </definition>

This works, and the menu item is displayed if the selected item is an instance of Class1 or Class2.

This all seems like an extremely nasty way to do things! When many of these are added, its going to be a maintenance and debugging nightmare.

Can anyone demonstrate a less XML-ish way of doing this? Pure programmatic methods in Java would be great!


回答1:


Core expressions are not working for toolbar items, for example. You can use the following workaround in command handlers:

public class SomeHandler {
    protected MToolItem toolItem;

    @CanExecute
    @Inject
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) @Optional ISelection selection)
    {
        boolean canExecute = ...
        setToolItemVisible(canExecute);
        ...
    }

    private void setToolItemVisible(final boolean visible) {
        if (toolItem != null) {
            Display.getDefault().asyncExec(new Runnable() {
                @Override
                public void run() {
                    toolItem.setVisible(visible);
                }
            });
        }
    }
}

where toolItem is retrieved by EModelService



来源:https://stackoverflow.com/questions/23567917/eclipse-e4-rcp-core-expressions-something-less-xml-hell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!