Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a toggle button's selection state in a view's toolbarin an eclipse rcp

I contributed a toggle-style toolbar contribution to my view in my RCP. Now, i want to know how to set the button's state (since it's a toggle button) from my view. Or, at least, how to initialize it's state after the view is loaded (the toggle state can vary, its not static)

I tried to call from my view: getViewSite().getActionBars().getMenuManager().getItems() (returns an array of IContributionElements), which i iterated over and looked for the id. but the array only contains the models of the buttons, and there is no possibility to change the selection through these objects.

Help!!

like image 908
simlei Avatar asked Dec 29 '22 05:12

simlei


1 Answers

In the definition of your command (in plug-in.xml) that the CommandContributionItem calls into, define a state element like the following:

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

The above will initialise the state (toggle on/off) to either true/false depending on what you specify after the 'RegistryToggleState:' section.

To change the state within your code, first get the reference to your ParamterizedCommand like you did before. Then get the reference to the underyling Commandobject from the ParamaterizedCommmand and call:

 HandlerUtil.toggleCommandState(command);
like image 92
tbone Avatar answered Jan 06 '23 14:01

tbone