RSS

Search Engine

Wednesday, June 23, 2010

Eclipse Data Binding with EMF

Create an EMF model "person.genmodel" with the following properties:

  • firstName - String

  • lastName - String

  • gender - String

  • isMarried - boolean

Create a new view "eclipsedatabinding.EMFExample" similar to UIExample and add this view to the perspective.

   
package eclipsedatabinding;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.emf.databinding.EMFObservables;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
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 domainmodelemf.DomainmodelemfFactory;
import domainmodelemf.DomainmodelemfPackage;
import domainmodelemf.IPerson;

public class EMFExample extends ViewPart {
public static final String ID = "eclipsedatabinding.EMFExample";
private IPerson lars;

public EMFExample() {

}

@Override
public void createPartControl(Composite parent) {

// Lets put thing to order
Layout layout = new GridLayout(2, false);
parent.setLayout(layout);

// Initialize the model
DomainmodelemfPackage.eINSTANCE.eClass();
// Retrieve the default factory singleton
DomainmodelemfFactory factory = DomainmodelemfFactory.eINSTANCE;

lars = factory.createIPerson();
lars.setFirstName("Lars");
lars.setLastName("Vogel");
lars.setGender("m");


// New text element
Text firstName = new Text(parent, SWT.NONE);
DataBindingContext bindingContext = new DataBindingContext();
firstName.setText("Hallo");
GridData gridData = new GridData();
gridData.horizontalAlignment = GridData.FILL;
gridData.horizontalSpan = 2;
firstName.setLayoutData(gridData);

bindingContext.bindValue(SWTObservables.observeText(firstName, SWT.Modify), EMFObservables.observeValue(lars,DomainmodelemfPackage.Literals.IPERSON__FIRST_NAME), null, null);



Button button1 = new Button(parent, SWT.PUSH);
button1.setText("Write model");
button1.addMouseListener(new MouseListener(){

@Override
public void mouseDoubleClick(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseDown(MouseEvent e) {
System.out.println(lars.getFirstName());
}

@Override
public void mouseUp(MouseEvent e) {
// TODO Auto-generated method stub

}



});

Button button2 = new Button(parent, SWT.PUSH);
button2.setText("Change model");
button2.addMouseListener(new MouseListener(){

@Override
public void mouseDoubleClick(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseDown(MouseEvent e) {
lars.setFirstName("Lars2");
}

@Override
public void mouseUp(MouseEvent e) {
// TODO Auto-generated method stub

}



});
}

@Override
public void setFocus() {
// TODO Auto-generated method stub

}

}

0 comments:

Post a Comment