Tuesday, February 1, 2011

The curious case of VisibleWhen

Almost every Eclipse developer has used it, but I have seen many cases, where the VisibleWhen tag is just copied. It is used to define the visibility of the menu item of your plugin. As an example you could show a specific menu item in the navigator right click menue of a certain type (e.g. a Java File). This post describes a typical example of the VisibleWhen in detail, thanks to Stephan for wrapping thing up.
A very easy way to test the VisibleWhen condition is to register an example command on Java elements. They can be created in the target (the compiled and started Eclipse instance). In this example I register an example command in the right click menue of an ICompilationUnit (Java Class) and an IJavaProject (Java Project). The following screenshots shows these two elements in the target.


The following example shows the menue entry, if one IJavaProject is selected.

      <menucontribution allpopups="false" locationuri="popup:org.eclipse.jdt.ui.PackageExplorer">
         <command commandid="menucontributions.commands.sampleCommand">
            <visiblewhen checkenabled="false">
               <and>
                  <iterate ifempty="false" operator="and">
                     <instanceof value="org.eclipse.jdt.core.IJavaProject">
                     </instanceof>
                  </iterate>
                    <count value="1">
                </count>
               </and>
            </visiblewhen>
         </command>
      </menucontribution>
   </extension>

-By setting the attribute checkEnabled to true the menu will check if the corresponding handler has been enabled; if it has been enabled then it will show the menue entry, otherwise it will not.

- is combining the conditions: In this case the "iterate" condition AND the "count" condition have to be true. Otherwise the item isn't shown.

- iterates over all selected elements and checks the conditions underneath for every element. If you directly right click an item, only one element is selected. If multi-selection is allowed, many elements can be selected on right click. ifEmpty=false means that if nothing is selected, the menu item isn't shown. operator="and" says that the conditions (in this case there is only one instanceof condition, but there can be more) must be true for all selected elements. operator="or" will show the menue entry, if the condition is true for at least one selected element.

-instanceof checks for every selection if the selected element is an instance of the type someClass in this case org.eclipse.jdt.core.IJavaProject.

- With count you can set the number of selected items. If more or less are selected, the menu item isn't shown.
You can also use th following parameters:
* Arbitrary number of items
? Zero or one item
+ One ore more items
! No items

The following example shows the menue entry if exactly two items are selected and both items are either an ICompilationUnit or an IJavaProject:

<visiblewhen checkenabled="true">
               <and>
                  <iterate ifempty="false" operator="and">
                     <or>
                        <instanceof value="org.eclipse.jdt.core.IJavaProject">
                        </instanceof>
                        <instanceof value="org.eclipse.jdt.core.ICompilationUnit">
                        </instanceof>
                     </or>
                  </iterate>
                  <count value="2">
                  </count>
               </and>
            </visiblewhen>

2 Kommentare:

  1. Do you guys ever check your posts for sanity? XML source code isn't visible because it isn't inserted into HTML right. Escape braces with lt/gt substitutes.

    ReplyDelete
  2. Thanks, I wasnt checking on the aggregator, as I use Syntax Highlighter, which parses unescaped xml correctly. I fixed it.

    ReplyDelete