Version 0.1
Copyright © 2010 Lars Vogel
20.01.2010
| Revision History | ||
|---|---|---|
| Revision 0.1 | 12.08.2007 | Lars Vogel |
| Split from main article | ||
Table of Contents
For a general introduction into Eclipse EMF please see Eclipse EMF Tutorial
This article focus on the observer / notification functionality of Eclipse EMF.
EMF has the possibility to inform observers / listeners about changes in the model. You can listen to changes of an individual object and to changes to all objects in a collection.
To test this notification create a new Java Project EMFNofication.
Create the model based on the following two interfaces and generate the model code from it.
package model;
import org.eclipse.emf.ecore.EObject;
/**
* @model
*/
public interface IPerson extends EObject {
/**
* @model
*/
public String getFirstName();
void setFirstName(String value);
}
package model;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
/**
* @model
* @generated
*/
public interface IPersonList extends EObject {
/**
* @model containment="true"
* @generated
*/
EList<IPerson> getPersons();
} // IPersonList
Create the following to two classes "ElementObserver" and "TotalObserver". ElementObserver listens to changes of the list, e.g. if an element is deleted or added to the list while TotalObserver listens also to changes in the list butin addition to changes of the elements contained in the list, e.g. if a values is changed of an element in the list.
package observers;
import model.IPerson;
import model.IPersonList;
import model.ModelFactory;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
public class ElementObserver {
private IPersonList persons;
public ElementObserver() {
ModelFactory factory = ModelFactory.eINSTANCE;
persons = factory.createIPersonList();
Adapter adapter = new AdapterImpl() {
public void notifyChanged(Notification notification) {
System.out
.println("Notfication received from the data model. Data model has changed!!!");
}
};
persons.eAdapters().add(adapter);
}
public void doStuff() {
ModelFactory factory = ModelFactory.eINSTANCE;
IPerson person = factory.createIPerson();
person.setFirstName("Lars");
System.out.println("I'm adding a person.");
persons.getPersons().add(person);
System.out.println("I'm changing a entry");
persons.getPersons().get(0).setFirstName("Lars2");
}
}
package observers;
import model.IPerson;
import model.IPersonList;
import model.ModelFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.util.EContentAdapter;
public class TotalObserver {
private IPersonList persons;
public TotalObserver() {
ModelFactory factory = ModelFactory.eINSTANCE;
persons = factory.createIPersonList();
EContentAdapter adapter = new EContentAdapter() {
public void notifyChanged(Notification notification) {
super.notifyChanged(notification);
System.out
.println("Notfication received from the data model. Data model has changed!!!");
}
};
persons.eAdapters().add(adapter);
}
public void doStuff() {
ModelFactory factory = ModelFactory.eINSTANCE;
IPerson person = factory.createIPerson();
person.setFirstName("Lars");
System.out.println("I'm adding a person.");
persons.getPersons().add(person);
System.out.println("I'm changing a entry");
IPerson person2 = persons.getPersons().get(0);
person2.setFirstName("Lars2");
}
}
Create a main class to test the different behavior and run the main method. ElementObserver will only react to the changes in the list, while TotalObserver will also recognize the change of the individual person object in the list.
package main;
import observers.ElementObserver;
import observers.TotalObserver;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
ElementObserver observ1 = new ElementObserver();
observ1.doStuff();
TotalObserver observ2 = new TotalObserver();
observ2.doStuff();
}
}
Thank you for practicing with this tutorial.
I maintain this tutorial in my private time. If you like the information please help me by using flattr or donating or by
|
Before posting questions, please see the vogella FAQ . If you have questions or find an error in this article please use the www.vogella.de Google Group . I have created a short list how to create good questions which might also help you. .
http://www.eclipse.org/modeling/emf Eclipse EMF Homepage
http://www.eclipse.org/modeling/emf/docs/ EMF Documentation
http://www.ibm.com/developerworks/opensource/library/os-ecemf1 Model with the Eclipse Modeling Framework, Part 1: Create UML models and generate code
http://www.ibm.com/developerworks/opensource/library/os-ecemf2 Model with the Eclipse Modeling Framework, Part 2: Generate code with Eclipse's Java Emitter Templates
http://www.eclipse.org/m2m/atl/ ATL allows model to model transformation for EMF