简体   繁体   English

基于选择的Eclipse RCP处理程序支持

[英]Eclipse RCP Handler enablement based on selection

I'm a bit stuck, because the resources are many and they don't clarfiy this issue for me. 我有点被困住了,因为资源很多,而且他们没有为我澄清这个问题。

Suppose I have a command , a handler , a property tester , and the result of these presented on the UI as a coolbar item . 假设我有一个命令 ,一个处理程序 ,一个属性测试器 ,并将这些结果作为一个酷炫的项目显示在UI上。

Now, suppose I have several view extending the same base (eg BaseView ). 现在,假设我有几个扩展相同基数的视图(例如BaseView )。 All of these views contain ColumnViewer s (eg TableViewer , TreeViewer ) which act as selection providers . 所有这些视图都包含ColumnViewer (例如TableViewerTreeViewer ),它们充当选择提供程序

  • How does the enableWhen and activeWhen configurations know about the selection in those viewers? enableWhenactiveWhen配置如何了解这些查看器中的选择? I can't imagine how the selection + instanceOf parameters work for ISelection (s). 我无法想象如何selection + instanceOf参数工作ISelection (S)。
  • How is the selected object passed to the property tester? 所选对象如何传递到属性测试器? What instance do the test method receive (as the receiver )? test方法接收什么实例(作为receiver )?
  • I noticed with a breakpoint that there are many passes through the setEnabled() method of the handler. 我注意到一个断点,该处理程序的setEnabled()方法有很多遍。 Is that normal behaviour? 那是正常的行为吗? Would it be okay to override the setEnabled ? 可以重写setEnabled吗?

Code seems a little bit irrelevant to me here. 代码在这里似乎与我无关。 But anyway, these snippets cover the questions: 但是无论如何,这些片段涵盖了以下问题:

// --------------------- 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.
    }
}

Each ViewPart (and editor part) has a separate selection maintained by the selection service and set by your selection providers. 每个ViewPart (和编辑器部件)都有一个由选择服务维护并由选择提供者设置的单独选择。 The enabledWhen and visibleWhen use the selection for the currently active part obtained from the selection service for the part. enabledWhenvisibleWhen使用从零件选择服务获得的当前活动零件的选择。

The property test calls are usually inside a <with> block in the enablement expression which establishes the object that is being tested. 属性测试调用通常位于启用表达式中的<with>块内,该表达式建立要测试的对象。 Something like: 就像是:

<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>

which is working with the current selection, requiring the selection to adapt to a particular type, and testing either of two properties. 它与当前选择配合使用,要求选择适应特定类型,并测试两个属性之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM