How to programmatically toggle state of command [message #492630] |
Wed, 21 October 2009 03:45  |
Eclipse User |
|
|
|
I have placed a command with style "toggle" on a view's toolbar as :
<command
id="myorg.plugin.bottomView.toggle.command"
name="Toggle">
<state
class="org.eclipse.jface.commands.ToggleState"
id="myorg.plugin.bottomView.toggleState">
</state>
</command>
In the handler on toggle command-click I get to know what's the current state and set the new state as :
State state = event.getCommand().getState ("myorg.plugin.bottomView.toggleState");
boolean currentState = (Boolean)state.getValue();
boolean newState = !currentState;
state.setValue(newState);
Further, based on the value of newState I take respective action.
Elsewhere in the plug-in on occurrence of certain event I need to toggle this command to false (off) if it is on. So I did the following :
// if toggle button is on make it off.
ICommandService commandService =
(ICommandService) PlatformUI.getWorkbench()
.getService(ICommandService.class);
Command toggleCommand = commandService.getCommand(
"myorg.plugin.bottomView.toggle.command");
State state = toggleCommand.getState
("myorg.plugin.bottomView.toggleState");
boolean currentState = (Boolean)state.getValue();
if(currentState) {
// turn it off
state.setValue(!currentState);
commandService.refreshElements(toggleCommand.getId(), null);
}
But the toggle command's state doesn't change to off (false) on the occurrence of certain event where this code has been added. It remains on (true).
What could be wrong here? Or the approach itself is flawed?
Please guide me to programmatically turn off the toggle command's state from a class in plug-in that is not a handler.
Thanks.
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.24801 seconds