Search code examples
javaeclipseeclipse-plugineclipse-rcpeclipse-pde

How to programmatically set an initial state for an Eclipse UI command?


I am extending org.eclipse.ui.commands to provide a new command to my menuContribution which has a toggle style.

Now I want to set an initial state, but it should not be a hard-coded state. Consequently, providing the following state is not an option:

  <state
         class="org.eclipse.ui.handlers.RegistryToggleState:true"
         id="org.eclipse.ui.commands.toggleState">
   </state>

The state should reflect a property in the preference page, there's no need to persist anything. Setting the state manually by invoking the methods in the plugin Activator does not work, because I will jump into the start method as soon as I click on the command for the first time, but that's too late since the context menu should already reflect the correct state of the preference property.

How can I solve this issue?


Solution

  • I think that you should implement your own State class which shall extend org.eclipse.jface.commands.ToggleState and override setState method. It can take any value, but ignore it and do what you want (read preferences etc.)

    I have not tried to do it, just guessing.