Support free tutorials









vogella training Training Books



Eclipse Modeling Framework (EMF) - Notifications and Adapters - Tutorial

Lars Vogel

Version 0.2

19.03.2014

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. Support this website
3.1. Thank you
3.2. Questions and Discussion
4. Links and Literature
4.1. Source Code
4.2. EMF Resources
4.3. vogella 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 called com.vogella.emf.notifications.

Create the model based on the following two interfaces and generate the model code from it.

package com.vogella.emf.notifications.model;

import org.eclipse.emf.ecore.EObject;

/** * @model */
public interface IPerson extends EObject {
/** * @model */
public String getFirstName(); void setFirstName(String value); }

package com.vogella.emf.notifications.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. The ElementObserver instance listens to changes of the list (delete, insert items) and an TotalObserver instance listens also in addition to changes of the attributes of the elements which are contained in the list.

package com.vogella.emf.notifications.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 com.vogella.emf.notifications.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.

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. Support this website

This tutorial is Open Content under the CC BY-NC-SA 3.0 DE license. Source code in this tutorial is distributed under the Eclipse Public License. See the vogella License page for details on the terms of reuse.

Writing and updating these tutorials is a lot of work. If this free community service was helpful, you can support the cause by giving a tip as well as reporting typos and factual errors.

3.1. Thank you

Please consider a contribution if this article helped you. It will help to maintain our content and our Open Source activities.

3.2. Questions and Discussion

If you find errors in this tutorial, please notify me (see the top of the page). Please note that due to the high volume of feedback I receive, I cannot answer questions to your implementation. Ensure you have read the vogella FAQ as I don't respond to questions already answered there.

4. Links and Literature

4.1. Source Code

Source Code of Examples

4.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

4.3. vogella Resources

vogella Training Android and Eclipse Training from the vogella team

Android Tutorial Introduction to Android Programming

GWT Tutorial Program in Java, compile to JavaScript and HTML

Eclipse RCP Tutorial Create native applications in Java

JUnit Tutorial Test your application

Git Tutorial Put all your files in a distributed version control system