| eAdapter, Notification and AdapterFactory [message #854704] |
Tue, 24 April 2012 03:08  |
Ludwig Moser Messages: 204 Registered: July 2009 |
Senior Member |
|
|
Hello
I've read through the EMF Documentation, which is pretty short for this part. (i only found stuff in the overview)
Usually i find tutorials or Documentation which helps me finding my way, but this time i cant find something matching (searching the wrong place?)
so far as i understood i can _ONLY_ register an adapter for an object.
i created a mini-example for getting a basic understanding.
a EClass Person and a EClass PersonList
<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0"
xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="model"
nsURI="http://model/1.0" nsPrefix="emf.model">
<eClassifiers xsi:type="ecore:EClass" name="Person">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="nick" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="PersonList">
<eStructuralFeatures xsi:type="ecore:EReference" name="persons" upperBound="-1"
eType="#//Person"/>
</eClassifiers>
</ecore:EPackage>
so then i wrote a small test app
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.util.EContentAdapter;
import emf.model.ModelFactory;
import emf.model.Person;
import emf.model.PersonList;
public class Main {
public static void main(String[] args) {
EContentAdapter adapter = new EContentAdapter() {
public void notifyChanged(Notification notification) {
// it does not make any difference if i call super or not?!
super.notifyChanged(notification);
System.out.println(String.format("data changed! (%s = '%s')",
notification.getNotifier().getClass().getSimpleName(),
notification.getNotifier()));
}
};
PersonList list = ModelFactory.eINSTANCE.createPersonList();
list.eAdapters().add(adapter);
Person me = ModelFactory.eINSTANCE.createPerson();
me.eAdapters().add(adapter);
System.out.println("added a person");
list.getPersons().add(me);
System.out.println("changing persons nick");
me.setNick("lumo");
// add more persons
Person ed = ModelFactory.eINSTANCE.createPerson();
ed.setNick("Ed");
System.out.println("adding another person");
list.getPersons().add(ed);
}
}
i am able to monitor if anyone got added or removed to/from the list.
but i do not get notifications for changes IN the list (altered elements)
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)
do i really have to add each person manually to the eadapter?
in the generated code i cant find an AdapterFactory which is mentioned in the documentation.
i tried
ModelPackage.Literals.PERSON.eAdapters().add(adapter);
which has no result on notifications (although it does not produce errors nor warnings)
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)
2) can i add an adapter to ALL available instances of that class eg. Person 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'
PS: if you have a source/book where i can read through a how to i'd be happy
does it make sense to get this book as it is 4 years old (new version announced at the end of year)
|
|
|
| Re: eAdapter, Notification and AdapterFactory [message #854729 is a reply to message #854704] |
Tue, 24 April 2012 03:33   |
Ed Merks Messages: 24564 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>
|
|
|
| Re: eAdapter, Notification and AdapterFactory [message #854817 is a reply to message #854729] |
Tue, 24 April 2012 05:01   |
Ludwig Moser Messages: 204 Registered: July 2009 |
Senior Member |
|
|
thanks for your explanations.
actually i do not use the resource yet. i am going to store the mode in a database not in a file nor url.
does it make sense to use everything in a resource even if i do not use it to save/load?
my model will get really big with a lot of loaded objects... (up to 100.000's)
one more question regarding adding the adapters.
i have a controller and views(or any other class).
so now the controller has no personlist nor persons filled. (therefore i can't add a adapter)
then the view opens (checks if there are persons in the controller.. there are none)
then the controller gains data (persons or list of persons)
how can i inform the view?
is this the point where the AdapterFactory and/or resource jumps in?
thanks in advance
Ludwig
EDIT: i just ordered that book!
[Updated on: Tue, 24 April 2012 05:38] Report message to a moderator
|
|
|
|
|
|
|
| Re: eAdapter, Notification and AdapterFactory [message #856066 is a reply to message #855888] |
Wed, 25 April 2012 06:37  |
Ludwig Moser Messages: 204 Registered: July 2009 |
Senior Member |
|
|
i do not know why i should need the editingDomain and the stack.
as far as i can see the main profit of it is undo redo features, which i do not need (actually i do not want it at all)
modifying the objects through commands seems ok (although it blows up the code) - so i would do it if i can see a profit from it.
did i miss an important part of the commandstack&editingdomain?
thanks in advance!
ludwig
|
|
|
Powered by
FUDForum. Page generated in 0.01849 seconds