'Visiblewhen' in eclipse menu. Want menu to only appear for particular file extensions

eclipse, eclipse-plugin, xml

Solution

You need to use something like:

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

The `with` establishes that you are working with the active context menu selection, this is not strictly necessary as it is the default. For a main menu the with value should be `selection`.

The current selection is a list so you need to use `iterate` to step through it.

The objects shown in views such as the Package or Project explorer are not actually the files but some user interface object representing the files. You need to use `adapt` to invoke the adapter manager on the user interface object to get the object you want. I have used `IResource` here because adapters to `IFile` are less common.

If you define a `content type` for your file type you can use something like:

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

for the test. This is a bit more flexible than relying on the file extension. The value shown in my example is for Java source files.

To show a menu when a particular editor is active using something like:

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

which tests for the Ant editor.

Problem

EDIT restating question and where I've got to: I've now reduced the issue down to a very small example: I have an eclipse plugin with a menu. It looks like this: I'd like that menu to only appear when viewing files of a particular file extension (let's say .txt for this example). Using Greg's answer below I have the following plugin.xml file: ``` <?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> ``` With this setup: But unfortunately this hides the menu for any and all file extensions. What am I doing wrong? ORIGINAL QUESTION FOLLOWS I've tried the solutions found at visibleWhen for command to appear in context menu and in several other places. I have an eclipse plugin with a menu. It looks like this: I'd like that menu to only appear when viewing files of a particular file extension (It's called 'source', and so if looking at a say java file with the plugin install there are suddenly two 'source' menus and that's just plain unhelpful). I'm using the 'visibleWhen' construct. I've tried testing the extension property: (resulting in this plugin.xml fragment) ``` <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> ``` But the menu is entirely hidden, even when I want to be visible. I've also tried testing the name property... Which gives: ``` <visibleWhen checkEnabled="false"> <test property="org.eclipse.core.resources.name" value="*.art"> </test> </visibleWhen> ``` But still hidden. What am I missing?

Original source

Related problems