<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Toggling a Command contribution</title>
	<atom:link href="http://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/feed/" rel="self" type="application/rss+xml" />
	<link>http://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/</link>
	<description>Eclipse Equinox OSGi</description>
	<lastBuildDate>Fri, 03 Feb 2012 14:17:31 +0100</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
	<item>
		<title>By: Parvez Ahmad E4</title>
		<link>http://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/comment-page-1/#comment-10850</link>
		<dc:creator>Parvez Ahmad E4</dc:creator>
		<pubDate>Sat, 29 Oct 2011 12:27:06 +0000</pubDate>
		<guid isPermaLink="false">http://eclipsesource.com/blogs/?p=212#comment-10850</guid>
		<description>How to do the same thing in Eclipse E4
Parvez Ahmad Hakim
Srinagar Kashmir
www.abobjects.com
I tried below but it did&#039;nt worked
 Command command2 = commandService.getCommand(PasteHandler.commandId);
 CopyHandler ah = (CopyHandler) command2.getHandler();
h.canExecute();</description>
		<content:encoded><![CDATA[<p>How to do the same thing in Eclipse E4<br />
Parvez Ahmad Hakim<br />
Srinagar Kashmir<br />
<a href="http://www.abobjects.com" rel="nofollow">http://www.abobjects.com</a><br />
I tried below but it did&#8217;nt worked<br />
 Command command2 = commandService.getCommand(PasteHandler.commandId);<br />
 CopyHandler ah = (CopyHandler) command2.getHandler();<br />
h.canExecute();</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ruslan</title>
		<link>http://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/comment-page-1/#comment-10841</link>
		<dc:creator>Ruslan</dc:creator>
		<pubDate>Thu, 27 Oct 2011 20:23:22 +0000</pubDate>
		<guid isPermaLink="false">http://eclipsesource.com/blogs/?p=212#comment-10841</guid>
		<description>Hello,

Can anyone tell, why it is so extremely complicated just to change state of the button and update others?
Why this simple thing can be an issue?</description>
		<content:encoded><![CDATA[<p>Hello,</p>
<p>Can anyone tell, why it is so extremely complicated just to change state of the button and update others?<br />
Why this simple thing can be an issue?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: eclipser</title>
		<link>http://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/comment-page-1/#comment-2786</link>
		<dc:creator>eclipser</dc:creator>
		<pubDate>Thu, 10 Sep 2009 13:34:40 +0000</pubDate>
		<guid isPermaLink="false">http://eclipsesource.com/blogs/?p=212#comment-2786</guid>
		<description>Thanks for good explanation, helped a lot.</description>
		<content:encoded><![CDATA[<p>Thanks for good explanation, helped a lot.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: TatsAush</title>
		<link>http://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/comment-page-1/#comment-1120</link>
		<dc:creator>TatsAush</dc:creator>
		<pubDate>Fri, 03 Apr 2009 22:59:26 +0000</pubDate>
		<guid isPermaLink="false">http://eclipsesource.com/blogs/?p=212#comment-1120</guid>
		<description>??... +1</description>
		<content:encoded><![CDATA[<p>??&#8230; +1</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew Eidsness</title>
		<link>http://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/comment-page-1/#comment-668</link>
		<dc:creator>Andrew Eidsness</dc:creator>
		<pubDate>Fri, 06 Mar 2009 19:08:28 +0000</pubDate>
		<guid isPermaLink="false">http://eclipsesource.com/blogs/?p=212#comment-668</guid>
		<description>Good post.  I did some digging to figure out how to synchronize the initial state of the widget with the initial value of the State.

There are a few places in the Eclipse code with comments about the state attribute and the IMenuStateIds.STYLE.  From what I can tell these are old references.  This is how things were done in the older IAction world, but it is no longer applicable in the new, declarative world.  For more information see eclipse bug 154130 (esp. the part after hahaha in https://bugs.eclipse.org/bugs/show_bug.cgi?id=154130#c27).

The bottom line seems to be that the command state is not linked (and is not planned to be linked) to the UI widget&#039;s state.

Comment 31 (in the same bug) has a pointer to the real solution.  In short, make your Handler implement IElementUpdater and then provide an implementation of updateElement that calls #setChecked on the argument uiElement.

A pretty simple and clean solution.  Unfortunately took me a few hours to find, hopefully this note will save other people some time.</description>
		<content:encoded><![CDATA[<p>Good post.  I did some digging to figure out how to synchronize the initial state of the widget with the initial value of the State.</p>
<p>There are a few places in the Eclipse code with comments about the state attribute and the IMenuStateIds.STYLE.  From what I can tell these are old references.  This is how things were done in the older IAction world, but it is no longer applicable in the new, declarative world.  For more information see eclipse bug 154130 (esp. the part after hahaha in <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=154130#c27" rel="nofollow">https://bugs.eclipse.org/bugs/show_bug.cgi?id=154130#c27</a>).</p>
<p>The bottom line seems to be that the command state is not linked (and is not planned to be linked) to the UI widget&#8217;s state.</p>
<p>Comment 31 (in the same bug) has a pointer to the real solution.  In short, make your Handler implement IElementUpdater and then provide an implementation of updateElement that calls #setChecked on the argument uiElement.</p>
<p>A pretty simple and clean solution.  Unfortunately took me a few hours to find, hopefully this note will save other people some time.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Pérez</title>
		<link>http://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/comment-page-1/#comment-336</link>
		<dc:creator>David Pérez</dc:creator>
		<pubDate>Fri, 30 Jan 2009 14:47:48 +0000</pubDate>
		<guid isPermaLink="false">http://eclipsesource.com/blogs/?p=212#comment-336</guid>
		<description>Ralf Ebert code works only partially....

Suppose you have 2 editor instances, and each one has a handler for the same command, but the checked state differs in each one.  Switching the active editor doesn&#039;t update the check state.

It looks like that updateElement() and refreshElements() do nothing.</description>
		<content:encoded><![CDATA[<p>Ralf Ebert code works only partially&#8230;.</p>
<p>Suppose you have 2 editor instances, and each one has a handler for the same command, but the checked state differs in each one.  Switching the active editor doesn&#8217;t update the check state.</p>
<p>It looks like that updateElement() and refreshElements() do nothing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ralf Ebert</title>
		<link>http://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/comment-page-1/#comment-258</link>
		<dc:creator>Ralf Ebert</dc:creator>
		<pubDate>Wed, 21 Jan 2009 22:59:05 +0000</pubDate>
		<guid isPermaLink="false">http://eclipsesource.com/blogs/?p=212#comment-258</guid>
		<description>http://www.ralfebert.de/eclipse/2009_01_21_togglehandler/</description>
		<content:encoded><![CDATA[<p><a href="http://www.ralfebert.de/eclipse/2009_01_21_togglehandler/" rel="nofollow">http://www.ralfebert.de/eclipse/2009_01_21_togglehandler/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ralf Ebert</title>
		<link>http://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/comment-page-1/#comment-257</link>
		<dc:creator>Ralf Ebert</dc:creator>
		<pubDate>Wed, 21 Jan 2009 22:29:57 +0000</pubDate>
		<guid isPermaLink="false">http://eclipsesource.com/blogs/?p=212#comment-257</guid>
		<description>Hmm, there seems to be no good way to do this. Using the parts of your post I built a base Handler class hiding all the details so this can be easily switched for some other, cleaner solution. Works only if the initial state is unchecked though :)



package de.ralfebert.rcputil;

import java.util.Map;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.State;
import org.eclipse.jface.menus.IMenuStateIds;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.commands.IElementUpdater;
import org.eclipse.ui.menus.UIElement;

/**
 * Use this handler for style=&quot;toggle&quot; command contributions. You need to
 * declare a state like this to use this:
 * 
 * 
 * 
 * 	 
 * 
 * 
 * 
 * The id=&quot;STYLE&quot; is chosen because of IMenuStateIds.STYLE - maybe this will
 * work without any Handler Foo in later Eclipse versions.
 * 
 * @author Ralf Ebert
 */
public abstract class ToggleHandler extends AbstractHandler implements IElementUpdater {

	private String commandId;

	public final Object execute(ExecutionEvent event) throws ExecutionException {
		ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
		this.commandId = event.getCommand().getId();

		State state = event.getCommand().getState(IMenuStateIds.STYLE);
		if (state == null)
			throw new ExecutionException(
					&quot;You need to declare a ToggleState with id=STYLE for your command to use ToggleHandler!&quot;);
		boolean currentState = (Boolean) state.getValue();
		boolean newState = !currentState;
		state.setValue(newState);
		executeToggle(event, newState);
		commandService.refreshElements(event.getCommand().getId(), null);

		// return value is reserved for future apis
		return null;
	}

	protected abstract void executeToggle(ExecutionEvent event, boolean newState);

	public void updateElement(UIElement element, Map parameters) {
		if (this.commandId != null) {
			ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(
					ICommandService.class);
			Command command = commandService.getCommand(commandId);
			element.setChecked((Boolean) command.getState(IMenuStateIds.STYLE).getValue());
		}
	}

}</description>
		<content:encoded><![CDATA[<p>Hmm, there seems to be no good way to do this. Using the parts of your post I built a base Handler class hiding all the details so this can be easily switched for some other, cleaner solution. Works only if the initial state is unchecked though <img src='http://eclipsesource.com/blogs/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>package de.ralfebert.rcputil;</p>
<p>import java.util.Map;</p>
<p>import org.eclipse.core.commands.AbstractHandler;<br />
import org.eclipse.core.commands.Command;<br />
import org.eclipse.core.commands.ExecutionEvent;<br />
import org.eclipse.core.commands.ExecutionException;<br />
import org.eclipse.core.commands.State;<br />
import org.eclipse.jface.menus.IMenuStateIds;<br />
import org.eclipse.ui.PlatformUI;<br />
import org.eclipse.ui.commands.ICommandService;<br />
import org.eclipse.ui.commands.IElementUpdater;<br />
import org.eclipse.ui.menus.UIElement;</p>
<p>/**<br />
 * Use this handler for style=&#8221;toggle&#8221; command contributions. You need to<br />
 * declare a state like this to use this:<br />
 *<br />
 *<br />
 *<br />
 *<br />
 *<br />
 *<br />
 *<br />
 * The id=&#8221;STYLE&#8221; is chosen because of IMenuStateIds.STYLE &#8211; maybe this will<br />
 * work without any Handler Foo in later Eclipse versions.<br />
 *<br />
 * @author Ralf Ebert<br />
 */<br />
public abstract class ToggleHandler extends AbstractHandler implements IElementUpdater {</p>
<p>	private String commandId;</p>
<p>	public final Object execute(ExecutionEvent event) throws ExecutionException {<br />
		ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);<br />
		this.commandId = event.getCommand().getId();</p>
<p>		State state = event.getCommand().getState(IMenuStateIds.STYLE);<br />
		if (state == null)<br />
			throw new ExecutionException(<br />
					&#8220;You need to declare a ToggleState with id=STYLE for your command to use ToggleHandler!&#8221;);<br />
		boolean currentState = (Boolean) state.getValue();<br />
		boolean newState = !currentState;<br />
		state.setValue(newState);<br />
		executeToggle(event, newState);<br />
		commandService.refreshElements(event.getCommand().getId(), null);</p>
<p>		// return value is reserved for future apis<br />
		return null;<br />
	}</p>
<p>	protected abstract void executeToggle(ExecutionEvent event, boolean newState);</p>
<p>	public void updateElement(UIElement element, Map parameters) {<br />
		if (this.commandId != null) {<br />
			ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(<br />
					ICommandService.class);<br />
			Command command = commandService.getCommand(commandId);<br />
			element.setChecked((Boolean) command.getState(IMenuStateIds.STYLE).getValue());<br />
		}<br />
	}</p>
<p>}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ralf Ebert</title>
		<link>http://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/comment-page-1/#comment-255</link>
		<dc:creator>Ralf Ebert</dc:creator>
		<pubDate>Wed, 21 Jan 2009 21:56:41 +0000</pubDate>
		<guid isPermaLink="false">http://eclipsesource.com/blogs/?p=212#comment-255</guid>
		<description>Hi Moritz,

it seems that the ToggleState does NOT track the state of the &quot;of the button in the UI&quot;. At least it is never updated by itself. In your example you set the state yourself by toggling the state in parallel:

state.setValue(!(Boolean) state.getValue());

But this is completely unrelated to the actual &quot;toggle&quot; state of the command, isn&#039;t it?

I found &quot;IMenuStateIds.STYLE&quot;, and according to the javadocs, you should get the widget state by naming the state &quot;STYLE&quot;. But this does not work as described.

Ralf</description>
		<content:encoded><![CDATA[<p>Hi Moritz,</p>
<p>it seems that the ToggleState does NOT track the state of the &#8220;of the button in the UI&#8221;. At least it is never updated by itself. In your example you set the state yourself by toggling the state in parallel:</p>
<p>state.setValue(!(Boolean) state.getValue());</p>
<p>But this is completely unrelated to the actual &#8220;toggle&#8221; state of the command, isn&#8217;t it?</p>
<p>I found &#8220;IMenuStateIds.STYLE&#8221;, and according to the javadocs, you should get the widget state by naming the state &#8220;STYLE&#8221;. But this does not work as described.</p>
<p>Ralf</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Moritz Post</title>
		<link>http://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/comment-page-1/#comment-242</link>
		<dc:creator>Moritz Post</dc:creator>
		<pubDate>Tue, 20 Jan 2009 16:01:12 +0000</pubDate>
		<guid isPermaLink="false">http://eclipsesource.com/blogs/?p=212#comment-242</guid>
		<description>Hi Craig

Good to see that you have also brought that question &lt;a href=&quot;http://www.eclipse.org/newsportal/article.php?id=943&amp;group=eclipse.platform.pde&quot; rel=&quot;nofollow&quot;&gt;over to the newsgroup&lt;/a&gt;. I have also heard of the &quot;:true&quot; flag on the command id but as for you, that didn&#039;t work for me. As in the newsgroup post, i&#039;ll also link to the relevant bug &lt;a href=&quot;https://bugs.eclipse.org/bugs/show_bug.cgi?id=221980&quot; rel=&quot;nofollow&quot;&gt;here&lt;/a&gt;.</description>
		<content:encoded><![CDATA[<p>Hi Craig</p>
<p>Good to see that you have also brought that question <a href="http://www.eclipse.org/newsportal/article.php?id=943&amp;group=eclipse.platform.pde" rel="nofollow">over to the newsgroup</a>. I have also heard of the &#8220;:true&#8221; flag on the command id but as for you, that didn&#8217;t work for me. As in the newsgroup post, i&#8217;ll also link to the relevant bug <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=221980" rel="nofollow">here</a>.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

