<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    How we use jface databinding in Deltacloud Tools
</h3>
<span style="margin-bottom: 10px;">
    modified by <a href="http://community.jboss.org/people/adietish">Andre Dietisheim</a> in <i>JBoss Tools</i> - <a href="http://community.jboss.org/docs/DOC-15964">View the full document</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><h1>Less code</h1><p>If you use jface databinding to code your swt views in Eclipse you'll get spared from writing listeners and updating code by hand. JFace databinding offers very nice abstractions and automatisms that offer funcitonalities for the vast majority of the tasks where you have to listen for user input and update the view accordingly.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h1><strong>Premise</strong></h1><p>If you implement a complex and highly dynamic UI in Eclipse you'll have to code many many many listener that wait for user actions. Those listeners mostly do nothing spectacular but update widgets and models in reaction to the user inputs. You end up with a lot of repetitive boilerplate code. UI frameworks in the non-java land (ex.<a class="jive-link-external-small" href="http://qt.nokia.com/products/"> Trolltechs QT</a>) already have approaches that are far more elegant and slick than what we knew for Swing and SWT.</p><p>By luck Eclipse has progressed in this area (since 2005!) and offers neat abstractions that help a lot in this area and lead to far more concise and a less verbose implementations: Jface Databinding!</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h1>Usecase</h1><p>We had to implement a wizard page that allows a user to create a connection to a deltacloud server. The user has to supply a name, an url and the credentials for it.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/10530/cloud-connection-wizard.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/10530/cloud-connection-wizard.png </span></a></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Sounds pretty simple at first sight. Looking closer at it you'll discover few buttons that shall be disabled if you enter inacurrate values. Furthermore we need to inform the user in a intuitive way and mark them by error decorations. The wizard page shall also show a global&#160; error message that reflects what's wrong in what the user entered.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/10531/invalid-url.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/10531/invalid-url.png </span></a></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h1 style="color: #333333; font-weight: bold; padding-top: 10px; padding-right: 0pt; padding-bottom: 4px; padding-left: 0pt; font-size: 18pt; margin: 0pt;"><span style="font-weight: normal; font-size: 13px;"><h1>Where's the source?</h1><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="padding: 0pt; margin: 0pt;">You may have a look at the implementation we discuss here at <a class="jive-link-external-small" href="http://anonsvn.jboss.org/repos/jbosstools/trunk/deltacloud/plugins/org.jboss.tools.deltacloud.ui/src/org/jboss/tools/internal/deltacloud/ui/wizards/CloudConnectionPage.java">CloudConnectionPage.java</a><span> </span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p></span></h1><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h1 style="color: #333333; font-weight: bold; padding-top: 10px; padding-right: 0pt; padding-bottom: 4px; padding-left: 0pt; font-size: 18pt; margin: 0pt;"><strong>A first step: only use unique names</strong></h1><p>Our GUI holds multiple connections to various deltacloud servers. The wizard allows you to edit existing connections or create new ones. The may of course not use names that are already picked for other connections.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/10533/duplicate-name.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/10533/duplicate-name.png </span></a></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>To be intuitive the wizard shall decorate the field that's invalid. The wizard shall also show you the full error text in the title area and disable the finish button if the user supplies invalid values.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/102-15964-18-10534/duplicate-name-markers.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/102-15964-18-10534/450-338/duplicate-name-markers.png </span></a></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h1><strong>How to decorate the name text field</strong></h1><p style="padding: 0pt; margin: 0pt;">Let us take a first step and implement the code that will decorate our text field if we enter an name that's already used.</p><p style="padding: 0pt; margin: 0pt;">Jface databinding is a framework that is built upon the principle of model-view-controller (<strong>MVC</strong>). It connects a view to a model and transfers the values the user enters in the view to the model. It of course also operates the other way round and updates the view if new values are set to the model.</p><p style="padding: 0pt; margin: 0pt;">If a invalid value is detected while transfering it, an error is generated. If a user supplies a duplicate name to our connection, we'll be able show the error that occurs while transfering this name to the model.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h2>Let us create a model</h2><p>As explained above, databinding operates on a model. We therefore need a model to store our values. We'll use a bean for that sake and provide notification capabilities with PropertyChangeSupport. PropertyChangeSupport, an implementation of the observer pattern, that's shipped with the jdk, allows jface databinding to observe value changes in our model. It will get notified as soon as we set new names to the model.</p><blockquote class="jive-quote"><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><div class="highlight" style="color: #000000; font-family: 'Times New Roman'; white-space: normal; font-size: 12pt;"><span style="color: #008000; font-weight: bold;">public</span> <span style="color: #008000; font-weight: bold;">class</span> <span style="color: #0000ff; font-weight: bold;">CloudConnectionModel</span> <span style="color: #008000; font-weight: bold;">extends</span> ObservablePojo <span style="color: #666666;">{</span> 
&#160;&#160;&#160;&#160; <span style="color: #008000; font-weight: bold;">public</span> <span style="color: #008000; font-weight: bold;">static</span> <span style="color: #008000; font-weight: bold;">final</span> String PROPERTY_NAME <span style="color: #666666;">=</span> <span style="color: #ba2121;">"name"</span><span style="color: #666666;">;</span> 
&#160;&#160;&#160;&#160; <span style="color: #008000; font-weight: bold;">private</span> String name<span style="color: #666666;">;</span> 
&#160;&#160;&#160;&#160; <span style="color: #008000; font-weight: bold;">public</span> <span style="color: #0000ff;">CloudConnectionModel</span><span style="color: #666666;">(</span>String name<span style="color: #666666;">)</span> <span style="color: #666666;">{</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #008000; font-weight: bold;">this</span><span style="color: #666666;">.</span><span style="color: #7d9029;">name</span> <span style="color: #666666;">=</span> name<span style="color: #666666;">;</span>&#160;&#160;&#160;&#160; <span style="color: #666666;">}</span>&#160;&#160;&#160;&#160; 
&#160;&#160;&#160;&#160; <span style="color: #008000; font-weight: bold;">public</span> String <span style="color: #0000ff;">getName</span><span style="color: #666666;">()</span> <span style="color: #666666;">{</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #008000; font-weight: bold;">return</span> name<span style="color: #666666;">;</span>&#160;&#160;&#160;&#160; <span style="color: #666666;">}</span> 
&#160;&#160;&#160;&#160; <span style="color: #008000; font-weight: bold;">public</span> <span style="color: #b00040;">void</span> <span style="color: #0000ff;">setName</span><span style="color: #666666;">(</span>String name<span style="color: #666666;">)</span> <span style="color: #666666;">{</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; getPropertyChangeSupport<span style="color: #666666;">().</span><span style="color: #7d9029;">firePropertyChange</span><span style="color: #666666;">(</span>PROPERTY_NAME<span style="color: #666666;">,</span> <span style="color: #008000; font-weight: bold;">this</span><span style="color: #666666;">.</span><span style="color: #7d9029;">name</span><span style="color: #666666;">,</span> <span style="color: #008000; font-weight: bold;">this</span><span style="color: #666666;">.</span><span style="color: #7d9029;">name</span> <span style="color: #666666;">=</span> name<span style="color: #666666;">);</span>&#160;&#160;&#160;&#160; <span style="color: #666666;">}</span><span style="color: #666666;">}</span></div></blockquote><div><span style="color: #666666;"><br/></span></div><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h2>Databinding, transfer the user input to our model!</h2><p>We now have a model that holds our name. We now have to listen for user input on the text widget and set the model accordingly. We of course don't want to do that manually, we want databinding to provide this functionality.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h2>A bit of theory: what the hell are observables?</h2><p style="padding: 0pt; margin: 0pt;">JFace databinding does not operate on the level of SWT listeners. It offers its own observer pattern. The main benefit here is that both ends of a <strong>MVC </strong>dont differ any longer, there are no SWT listeners to listen to changes in widgets nor PropertyChangeSupprt listeners to act upon changes in a model. Jface databinding observers <strong>Observables</strong>. Any participant to the jface databinding framework must implement the Observable interface. It of course provides adapters that adapt the various observer implementations that exist in the wild.</p><p style="padding: 0pt; margin: 0pt;"><a href="http://community.jboss.org/servlet/JiveServlet/showImage/102-15964-18-10539/observables.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/102-15964-18-10539/450-367/observables.png </span></a></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h2>Databdining, adapt those beasts!</h2><p style="padding: 0pt; margin: 0pt;">We need an <strong>Observable</strong> for our model since our model uses PropertyChangeSupport to notify observers. Jface databinding offers a factory that adapts the PropertyChangeSupport we used. To tell databindirng to observe changes in the name property we do the following:</p><blockquote class="jive-quote"><div id="_mcePaste" style="position: absolute; left: -10000px; top: 3053px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">IObservableValue beanPropertyObservable = BeanProperties.value(CloudConnectionModel.class, "name")</div><div> </div><p style="padding: 0pt; margin: 0pt;">IObservableValue beanPropertyObservable = BeanProperties.value(CloudConnectionModel.class, "name").observe(connectionModel)</p><div> </div></blockquote><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="padding: 0pt; margin: 0pt;">We also need to provide an observable for the text widget, that allows the user to enter the name of the connection. We tell the appropriate observable factory it to listen to text modifications on the text widget:</p><blockquote class="jive-quote"><p style="padding: 0pt; margin: 0pt;">IObservableValue textObservable = WidgetProperties.text(SWT.Modify).observe(nameText)</p></blockquote><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h2>Now connect both ends</h2><p>Now that we have observables on both ends (model and widget), we can tell databinding to connect both ends of our <strong>MVC</strong> and transfer the values between them. Databinding does that if we instruct the <strong>DataBindingContext</strong> to create a bind them:</p><blockquote class="jive-quote"><p>DataBindingContext dbc = new DataBindingContext();</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>dbc.bindValue(</p><p><span style="white-space: pre;">&#160;&#160;&#160;&#160; </span>WidgetProperties.text(SWT.Modify).observe(nameText), </p><p><span style="white-space: pre;">&#160;&#160;&#160;&#160; </span>BeanProperties.value(CloudConnectionModel.class, CloudConnectionModel.PROPERTY_NAME).observe(connectionModel));</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p></blockquote><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h2>But hey, inputs are not always valid</h2><p>We now told databinding to transfer the name the user enters in our text widget to our model and to update the widget if we set a new name to the model. What we still miss is the ability to constrain user inputs.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/10542/convert-validate.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/10542/convert-validate.png </span></a></p><p>Jface databinding offers the ability to convert and validate values while it transfers values from one end to the other. In other words we can tell it to validate the name before it sets the user input to the model.Databinding uses <strong>IValidator</strong> instances for that sake:</p><blockquote class="jive-quote"><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>private class CloudNameValidator implements IValidator {</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><span style="white-space: pre;">&#160;&#160;&#160;&#160; </span>public IStatus validate(Object value) {</p><p><span style="white-space: pre;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>String connectionName = (String) value;</p><p><span style="white-space: pre;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>if (DeltaCloudManager.getDefault().findCloud(connectionName) != null) {</p><p><span style="white-space: pre;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>return <strong>ValidationStatus.error("The name chosen in already in use");</strong></p><p><span style="white-space: pre;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>} else {</p><p><span style="white-space: pre;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>return ValidationStatus.ok();</p><p><span style="white-space: pre;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>}</p><p><span style="white-space: pre;">&#160;&#160;&#160;&#160; </span>}</p><p>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p></blockquote><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>The binding we created now has to be told to use our CloudNameValidator. We do that in a custom <strong>UpdateValueStrategy</strong>. As we saw above, databinding transfers values both ways: from the view to the model and the other way round. Databinding therefore offers strategies for both transfers. We only want to validate names that get transfered from the text widget, where the user may enter new names, to the model. We dont need validation where new names set in the model get propagated to the text widget. We therefore only set a single UpateValueStrategy and stick to the default (that does not validate) for the other strategy by indicating <strong>null</strong>.</p><blockquote class="jive-quote"><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>dbc.bindValue(</p><p><span style="white-space: pre;">&#160;&#160;&#160;&#160; </span>WidgetProperties.text(SWT.Modify).observe(nameText), </p><p><span style="white-space: pre;">&#160;&#160;&#160;&#160; </span>BeanProperties.value(CloudConnectionModel.class, CloudConnectionModel.PROPERTY_NAME).observe(connectionModel),</p><p><span style="white-space: pre;">&#160;&#160;&#160;&#160; </span><strong>new UpdateValueStrategy().setBeforeSetValidator(new CloudNameValidator()),</strong></p><p><span style="white-space: pre;">&#160;&#160;&#160;&#160; </span>null</p><p>);</p><div> </div><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p></blockquote><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h1>Disable finish button</h1><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/102-15964-18-10538/enable-disable-finish.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/102-15964-18-10538/250-50/enable-disable-finish.png </span></a></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>In non-technical pure logical terms there's a single trigger to all these markers: You entered a bad value to a field.</p><p>The wizard reacts by decorating the text field that holds a the duplicate name, it displays the error in the header and it disabled the Finish button.</p><p>Jface databinding offers a context that holds a global validity state for a form. If a form gets invalid, the golbal state gets invalid, too. A jface wizards is built upon several pages. Such a page is a form that may be globally valid or invalid.</p><blockquote class="jive-quote"><p>DataBindingContext dbc = new DataBindingContext();</p></blockquote><p>To enable/disable the Finish button you now need to connect the wizard page to the DataBindingContext. Jface databinding will then enable/disable the Finish (or the Next Page button for instance) automatically for you. To bind the validity of the databinding context to the page validity, you only need 1 line of code:</p><blockquote class="jive-quote"><p>WizardPageSupport.create(this, dbc);</p></blockquote><p>Jface databinding will now enable the finish button (or the next-page button) for you as soon as the form aka the databinding context is valid.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h2>WORK IN PROGRESS</h2><h1>Conclusion</h1></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Comment by <a href="http://community.jboss.org/docs/DOC-15964">going to Community</a></p>

        <p style="margin: 0;">Create a new document in JBoss Tools at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2128">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>