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"

(Data Binding Tutorial)
(A first simple binding)
Line 17: Line 17:
 
<p>That Text name represents the Target of our binding. Let's add a simple Model:</p>
 
<p>That Text name represents the Target of our binding. Let's add a simple Model:</p>
 
<pre>
 
<pre>
static class Person {
+
static class Person extends ModelObject{
 
// A property...
 
// A property...
 
String name = "HelloWorld";
 
String name = "HelloWorld";
Line 30: Line 30:
 
}
 
}
 
</pre>
 
</pre>
 +
<p>As you may have noticed, the Person Class extends '''Model Object'''. If you want to bind your Domain Models, you'll have to ensure support for PropertyChangeSupport. The easiest way to achieve this, is to extend Model Object. You'll find a suitable Template in org.eclipse.jface.examples.databinding package.</p>
 
<p>To create your first binding, you need to instantiate a '''DataBindingContext'''. Create a method like this:</p>
 
<p>To create your first binding, you need to instantiate a '''DataBindingContext'''. Create a method like this:</p>
 
<pre>
 
<pre>

Revision as of 14:03, 13 October 2007

Data Binding Tutorial

--Frank.schaare.gmail.com 20:24, 12 October 2007 (EDT)

Terms

  • Model: a Model represents the Domain Model of your Application.
  • Target: a Target represents the GUI side.

A first simple binding

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:

	public void createPartControl(Composite parent){

	name = new Text(parent, SWT.BORDER);
	final GridData gd_name = new GridData(SWT.FILL, SWT.CENTER, true, false);
	name.setLayoutData(gd_name);

	}

That Text name represents the Target of our binding. Let's add a simple Model:

	static class Person extends ModelObject{
		// A property...
		String name = "HelloWorld";

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
	}

As you may have noticed, the Person Class extends Model Object. If you want to bind your Domain Models, you'll have to ensure support for PropertyChangeSupport. The easiest way to achieve this, is to extend Model Object. You'll find a suitable Template in org.eclipse.jface.examples.databinding package.

To create your first binding, you need to instantiate a DataBindingContext. Create a method like this:

	private DataBindingContext initDataBindings() {
	DataBindingContext bindingContext = new DataBindingContext();

	return bindingContext;
	}

Next, we add a IObservableValue Object for the Target:

	private DataBindingContext initDataBindings() {
	DataBindingContext bindingContext = new DataBindingContext();

        IObservableValue nameTextObserveWidget = SWTObservables.observeText(name, SWT.FocusOut);

	return bindingContext;
	}

The observeText method takes two params

  • name: the observed Control
  • event: when the model will be updated. Choices are SWT.FocusOut, SWT.Modify or SWT.NONE

Second, we add a IObservableValue Object for the Model:

	private DataBindingContext initDataBindings() {
	DataBindingContext bindingContext = new DataBindingContext();
        IObservableValue nameTextObserveWidget = SWTObservables.observeText(name, SWT.FocusOut);

	IObservableValue personNameObserveValue = BeansObservables.observeValue(person, "name");

	return bindingContext;
	}

The observeValue method takes two params

  • bean: the observed bean
  • propertyName: the name of the observed property, in our case the persons name.

Last, we need to bind Target and Model. This is done through the DataBindingContext#bindValue method:

	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;
	}

The bindValue method takes four params

  • targetObservableValue: our first created IObservableValue
  • modelObservableValue: our second created IObservableValue
  • UpdateValueStrategy targetToModel: will be dicusssed later
  • UpdateValueStrategy modelToTarget: will be dicusssed later

That's it. You successfully created your first JFace Data Binding !

Copyright © Eclipse Foundation, Inc. All Rights Reserved.