<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>EclipseSource Blog &#187; state</title>
	<atom:link href="http://eclipsesource.com/blogs/tag/state/feed/" rel="self" type="application/rss+xml" />
	<link>http://eclipsesource.com/blogs</link>
	<description>Eclipse Equinox OSGi</description>
	<lastBuildDate>Fri, 18 May 2012 15:00:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Toggling a Command contribution</title>
		<link>http://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/</link>
		<comments>http://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 09:40:16 +0000</pubDate>
		<dc:creator>Moritz Post</dc:creator>
				<category><![CDATA[eclipse]]></category>
		<category><![CDATA[syndicate]]></category>
		<category><![CDATA[commands]]></category>
		<category><![CDATA[state]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[toggle]]></category>

		<guid isPermaLink="false">http://eclipsesource.com/blogs/?p=212</guid>
		<description><![CDATA[Every once in a while something just doesn&#8217;t happen to be as intiutive as you would have liked it to be. Lately I was trying to contribute a simple command based toggle button to the workbench. Although it is simple to actually provide the menu contribution and to put the button in visual &#8220;toggle&#8221; mode, [...]]]></description>
			<content:encoded><![CDATA[<p>Every once in a while something just doesn&#8217;t happen to be as intiutive as you would have liked it to be. Lately I was trying to contribute a simple command based toggle button to the workbench. Although it is simple to actually provide the menu contribution and to put the button in visual &#8220;toggle&#8221; mode, it was so straight forward to actually obtain the state of the button in the UI.</p>
<p>To make the menu contribution show up as a toggleable button you have to provide the <em>style</em> value &#8220;toggle&#8221; on your commands menu contribution:</p>
<pre class="brush: java">&lt;extension point=&quot;org.eclipse.ui.menus&quot;&gt;
  &lt;menuContribution locationURI=&quot;...&quot;&gt;
    &lt;command commandId=&quot;org.eclipse.example.command.toggle&quot;
              style=&quot;toggle&quot; /&gt;
  &lt;/menuContribution&gt;
&lt;/extension&gt;</pre>
<p>Next we have too keep track of our actual toggle state. Since it is possible to have multiple menu contributions for the same command, we have to keep track of the state in a central place. Imagine a toggle button triggerable from the main menu and a views toolbar. The state of these buttons are keept in sync by storing the state directly in the command.  The key to this is the <em>org.eclipse.jface.commands.ToggleState</em>. This implementation of <em>org.eclipse.core.commands.State</em> is a wrapper for a <em>boolean</em>. To attach such a <em>State </em>object to a command, it is provided during the command declaration:</p>
<pre class="brush: java">&lt;command id=&quot;org.eclipse.example.command.toggle&quot;
         name=&quot;Toggle Me&quot;&gt;
  &lt;state class=&quot;org.eclipse.jface.commands.ToggleState&quot;
         id=&quot;org.eclipse.example.command.toggleState&quot; /&gt;
&lt;/command&gt;</pre>
<p>The <em>state</em> element takes the <em>ToggleState</em> class as a state provider and attaches itself to the command. The id of the state has to be unique to identfy the state. With the state attached to our command, how can we access it? In the <em>Handler</em>, reacting to the invocation of the menu contribution, we have to access the current state keept in the ToggleState. The following code demonstrates just that:</p>
<pre class="brush: java">ICommandService service =
(ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
Command command = service.getCommand(&quot;org.eclipse.example.command.toggle&quot;);
State state = command.getState(&quot;org.eclipse.example.command.toggleState&quot;);
state.setValue(!(Boolean) state.getValue());</pre>
<p>We obtain our command from the <em>ICommandService </em>and ask it for our toggleState stored within the command. Next we just flip the boolean as to create the new state. Now we can just do whatever we want to do in our <em>Handler</em> using the new state. Great, isn&#8217;t it? Of course the <em>ToggleState</em> is only one possible implementation of <em>State</em>. One could also imagine multiple states, radio states, text states etc. Also the <em>State</em> class has a specialization (<em>PersistableState</em>), which can be persisted to the preferences as to keep track of the (toggle) states of your buttons.</p>
<p>But there is thing left to do: what happens to this other button which is also a menu contribution for our command? We need to toggle the pressed state of it, as to reflect the state of the first button. To do so, a <em>Handler</em> has to implement <em>IElementUpdater </em>providing the method updateElements(&#8230;). It gets an UIElement as a parementer, which can be used to trigger the toggle state:</p>
<pre class="brush: java">public void updateElement(UIElement element, Map paramters) {
  element.setChecked(isSelected);
}</pre>
<p>To broadcast the refresh event to all menu contributions we use the <em>ICommandService</em> in our Handlers:</p>
<pre class="brush: java">commandService.refreshElements(executionEvent.getCommand().getId(), null);</pre>
<p>I hope you find the information here valuable and i am looking forward to your comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsesource.com/blogs/2009/01/15/toggling-a-command-contribution/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>

