【问题标题】:Eclipse RCP Handler enablement based on selection基于选择的 Eclipse RCP Handler 启用
【发布时间】:2014-01-09 18:53:01
【问题描述】:

我有点卡住了,因为资源很多,他们并没有为我澄清这个问题。

假设我有一个 command、一个 handler、一个 property tester,并且这些结果在 UI 上显示为 酷栏项目

现在,假设我有多个视图扩展同一个基础(例如BaseView)。所有这些视图都包含ColumnViewers(例如TableViewerTreeViewer),它们充当选择提供者

  • enableWhenactiveWhen 配置如何知道这些查看器中的选择?我无法想象selection + instanceOf 参数如何为ISelection(s) 工作。
  • 如何将选定对象传递给属性测试器? test 方法接收什么实例(作为receiver)?
  • 我注意到有许多通过处理程序的setEnabled() 方法的断点。这是正常行为吗?可以覆盖setEnabled吗?

这里的代码对我来说似乎有点无关紧要。但无论如何,这些 sn-ps 涵盖了以下问题:

// --------------------- 1 -----------------------

  <handler
        class="com.example.ggrec.handlers.SampleHandler"
        commandId="com.example.ggrec.commands.sampleCommand">
     <enabledWhen>
        <with
              variable="selection">
           <instanceof
                 value="org.eclipse.jface.viewers.ISelection">
           </instanceof>
        </with>
     </enabledWhen>
  </handler>

// --------------------- 2 -----------------------

  <propertyTester
        class="com.example.ggrec.propertyTesters.SamplePropertyTester"
        id="com.example.ggrec.samplePropertyTester"
        namespace="com.example.ggrec.propertyTesters"
        properties="simpleTest"
        type="java.lang.Object">
  </propertyTester>

// --------------------- 3 -----------------------

/**
 * 
 * @author ggrec
 *
 */
public class SamplePropertyTester extends PropertyTester
{
    @Override
    public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue)
    {
        if (receiver instanceof ISelection) // What instance is this?
            System.out.println("RAINBOWS");

        return true;
    }
}

// --------------------- 4 -----------------------

/**
 * 
 * @author ggrec
 *
 */
public class SampleHandler extends AbstractHandler
{
    @Override
    public Object execute(final ExecutionEvent event) throws ExecutionException 
    {
        final IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
        MessageDialog.openInformation(window.getShell(), "", "meh");

        return null;
    }

    @Override
    public void setEnabled(final Object evaluationContext)
    {
        super.setEnabled(evaluationContext); // Goes like crazy through here.
    }
}

【问题讨论】:

    标签: java eclipse eclipse-plugin swt eclipse-rcp


    【解决方案1】:

    每个ViewPart(和编辑器部分)都有一个单独的选择,由选择服务维护并由您的选择提供者设置。 enabledWhenvisibleWhen 使用从零件选择服务获得的当前活动零件的选择。

    属性测试调用通常位于启用表达式中的&lt;with&gt; 块内,该块建立正在测试的对象。比如:

    <with
        variable="org.eclipse.ui.selection">
       <iterate
             operator="or">
          <adapt
                type="music.resources.data.IMusicFile">
                <or>
                    <test property="music.isMusicOrPlaylist"/>
                    <test property="music.isVideo"/>
                </or>
           </adapt>
        </iterate>
    </with>
    

    与当前选择一起工作,要求选择适应特定类型,并测试两个属性中的一个。

    【讨论】:

    • 启用参数的有趣选择。在这种情况下,iterateadapt 如何派上用场?
    • 如果树/表格允许选择多个项目iterate 依次查看每个选择,因此指定or 时,只有一个选择必须匹配。 adapt 使用IAdapterFactory 接口将指定类型与选中的对象进行匹配(因此UI对象不必直接实现该接口)。
    • 很好的解释。现在,我还有一个问题。为什么不断地调用属性测试器?如果我在test 方法中有几条业务逻辑,那会不会拖慢整个系统?
    • 我没有看到我的测试人员一直被调用,只是当视图中的某些内容发生变化时(但发生这种情况时可能会有很多调用)。他们仍然不应该做任何太昂贵的事情,否则 UI 可能会无响应。我的示例中的测试人员只是查看文件内容类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 2010-11-19
    • 1970-01-01
    • 2016-07-16
    相关资源
    最近更新 更多