visibleWhen命令显示在上下文菜单中

bli*_*ool 8 eclipse eclipse-plugin

我正在尝试使用menuContribution中的'visibleWhen'表达式在上下文菜单中配置命令的可见性.我想要做的是只有在以下情况下才能在上下文菜单中显示命令:

  1. 在资源视图(或包视图)中右键单击某些文件类型(资源)
  2. 右键单击文件类型为open的相应编辑器.它可以检测到我的编辑器是打开的还是编辑器打开了某个资源.

我已经完成了第一次使用'visibleWhen'>'selection(with)'>'iterate'>'org.eclipse.core.resources.IResource(adapt)'然后检查资源的文件扩展名.代码如下所示.但是,我不确定如何只在右键单击正确的编辑器时才能显示相同的命令,该编辑器打开了具有正确扩展名的文件 - ext1,ext2.

检查我的编辑器是否处于活动状态可以解决第二个问题但似乎没有帮助,因为如果我点击不是我的类型的文件,它仍然会在上下文菜单中显示该命令.

有什么建议?"Eclipse插件(第3版)"显示了编辑器上下文菜单的一些示例,但它使用了操作,我想坚持使用命令.

谢谢!!


  <menuContribution
        allPopups="false"
        locationURI="popup:org.eclipse.ui.popup.any?before=additions">
     <separator
           name="com.test.ide.separator1"
           visible="true">
     </separator>
     <menu
           icon="icons/sample.gif"
           label="Test Menu">
        <command
              commandId="com.test.commands.testCommand1"
              icon="icons/sample.gif"
              label="testCommand1"
              style="push"
              tooltip="This is a test command">
           <visibleWhen
                 checkEnabled="false">
              <with
                    variable="selection">
                 <iterate
                       ifEmpty="false"
                       operator="or">
                    <adapt
                          type="org.eclipse.core.resources.IResource">
                       <or>
                          <test
                                property="org.eclipse.core.resources.extension"
                                value="ext1">
                          </test>
                          <test
                                property="org.eclipse.core.resources.extension"
                                value="ext2">
                          </test>
                       </or>
                    </adapt>
                 </iterate>
              </with>
           </visibleWhen>
        </command>
     </menu>
  </menuContribution>
Run Code Online (Sandbox Code Playgroud)

Pau*_*ter 9

@blissfool,我建议稍微改组一下.您可以将基本测试(这是正确的)放在一个org.eclipse.core.expressions.definitions块中:

<extension point="org.eclipse.core.expressions.definitions">
   <definition id="org.eclipse.example.testExtension">
      <adapt type="org.eclipse.core.resources.IResource">
         <or>
             <test property="org.eclipse.core.resources.extension"
                   value="ext1">
             </test>
             <test property="org.eclipse.core.resources.extension"
                   value="ext2">
             </test>
         </or>
      </adapt>
   </definition>
</extension>
Run Code Online (Sandbox Code Playgroud)

然后在你的可见时将activeEditorInput测试移到顶部:

<visibleWhen>
   <or>
      <with variable="selection">
         <iterate ifEmtpy="false">
           <reference definitionId="org.eclipse.example.testExtension"/>
         </iterate>
      </with>
      <with variable="activeEditorInput">
        <reference definitionId="org.eclipse.example.testExtension"/>
      </with>
   </or>
</visibleWhen>
Run Code Online (Sandbox Code Playgroud)


bli*_*ool 1

我能够用with我遇到的一个变量来完成它。使用上面相同的代码示例:

  • <or><iterate>块内添加块
  • 将现有<adapt>块放入新<or>块中
  • 添加一个名为的新with变量activeEditorInput

这是新的代码示例。

<iterate ifEmpty="false" operator="or">
  <or>
    <adapt type="org.eclipse.core.resources.IResource">
      <or>
        ...test extensions
      </or>
    </adapt>
    <with variable="activeEditorInput">
      <adapt type="org.eclipse.core.resources.IResource">
        <or>
          ...test extensions
        </or>
      </adapt>
    </with>
  </or>
</iterate>
Run Code Online (Sandbox Code Playgroud)