RSS

Search Engine

Wednesday, June 23, 2010

Using Eclipse Data Binding with Pojos

5.1. Overview

The example before should how you can use databinding to synchronize changes between the UI and the model. If you only need to use to update the model from the UI you can use the class PojoObservables.

5.2. Example

Create a new Eclipse RCP project "de.vogella.databinding.personpojo" use "Hello RCP" as a template (see Create your first Eclipse RCP application for details).

Create the following domain 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"as a dependency to your plugin.

Create a view with the ID "de.vogella.databinding.personpojo.myview" and implement the following code.

    
package de.vogella.databinding.personpojo;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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 MyView extends ViewPart {
public static final String ID = "de.vogella.databinding.personpojo.myview";

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

public MyView() {
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);
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();
bindingContext.bindValue(SWTObservables.observeText(firstName,
SWT.Modify), PojoObservables.observeValue(person, "firstName"),
null, null);

bindingContext.bindValue(SWTObservables.observeSelection(married),
PojoObservables.observeValue(person, "married"), null, null);
}

@Override
public void setFocus() {

}

}

Add your view to the perspective.

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

0 comments:

Post a Comment