Version 1.3
Copyright © 2010, 2011, 2012 Lars Vogel
08.09.2012
| Revision History | |||
|---|---|---|---|
| Revision 0.1 | 25.09.2010 | Lars Vogel |
Created |
| Revision 0.2 - 1.3 | 05.10.2010 - 08.09.2012 | Lars Vogel |
bugfixes and enhancements |
Table of Contents
This article covers JFace Data Binding together with the Eclipse Modeling Framework (EMF) .
This tutorial assumes 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.
For an introduction to JFace Data Binding please see JFace Data Binding Tutorial
. For an introduction to Eclipse EMF please see Eclipse EMF Tutorial.
Create an RCP project called
de.vogella.databinding.emf.swt
based on
the
RCP
application with a view
. Add the
org.eclipse.emf.ecore
and the JFace data binding plug-ins
as a
plug-in dependency.
Create an EMF model called
person.ecore. Create a class called
Phone
with the
number
property. Create also a
class called
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
View
class according to the following listing. The coding demonstrates
how
you can use
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.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.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"; private Text firstName; @Override public void createPartControl(Composite parent) { final Person person = createPerson(); Layout layout = new GridLayout(2, false); parent.setLayout(layout); firstName = new Text(parent, SWT.NONE); 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); 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.getPhone().getNumber()); } }); Button button2 = new Button(parent, SWT.PUSH); button2.setText("Change model"); button2.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { person.setFirstName("Lars2"); String reversedNumber = new StringBuffer(person.getPhone() .getNumber()).reverse().toString(); person.getPhone().setNumber(reversedNumber); } }); DataBindingContext bindingContext = new DataBindingContext(); 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)); } private Person createPerson() { // 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); return person; } @Override public void setFocus() { firstName.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
vogella Training Android and Eclipse Training from the vogella team
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