|
Re: eAdapter, Notification and AdapterFactory [message #854729 is a reply to message #854704] |
Tue, 24 April 2012 07:33 |
Ed Merks Messages: 33113 Registered: July 2009 |
Senior Member |
|
|
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Ludwig,<br>
<br>
Comments below.<br>
<br>
On 24/04/2012 9:08 AM, Ludwig Moser wrote:
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">Hello
<br>
<br>
I've read through the
<a class="moz-txt-link-freetext" href="http://help.eclipse.org/ganymede/index.jsp?topic=/org.eclipse.emf.doc/references/overview/EMF.html">http://help.eclipse.org/ganymede/index.jsp?topic=/org.eclipse.emf.doc/references/overview/EMF.html</a>,
which is pretty short for this part. (i only found stuff in the
overview)
<br>
Usually i find tutorials or Documentation which helps me finding
my way, but this time i cant find something matching (searching
the wrong place?)
<br>
</blockquote>
There is the EMF book...<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">
<br>
so far as i understood i can _ONLY_ register an adapter for an
object.
<br>
</blockquote>
Yes, only on a Notifier.<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">i
created a mini-example for getting a basic understanding.
<br>
<br>
a EClass Person and a EClass PersonList
<br>
<br>
<?xml version="1.0" encoding="UTF-8"?>
<br>
<ecore:EPackage xmi:version="2.0"
<br>
xmlns:xmi=<a class="moz-txt-link-rfc2396E" href="http://www.omg.org/XMI">"http://www.omg.org/XMI"</a>
xmlns:xsi=<a class="moz-txt-link-rfc2396E" href="http://www.w3.org/2001/XMLSchema-instance">"http://www.w3.org/2001/XMLSchema-instance"</a>
<br>
xmlns:ecore=<a class="moz-txt-link-rfc2396E" href="http://www.eclipse.org/emf/2002/Ecore">"http://www.eclipse.org/emf/2002/Ecore"</a>
name="model"
<br>
nsURI=<a class="moz-txt-link-rfc2396E" href="http://model/1.0">"http://model/1.0"</a> nsPrefix="emf.model">
<br>
<eClassifiers xsi:type="ecore:EClass" name="Person">
<br>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="nick"
eType="ecore:EDataType
<a class="moz-txt-link-freetext" href="http://www.eclipse.org/emf/2002/Ecore#//EString">http://www.eclipse.org/emf/2002/Ecore#//EString</a>"/>
<br>
</eClassifiers>
<br>
<eClassifiers xsi:type="ecore:EClass" name="PersonList">
<br>
<eStructuralFeatures xsi:type="ecore:EReference"
name="persons" upperBound="-1"
<br>
eType="#//Person"/>
<br>
</eClassifiers>
<br>
</ecore:EPackage>
<br>
<br>
<br>
so then i wrote a small test app
<br>
<br>
<br>
import org.eclipse.emf.common.notify.Notification;
<br>
import org.eclipse.emf.ecore.util.EContentAdapter;
<br>
<br>
import emf.model.ModelFactory;
<br>
import emf.model.Person;
<br>
import emf.model.PersonList;
<br>
<br>
public class Main {
<br>
<br>
public static void main(String[] args) {
<br>
EContentAdapter adapter = new EContentAdapter() {
<br>
public void notifyChanged(Notification notification) {
<br>
// it does not make any difference if i call super
or not?!
<br>
super.notifyChanged(notification);<br>
</blockquote>
If you look at what calling super actually does, does that answer
your question?<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">
System.out.println(String.format("data changed! (%s = '%s')",
<br>
notification.getNotifier().getClass().getSimpleName(),
<br>
notification.getNotifier()));
<br>
}
<br>
};
<br>
<br>
PersonList list =
ModelFactory.eINSTANCE.createPersonList();
<br>
list.eAdapters().add(adapter);
<br>
<br>
Person me = ModelFactory.eINSTANCE.createPerson();
<br>
me.eAdapters().add(adapter);
<br>
System.out.println("added a person");
<br>
list.getPersons().add(me);
<br>
System.out.println("changing persons nick");
<br>
me.setNick("lumo");
<br>
<br>
// add more persons
<br>
Person ed = ModelFactory.eINSTANCE.createPerson();
<br>
ed.setNick("Ed");
<br>
System.out.println("adding another person");
<br>
list.getPersons().add(ed);
<br>
}
<br>
}
<br>
<br>
<br>
i am able to monitor if anyone got added or removed to/from the
list.
<br>
</blockquote>
Yes.<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">but
i do not get notifications for changes IN the list (altered
elements)
<br>
actually i thought calling super.notifyChanged(notification);
should exactly do this, but it does not (actually it makes no
difference at all if i call it or not)
<br>
</blockquote>
It will attach the adapter to any EObjects<b> contained </b>by the
EObject already being adapted. But your PersonList doesn't have any
containment references there are no contained things to listen to.<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">
<br>
do i really have to add each person manually to the eadapter?
<br>
</blockquote>
If you put everything in a resource you could ad the adapter to the
resource and it will attach to all objects contained by that
resource.<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">in
the generated code i cant find an AdapterFactory which is
mentioned in the documentation.
<br>
</blockquote>
In the util package there should, by default, be an
XyzAdapterFactory (ModelAdapterFactory in your case).<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">
<br>
i tried
<br>
ModelPackage.Literals.PERSON.eAdapters().add(adapter);
<br>
</blockquote>
The model itself should never change, so this is pointless.<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">which
has no result on notifications (although it does not produce
errors nor warnings)
<br>
<br>
1) is there a possibility to add a adapter to listen for _ALL_
changes on a Class (eg. PersonList also notifies when a containing
Person gets changed)
<br>
</blockquote>
Person is not contained, it's only referenced via non-containment
reference.<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">2)
can i add an adapter to ALL available instances of that class eg.
Person </blockquote>
No.<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">but
it does not only notify me about instance 'me' rather than 'me'
and 'ed' from the example (without adding it manually to 'me' and
'ed'
<br>
</blockquote>
Note that in order to persist models, all objects need to be
contained in a resource and resources are generally contained in a
resource set, so if you follow that approach (which you'll have to
to eventually to support persistence), you can add the content
adapter to the resource set and you'll be able to monitor all
changes to all objects contained by that resource set.<br>
<blockquote cite="mid:jn5jh7$767$1@xxxxxxxxe.org" type="cite">
<br>
<br>
PS: if you have a source/book where i can read through a how to
i'd be happy
<br>
does it make sense to get
<a class="moz-txt-link-freetext" href="http://www.amazon.de/EMF-Eclipse-Modeling-Framework-Edition/dp/0321331885/ref=sr_1_1?ie=UTF8&qid=1335249683&sr=8-1">http://www.amazon.de/EMF-Eclipse-Modeling-Framework-Edition/dp/0321331885/ref=sr_1_1?ie=UTF8&qid=1335249683&sr=8-1</a>
as it is 4 years old (new version announced at the end of year)
<br>
</blockquote>
Yes, everything in the second edition work as documented.<br>
</body>
</html>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02480 seconds