l e m m s t e r . d e
  • Home
  • impressum
  • talks
  • Categories
  • Tags
  • Archives

Viewers and 1:n associations with the EMF databinding

Examples on the evolving EMF databinding are still limited, so here's what I've learned recently. The use case is as follows, display the Collection (more precisely an EList) of Addresses a Person object owns (Person#addresses) in a JFace TableViewer.

Before things can get started, one needs to grab a recent version (recent in terms of HEAD because of #216440) of EMF 2.4, which in turn requires platform 3.4. After defining EMF as a plug-in dependency, the Viewer's Content- and LabelProvider needs to be changed to:

// The model object which should be shown
Person aPerson = ModelFactory.eINSTANCE.createPerson();
...

// The content provider is responsible to handle add and
// remove notification for the Person#address EList
ObservableListContentProvider provider = new ObservableListContentProvider();
viewer.setContentProvider(provider);

// The label provider in turn handles the addresses
// The EStructuralFeature[] defines which fields get shown
// in the TableViewer columns
IObservableSet knownElements = provider.getKnownElements();
IObservableMap[] observeMaps = EMFObservables.
    observeMaps(knownElements, new EStructuralFeature[] {
    ModelPackage.Literals.PERSON_ADDRESS__STREET,
    ModelPackage.Literals.PERSON_ADDRESS__NUMBER,
    ModelPackage.Literals.PERSON_ADDRESS__ZIP,
    ModelPackage.Literals.PERSON_ADDRESS__COUNTRY);
ObservableMapLabelProvider labelProvider = 
    new ObservableMapLabelProvider(observeMaps);
viewer.setLabelProvider(labelProvider);

// Person#addresses is the Viewer's input
viewer.setInput(EMFObservables.observeList(Realm.getDefault(), aPerson, 
    ModelPackage.Literals.PERSON_ADDRESSES));

This is pretty much everything what's necessary to display the Addresses details in the TableViewer and to react to EMF notifications. One doesn't even have to write a custom Content- and LabelProviders. Though the databinding has some limitation in this early incarnation:

  • One might want to delegate to an existing EMF edit in the LabelProvider, thus an EMF version of the ObservableMapLabelProvider would be required

  • No support for non EMF/custom columns. If a table column is to be filled by a different source than an EMF field e.g. calculation usually done in the LabelProvider

  • EMF proxies don't get resolved under certain conditions which result in NPEs in the LabelProvider or Comparators

Edit: Following Ed's suggestion I've created a EMF recipe and bug 216748


Published

Jan 28, 2008

Category

eclipse

Contact

  • Powered by Pelican. Theme: Elegant