【问题标题】:visibleWhen for command to appear in context menuvisibleWhen 命令出现在上下文菜单中
【发布时间】:2011-08-01 07:29:11
【问题描述】:

我正在尝试使用 menuContribution 中的“visibleWhen”表达式在上下文菜单中配置命令的可见性。我要做的是仅在以下情况下使命令在上下文菜单中可见:

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

我已经使用 '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>

【问题讨论】:

    标签: java eclipse eclipse-plugin


    【解决方案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>
    

    【讨论】:

    • 啊。正确的。感谢那。我阅读了 org.eclipse.core.expressions.definitions 并考虑了如何减少重复条件,但没有将两者放在一起。我太专注于让功能发挥作用。干杯~:)
    【解决方案2】:

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

    • 在&lt;iterate&gt; 块内添加&lt;or&gt; 块
    • 将现有的&lt;adapt&gt; 块放入新的&lt;or&gt; 块中
    • 添加一个名为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>
    

    【讨论】:

    • 没错,但我建议稍微重组优化:
    【解决方案3】:

    您可以实现自己的PropertyTester。

    【讨论】:

    • 谢谢。我还不太熟悉自定义 PropertyTester,甚至还不太熟悉插件 API。我确实遇到了另一个帮助我完成这项工作的“with”变量!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多