Monday, February 23, 2009

PgUp and PgDwn through Editors

Today started off pretty good. My favorite music-director (composer) A R Rahman won the Best Original Score Oscar for the movie Slumdog Millionaire ... and i got a "Welcome to the Planet" mail from Planeteclipse.org

This motivated me to write something on this day ... just to keep a note. Being true to my blog's "tweakeclipse" self ... I want to tell you about a nice to have and lesser known (as far as i know) navigation feature in Eclipse Editors. You can use Ctrl + PgUp and Ctrl + PgDwn to move across the open editors! This is one more shortcut amongst many as listed in this blog entry by Nirav Thaker. One of my guru's says that this shortcut is inspired from MS Excel.

Hope this helps!

Monday, February 16, 2009

Create Maps in EMF - the other way

EMF wiki on Eclipsepedia has a post which shows you one method of creating maps in EMF.
I feel that the suggested method in wiki will not exactly allow you to access a map as it should be.
like say - myMap.put("key", value); Instead you'll have to do:

myMap.eSet(keyFeature,
"key");
myMap.eSet(valueFeature, valueObj);

After some trails here's the second method summarized in terms of steps.
To create a map using Reflective Ecore Editor:
  • create an EAttribute and set a name (let's say _map) to it.
  • set _map's EType as EMap [java.util.Map]
  • create an ETypedParameter for key and set EType accordingly
  • crate another ETypedParameter for value and set EType accordingly.

here is a snap shot of how a map looks in the Ecore Editor.

what happens behind the scenes of the editor ?
well, these lines of code are executed.


EAttribute _map = EcoreFactory.eINSTANCE.createEAttribute();
_map.setName("_map");
EGenericType _mapType = EcoreFactory.
eINSTANCE.createEGenericType();
_mapType.setEClassifier(EcorePackage.
eINSTANCE.getEMap());
// set key
EGenericType key = EcoreFactory.
eINSTANCE.createEGenericType();
key.setEClassifier(EcorePackage.
eINSTANCE.getEString());
// set key
EGenericType value = EcoreFactory.
eINSTANCE.createEGenericType();
value.setEClassifier(EcorePackage.
eINSTANCE.getEObject());
// add them
_mapType.getETypeArguments().add(key);
_mapType.getETypeArguments().add(value);
_map.setEGenericType(_mapType);


You have to use it like this:

Map _map = (Map) instance
.eGet(_mapFeature); // this is now java.util.Map
// don't forget to set the changes on _map back to instance!

Happy mapping!

Tuesday, February 10, 2009

Add branding to the XML files you generate from your EMF model

What if you want to suggest that an XML file is generated using your product?
If you look at some of these commercial tools they add a comment which proudly says "used XXX v a.b.c to generate this file." Just out of curiosity I wanted to do the same using EMF resource save api.

Guess what! It is possible to do such a thing with EMF resource api. There's this interface called ResourceHandler inside org.eclipse.emf.ecore.xmi.XMLResource.java which can be implemented as required. I have provided an example for your benifit.

public class MyResourceHandler implements ResourceHandler {

/*
* you have the file handle of the XML!
*/
public void postSave(XMLResource resource, OutputStream outputStream,
Map options) {
// append your comment here.
byte[] trailcomment = "some comment";
// enclose it in XML comment tags.
try {
outputStream.write(trailComment);
}
catch (IOException e) {

// may be log something here.
}
// do NOT close the stream here. EMF resource save will eventually do it for you.
}
}

You can find the link to the EMF newsgroup thread here.