Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "JFace Data Binding/Tutorial"

(A first simple binding)
(Replaced content with "== Data Binding Tutorial == Please see [http://www.vogella.com/articles/EclipseDataBinding/article.html Eclipse JFace Databinding and Validation - Tutorial]")
 
(18 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
== Data Binding Tutorial ==
 
== Data Binding Tutorial ==
=== Terms ===
 
* Model: a Model represents the Domain Model of your Application.
 
* Target: a Target represents the GUI side.
 
=== A first simple binding ===
 
<p>Looking at the example snippets, you'll see a few SWT examples. This time, we'll create a very simple RCP view. Our createPartControl method has just one Text Element:</p>
 
<pre>
 
public void createPartControl(Composite parent){
 
  
name = new Text(testGroup, SWT.BORDER);
+
Please see [http://www.vogella.com/articles/EclipseDataBinding/article.html Eclipse JFace Databinding and Validation - Tutorial]
final GridData gd_name = new GridData(SWT.FILL, SWT.CENTER, true, false);
+
name.setLayoutData(gd_name);
+
 
+
}
+
</pre>
+
<p>That Text name represents the Target of our binding. Let's add a simple Model:</p>
+
<pre>
+
static class Person {
+
// A property...
+
String name = "HelloWorld";
+
 
+
public String getName() {
+
return name;
+
}
+
 
+
public void setName(String name) {
+
this.name = name;
+
}
+
}
+
</pre>
+
<p>To create your first binding, you need to instantiate a '''DataBindingContext'''. Create a method like this:</p>
+
<pre>
+
private DataBindingContext initDataBindings() {
+
DataBindingContext bindingContext = new DataBindingContext();
+
 
+
return bindingContext;
+
}
+
</pre>
+
<p>Next, we add a '''IObservableValue''' Object for the Target:</p>
+
<pre>
+
private DataBindingContext initDataBindings() {
+
DataBindingContext bindingContext = new DataBindingContext();
+
 
+
        IObservableValue nameTextObserveWidget = SWTObservables.observeText(name, SWT.FocusOut);
+
 
+
return bindingContext;
+
}
+
</pre>
+
<p>The observeText method takes two params
+
<ul>
+
<li>name: the observed Control</li>
+
<li>event: when the model will be updated. Choices are SWT.FocusOut, SWT.Modify or SWT.NONE</li>
+
</ul>
+
</p>
+
<p>Second, we add a '''IObservableValue''' Object for the Model:</p>
+
<pre>
+
private DataBindingContext initDataBindings() {
+
DataBindingContext bindingContext = new DataBindingContext();
+
        IObservableValue nameTextObserveWidget = SWTObservables.observeText(name, SWT.FocusOut);
+
 
+
IObservableValue personNameObserveValue = BeansObservables.observeValue(person, "name");
+
 
+
return bindingContext;
+
}
+
</pre>
+
<p>The observeValue method takes two params
+
<ul>
+
<li>bean: the observed bean</li>
+
<li>propertyName: the name of the observed property, in our case the persons name.</li>
+
</ul>
+
</p>
+
<p>Last, we need to bind Target and Model. This is done through the DataBindingContext#bindValue method:</p>
+
<pre>
+
private DataBindingContext initDataBindings() {
+
DataBindingContext bindingContext = new DataBindingContext();
+
        IObservableValue nameTextObserveWidget = SWTObservables.observeText(name, SWT.FocusOut);
+
IObservableValue personNameObserveValue = BeansObservables.observeValue(person, "name");
+
 
+
bindingContext.bindValue(nameTextObserveWidget, personNameObserveValue, null, null);
+
 
+
return bindingContext;
+
}
+
</pre>
+
<p>The bindValue method takes four params
+
<ul>
+
<li>targetObservableValue: our first created IObservableValue </li>
+
<li>modelObservableValue: our second created IObservableValue </li>
+
<li>UpdateValueStrategy targetToModel: will be dicusssed later</li>
+
<li>UpdateValueStrategy modelToTarget: will be dicusssed later</li>
+
</ul>
+
</p>
+
<p>That's it. You successfully created your first JFace Data Binding !</p>
+

Latest revision as of 17:31, 13 July 2015

Data Binding Tutorial

Please see Eclipse JFace Databinding and Validation - Tutorial

Back to the top