Search code examples
javaeclipse-rcprcp

RCP visiblewhen programmatically (Java)


I have a command into the plugin.xml which will add a new menu button. This button should not be visible all the time, hence I would like to check a complex condition from Java code to decide when it have to be visible.

I know that there is a visiblewhen and a hidewhen possibility, but I don't know how can let a Java class/method to make the decision.


Solution

  • For this check the enabled state of the command is used, which is determined by the return value of IHandler.isEnabled().

    In the plugin.xml the contribution of the command to the menu has to have the visibleWhen element and checkEnabled="true". In Eclipse you can right click the command contribution and add visible when, in the plugin.xml it looks like this:

         <command
               commandId="...">
            <visibleWhen
                  checkEnabled="true">
            </visibleWhen>
         </command>
    

    To enable/disable the command you have to implement the isEnabled() method from org.eclipse.core.commands.IHandler (or override from AbstractHandler) in your command handler and return false, if the menu entry should be hidden.