Version 0.3
Copyright © 2010 Lars Vogel
17.10.2010
| Revision History | ||
|---|---|---|
| Revision 0.1 | 25.09.2010 | Lars Vogel |
| Splitted from main databinding article | ||
| Revision 0.2 - 0.3 | 05.10.2010 - 17.10.2010 | Lars Vogel |
| bugfixes and enhancements | ||
Table of Contents
JFace Data Binding connects your domain model and the user interface. This article covers Data Binding together with Eclipse EMF. For an introduction to JFace Data Binding please see JFace Data Binding Tutorial . For an introduction to Eclipse EMF please see Eclipse EMF Tutorial.
One of the cool features in JFace Data Binding is that you have access to nestled properties.
The remainder of this tutorial assume that you are familiar with the base concepts of EMF and JFace Data binding and will focus on the additional information how to use both frameworks together.
Create an RCP project "de.vogella.databinding.emf.swt" based on the "RCP application with a view". Add "org.eclipse.emf.ecore" as a plugin dependency.
Create an EMF model "person.ecore". Create the class "Phone" with the property "number". Create also the class "Person" which should have the following properties:
firstName - String
lastName - String
gender - String
isMarried - boolean
phone - Phone
The result should look similar to the following.

Change the class View to the following. The coding demonstrates how you can use JFace Data Binding to access model properties. It also demonstrates how to access nestled properties to the phone class with is part of person.
package de.vogella.databinding.emf.swt; import org.eclipse.core.databinding.DataBindingContext; import org.eclipse.emf.databinding.EMFProperties; import org.eclipse.emf.databinding.FeaturePath; import org.eclipse.jface.databinding.swt.WidgetProperties; import org.eclipse.swt.SWT; import org.eclipse.swt.events.MouseAdapter; import org.eclipse.swt.events.MouseEvent; 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.Layout; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.part.ViewPart; import de.vogella.databinding.emf.swt.model.ModelFactory; import de.vogella.databinding.emf.swt.model.ModelPackage; import de.vogella.databinding.emf.swt.model.Person; import de.vogella.databinding.emf.swt.model.Phone; public class View extends ViewPart { public static final String ID = "de.vogella.databinding.emf.swt.view"; @Override public void createPartControl(Composite parent) { // Lets put thing to order Layout layout = new GridLayout(2, false); parent.setLayout(layout); // Initialize the model ModelPackage.eINSTANCE.eClass(); // Retrieve the default factory singleton ModelFactory factory = ModelFactory.eINSTANCE; final Person person = factory.createPerson(); person.setFirstName("Lars"); person.setLastName("Vogel"); person.setGender("m"); Phone phone = factory.createPhone(); phone.setNumber("0123456789"); person.setPhone(phone); // New text element Text firstName = new Text(parent, SWT.NONE); DataBindingContext bindingContext = new DataBindingContext(); GridData gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.horizontalSpan = 2; firstName.setLayoutData(gridData); Text phoneNumber = new Text(parent, SWT.NONE); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.horizontalSpan = 2; phoneNumber.setLayoutData(gridData); bindingContext.bindValue( WidgetProperties.text(SWT.Modify).observe(firstName), EMFProperties.value(ModelPackage.Literals.PERSON__FIRST_NAME) .observe(person)); FeaturePath feature = FeaturePath.fromList( ModelPackage.Literals.PERSON__PHONE, ModelPackage.Literals.PHONE__NUMBER); bindingContext.bindValue( WidgetProperties.text(SWT.Modify).observe(phoneNumber), EMFProperties.value(feature).observe(person)); Button button1 = new Button(parent, SWT.PUSH); button1.setText("Write model"); button1.addMouseListener(new MouseAdapter() { @Override public void mouseDown(MouseEvent e) { System.out.println(person.getFirstName()); System.out.println(person.getPhone().getNumber()); } }); Button button2 = new Button(parent, SWT.PUSH); button2.setText("Change model"); button2.addMouseListener(new MouseAdapter() { @Override public void mouseDown(MouseEvent e) { person.setFirstName("Lars2"); String reversedNumber = new StringBuffer(person.getPhone() .getNumber()).reverse().toString(); person.getPhone().setNumber(reversedNumber); } }); } public void setFocus() { } }
Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.com Google Group. I have created a short list how to create good questions which might also help you.
Wiki about the JFace Data Binding
Galileo: EMF-Databinding – Part 1 from Tom Schindl
Eclipse EMF Databinding and the Properties API - Part 2 from from Tom Schindl
Setting up a TreeViewer with EMF-Databinding - Part 3 from from Tom Schindl
Eclipse RCP Training (German) Eclipse RCP Training with Lars Vogel
Android Tutorial Introduction to Android Programming
GWT Tutorial Program in Java and compile to JavaScript and HTML
Eclipse RCP Tutorial Create native applications in Java
JUnit Tutorial Test your application
Git Tutorial Put everything you have under distributed version control system