Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

5. JFace Data Binding with POJOs

If you only need to update the model from the UI you can use the class "PojoObservables". This this case you Java model objects do not need to implement "PropertyChangeSupport" and can be a POJO. Changes from the model will in this case not be reflected in the UI. If you only modify your data via the User Interface this is a feasible solution for you.

Create a new Eclipse RCP project "de.vogella.databinding.personpojo" with the "RCP application with a view" template. Create the following data model.

			
package de.vogella.databinding.personpojo.model;

public class PersonPojo {
	private String firstName;
	private String lastName;
	private boolean married;
	private String gender;
	private Integer age;

	public PersonPojo() {
	}

	public String getFirstName() {
		return firstName;
	}

	public String getGender() {
		return gender;
	}

	public String getLastName() {
		return lastName;
	}

	public boolean isMarried() {
		return married;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public void setMarried(boolean isMarried) {
		this.married = isMarried;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}


		

Add the plugins "org.eclipse.core.databinding", "org.eclipse.core.databinding.beans" and "org.eclipse.jface.databinding" and "org.eclipse.core.databinding.property" as a dependency to your plugin.

Change your class "View" to the following:

			
package de.vogella.databinding.personpojo;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.PojoProperties;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import de.vogella.databinding.personpojo.model.PersonPojo;

public class View extends ViewPart {

	private PersonPojo person;
	private Text firstName;
	private Button married;

	public View() {
		person = new PersonPojo();
		person.setFirstName("Joe");
		person.setLastName("Jim");
	}

	@Override
	public void createPartControl(Composite parent) {
		Layout layout = new GridLayout(2, false);
		parent.setLayout(layout);
		Label label = new Label(parent, SWT.NONE);
		label.setText("FirstName");
		firstName = new Text(parent, SWT.BORDER);
		firstName.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
		Label marriedLabel = new Label(parent, SWT.NONE);
		marriedLabel.setText("Maried");
		married = new Button(parent, SWT.CHECK);
		bindValues();

		Button button1 = new Button(parent, SWT.PUSH);
		button1.setText("Write model");
		button1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				System.out.println(person.getFirstName());
				System.out.println(person.isMarried());
			}
		});

	}

	private void bindValues() {
		DataBindingContext bindingContext = new DataBindingContext();
		IObservableValue myModel = PojoProperties.value(PersonPojo.class,
				"firstName").observe(person);
		IObservableValue myWidget = WidgetProperties.text(SWT.Modify).observe(
				firstName);
		bindingContext.bindValue(myWidget, myModel);
		myModel = PojoProperties.value(PersonPojo.class, "married").observe(
				person);
		myWidget = WidgetProperties.selection().observe(married);
		bindingContext.bindValue(myWidget, myModel);
	}

	@Override
	public void setFocus() {

	}

}

		

Run the example and test it. Each time you change the UI element then model changes automatically.