日食菜单中的'Visiblewhen'.希望菜单仅针对特定文件扩展名显示

Joe*_*Joe 3 xml eclipse eclipse-plugin

编辑重述问题和我需要的地方:

我现在把问题简化为一个非常小的例子:我有一个带菜单的eclipse插件.它看起来像这样:

在此输入图像描述

我希望该菜单仅在查看特定文件扩展名的文件时出现(假设本例中为.txt).

使用Greg的答案我有以下plugin.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            locationURI="menu:org.eclipse.ui.main.menu?after=additions">
         <menu
               id="ARTful.menus.sampleMenu"
               label="Hide Me"
               mnemonic="M">
            <command
                  commandId="ArtEditor.command.format"
                  id="ARTful.menus.sampleCommand"
                  mnemonic="S"
                  tooltip="Hello!">
            </command>

             <visibleWhen
       checkEnabled="false">
   <with variable="selection">
      <iterate
           ifEmpty="false">
         <adapt type="org.eclipse.core.resources.IResource">
              <test property="org.eclipse.core.resources.extension" value="txt" />
         </adapt>
      </iterate>
   </with>
</visibleWhen>
</menu>
      </menuContribution>
   </extension>

</plugin>
Run Code Online (Sandbox Code Playgroud)

使用此设置:

在此输入图像描述

但遗憾的是,这会隐藏任何和所有文件扩展名的菜单.我究竟做错了什么?

原始问题如下

我已经尝试了在visibleWhen中找到的解决方案,以便命令出现在上下文菜单和其他几个地方.

我有一个带菜单的eclipse插件.它看起来像这样:

在此输入图像描述

我希望该菜单仅在查看特定文件扩展名的文件时显示(它被称为'source',因此如果查看带有插件安装的说java文件,则会突然出现两个'源'菜单,这简直无益).

我正在使用'visibleWhen'结构.

我试过测试扩展属性:

在此输入图像描述

(导致这个plugin.xml片段)

<menuContribution
            locationURI="menu:org.eclipse.ui.main.menu?after=additions">
         <menu
               id="ARTful.menus.sampleMenu"
               label="Source"
               mnemonic="M">
            <command
                  commandId="ArtEditor.command.format"
                  id="ARTful.menus.sampleCommand"
                  mnemonic="S"
                  tooltip="Format">
            </command>
            <command
                  commandId="ArtEditor.command.latex"
                  style="push"
                  tooltip="LaTex Output">
            </command>
            <command
                  commandId="ArtEditor.command.format.alpha"
                  style="push">
            </command>
            <visibleWhen
                  checkEnabled="false">
               <test
                     property="org.eclipse.core.resources.extension"
                     value="art">
               </test>
            </visibleWhen>


             </menu>

      </menuContribution>
Run Code Online (Sandbox Code Playgroud)

但即使我希望可见,菜单也完全隐藏了.我也尝试过测试名称属性......

在此输入图像描述

这使:

  <visibleWhen
          checkEnabled="false">
       <test
             property="org.eclipse.core.resources.name"
             value="*.art">
       </test>
    </visibleWhen>
Run Code Online (Sandbox Code Playgroud)

但仍然隐藏着.我错过了什么?

gre*_*449 5

你需要使用类似的东西:

<visibleWhen
       checkEnabled="false">
   <with variable="activeMenuSelection">
      <iterate
           ifEmpty="false">
         <adapt type="org.eclipse.core.resources.IResource">
              <test property="org.eclipse.core.resources.extension" value="art" />
         </adapt>
      </iterate>
   </with>
</visibleWhen>
Run Code Online (Sandbox Code Playgroud)

with你与活跃上下文菜单中选择工作的规定,因为它是默认的,这不是绝对必要的.对于主菜单,有值应该是selection.

当前选择是一个列表,因此您需要使用iterate它来逐步完成它.

视图中显示的对象(如Package或Project explorer)实际上不是文件,而是一些表示文件的用户界面对象.您需要使用adapt调用用户界面对象上的适配器管理器来获取所需的对象.我在IResource这里使用过,因为适配器IFile不太常见.

如果content type为文件类型定义a ,则可以使用以下内容:

<test property="org.eclipse.core.resources.contentTypeId" value="org.eclipse.jdt.core.javaSource" />
Run Code Online (Sandbox Code Playgroud)

为了测试.这比依赖文件扩展名更灵活.我的示例中显示的值是针对Java源文件的.

使用以下内容显示特定编辑器处于活动状态时的菜单:

<with
    variable="activeEditorId">
    <equals
         value="org.eclipse.ant.ui.internal.editor.AntEditor">
   </equals>
</with>
Run Code Online (Sandbox Code Playgroud)

哪个测试Ant编辑器.