Home » Modeling » EMF » Need an Adapter to be notified of changing references (with containment="false")
Need an Adapter to be notified of changing references (with containment="false") [message #1066996] |
Fri, 05 July 2013 07:29  |
Eclipse User |
|
|
|
Hello,
I am looking for a possibility to notify a model instance when a referenced (NOT contained) feature is modified.
Let's say I have a "Folder" model with a list of "Shortcut" models as children (with containment="false"). Whenever any property of a "Shortcut" changes, all parent "Folder"s must react. This is easy, if the children were contained in the "Folder": then I could simply add a EContentAdapter(){...} to the "Folder"'s eAdapters().
But the children are NOT contained. What can I do? Neither EContentAdapters nor AdapterImpls added to the "Folder" react, when a "Shortcut" is modified.
Thank you
Franz
|
|
|
Re: Need an Adapter to be notified of changing references (with containment="false" [message #1067030 is a reply to message #1066996] |
Fri, 05 July 2013 09:34   |
Eclipse User |
|
|
|
On 05-07-13 14:39, Franz Thiele wrote:
> Hello,
> I am looking for a possibility to notify a model instance when a
> referenced (NOT contained) feature is modified.
>
> Let's say I have a "Folder" model with a list of "Shortcut" models as
> children (with containment="false"). Whenever any property of a
> "Shortcut" changes, all parent "Folder"s must react. This is easy, if
> the children were contained in the "Folder": then I could simply add a
> EContentAdapter(){...} to the "Folder"'s eAdapters().
> But the children are NOT contained. What can I do? Neither
> EContentAdapters nor AdapterImpls added to the "Folder" react, when a
> "Shortcut" is modified.
It's not that hard to write your own... (Ok, I must admit the last two
weeks I have spend considerable time customing a CDO version of it...).
One thing to keep in mind, is that the methods set/unsetTarget() are
called when adding the Adapter to the eAdapters() list.
In case of EContentAdapter, the notification method can be changed to
'react' on a crossref. I think this part should be changed:
protected void selfAdapt(Notification notification)
{
.....
else if (notifier instanceof EObject)
{
Object feature = notification.getFeature();
if (feature instanceof EReference)
{
EReference eReference = (EReference)feature;
if (eReference.isContainment())
{
handleContainment(notification);
}
}
}
}
to (Remove the isContainment() check).
protected void selfAdapt(Notification notification)
{
.....
else if (notifier instanceof EObject)
{
Object feature = notification.getFeature();
if (feature instanceof EReference)
{
EReference eReference = (EReference)feature;
handleContainment(notification);
}
}
}
Then perhaps you can also check for the feature you want to adapt to:
Notification.getFeature() ... Your implementation could do a check
on which features you want self-adaptation to occur
When removing (unsetTarget()) , the EContentAdapter will call
target.eContents()
so here also, you will need to change it so perhaps EList<EObject>
eCrossReferences() again checking the feature to unset as well.
Would also recommend to write a solid test case, it's easy to make a
mistake.. before you know it you will have multiple adapters or none on
the same object.
Hope this helps.
Christophe
>
> Thank you
> Franz
|
|
| |
Re: Need an Adapter to be notified of changing references (with containment=&quot;false& [message #1067057 is a reply to message #1067037] |
Fri, 05 July 2013 11:39   |
Eclipse User |
|
|
|
Franz,
Comments below.
On 05/07/2013 4:37 PM, Franz Thiele wrote:
> Thank you very much for your quick reply.
> Do you say that there is definitely no solution using some available
> code (I wish there was an EReferenceAdapter)? Then I will follow your
> suggestion to modify the EContentAdapter.
I suppose one question to you is, where are all these things contained?
An EContentAdapter on the overall resource set will listen to everything...
Also, how many of these adapters are you planning to maintain? Just
one, will each folder have one? Is the relation bidirectional? Does
the folder structure induce a tree?
> There may be a problem, because EObjects can reference each other in a
> circular way, causing infinite notification loops (if not detected).
> Is this possibly the reason why the EContentAdapter only listens to
> contained features? There must be hundreds of use cases, where
> listening to non-contained features is required...
It's more of a question of which set of objects you want to listen
to. If it's all objects you can reach by crawling all references, you
might as well listen to the whole resource set because that's typically
what that reduces to.
>
> Franz
|
|
|
Re: Need an Adapter to be notified of changing references (with containment=&quot;false& [message #1067070 is a reply to message #1067057] |
Fri, 05 July 2013 12:28   |
Eclipse User |
|
|
|
Ed Merks wrote on Fri, 05 July 2013 11:39
On 05/07/2013 4:37 PM, Franz Thiele wrote:
> Thank you very much for your quick reply.
> Do you say that there is definitely no solution using some available
> code (I wish there was an EReferenceAdapter)? Then I will follow your
> suggestion to modify the EContentAdapter.
I suppose one question to you is, where are all these things contained?
An EContentAdapter on the overall resource set will listen to everything...
You are right. I have described a simplified version. Actually, every shortcut is contained in a ShortCutContainer (extends Folder), which is instantiated exactly once. Every Folder (and other Objects) can then reference any ShortCut and should be notified of changes.
Quote:
Also, how many of these adapters are you planning to maintain? Just
one, will each folder have one? Is the relation bidirectional? Does
the folder structure induce a tree?
Currently, I have one EContentAdapter for the ResourceSet.
At the moment, I am using unidirectional references.
And yes, the folder structure works like a tree (think of a disk file system).
Quote:
> There may be a problem, because EObjects can reference each other in a
> circular way, causing infinite notification loops (if not detected).
> Is this possibly the reason why the EContentAdapter only listens to
> contained features? There must be hundreds of use cases, where
> listening to non-contained features is required...
It's more of a question of which set of objects you want to listen
to. If it's all objects you can reach by crawling all references, you
might as well listen to the whole resource set because that's typically
what that reduces to.
I will try using bidirectional references and walk backwards from the Notifier, keeping track of visited objectes in order to avoid infinite loops.
Thank you for the help! It seems like I first need a better understanding of the Notification mechanism before I come back with more specific questions.
Franz
|
|
|
Re: Need an Adapter to be notified of changing references (with containment=&amp;quot;false& [message #1067074 is a reply to message #1067070] |
Fri, 05 July 2013 12:50  |
Eclipse User |
|
|
|
Franz,
Comments below.
On 05/07/2013 6:28 PM, Franz Thiele wrote:
> Ed Merks wrote on Fri, 05 July 2013 11:39
>> On 05/07/2013 4:37 PM, Franz Thiele wrote:
>> > Thank you very much for your quick reply.
>> > Do you say that there is definitely no solution using some
>> available > code (I wish there was an EReferenceAdapter)? Then I will
>> follow your > suggestion to modify the EContentAdapter.
>> I suppose one question to you is, where are all these things
>> contained? An EContentAdapter on the overall resource set will
>> listen to everything...
>
> You are right. I have described a simplified version. Actually, every
> shortcut is contained in a ShortCutContainer (extends Folder), which
> is instantiated exactly once. Every Folder (and other Objects) can
> then reference any ShortCut and should be notified of changes.
I see.
> Quote:
>> Also, how many of these adapters are you planning to maintain? Just
>> one, will each folder have one? Is the relation bidirectional? Does
>> the folder structure induce a tree?
>
> Currently, I have one EContentAdapter for the ResourceSet.
> At the moment, I am using unidirectional references.
Bidirectional references are convenient, but have a significant impact
on serialization, i.e., generally both must be transient or both must be
non-transient.
> And yes, the folder structure works like a tree (think of a disk file
> system).
That's what I figured...
>
> Quote:
>> > There may be a problem, because EObjects can reference each other
>> in a > circular way, causing infinite notification loops (if not
>> detected). > Is this possibly the reason why the EContentAdapter only
>> listens to > contained features? There must be hundreds of use cases,
>> where > listening to non-contained features is required...
>> It's more of a question of which set of objects you want to listen
>> to. If it's all objects you can reach by crawling all references,
>> you might as well listen to the whole resource set because that's
>> typically what that reduces to.
>
> I will try using bidirectional references and walk backwards from the
> Notifier, keeping track of visited objectes in order to avoid infinite
> loops.
There's also ECrossReferenceAdapter which is must like an
EContentAdapter, but is very helpful for knowing inverses for all
unidirectional references
>
> Thank you for the help! It seems like I first need a better
> understanding of the Notification mechanism before I come back with
> more specific questions.
Once you understand it, you'll realize it's really quite simple...
If you need alternative ways of determine which set of things you want
to listen to, EContentAdapter and ECrossReferenceAdapters are good
examples to understand how to handle all the different types of
notifications...
>
> Franz
>
|
|
|
Goto Forum:
Current Time: Tue Jul 08 19:50:53 EDT 2025
Powered by FUDForum. Page generated in 0.25990 seconds
|