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. About this website
4. Links and Literature
4.1. EMF Resources
4.2. vogella GmbH training and consulting support

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

4. Links and Literature

4.1. EMF Resources

Eclipse EMF Homepage

EMF Documentation

Ecore translation

Eclipse ATL - allows model to model transformation for EMF

4.2. vogella GmbH training and consulting support

TRAINING SERVICE & SUPPORT
The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”. The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.