【问题标题】:'Visiblewhen' in eclipse menu. Want menu to only appear for particular file extensionseclipse 菜单中的“Visiblewhen”。希望菜单仅针对特定文件扩展名出现
【发布时间】:2014-07-21 14:01:40
【问题描述】:

编辑重述问题以及我要去哪里:

我现在已将问题简化为一个非常小的示例:我有一个带有菜单的 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>

使用此设置:

但不幸的是,这隐藏了所有文件扩展名的菜单。我究竟做错了什么?

原始问题如下

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

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

我希望该菜单仅在查看特定文件扩展名的文件时出现(它称为“源”,因此如果查看带有插件安装的说 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>

但菜单完全隐藏,即使我想显示。我也试过测试 name 属性...

这给出了:

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

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

【问题讨论】:

    标签: xml eclipse eclipse-plugin


    【解决方案1】:

    你需要使用类似的东西:

    <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 来逐步完成它。

    在包或项目浏览器等视图中显示的对象实际上不是文件,而是代表文件的一些用户界面对象。您需要使用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 编辑器。

    【讨论】:

    • 所以我从你的解释中学到了很多东西(因此投了赞成票),但不幸的是,这仍然在 all 情况下隐藏了菜单......我会多一点一出戏(尽管有关调试的建议会有所帮助...我很确定我不能在 xml 中放置断点...
    • 我在发布之前对此进行了测试,它对我有用。您正在使用什么视图(包资源管理器、项目资源管理器......)?
    • 啊 - 可能有些混乱(我的错,我应该更清楚,我链接的问题没有帮助)我正在查看主菜单栏菜单而不是上下文菜单 -这有什么区别吗? (我已将问题编辑得更清楚)?
    • 对于主菜单,with 变量应该是 selection
    • 不幸的是,我仍在苦苦挣扎 - 我已经尽可能减少了我的示例,我仍然可以看到该行为,并且我已经将更新后的 plugin.xml 发布到问题中,希望让事情变得更容易...... :(
    猜你喜欢
    • 1970-01-01
    • 2011-08-01
    • 2019-12-01
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多