Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » EContentAdapter that follows non containment references
EContentAdapter that follows non containment references [message #419385] Wed, 21 May 2008 18:32 Go to next message
Will Horn is currently offline Will HornFriend
Messages: 265
Registered: July 2009
Senior Member
I find myself occasionally needing to listen for notifications on non-contained features. I made a
subclass of EContentAdapter to do this and was wondering if there is something similar that already
exists. If not, maybe this could be made available to others.

Also, I would appreciate any comments or warnings about my approach:

public class CrossDocumentContentAdapter extends EContentAdapter
{
/**
* Cross document reference features to follow
*/
private final Set<EReference> crossDocumentFeatures;

public CrossDocumentContentAdapter()
{
this.crossDocumentFeatures = null;
}

public CrossDocumentContentAdapter(Collection<EReference> crossDocumentFeatures)
{
this.crossDocumentFeatures = new HashSet<EReference>(crossDocumentFeatures);
}

@Override
protected void setTarget(EObject target)
{
super.setTarget(target);
for (EContentsEList.FeatureIterator<EObject> featureIterator =
(EContentsEList.FeatureIterator<EObject>) target.eCrossReferences()

.iterator(); featureIterator.hasNext();)
{
Notifier notifier = featureIterator.next();
EStructuralFeature feature = featureIterator.feature();
if (crossDocumentFeatures == null || crossDocumentFeatures.contains(feature))
{
addAdapter(notifier);
}
}
}

@Override
protected void unsetTarget(EObject target)
{
super.unsetTarget(target);
for (EContentsEList.FeatureIterator<EObject> featureIterator =
(EContentsEList.FeatureIterator<EObject>) target.eCrossReferences()

.iterator(); featureIterator.hasNext();)
{
Notifier notifier = featureIterator.next();
EStructuralFeature feature = featureIterator.feature();
if (crossDocumentFeatures == null || crossDocumentFeatures.contains(feature))
{
removeAdapter(notifier);
}
}
}

@Override
protected void selfAdapt(Notification notification)
{
super.selfAdapt(notification);
if (notification.getNotifier() instanceof EObject)
{
Object feature = notification.getFeature();
if (feature instanceof EReference)
{
EReference eReference = (EReference) feature;
if (!eReference.isContainment())
{
handleContainment(notification);
}
}
}
}
}
Re: EContentAdapter that follows non containment references [message #419393 is a reply to message #419385] Wed, 21 May 2008 21:35 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------000208020905080109030508
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Will,

Maybe, for the time being, you could write this up as an EMF recipe in
the wiki.

http://wiki.eclipse.org/EMF/Recipes

Will Horn wrote:
> I find myself occasionally needing to listen for notifications on
> non-contained features. I made a subclass of EContentAdapter to do
> this and was wondering if there is something similar that already
> exists. If not, maybe this could be made available to others.
>
> Also, I would appreciate any comments or warnings about my approach:
>
> public class CrossDocumentContentAdapter extends EContentAdapter
> {
> /**
> * Cross document reference features to follow
> */
> private final Set<EReference> crossDocumentFeatures;
>
> public CrossDocumentContentAdapter()
> {
> this.crossDocumentFeatures = null;
> }
>
> public CrossDocumentContentAdapter(Collection<EReference>
> crossDocumentFeatures)
> {
> this.crossDocumentFeatures = new
> HashSet<EReference>(crossDocumentFeatures);
> }
>
> @Override
> protected void setTarget(EObject target)
> {
> super.setTarget(target);
> for (EContentsEList.FeatureIterator<EObject> featureIterator =
> (EContentsEList.FeatureIterator<EObject>) target.eCrossReferences()
>
> .iterator(); featureIterator.hasNext();)
> {
> Notifier notifier = featureIterator.next();
> EStructuralFeature feature = featureIterator.feature();
> if (crossDocumentFeatures == null ||
> crossDocumentFeatures.contains(feature))
> {
> addAdapter(notifier);
> }
> }
> }
>
> @Override
> protected void unsetTarget(EObject target)
> {
> super.unsetTarget(target);
> for (EContentsEList.FeatureIterator<EObject> featureIterator =
> (EContentsEList.FeatureIterator<EObject>) target.eCrossReferences()
>
> .iterator(); featureIterator.hasNext();)
> {
> Notifier notifier = featureIterator.next();
> EStructuralFeature feature = featureIterator.feature();
> if (crossDocumentFeatures == null ||
> crossDocumentFeatures.contains(feature))
> {
> removeAdapter(notifier);
> }
> }
> }
>
> @Override
> protected void selfAdapt(Notification notification)
> {
> super.selfAdapt(notification);
> if (notification.getNotifier() instanceof EObject)
> {
> Object feature = notification.getFeature();
> if (feature instanceof EReference)
> {
> EReference eReference = (EReference) feature;
> if (!eReference.isContainment())
> {
> handleContainment(notification);
> }
> }
> }
> }
> }


--------------000208020905080109030508
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Will,<br>
<br>
Maybe, for the time being, you could write this up as an EMF recipe in
the wiki.<br>
<blockquote><a href="http://wiki.eclipse.org/EMF/Recipes">http://wiki.eclipse.org/EMF/Recipes</a><br>
<br>
</blockquote>
Will Horn wrote:
<blockquote cite="mid:g11ptb$9b1$1@build.eclipse.org" type="cite">I
find myself occasionally needing to listen for notifications on
non-contained features.&nbsp; I made a subclass of EContentAdapter to do
this and was wondering if there is something similar that already
exists.&nbsp; If not, maybe this could be made available to others.
<br>
<br>
Also, I would appreciate any comments or warnings about my approach:
<br>
<br>
public class CrossDocumentContentAdapter extends EContentAdapter
<br>
{
<br>
&nbsp;&nbsp;&nbsp; /**
<br>
&nbsp;&nbsp;&nbsp;&nbsp; * Cross document reference features to follow
<br>
&nbsp;&nbsp;&nbsp;&nbsp; */
<br>
&nbsp;&nbsp;&nbsp; private final Set&lt;EReference&gt; crossDocumentFeatures;
<br>
<br>
&nbsp;&nbsp;&nbsp; public CrossDocumentContentAdapter()
<br>
&nbsp;&nbsp;&nbsp; {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; this.crossDocumentFeatures = null;
<br>
&nbsp;&nbsp;&nbsp; }
<br>
<br>
&nbsp;&nbsp;&nbsp; public CrossDocumentContentAdapter(Collection&lt;EReference& ;gt;
crossDocumentFeatures)
<br>
&nbsp;&nbsp;&nbsp; {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; this.crossDocumentFeatures = new
HashSet&lt;EReference&gt;(crossDocumentFeatures);
<br>
&nbsp;&nbsp;&nbsp; }
<br>
<br>
&nbsp;&nbsp;&nbsp; @Override
<br>
&nbsp;&nbsp;&nbsp; protected void setTarget(EObject target)
<br>
&nbsp;&nbsp;&nbsp; {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; super.setTarget(target);
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; for (EContentsEList.FeatureIterator&lt;EObject&gt;
featureIterator = (EContentsEList.FeatureIterator&lt;EObject&gt;)
target.eCrossReferences()
<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; .iterator(); featureIterator.hasNext();)
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Notifier notifier = featureIterator.next();
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EStructuralFeature feature = featureIterator.feature();
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (crossDocumentFeatures == null ||
crossDocumentFeatures.contains(feature))
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; addAdapter(notifier);
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; }
<br>
&nbsp;&nbsp;&nbsp; }
<br>
<br>
&nbsp;&nbsp;&nbsp; @Override
<br>
&nbsp;&nbsp;&nbsp; protected void unsetTarget(EObject target)
<br>
&nbsp;&nbsp;&nbsp; {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; super.unsetTarget(target);
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; for (EContentsEList.FeatureIterator&lt;EObject&gt;
featureIterator = (EContentsEList.FeatureIterator&lt;EObject&gt;)
target.eCrossReferences()
<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; .iterator(); featureIterator.hasNext();)
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Notifier notifier = featureIterator.next();
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EStructuralFeature feature = featureIterator.feature();
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (crossDocumentFeatures == null ||
crossDocumentFeatures.contains(feature))
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; removeAdapter(notifier);
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; }
<br>
&nbsp;&nbsp;&nbsp; }
<br>
<br>
&nbsp;&nbsp;&nbsp; @Override
<br>
&nbsp;&nbsp;&nbsp; protected void selfAdapt(Notification notification)
<br>
&nbsp;&nbsp;&nbsp; {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; super.selfAdapt(notification);
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; if (notification.getNotifier() instanceof EObject)
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Object feature = notification.getFeature();
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (feature instanceof EReference)
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; EReference eReference = (EReference) feature;
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (!eReference.isContainment())
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; handleContainment(notification);
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; }
<br>
&nbsp;&nbsp;&nbsp; }
<br>
}
<br>
</blockquote>
<br>
</body>
</html>

