首页 文章

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

提问于
浏览
8

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

  • 在资源视图(或包视图)中右键单击某些文件类型(资源)

  • 右键单击文件类型为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>

3 回答

  • 1

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

    然后在你的可见时将 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>
    
  • 1

    你可以实现自己的PropertyTester .

  • 9

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

    • <iterate> 块中添加 <or>

    • 将现有的 <adapt> 块放在新的 <or> 块中

    • 添加一个名为 activeEditorInput 的新 with 变量

    这是新的代码示例 .

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

相关问题