Wednesday, November 20, 2013

Customizing item provider adapter in EMF

By default all item provider adapters in EMF extend ItemProviderAdapter, however sometimes it's necessary to change base class for the adapters to something else. For example, today I was reading this post about suppressing DocumentRoot during model load and Ed Merks suggested specializing ResourceItemProvider to skip root. Great, but do I really need to update all my adapters to extend new class, manually? Nope, GenModel to the rescue once again this time with its attribute 'Provider Root Extends Class'. I just added 'org.eclipse.emf.edit.provider.resource.ResourceItemProvider' to the attribute, regenerated edit code and voilĂ , all my adapters extend ResourceItemProvider.

Tuesday, November 12, 2013

Making Annotation part of the model when generating EMF model

When generating EMF model from XSD or otherwise it is beneficial to make annotations such as documentation part of the model. This information can be used to communicate to the user what particular model object does. Unfortunately while this seems like a easy task it did not work for me right away.
 There is a nifty class EcoreUtil that has vast amount of information that you can get from your model. One of the methods that was useful to me was EcoreUtil.getDocumentation(EModelElement element) and to use it all I need to do is:
EObject myObj = ...
String documentation = EcoreUtil.getDocumentation(myObj.eClass());
Simple enough, but it did not work. I was always getting null for documentation. Looking further and doing some debugging in getDocumentation() method I saw that first line was:
protected static final String GEN_MODEL_PACKAGE_NS_URI = "";
EAnnotation eAnnotation = eModelElement.getEAnnotation(GEN_MODEL_PACKAGE_NS_URI);
Which suggests that obviously code should look for annotations with GEN_MODEL_PACKAGE_NS_URI.
However, I was just not getting my annotation which in fact had URI of Looking in GenModel class that is responsible for generating my model, there was a method GenModel.setSuppressGenModelAnnotations(boolean bool). Naming of the method suggests what it does and in fact in my genmodel's Model properties there is an entry 'Suppress GenModel Annotations' which is set to 'true' by default. Setting this property to false generated my annotations. I am still not sure why it's set to 'true' by default as setting to 'false' would make more sense in my opinion.

Blogger Syntax Highliter