<?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; datetime</title>
	<atom:link href="http://eclipsesource.com/blogs/tag/datetime/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>Databinding: A Custom Observable for a Widget</title>
		<link>http://eclipsesource.com/blogs/2009/02/03/databinding-a-custom-observable-for-your-widget/</link>
		<comments>http://eclipsesource.com/blogs/2009/02/03/databinding-a-custom-observable-for-your-widget/#comments</comments>
		<pubDate>Tue, 03 Feb 2009 15:41:20 +0000</pubDate>
		<dc:creator>Moritz Post</dc:creator>
				<category><![CDATA[eclipse]]></category>
		<category><![CDATA[syndicate]]></category>
		<category><![CDATA[databinding]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[jface]]></category>
		<category><![CDATA[observable]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://eclipsesource.com/blogs/?p=289</guid>
		<description><![CDATA[The introduction of the databinding framework in Eclipse 3.3 is with no doubt one of the most useful tools in the hands of the form developer. The ability to transform and validate user input in such a flexible and reusable way is a great enhancement. But where there is light, there is shadow. Sometimes there [...]]]></description>
			<content:encoded><![CDATA[<p>The introduction of the <a href="http://wiki.eclipse.org/index.php/JFace_Data_Binding">databinding framework</a> in Eclipse 3.3 is with no doubt one of the most useful tools in the hands of the form developer. The ability to transform and validate user input in such a flexible and reusable way is a great enhancement. But where there is light, there is shadow. Sometimes there is just no <em>IObservable </em>available for your target or model object. This blog entry will demonstrate how easy it can be to create a custom <em>IObservable </em>for a DateTime widget.</p>
<p>The <em>DateTime </em>widget represents one value: a <em>java.util.Date</em>. This <em>Date </em>object is the one we want to get and set on the target (target being the UI widget). Therefore we wrap the <em>DateTime </em>in an <em>IObservableValue </em>by extending the <em>AbstractObservableValue</em> class. Essentially an <em>IObservable*</em> offers methods to get and set data, to determine the type of data and to register listeners to be notified of changes. The following code demonstrates a skeleton implementation of an <em>IObservableValue</em>.</p>
<pre class="brush: java">
public class DateTimeObservableValue extends AbstractObservableValue {

  private final DateTime dateTime;
  Listener listener = new Listener() { ... };

  public DateTimeObservableValue(final DateTime dateTime) {
    this.dateTime = dateTime;
    this.dateTime.addSelectionListener(this.listener);
  }

  @Override
  protected Object doGetValue() {
    // the utility method creates a Date from the DateTime
    return dateTimeToDate();
  }

  @Override
    protected void doSetValue(final Object value) {
      if (value instanceof Date) {
        // the utility method sets the date on the DateTime
        dateToDateTime((Date) value);
      }
  }

  @Override
    public Object getValueType() {
    return Date.class;
  }

  @Override
    public synchronized void dispose() {
    this.dateTime.removeSelectionListener(this.listener);
    super.dispose();
  }
}
</pre>
<p>The implementation details are not very special. The <em>getValueType() </em>method has to return the type represented by this <em>IObservableValue</em> (which is the type <em>Date</em>). The do methods set and get the <em>Date </em>value. Since the observable has to propagate changes in the <em>DateTime</em> widget as soon as they ocurre, we attach a listener on the <em>DateTime </em>widget to inform any registered <em>IValueChangeListener </em>of the event. The listener implementation looks like the following:</p>
<pre class="brush: java">
Listener listener = new Listener() {

  @Override
  public void handleEvent(final Event event) {
    Date newValue = dateTimeToDate();

    if (!newValue.equals(DateTimeObservableValue.this.oldValue)) {
      fireValueChange(Diffs.createValueDiff(DateTimeObservableValue.this.oldValue, newValue));
      DateTimeObservableValue.this.oldValue = newValue;
    }
  }
};
</pre>
<p>In the <em>DateTime</em> listener we inform any interested <em>IValueChangeListener</em> of our <em>DateTimeObservableValue</em>. In order to avoid unnecessary propagation of update events in the databinding context, we compare the last set <em>Date</em> in the <em>IObservableValue</em> with the new value. Next we create a <em>ValueDiff</em> from our new date value and fire the the value change event. The advantage of listening to the changes in the DateTime widget, is that we are able to fire events which are either invoked by the user changing the DateTime widget or by programmatic changes of the <em>IObservableValue</em>s wrapped <em>Date</em>.</p>
<p>You can download the full listing of the observable class here: <a href='http://eclipsesource.com/blogs/2009/02/03/databinding-a-custom-observable-for-your-widget/datetimeobservablevalue/' rel='attachment wp-att-332'>DateTimeObservableValue.zip</a></p>
<p>As we can see, it is quite easy to write a custom observable for any kind of widget or datastructure, represented by a single value&#8230;  So, how do you embed your data in custom observables? Any obstacles you had to overcome? Problems you faced? Share them with us. <img src='http://eclipsesource.com/blogs/wp-includes/images/smilies/icon_smile.gif' alt="icon smile Databinding: A Custom Observable for a Widget" class='wp-smiley' title="Databinding: A Custom Observable for a Widget" /> </p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsesource.com/blogs/2009/02/03/databinding-a-custom-observable-for-your-widget/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

