首页 文章

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

提问于
浏览
3

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

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

enter image description here

我希望该菜单仅在查看特定文件扩展名的文件时出现(假设本例中为.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>

使用此设置:

enter image description here

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

原始问题如下

我已经尝试了在visibleWhen for command to appear in context menu和其他几个地方找到的解决方案 .

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

enter image description here

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

我正在使用'visibleWhen'结构 .

我试过测试扩展属性:

enter image description here

(导致这个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>

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

enter image description here

这使:

<visibleWhen
          checkEnabled="false">
       <test
             property="org.eclipse.core.resources.name"
             value="*.art">
       </test>
    </visibleWhen>

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

1 回答

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

    with 确定您正在使用活动上下文菜单选项,这不是必需的,因为它是默认选项 . 对于主菜单,with值应为 selection .

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

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

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

    <test property="org.eclipse.core.resources.contentTypeId" value="org.eclipse.jdt.core.javaSource" />
    

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

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

    <with
        variable="activeEditorId">
        <equals
             value="org.eclipse.ant.ui.internal.editor.AntEditor">
       </equals>
    </with>
    

    哪个测试Ant编辑器 .

相关问题