|
Re: EContentAdapter that follows non containment references [message #419393 is a reply to message #419385] |
Wed, 21 May 2008 21:35 |
Ed Merks 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. 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.
<br>
<br>
Also, I would appreciate any comments or warnings about my approach:
<br>
<br>
public class CrossDocumentContentAdapter extends EContentAdapter
<br>
{
<br>
/**
<br>
* Cross document reference features to follow
<br>
*/
<br>
private final Set<EReference> crossDocumentFeatures;
<br>
<br>
public CrossDocumentContentAdapter()
<br>
{
<br>
this.crossDocumentFeatures = null;
<br>
}
<br>
<br>
public CrossDocumentContentAdapter(Collection<EReference& ;gt;
crossDocumentFeatures)
<br>
{
<br>
this.crossDocumentFeatures = new
HashSet<EReference>(crossDocumentFeatures);
<br>
}
<br>
<br>
@Override
<br>
protected void setTarget(EObject target)
<br>
{
<br>
super.setTarget(target);
<br>
for (EContentsEList.FeatureIterator<EObject>
featureIterator = (EContentsEList.FeatureIterator<EObject>)
target.eCrossReferences()
<br>
<br>
.iterator(); featureIterator.hasNext();)
<br>
{
<br>
Notifier notifier = featureIterator.next();
<br>
EStructuralFeature feature = featureIterator.feature();
<br>
if (crossDocumentFeatures == null ||
crossDocumentFeatures.contains(feature))
<br>
{
<br>
addAdapter(notifier);
<br>
}
<br>
}
<br>
}
<br>
<br>
@Override
<br>
protected void unsetTarget(EObject target)
<br>
{
<br>
super.unsetTarget(target);
<br>
for (EContentsEList.FeatureIterator<EObject>
featureIterator = (EContentsEList.FeatureIterator<EObject>)
target.eCrossReferences()
<br>
<br>
.iterator(); featureIterator.hasNext();)
<br>
{
<br>
Notifier notifier = featureIterator.next();
<br>
EStructuralFeature feature = featureIterator.feature();
<br>
if (crossDocumentFeatures == null ||
crossDocumentFeatures.contains(feature))
<br>
{
<br>
removeAdapter(notifier);
<br>
}
<br>
}
<br>
}
<br>
<br>
@Override
<br>
protected void selfAdapt(Notification notification)
<br>
{
<br>
super.selfAdapt(notification);
<br>
if (notification.getNotifier() instanceof EObject)
<br>
{
<br>
Object feature = notification.getFeature();
<br>
if (feature instanceof EReference)
<br>
{
<br>
EReference eReference = (EReference) feature;
<br>
if (!eReference.isContainment())
<br>
{
<br>
handleContainment(notification);
<br>
}
<br>
}
<br>
}
<br>
}
<br>
}
<br>
</blockquote>
<br>
</body>
</html>
--------------000208020905080109030508--
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 |
Will Horn 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);
>>>> }
>>>> }
>>>> }
>>>> }
>>>> }
>>>
|
|
|
Powered by
FUDForum. Page generated in 0.03208 seconds