February 23, 2009

Adding Listeners in EMF

In EMF, listeners are added like adapters. Lets say, you have a Person EObject with Name EAttribute. Here is a code snippet that will listen to changes in Name:

 

Person person = ExamplePackage.eINSTANCE.createPerson();
person.eAdapters().add(
            new AdapterImpl() {
                @Override
                public void notifyChanged(Notification notification) {
                    // Listen for changes to features.
                    switch (notification.getFeatureID(Person.class)) {
                    case ExamplePackage.PERSON__NAME:
                        if(notification.getEventType() == Notification.SET) {
                              // do something
                           }
                    }
                }
            }
        );

No comments: