Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

EMF Tips n Tricks

1.
Eclipse Modeling Framework – Tips n Tricks1.Design a Model Provider APIAlways invoke the singleton instance of a ModelProvider from anywhere in theapplication as a single point contact for managing the lifecycle and behavior of model Ø It should contain the corresponding editing domain, file resource, resource set, associated editor part, navigator and tabbed propertypage sheet Ø It should maintain a single copy of the domain model in the memory. getModel(IResource resource) Ø It should maintain the list of cross-references Ø It should be a resource-change listener so that o it can un-load the model and delete the emf resource when the file resource is deleted o It can modify the resource-uri when the file is moved / renamed Ø Whenever the resource uri is changed then using EcoreUtil CrossReferencer ModelProvider should find out what all models should be refactored Ø ModelProvider should also register all required ItemAdapterFactories Ø It should provide the custom persistence policy for the model if needed2.Item Provider is the single-most powerful feature in EMF. They should be utilizedto the fullest. It provides the functions to Ø Implement content and label provider By using mixin interfaces – delegate pattern Public Object[] getchilren(Object object){ ITreeItemContentProvider adapter = (ITreeItemContentProvider ) adapterFactory.adapt(object, ITreeItemContentProvider .class); return aapter.getChildren(object).toArray(); } Ø Adapt the model objects to implement whatever interfaces the editors and views need. Ø Propagate the change-notifications to viewers Ø Act as command factory Ø Provide a property source for model objects3. Effective usage of Common Command FwkgetResult() should be overridden to return relevant model objects so that(1) result of one command can be input to another command

3.
}6. Why Notification Listeners in EMF are also called Adapters ?A. Apart from observing the changes in eObjects, they also help - extending thebehavior i.e. support additional interfaces without subclassing - (Adapter pattern)Attaching an Adapter as Observer :Adapter myEObjectObserver = ...myEObjectObserver.eAdapters().add(myEObjectObserver);Attaching an Adapter as a Behaviour Extension :MyEObject myEObject = ....AdapterFactory myEObjectAdapterFactory = ....Object myEObjectType <==if(myEObjectAdapterFactory.isFactoryType(myEObjectType )){Adapter myEObjectAdapter = myEObjectAdapterFactory.adapt(myEObject, myEObjectType);.....}Attaching an adapter to all the eobjects under the rootSo far we have seen how to adapt to the changes to a particular EObject.Q. Now how to adapt to all the objects in the containment hierarchy, a resource and a setof related resources :EContentAdapter - can be attached to a root object, a resource or even a resourseset.7. What is resource-proxy and on-demand resource loading ?A. Say PO Entity simply refers to an Item Entity i.e. there is no by-valueaggregation/strong composition i.e. no containment reference between PO and ItemEObjects.Basically there is a cross-document referenceSo in this case there will be one poReource with poURI and an itemResource withitemURI.

4.
When we call rsourseSet.getReource(poURI) --> the ResourseSet will start traversingthe resource eobject-tree and load all eObjects. But for any references to objects initemResource , instead of traversing itemResource the ResourseSet will set thereferences to Proxies (* an uninitialized instance of the actual target class * with theactual URI).When - po.getItem() - will be invoked, the proxies will be resolved and actual ItemEObjects will be loaded on demand.8. How to get the objects modified during the last execute() / undo() / redo() ?>> command.getAffectedObjects()These objects should be used for selecting/highlighting the viewers9.Some useful Commands :>> Moving objects up - down in Viewer : MoveCommand>> Replacing an object in multiplicity-many features : ReplaceCommand>> Creating a duplicate object : CopyCommand>> Create a new object and add it to a feature of EObject : CreateChildCommand>> Delete an eobject from parent container along with all references : DeleteCommand10. What is the role of Editing Domain ?AdapterFactoryEditingDomain -- (i) creates command thru item provider, (ii)maintaining editors commandstack, (iii) maintaining resource set11. Ecore Model Optimization ">> If a particular reference is always -containment- and can never be cross-document;then resolveProxies should be set to false11. How to define a custom data type ?>> We should not refer a highly complicated Java Class as data type.>> Instead we should model the Java_Class as EClass and then create an EDtaTypewhere instanceClass should refer to that EClass.12. How to maintain in-memory temporary ELists which need not be persisted ?These model elements should be declared as - volatile, transient, non-changeable anddeived.public EList getSpecialModelObjects(){ ArrayList specialModelObjects = new ArrayList(); for(... getmodelObjects() ... ){

5.
if(...){ specialModelObjects.add(); } }return new ECoreEList.UnmodifiableEList(this, , specialComposites.siz(),specialComposites.toArray());}13. What to do if you want to create a unique list which should contain elements ofa particular type ? Elist locations = new EDataTypeUniqueEList(String.class, this,EPO2Package.GLOBAL_ADDRESS_LOCTION);14. How to suppress creation of eobjects ?Simply clear the GenModel property – “Root Extends Interface” –15. How to control the commands appearance i.e. decorate the actions ?The item provider is an IchangeNotifier to support DECORATOR pattern – anItemProviderDecorator registers itself as a listener for the Item Provider that itdecorates.The commands need to implement CommandActionDelegate interface to provideimplementation for retrieving images and labels.16. How to use custom adapter factories ?Create a ComposedAdapterFactory and include the item provider for the classes that arenot part of the model along with the regular item provider factories.17. How to refresh a viewer and set the selection on particular elements ?editingDomain.getCommandStack().addCommandStackListener( newCommandStacklistener() {public void commandStackChanged(final EventObject event) { getContainer().getDisplay().asyncExec( new Runnable() { public void run() { firePropertyChange(IEditorPart.PROP_Dirty); Command mostRecentCommand = ((CommandStack)event.getSource()).getMostRecentCommand(); if (mostRecentCommand != null) { setSelectionToViewer(mostRecentCommand.getAffectedObjects()); } // --- set selection