Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » How to programmatically toggle state of command
How to programmatically toggle state of command [message #492630] Wed, 21 October 2009 07:45 Go to next message
Prasanna K is currently offline Prasanna KFriend
Messages: 78
Registered: July 2009
Member
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.
Re: How to programmatically toggle state of command [message #492811 is a reply to message #492630] Wed, 21 October 2009 19:30 Go to previous messageGo to next message
Prakash G.R. is currently offline Prakash G.R.Friend
Messages: 621
Registered: July 2009
Senior Member
kaprasi wrote:
> 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>

Command Framework determines the toggle state & radio state by
predefined ids: You should be using the id:
org.eclipse.ui.commands.toggleState & org.eclipse.ui.commands.radioState

> 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);

HandlerUtil has a helper method for this.

> 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).

It might be because of the wrong state id.

Check :

http://blog.eclipse-tips.com/2009/03/commands-part-6-toggle- radio-menu.html
http://wiki.eclipse.org/Menu_Contributions/Toggle_Button_Com mand
http://wiki.eclipse.org/Menu_Contributions/Radio_Button_Comm and

- Prakash

Platform UI Team, IBM
http://blog.eclipse-tips.com
Re: How to programmatically toggle state of command [message #492894 is a reply to message #492811] Thu, 22 October 2009 09:36 Go to previous message
Prasanna K is currently offline Prasanna KFriend
Messages: 78
Registered: July 2009
Member

Prakash G.R. wrote on Wed, 21 October 2009 15:30
kaprasi wrote:
> 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>

Command Framework determines the toggle state & radio state by
predefined ids: You should be using the id:
org.eclipse.ui.commands.toggleState & org.eclipse.ui.commands.radioState

> 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);

HandlerUtil has a helper method for this.

> 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).

It might be because of the wrong state id.

Check :

http://blog.eclipse-tips.com/2009/03/commands-part-6-toggle- radio-menu.html
http://wiki.eclipse.org/Menu_Contributions/Toggle_Button_Com mand
http://wiki.eclipse.org/Menu_Contributions/Radio_Button_Comm and

- Prakash

Platform UI Team, IBM
http://blog.eclipse-tips.com


Hi Prakash,

I am using eclipse-3.4.2 and HandlerUtil.toggleCommandState() is available only in 3.5 Sad

I did use org.eclipse.ui.commands.toggleState instead of my own state id like myorg.plugin.bottomView.toggleState but the behaviour of toggle button didn't change.
Actually the toggle state gets turned off internally when I call this refresh method but the toggle button doesn't come back up (turn off) visually :
// 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);
		}



Any idea what could be wrong? I really appreciate you helping me out.
Thanks.
Previous Topic:Icons of my markers do not appear on th resources
Next Topic:Installing TPTP freezes --need help!
Goto Forum:
  


Current Time: Tue Mar 19 06:58:57 GMT 2024

Powered by FUDForum. Page generated in 0.01662 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top