--------------000208020905080109030508--


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: EContentAdapter that follows non containment references [message #419402 is a reply to message #419393] Wed, 21 May 2008 22:59 Go to previous messageGo to next message
Will Horn is currently offline Will HornFriend
Messages: 265
Registered: July 2009
Senior Member
Ed,

I guess that means you think it's okay :o) I didn't want to lead anyone down a dangerous path.

http://wiki.eclipse.org/EMF/Recipes#Recipe:_Subclass_EConten tAdapter_to_receive_notifications_across_non-containment_ref erences

Hope it helps someone.

Ed Merks wrote:
> Will,
>
> Maybe, for the time being, you could write this up as an EMF recipe in
> the wiki.
>
> http://wiki.eclipse.org/EMF/Recipes
>
> Will Horn wrote:
>> I find myself occasionally needing to listen for notifications on
>> non-contained features. I made a subclass of EContentAdapter to do
>> this and was wondering if there is something similar that already
>> exists. If not, maybe this could be made available to others.
>>
>> Also, I would appreciate any comments or warnings about my approach:
>>
>> public class CrossDocumentContentAdapter extends EContentAdapter
>> {
>> /**
>> * Cross document reference features to follow
>> */
>> private final Set<EReference> crossDocumentFeatures;
>>
>> public CrossDocumentContentAdapter()
>> {
>> this.crossDocumentFeatures = null;
>> }
>>
>> public CrossDocumentContentAdapter(Collection<EReference>
>> crossDocumentFeatures)
>> {
>> this.crossDocumentFeatures = new
>> HashSet<EReference>(crossDocumentFeatures);
>> }
>>
>> @Override
>> protected void setTarget(EObject target)
>> {
>> super.setTarget(target);
>> for (EContentsEList.FeatureIterator<EObject> featureIterator =
>> (EContentsEList.FeatureIterator<EObject>) target.eCrossReferences()
>>
>> .iterator(); featureIterator.hasNext();)
>> {
>> Notifier notifier = featureIterator.next();
>> EStructuralFeature feature = featureIterator.feature();
>> if (crossDocumentFeatures == null ||
>> crossDocumentFeatures.contains(feature))
>> {
>> addAdapter(notifier);
>> }
>> }
>> }
>>
>> @Override
>> protected void unsetTarget(EObject target)
>> {
>> super.unsetTarget(target);
>> for (EContentsEList.FeatureIterator<EObject> featureIterator =
>> (EContentsEList.FeatureIterator<EObject>) target.eCrossReferences()
>>
>> .iterator(); featureIterator.hasNext();)
>> {
>> Notifier notifier = featureIterator.next();
>> EStructuralFeature feature = featureIterator.feature();
>> if (crossDocumentFeatures == null ||
>> crossDocumentFeatures.contains(feature))
>> {
>> removeAdapter(notifier);
>> }
>> }
>> }
>>
>> @Override
>> protected void selfAdapt(Notification notification)
>> {
>> super.selfAdapt(notification);
>> if (notification.getNotifier() instanceof EObject)
>> {
>> Object feature = notification.getFeature();
>> if (feature instanceof EReference)
>> {
>> EReference eReference = (EReference) feature;
>> if (!eReference.isContainment())
>> {
>> handleContainment(notification);
>> }
>> }
>> }
>> }
>> }
>
Re: EContentAdapter that follows non containment references [message #419432 is a reply to message #419402] Thu, 22 May 2008 11:45 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Will,

Yes, it looks fine. Hmmm. Shouldn't the selfAdapt method be checking
if the reference is in the crossDocumentFeatures set?


Will Horn wrote:
> Ed,
>
> I guess that means you think it's okay :o) I didn't want to lead
> anyone down a dangerous path.
>
> http://wiki.eclipse.org/EMF/Recipes#Recipe:_Subclass_EConten tAdapter_to_receive_notifications_across_non-containment_ref erences
>
>
> Hope it helps someone.
>
> Ed Merks wrote:
>> Will,
>>
>> Maybe, for the time being, you could write this up as an EMF recipe
>> in the wiki.
>>
>> http://wiki.eclipse.org/EMF/Recipes
>>
>> Will Horn wrote:
>>> I find myself occasionally needing to listen for notifications on
>>> non-contained features. I made a subclass of EContentAdapter to do
>>> this and was wondering if there is something similar that already
>>> exists. If not, maybe this could be made available to others.
>>>
>>> Also, I would appreciate any comments or warnings about my approach:
>>>
>>> public class CrossDocumentContentAdapter extends EContentAdapter
>>> {
>>> /**
>>> * Cross document reference features to follow
>>> */
>>> private final Set<EReference> crossDocumentFeatures;
>>>
>>> public CrossDocumentContentAdapter()
>>> {
>>> this.crossDocumentFeatures = null;
>>> }
>>>
>>> public CrossDocumentContentAdapter(Collection<EReference>
>>> crossDocumentFeatures)
>>> {
>>> this.crossDocumentFeatures = new
>>> HashSet<EReference>(crossDocumentFeatures);
>>> }
>>>
>>> @Override
>>> protected void setTarget(EObject target)
>>> {
>>> super.setTarget(target);
>>> for (EContentsEList.FeatureIterator<EObject> featureIterator
>>> = (EContentsEList.FeatureIterator<EObject>) target.eCrossReferences()
>>>
>>> .iterator(); featureIterator.hasNext();)
>>> {
>>> Notifier notifier = featureIterator.next();
>>> EStructuralFeature feature = featureIterator.feature();
>>> if (crossDocumentFeatures == null ||
>>> crossDocumentFeatures.contains(feature))
>>> {
>>> addAdapter(notifier);
>>> }
>>> }
>>> }
>>>
>>> @Override
>>> protected void unsetTarget(EObject target)
>>> {
>>> super.unsetTarget(target);
>>> for (EContentsEList.FeatureIterator<EObject> featureIterator
>>> = (EContentsEList.FeatureIterator<EObject>) target.eCrossReferences()
>>>
>>> .iterator(); featureIterator.hasNext();)
>>> {
>>> Notifier notifier = featureIterator.next();
>>> EStructuralFeature feature = featureIterator.feature();
>>> if (crossDocumentFeatures == null ||
>>> crossDocumentFeatures.contains(feature))
>>> {
>>> removeAdapter(notifier);
>>> }
>>> }
>>> }
>>>
>>> @Override
>>> protected void selfAdapt(Notification notification)
>>> {
>>> super.selfAdapt(notification);
>>> if (notification.getNotifier() instanceof EObject)
>>> {
>>> Object feature = notification.getFeature();
>>> if (feature instanceof EReference)
>>> {
>>> EReference eReference = (EReference) feature;
>>> if (!eReference.isContainment())
>>> {
>>> handleContainment(notification);
>>> }
>>> }
>>> }
>>> }
>>> }
>>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: EContentAdapter that follows non containment references [message #419450 is a reply to message #419432] Thu, 22 May 2008 22:22 Go to previous message
Will Horn is currently offline Will HornFriend
Messages: 265
Registered: July 2009
Senior Member
Yeah! Broke that while cleaning up my code for the rest of the world :)

Fixed on the WIKI, also made it a bit more generic:

public class CrossDocumentContentAdapter extends EContentAdapter
{
public CrossDocumentContentAdapter()
{
super();
}

/**
* By default, all cross document references are followed. Usually this is
* not a great idea so this class can be subclassed to customize.
*
* @param feature
* a cross document reference
* @return whether the adapter should follow it
*/
protected boolean shouldAdapt(EStructuralFeature feature)
{
return true;
}

@Override
protected void setTarget(EObject target)
{
super.setTarget(target);
for (EContentsEList.FeatureIterator<EObject> featureIterator =
(EContentsEList.FeatureIterator<EObject>) target.eCrossReferences()

.iterator(); featureIterator.hasNext();)
{
Notifier notifier = featureIterator.next();
EStructuralFeature feature = featureIterator.feature();
if (shouldAdapt(feature))
{
addAdapter(notifier);
}
}
}

@Override
protected void unsetTarget(EObject target)
{
super.unsetTarget(target);
for (EContentsEList.FeatureIterator<EObject> featureIterator =
(EContentsEList.FeatureIterator<EObject>) target.eCrossReferences()

.iterator(); featureIterator.hasNext();)
{
Notifier notifier = featureIterator.next();
EStructuralFeature feature = featureIterator.feature();
if (shouldAdapt(feature))
{
removeAdapter(notifier);
}
}
}

@Override
protected void selfAdapt(Notification notification)
{
super.selfAdapt(notification);
if (notification.getNotifier() instanceof EObject)
{
Object feature = notification.getFeature();
if (feature instanceof EReference)
{
EReference eReference = (EReference) feature;
if (!eReference.isContainment() && shouldAdapt(eReference))
{
handleContainment(notification);
}
}
}
}

}

Ed Merks wrote:
> Will,
>
> Yes, it looks fine. Hmmm. Shouldn't the selfAdapt method be checking
> if the reference is in the crossDocumentFeatures set?
>
>
> Will Horn wrote:
>> Ed,
>>
>> I guess that means you think it's okay :o) I didn't want to lead
>> anyone down a dangerous path.
>>
>> http://wiki.eclipse.org/EMF/Recipes#Recipe:_Subclass_EConten tAdapter_to_receive_notifications_across_non-containment_ref erences
>>
>>
>> Hope it helps someone.
>>
>> Ed Merks wrote:
>>> Will,
>>>
>>> Maybe, for the time being, you could write this up as an EMF recipe
>>> in the wiki.
>>>
>>> http://wiki.eclipse.org/EMF/Recipes
>>>
>>> Will Horn wrote:
>>>> I find myself occasionally needing to listen for notifications on
>>>> non-contained features. I made a subclass of EContentAdapter to do
>>>> this and was wondering if there is something similar that already
>>>> exists. If not, maybe this could be made available to others.
>>>>
>>>> Also, I would appreciate any comments or warnings about my approach:
>>>>
>>>> public class CrossDocumentContentAdapter extends EContentAdapter
>>>> {
>>>> /**
>>>> * Cross document reference features to follow
>>>> */
>>>> private final Set<EReference> crossDocumentFeatures;
>>>>
>>>> public CrossDocumentContentAdapter()
>>>> {
>>>> this.crossDocumentFeatures = null;
>>>> }
>>>>
>>>> public CrossDocumentContentAdapter(Collection<EReference>
>>>> crossDocumentFeatures)
>>>> {
>>>> this.crossDocumentFeatures = new
>>>> HashSet<EReference>(crossDocumentFeatures);
>>>> }
>>>>
>>>> @Override
>>>> protected void setTarget(EObject target)
>>>> {
>>>> super.setTarget(target);
>>>> for (EContentsEList.FeatureIterator<EObject> featureIterator
>>>> = (EContentsEList.FeatureIterator<EObject>) target.eCrossReferences()
>>>>
>>>> .iterator(); featureIterator.hasNext();)
>>>> {
>>>> Notifier notifier = featureIterator.next();
>>>> EStructuralFeature feature = featureIterator.feature();
>>>> if (crossDocumentFeatures == null ||
>>>> crossDocumentFeatures.contains(feature))
>>>> {
>>>> addAdapter(notifier);
>>>> }
>>>> }
>>>> }
>>>>
>>>> @Override
>>>> protected void unsetTarget(EObject target)
>>>> {
>>>> super.unsetTarget(target);
>>>> for (EContentsEList.FeatureIterator<EObject> featureIterator
>>>> = (EContentsEList.FeatureIterator<EObject>) target.eCrossReferences()
>>>>
>>>> .iterator(); featureIterator.hasNext();)
>>>> {
>>>> Notifier notifier = featureIterator.next();
>>>> EStructuralFeature feature = featureIterator.feature();
>>>> if (crossDocumentFeatures == null ||
>>>> crossDocumentFeatures.contains(feature))
>>>> {
>>>> removeAdapter(notifier);
>>>> }
>>>> }
>>>> }
>>>>
>>>> @Override
>>>> protected void selfAdapt(Notification notification)
>>>> {
>>>> super.selfAdapt(notification);
>>>> if (notification.getNotifier() instanceof EObject)
>>>> {
>>>> Object feature = notification.getFeature();
>>>> if (feature instanceof EReference)
>>>> {
>>>> EReference eReference = (EReference) feature;
>>>> if (!eReference.isContainment())
>>>> {
>>>> handleContainment(notification);
>>>> }
>>>> }
>>>> }
>>>> }
>>>> }
>>>
Previous Topic:problem with dynamic templates
Next Topic:Implementation of a new FileSystem based on Reusable Asset
Goto Forum:
  


Current Time: Fri Apr 19 19:48:43 GMT 2024

Powered by FUDForum. Page generated in 0.03208 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top