基于选择的Eclipse RCP处理程序启用

时间:2014-01-09 18:53:01

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

我有点卡住了,因为资源很多,他们不会为我解释这个问题。

假设我有一个命令,一个处理程序,一个属性测试器,并且这些结果显示在UI上作为酷吧项目

现在,假设我有几个视图扩展相同的基础(例如BaseView)。所有这些视图都包含ColumnViewer s(例如TableViewerTreeViewer),其作为选择提供商

  • enableWhenactiveWhen配置如何了解这些观看者的选择?我无法想象selection + instanceOf参数如何适用于ISelection
  • 如何将所选对象传递给属性测试器? test方法接收什么实例(作为receiver)?
  • 我注意到有一个断点,通过处理程序的setEnabled()方法有很多次传递。这是正常的行为吗?是否可以覆盖setEnabled

代码在这里似乎与我无关。但无论如何,这些片段涵盖了这些问题:

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

1 个答案:

答案 0 :(得分:1)

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

属性测试调用通常位于enablement表达式中的<with>块内,该块用于建立正在测试的对象。类似的东西:

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

正在使用当前选择,要求选择适应特定类型,并测试两个属性中的任何一个。