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

时间:2011-04-08 07:43:52

标签: eclipse eclipse-plugin

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

  1. 在资源视图(或包视图)中右键单击某些文件类型(资源)
  2. 右键单击文件类型为open的相应编辑器。它可以检测到我的编辑器是打开的还是编辑器打开了某个资源。
  3. 我已经完成了第一次使用'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 个答案:

答案 0 :(得分: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>

然后在您的可见内容中将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 :(得分:1)

您可以实施自己的PropertyTester

答案 2 :(得分: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>
相关问题