Java, Eclipse and Web programming Tutorials
Follow me on twitter About Lars Vogel

Eclipse Modeling Framework (EMF) - Notifications and Adapters

Lars Vogel

Version 0.1

20.01.2010

Revision History
Revision 0.1 12.08.2007Lars Vogel
Split from main article

Eclipse Adapters and Notification

This article describes the notification functionality of Eclipse EMF.


Table of Contents

1. Models and Eclipse EMF
2. Notifications and Adapters with Adapter and EContentAdapter
3. Thank you
4. Questions and Discussion
5. Links and Literature
5.1. Source Code
5.2. EMF Resources
5.3. Other Resources

1. Models and Eclipse EMF

For a general introduction into Eclipse EMF please see Eclipse EMF Tutorial

This article focus on the observer / notification functionality of Eclipse EMF.

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

		

3. Thank you

Thank you for practicing with this tutorial.

Please note that I maintain this website in my private time. If you like the information I'm providing please help me by donating.

4. Questions and Discussion

For questions and discussion around this article please use the www.vogella.de Google Group. Also if you note an error in this article please post the error and if possible the correction to the Group.

I believe the following is a very good guideline for asking questions in general and also for the Google group How To Ask Questions The Smart Way.

5. Links and Literature

5.1. Source Code

http://www.vogella.de/code/codeeclipse.html Source Code of Examples

5.2. EMF Resources

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