views:

29

answers:

2

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

A: 

You can cast the item to ActionContributionItem, get the Action and call setChecked():

((ActionContributionItem) item).getAction().setChecked(true);
Fabian Steeg
My item is of the type CommandContributionItem. I can call getCommand() instead of getAction() (returning a ParameterizedCommand type), but then there are no methods that help me further
simlei
@simlei Ah, sorry - I'm using Actions in my toolbar, don't know how to do it with Commands
Fabian Steeg
A: 

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);
tbone
Thank you, this pinpointed everything. Really precise and sharp, thanks again!
simlei
Addition: if anyone is wondering if its just possible to toggle the state of the menu item -- just look at the code in the HandlerUtil toggle method and adapt it.
simlei