RSS

Search Engine

Sunday, August 8, 2010

Notifications and Adapters with Adapter and EContentAdapter

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 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();
}
}

0 comments:

Post a Comment