Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » A RemoveChild Command: DELETING NON-CONTAINMENT CHILD Reference
A RemoveChild Command: DELETING NON-CONTAINMENT CHILD Reference [message #427230] Fri, 06 February 2009 21:48 Go to next message
Maatari is currently offline MaatariFriend
Messages: 4
Registered: July 2009
Junior Member
Dear All,

Having an Object A parent of Object B trough a non-containment reference,
I would like to delete Object B with a specific behavior. Indeed I would
like that if Object A is the only non-containing Parent, then that B get
deleted completely (EcoreUtil.delete). Otherwise We just remove it from A
(EcoreUtil.remove). I illustrate the idea in the following code. This
Command should be called when a remove is about to be executed on a child
feature. This is due to the fact that the default behavior of my EMF
editor is not the full delete (Do not delete all reference). Therefore I
have to implement my own delete behavior. Globally, the idea is that, when
you are removed from your container we delete you completly. If you are
removed from a Non-Containing Parent then we check that, it is the only
containg parent in which you are and delete you if it is the case.


public class RemoveChildCommand extends ChangeCommand {

protected EditingDomain mydomain = null;
protected EObject myowner = null;
protected EStructuralFeature myfeature = null;
protected Collection<EObject> myselection = null;

public RemoveChildCommand(EditingDomain domain, EObject owner,
EStructuralFeature feature, Collection<?> collection) {

super(domain.getResourceSet());

mydomain = domain;
myowner = owner;
myfeature = feature;
myselection = (Collection<EObject>)(collection == null ?
Collections.EMPTY_LIST : collection);

}


@Override
protected void doExecute() {


for (EObject selectedElt : myselection) {


if (myowner.eClass() == selectedElt.eContainer().eClass()) { //Deleting
from the Container

EcoreUtil.delete(selectedElt, true);
}
else { //Deleting from a ParentCrossReferencer

if (new
ParentCrossReferencer(mydomain.getResourceSet()).findUsage(s electedElt).size()
< 2 ) {

EcoreUtil.delete(selectedElt, true);
}
else {

EcoreUtil.remove(myowner, myfeature, selectedElt);
}
}
}
}


Until now unless I'm wrong somewhere, the algorithm seams pretty simple.
As you can see above, I extended a ChangeCommad, to record all that is
happening. Alowwing me to have a undo, redo, etc...


However, suprisingly, this where things get very complicate. As you can
see I am stuck with the use of a crossReferencer. I need a
Crossreferencer that is capable of telling me who are the Non-Containing
Parent or the ParentCrossReferencer of that Object. I have Attached the
try I have done, but as you can see, my last attempt pop me out a cast
exeption.

/**
* We check that the crossReferencedEObject is within the children of
the referencing Object.
* This method is heavy, it would be better to check the children
feature of the object against the reference.
*/
@SuppressWarnings("unchecked")
@Override
protected boolean crossReference(EObject object, EReference reference,
EObject crossReferencedEObject) {

if (super.crossReference(object, reference, crossReferencedEObject)) {

Collection<EObject> children = mydomain.getChildren(object) == null ?
Collections.EMPTY_LIST : mydomain.getChildren(object) ;

for(EObject child : children) {
if ( EcoreUtil.equals(child, crossReferencedEObject))
return true;
}

}

return false;
}

}



Please does any one has an Idea on how to solve this problem of
ParentCross-Referencer or Non-Containg Parent ?


Many thanks,
Maatari
Re: A RemoveChild Command: DELETING NON-CONTAINMENT CHILD Reference [message #427235 is a reply to message #427230] Sat, 07 February 2009 12:43 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
Maatari,

Comments below.


Maatari wrote:
> Dear All,
> Having an Object A parent of Object B trough a non-containment
> reference, I would like to delete Object B with a specific behavior.
> Indeed I would like that if Object A is the only non-containing
> Parent, then that B get deleted completely (EcoreUtil.delete).
> Otherwise We just remove it from A (EcoreUtil.remove). I illustrate
> the idea in the following code. This Command should be called when a
> remove is about to be executed on a child feature. This is due to the
> fact that the default behavior of my EMF editor is not the full delete
> (Do not delete all reference). Therefore I have to implement my own
> delete behavior. Globally, the idea is that, when you are removed from
> your container we delete you completly. If you are removed from a
> Non-Containing Parent then we check that, it is the only containg
> parent in which you are and delete you if it is the case.
>
>
> public class RemoveChildCommand extends ChangeCommand {
>
> protected EditingDomain mydomain = null;
> protected EObject myowner = null;
> protected EStructuralFeature myfeature = null;
> protected Collection<EObject> myselection = null;
>
> public RemoveChildCommand(EditingDomain domain, EObject owner,
> EStructuralFeature feature, Collection<?> collection) {
>
> super(domain.getResourceSet());
>
> mydomain = domain;
> myowner = owner;
> myfeature = feature;
> myselection = (Collection<EObject>)(collection == null ?
> Collections.EMPTY_LIST : collection);
> }
>
>
> @Override
> protected void doExecute() {
>
>
> for (EObject selectedElt : myselection) {
>
>
> if (myowner.eClass() == selectedElt.eContainer().eClass())
> { //Deleting from the Container
What about inheritance?
>
> EcoreUtil.delete(selectedElt, true);
> }
> else { //Deleting from a ParentCrossReferencer
>
> if (new
> ParentCrossReferencer(mydomain.getResourceSet()).findUsage(s electedElt).size()
> < 2 ) {
>
> EcoreUtil.delete(selectedElt, true);
> }
> else {
>
> EcoreUtil.remove(myowner, myfeature,
> selectedElt);
> }
> }
> }
> }
>
>
> Until now unless I'm wrong somewhere, the algorithm seams pretty
> simple. As you can see above, I extended a ChangeCommad, to record all
> that is happening. Alowwing me to have a undo, redo, etc...
>
>
> However, suprisingly, this where things get very complicate. As you
> can see I am stuck with the use of a crossReferencer. I need a
> Crossreferencer that is capable of telling me who are the
> Non-Containing Parent or the ParentCrossReferencer of that Object. I
> have Attached the try I have done, but as you can see, my last attempt
> pop me out a cast exeption.
>
> /**
> * We check that the crossReferencedEObject is within the
> children of the referencing Object.
> * This method is heavy, it would be better to check the
> children feature of the object against the reference.
> */
> @SuppressWarnings("unchecked")
> @Override
> protected boolean crossReference(EObject object, EReference
> reference, EObject crossReferencedEObject) {
>
> if (super.crossReference(object, reference,
> crossReferencedEObject)) {
>
> Collection<EObject> children =
> mydomain.getChildren(object) == null ? Collections.EMPTY_LIST :
> mydomain.getChildren(object) ;
I think getChildren always returns a collection....
>
> for(EObject child : children) {
> if ( EcoreUtil.equals(child, crossReferencedEObject))
Why a structural equality test rather than an identity test? Don't you
know exactly which feature is involved? E.g.,
XyzPackage.Literals.PARENT_CLASS__NON_CONTAINMENT_CHILD_FEAT URE? So
aren't you just trying to cross reference only that one feature so it's
just a quick == test on the feature and that's it?
> return true;
> }
>
> }
>
> return false;
> }
>
> }
>
>
>
> Please does any one has an Idea on how to solve this problem of
> ParentCross-Referencer or Non-Containg Parent ?
>
>
> Many thanks,
> Maatari
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: A RemoveChild Command: DELETING NON-CONTAINMENT CHILD Reference [message #427240 is a reply to message #427235] Sat, 07 February 2009 14:37 Go to previous messageGo to next message
maatari is currently offline maatariFriend
Messages: 74
Registered: July 2009
Member
> This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

--B_3316865856_3342577
Content-type: text/plain;
charset="ISO-8859-1"
Content-transfer-encoding: 8bit

Hi Ed,

Thank you for getting back at me.

I have solve my problem yesterday night.
Here is my new cross-Reference Method :


/** * We check that the crossReferencedEObject is within the
children of the referencing Object. * This method is heavy, it would
be better to check the children feature of the object against the reference.
* Unfortunately, mydomain wich is of type EditingDomain interface which has
no getchildrenFeature */ @Override protected boolean
crossReference(EObject object, EReference reference, EObject
crossReferencedEObject) { if
(super.crossReference(object, reference, crossReferencedEObject)) {
Collection<Object> children = mydomain.getChildren(object) == null ?
Collections.EMPTY_LIST : mydomain.getChildren(object) ;
for(Object child : children) { if (child instanceof
IWrapperItemProvider) if
(EcoreUtil.equals((EObject)((IWrapperItemProvider)child).get Value(),
crossReferencedEObject)) return true;
if (child instanceof EObject) if (
EcoreUtil.equals((EObject)child, crossReferencedEObject))
return true; try { throw new
Exception ("UnSupported object child:" + child.toString());
} catch (Exception e) { e.printStackTrace();
} } } return
false; }



To answer to your question: I want the command to be generic. In the sense
that whenever you have child to remove the behaviors should be the same.
If it is my container, you delete otherwise you check my remaining
parent-crossreferencer. As those parent cross-referencer could change with
the model evolution, I certainly can not check in advance the feature. The
best way would have been to have The interface IEditingDomain to provide a
mean to retrieve the children feature. Indeed the function exist in the
ITemproviderAdapter. Actually it is the function I use in my Itemproviders
in my override function CreateRemoveCommand, to check is the feature to
remove from is in the list of children feature or not. Then, if it is, it
call my Command RemoveChildCommand, which as mentioned above check if it is
a Containment removal or a ParentCrossReference Removal.

@Override protected Command createRemoveCommand(EditingDomain domain,
EObject owner, EStructuralFeature feature, Collection<?>
collection) { if
(this.getChildrenFeatures(owner).contains(feature)) { return new
RemoveChildCommand(domain, owner, feature, collection); }
return super.createRemoveCommand(domain, owner, feature, collection); }


The function above illustrate, the test of feature. With that test, I think
the function is acceptable regarding performance issue. If you have any
other suggestions?

However concerning the question
Re: A RemoveChild Command: DELETING NON-CONTAINMENT CHILD Reference [message #427246 is a reply to message #427240] Sat, 07 February 2009 18:33 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------030205090901020302030506
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Mattari,

Comments below.

Daniel OKOUYA wrote:
> Hi Ed,
>
> Thank you for getting back at me.
>
> I have solve my problem yesterday night.
> Here is my new cross-Reference Method :
THe formatting makes it impossible to read. :-(
>
>
> /** * We check that the crossReferencedEObject is within the
> children of the referencing Object. * This method is heavy, it
> would be better to check the children feature of the object against
> the reference. * Unfortunately, mydomain wich is of type
> EditingDomain interface which has no getchildrenFeature */
> @Override protected boolean crossReference(EObject
> object, EReference reference, EObject crossReferencedEObject) {
> if (super.crossReference(object,
> reference, crossReferencedEObject)) {
> Collection<Object> children =
> mydomain.getChildren(object) == null ? Collections.EMPTY_LIST :
> mydomain.getChildren(object) ;
> for(Object child : children) { if
> (child instanceof IWrapperItemProvider) if
> (EcoreUtil.equals((EObject)((IWrapperItemProvider)child).get Value(),
> crossReferencedEObject)) return true;
> if (child instanceof EObject)
> if ( EcoreUtil.equals((EObject)child,
> crossReferencedEObject)) return true;
> try { throw new Exception
> ("UnSupported object child:" + child.toString()); }
> catch (Exception e) { e.printStackTrace();
> } } }
> return false; }
>
>
>
> To answer to your question: I want the command to be generic. In the
> sense that whenever you have child to remove the behaviors should be
> the same.
> If it is my container, you delete otherwise you check my remaining
> parent-crossreferencer. As those parent cross-referencer could change
> with the model evolution, I certainly can not check in advance the
> feature. The best way would have been to have The interface
> IEditingDomain to provide a mean to retrieve the children feature.
> Indeed the function exist in the ITemproviderAdapter. Actually it is
> the function I use in my Itemproviders in my override function
> CreateRemoveCommand, to check is the feature to remove from is in the
> list of children feature or not. Then, if it is, it call my Command
> RemoveChildCommand, which as mentioned above check if it is a
> Containment removal or a ParentCrossReference Removal.
>
> @Override protected Command createRemoveCommand(EditingDomain
> domain, EObject owner, EStructuralFeature feature,
> Collection<?> collection) { if
> (this.getChildrenFeatures(owner).contains(feature)) {
> return new RemoveChildCommand(domain, owner, feature,
> collection); } return
> super.createRemoveCommand(domain, owner, feature, collection); }
>
>
> The function above illustrate, the test of feature. With that test, I
> think the function is acceptable regarding performance issue. If you
> have any other suggestions?
That seems better.
>
> However concerning the question "> What about inheritance?" in :
>
> *if (myowner.eClass() == selectedElt.eContainer().eClass()) *
This is like doing x.getClass() == y.getClass() when I suspect you want
to something using EClass.isSuperTypeOf. so that a derived class of
works too.
> *{ //Deleting from the Container
> EcoreUtil.delete(selectedElt, true); }
>
> *Could you please, be more precise here, I'm not sure to see what you
> would like me to see?
>
> Regards,
> Maatari
>
> On 2/7/09 1:43 PM, in article gmjvmg$vp6$1@build.eclipse.org, "Ed
> Merks" <Ed.Merks@gmail.com> wrote:
>
> > Maatari,
> >
> > Comments below.
> >
> >
> > Maatari wrote:
> >> Dear All,
> >> Having an Object A parent of Object B trough a non-containment
> >> reference, I would like to delete Object B with a specific behavior.
> >> Indeed I would like that if Object A is the only non-containing
> >> Parent, then that B get deleted completely (EcoreUtil.delete).
> >> Otherwise We just remove it from A (EcoreUtil.remove). I illustrate
> >> the idea in the following code. This Command should be called when a
> >> remove is about to be executed on a child feature. This is due to the
> >> fact that the default behavior of my EMF editor is not the full delete
> >> (Do not delete all reference). Therefore I have to implement my own
> >> delete behavior. Globally, the idea is that, when you are removed from
> >> your container we delete you completly. If you are removed from a
> >> Non-Containing Parent then we check that, it is the only containg
> >> parent in which you are and delete you if it is the case.
> >>
> >>
> >> public class RemoveChildCommand extends ChangeCommand {
> >>
> >> protected EditingDomain mydomain = null;
> >> protected EObject myowner = null;
> >> protected EStructuralFeature myfeature = null;
> >> protected Collection<EObject> myselection = null;
> >>
> >> public RemoveChildCommand(EditingDomain domain, EObject owner,
> >> EStructuralFeature feature, Collection<?> collection) {
> >>
> >> super(domain.getResourceSet());
> >>
> >> mydomain = domain;
> >> myowner = owner;
> >> myfeature = feature;
> >> myselection = (Collection<EObject>)(collection == null ?
> >> Collections.EMPTY_LIST : collection);
> >> }
> >>
> >>
> >> @Override
> >> protected void doExecute() {
> >>
> >>
> >> for (EObject selectedElt : myselection) {
> >>
> >>
> >> if (myowner.eClass() == selectedElt.eContainer().eClass())
> >> { //Deleting from the Container
> > What about inheritance?
> >>
> >> EcoreUtil.delete(selectedElt, true);
> >> }
> >> else { //Deleting from a ParentCrossReferencer
> >>
> >> if (new
> >>
> ParentCrossReferencer(mydomain.getResourceSet()).findUsage(s electedElt).size(
> >> )
> >> < 2 ) {
> >>
> >> EcoreUtil.delete(selectedElt, true);
> >> }
> >> else {
> >>
> >> EcoreUtil.remove(myowner, myfeature,
> >> selectedElt);
> >> }
> >> }
> >> }
> >> }
> >>
> >>
> >> Until now unless I'm wrong somewhere, the algorithm seams pretty
> >> simple. As you can see above, I extended a ChangeCommad, to record all
> >> that is happening. Alowwing me to have a undo, redo, etc...
> >>
> >>
> >> However, suprisingly, this where things get very complicate. As you
> >> can see I am stuck with the use of a crossReferencer. I need a
> >> Crossreferencer that is capable of telling me who are the
> >> Non-Containing Parent or the ParentCrossReferencer of that Object. I
> >> have Attached the try I have done, but as you can see, my last attempt
> >> pop me out a cast exeption.
> >>
> >> /**
> >> * We check that the crossReferencedEObject is within the
> >> children of the referencing Object.
> >> * This method is heavy, it would be better to check the
> >> children feature of the object against the reference.
> >> */
> >> @SuppressWarnings("unchecked")
> >> @Override
> >> protected boolean crossReference(EObject object, EReference
> >> reference, EObject crossReferencedEObject) {
> >>
> >> if (super.crossReference(object, reference,
> >> crossReferencedEObject)) {
> >>
> >> Collection<EObject> children =
> >> mydomain.getChildren(object) == null ? Collections.EMPTY_LIST :
> >> mydomain.getChildren(object) ;
> > I think getChildren always returns a collection....
> >>
> >> for(EObject child : children) {
> >> if ( EcoreUtil.equals(child,
> crossReferencedEObject))
> > Why a structural equality test rather than an identity test? Don't you
> > know exactly which feature is involved? E.g.,
> > XyzPackage.Literals.PARENT_CLASS__NON_CONTAINMENT_CHILD_FEAT URE? So
> > aren't you just trying to cross reference only that one feature so it's
> > just a quick == test on the feature and that's it?
> >> return true;
> >> }
> >>
> >> }
> >>
> >> return false;
> >> }
> >>
> >> }
> >>
> >>
> >>
> >> Please does any one has an Idea on how to solve this problem of
> >> ParentCross-Referencer or Non-Containg Parent ?
> >>
> >>
> >> Many thanks,
> >> Maatari
> >>

--------------030205090901020302030506
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">
Mattari,<br>
<br>
Comments below.<br>
<br>
Daniel OKOUYA wrote:
<blockquote cite="mid:C5B35B3E.B0A%25okouya_d@yahoo.fr" type="cite">
<title>Re: A RemoveChild Command: DELETING NON-CONTAINMENT CHILD
Reference</title>
<font face="Calibri, Verdana, Helvetica, Arial"><span
style="font-size: 11pt;">Hi Ed, <br>
<br>
Thank you for getting back at me.<br>
<br>
I have solve my problem yesterday night. <br>
Here is my new cross-Reference Method :<br>
</span></font></blockquote>
THe formatting makes it impossible to read. :-(<br>
<blockquote cite="mid:C5B35B3E.B0A%25okouya_d@yahoo.fr" type="cite"><font
face="Calibri, Verdana, Helvetica, Arial"><span
style="font-size: 11pt;"><br>
<br>
/** &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;* We check that the crossReferencedEObject is within the
children of the referencing Object. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;* This method is heavy, it
would be better to check the children feature of the object against the
reference. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;* Unfortunately, mydomain wich is of type
EditingDomain interface which has no getchildrenFeature &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;*/
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;@Override &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;protected boolean crossReference(EObject
object, EReference reference, EObject crossReferencedEObject) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (super.crossReference(object,
reference, crossReferencedEObject)) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Collection&lt;Object&g t; children =
mydomain.getChildren(object) == null ? Collections.EMPTY_LIST :
mydomain.getChildren(object) ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;for(Object &nbsp;child : children) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;if
(child instanceof IWrapperItemProvider) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if
(EcoreUtil.equals((EObject)((IWrapperItemProvider)child).get Value(),
crossReferencedEObject)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;return true;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;if (child instanceof EObject)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( EcoreUtil.equals((EObject)child,
crossReferencedEObject)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;return true;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;try { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new Exception
("UnSupported object child:" + child.toString()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;}
catch (Exception e) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printSta ckTrace();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return &nbsp;false; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;} <br>
<br>
<br>
<br>
To answer to your question: I want the command to be generic. In the
sense that whenever you have child to remove the behaviors should be
the same.<br>
If it is my container, you delete otherwise you check my remaining
parent-crossreferencer. As those parent cross-referencer could change
with the model evolution, I certainly can not check in advance the
feature. The best way would have been to have The interface
IEditingDomain to provide a mean to retrieve the children feature.
Indeed the function exist in the ITemproviderAdapter. Actually it is
the function I use in my Itemproviders in my override function
CreateRemoveCommand, to check is the feature to remove from is in the
list of children feature or not. Then, if it is, it call my Command
RemoveChildCommand, which as mentioned above check if it is a
Containment removal or a ParentCrossReference Removal.<br>
<br>
@Override &nbsp;&nbsp;&nbsp;protected Command createRemoveCommand(EditingDomain
domain, EObject owner, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EStructura lFeature feature,
Collection&lt;?&gt; collection) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;if
(this.getChildrenFeatures(owner).contains(feature)) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return
new RemoveChildCommand(domain, owner, feature, collection); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;return super.createRemoveCommand(domain, owner, feature,
collection); &nbsp;&nbsp;&nbsp;}<br>
<br>
<br>
The function above illustrate, the test of feature. With that test, I
think the function is acceptable regarding performance issue. If you
have any other suggestions?<br>
</span></font></blockquote>
That seems better.<br>
<blockquote cite="mid:C5B35B3E.B0A%25okouya_d@yahoo.fr" type="cite"><font
face="Calibri, Verdana, Helvetica, Arial"><span
style="font-size: 11pt;"><br>
However concerning the question &#8220;<font color="#0000fe">&gt; What about
inheritance?</font>&#8221; in :<br>
<br>
<b>if (myowner.eClass() == selectedElt.eContainer().eClass()) </b></span></font></blockquote>
This is like doing x.getClass() == y.getClass() when I suspect you want
to something using EClass.isSuperTypeOf. so that a derived class of
works too.<br>
<blockquote cite="mid:C5B35B3E.B0A%25okouya_d@yahoo.fr" type="cite"><font
face="Calibri, Verdana, Helvetica, Arial"><span
style="font-size: 11pt;"><b>{ //Deleting from &nbsp;the Container
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;EcoreUtil.delete(selectedElt, &nbsp;true);
&nbsp;&nbsp;&nbsp;}<br>
<br>
</b>Could you please, be more precise here, I&#8217;m not sure to see what
you would like me to see?<br>
<br>
Regards,<br>
Maatari<br>
<br>
On 2/7/09 1:43 PM, in article <a moz-do-not-send="true"
href="gmjvmg$vp6$1@build.eclipse.org">gmjvmg$vp6$1@build.eclipse.org</a>,
"Ed Merks" &lt;<a moz-do-not-send="true" href="Ed.Merks@gmail.com">Ed.Merks@gmail.com</a>&gt;
wrote:<br>
<br>
<font color="#0000ff">&gt; Maatari,<br>
&gt; <br>
&gt; Comments below.<br>
&gt; <br>
&gt; <br>
&gt; Maatari wrote:<br>
</font><font color="#008000">&gt;&gt; Dear All,<br>
&gt;&gt; Having an Object A parent of Object B trough a non-containment
<br>
&gt;&gt; reference, I would like to delete Object B with a specific
behavior. <br>
&gt;&gt; Indeed I would like that if Object A is the only
non-containing <br>
&gt;&gt; Parent, then that B get deleted completely (EcoreUtil.delete).
<br>
&gt;&gt; Otherwise We just remove it from A (EcoreUtil.remove). I
illustrate <br>
&gt;&gt; the idea in the following code. This Command should be called
when a <br>
&gt;&gt; remove is about to be executed on a child feature. This is due
to the <br>
&gt;&gt; fact that the default behavior of my EMF editor is not the
full delete <br>
&gt;&gt; (Do not delete all reference). Therefore I have to implement
my own <br>
&gt;&gt; delete behavior. Globally, the idea is that, when you are
removed from <br>
&gt;&gt; your container we delete you completly. If you are removed
from a <br>
&gt;&gt; Non-Containing Parent then we check that, it is the only
containg <br>
&gt;&gt; parent in which you are and delete you if it is the case.<br>
&gt;&gt; <br>
&gt;&gt; <br>
&gt;&gt; public class RemoveChildCommand extends ChangeCommand {<br>
&gt;&gt; <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;protected EditingDomain mydomain &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;= null;<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;protected EObject &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;myowner &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;=
null;<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;protected EStructuralFeature myfeature &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;= null;<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;protected Collection&lt;EObject&gt; myselection = null;<br>
&gt;&gt; <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;public RemoveChildCommand(EditingDomain domain, EObject
owner, <br>
&gt;&gt; EStructuralFeature feature, Collection&lt;?&gt; collection) {<br>
&gt;&gt; <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;super(domain.getResourceSet()); <br>
&gt;&gt; <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;mydomain &nbsp;&nbsp;&nbsp;= domain;<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;myowner &nbsp;&nbsp;&nbsp;&nbsp;= owner;<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;myfeature &nbsp;&nbsp;= feature;<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;myselection = (Collection&lt;EObject&gt;)(collection
== null ? <br>
&gt;&gt; Collections.EMPTY_LIST : collection);<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;} <br>
&gt;&gt; <br>
&gt;&gt; <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;@Override<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;protected void doExecute() {<br>
&gt;&gt; <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;for (EObject selectedElt : myselection) {<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (myowner.eClass() ==
selectedElt.eContainer().eClass()) <br>
&gt;&gt; { //Deleting from the Container<br>
</font><font color="#0000ff">&gt; What about inheritance?<br>
</font><font color="#008000">&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;EcoreUtil.delete(sel ectedElt, true);<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else { //Deleting from a ParentCrossReferencer<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;if (new <br>
&gt;&gt;
ParentCrossReferencer(mydomain.getResourceSet()).findUsage(s electedElt).size( <br>
&gt;&gt; ) <br>
&gt;&gt; &lt; 2 ) {<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;EcoreUtil.delete(selectedElt, true);<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;} <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;else {<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EcoreUtil.remove(myowner, myfeature, <br>
&gt;&gt; selectedElt); &nbsp;&nbsp;<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;} &nbsp;&nbsp;<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;} <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;}<br>
&gt;&gt; <br>
&gt;&gt; <br>
&gt;&gt; Until now unless I'm wrong somewhere, the algorithm seams
pretty <br>
&gt;&gt; simple. As you can see above, I extended a ChangeCommad, to
record all <br>
&gt;&gt; that is happening. Alowwing me to have a undo, redo, etc...<br>
&gt;&gt; <br>
&gt;&gt; <br>
&gt;&gt; However, suprisingly, this where things get very complicate.
As you <br>
&gt;&gt; can see I am stuck with the use of a crossReferencer. &nbsp;I need
a <br>
&gt;&gt; Crossreferencer that is capable of telling me who are the <br>
&gt;&gt; Non-Containing Parent or the ParentCrossReferencer of that
Object. I <br>
&gt;&gt; have Attached the try I have done, but as you can see, my last
attempt <br>
&gt;&gt; pop me out a cast exeption.<br>
&gt;&gt; <br>
&gt;&gt; /**<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;* We check that the crossReferencedEObject is within
the <br>
&gt;&gt; children of the referencing Object.<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;* This method is heavy, it would be better to check
the <br>
&gt;&gt; children feature of the object against the reference.<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;*/ <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;@SuppressWarnings( "unchecked")<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;@Override <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;protected boolean crossReference(EObject object,
EReference <br>
&gt;&gt; reference, EObject crossReferencedEObject) {<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (super.crossReference(object, reference, <br>
&gt;&gt; crossReferencedEObject)) {<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>

&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;Collection&lt;EO bject&gt; children = <br>
&gt;&gt; mydomain.getChildren(object) == null ? Collections.EMPTY_LIST
: <br>
&gt;&gt; mydomain.getChildren(object) ;<br>
</font><font color="#0000ff">&gt; I think getChildren always returns
a collection....<br>
</font><font color="#008000">&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;for(EObject &nbsp;child : children) {<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;if ( EcoreUtil.equals(child,
crossReferencedEObject))<br>
</font><font color="#0000ff">&gt; Why a structural equality test
rather than an identity test? &nbsp;&nbsp;Don't you <br>
&gt; know exactly which feature is involved? E.g., <br>
&gt; XyzPackage.Literals.PARENT_CLASS__NON_CONTAINMENT_CHILD_FEAT URE?
&nbsp;So <br>
&gt; aren't you just trying to cross reference only that one feature so
it's <br>
&gt; just a quick == test on the feature and that's it?<br>
</font><font color="#008000">&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return
true;<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;} <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return &nbsp;false;<br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;} <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; <br>
&gt;&gt; &nbsp;&nbsp;&nbsp;&nbsp;}<br>
&gt;&gt; <br>
&gt;&gt; <br>
&gt;&gt; <br>
&gt;&gt; Please does any one has an Idea on how to solve this problem
of <br>
&gt;&gt; ParentCross-Referencer or Non-Containg Parent ?<br>
&gt;&gt; <br>
&gt;&gt; <br>
&gt;&gt; Many thanks,<br>
&gt;&gt; Maatari<br>
&gt;&gt; <br>
</font></span></font>
</blockquote>
</body>
</html>

--------------030205090901020302030506--


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: A RemoveChild Command: DELETING NON-CONTAINMENT CHILD Reference [message #427247 is a reply to message #427246] Sat, 07 February 2009 19:45 Go to previous messageGo to next message
Maatari is currently offline MaatariFriend
Messages: 4
Registered: July 2009
Junior Member
Hi,

I'm so sorry about the formatting, I have problem dealing with it but it
will come. Soon I will know how to manage that with my news read :-)


Please find in the following my current code:


1-The code in my Itemproviders overriding the removeCommand to deal with
child feature removal.


@Override
protected Command createRemoveCommand(EditingDomain domain, EObject owner,
EStructuralFeature feature, Collection<?> collection) {

if (this.getChildrenFeatures(owner).contains(feature)) {
return new RemoveChildCommand(domain, owner, feature, collection);
}

return super.createRemoveCommand(domain, owner, feature, collection);
}

2-The class RemoveCommand with the crossReferencer Inside:

public class RemoveChildCommand extends ChangeCommand {


static int i = 0;
protected EditingDomain mydomain = null;
protected EObject myowner = null;
protected EStructuralFeature myfeature = null;
protected Collection<EObject> myselection = null;


protected class ParentCrossReferencer extends UsageCrossReferencer {

private static final long serialVersionUID = 1L;


@Override
protected Collection<Setting> findUsage(EObject object) {
return super.findUsage(object);
}

protected ParentCrossReferencer(ResourceSet arg0) {
super(arg0);
}

/**
* We check that the crossReferencedEObject is within the children of
the referencing Object.
* This method is heavy, it would be better to check the children
feature of the object against the reference.
* Unfortunately, mydomain wich is of type EditingDomain interface which
has no getchildrenFeature
*/
@SuppressWarnings("unchecked")
@Override
protected boolean crossReference(EObject object, EReference reference,
EObject crossReferencedEObject) {


if (super.crossReference(object, reference, crossReferencedEObject)) {

Collection<Object> children = mydomain.getChildren(object) == null ?
Collections.EMPTY_LIST : mydomain.getChildren(object) ;

for(Object child : children) {
if (child instanceof IWrapperItemProvider)
if
(EcoreUtil.equals((EObject)((IWrapperItemProvider)child).get Value(),
crossReferencedEObject))
return true;
if (child instanceof EObject)
if ( EcoreUtil.equals((EObject)child, crossReferencedEObject))
return true;
try {
throw new Exception ("UnSupported object child:" + child.toString());
} catch (Exception e) {
e.printStackTrace();
}
}

}
return false;
}

}


@SuppressWarnings("unchecked")
public RemoveChildCommand(EditingDomain domain, EObject owner,
EStructuralFeature feature, Collection<?> collection) {

super(domain.getResourceSet());

mydomain = domain;
myowner = owner;
myfeature = feature;
myselection = (Collection<EObject>)(collection == null ?
Collections.EMPTY_LIST : collection);

}


@Override
protected void doExecute() {


for (EObject selectedElt : myselection) {


if (myowner.eClass() == selectedElt.eContainer().eClass()) { //Deleting
from the Container

EcoreUtil.delete(selectedElt, true);
}
else { //Deleting from a ParentCrossReferencer

if (new
ParentCrossReferencer(mydomain.getResourceSet()).findUsage(s electedElt).size()
< 2 ) {

EcoreUtil.delete(selectedElt, true);
}
else {

EcoreUtil.remove(myowner, myfeature, selectedElt);
}
}
}
}
}

This code Work And I have the delete I want. However, as mentioned
already, the check is so much heavy. In fact my question is how to do that
generically as i did. Is there any way to obtain the item adapter directly
instead of using mydomain only. This way I could use get children feature
and check that the current reference is in the list. Otherwise, my advise
is to add this fuction to the EditingDomain interface. I mean, what is the
problem with having this function is in any case the Editingdomain is just
a delagator....?

Concerning the behavior of my delete, I think I'm pretty satisfy with it
its a quite good one. What do you think of it, would have you done it the
same way?
Re: A RemoveChild Command: DELETING NON-CONTAINMENT CHILD Reference [message #427248 is a reply to message #427246] Sat, 07 February 2009 20:19 Go to previous messageGo to next message
Maatari is currently offline MaatariFriend
Messages: 4
Registered: July 2009
Junior Member
I think there was some mistake in my preceding code.




protected boolean crossReference(EObject object, EReference reference,
EObject crossReferencedEObject) {


if (super.crossReference(object, reference, crossReferencedEObject)) {

Collection<Object> children = mydomain.getChildren(object) == null ?
Collections.EMPTY_LIST : mydomain.getChildren(object) ;

for(Object child : children) {
if (child instanceof IWrapperItemProvider)
if
(EcoreUtil.equals((EObject)((IWrapperItemProvider)child).get Value(),
crossReferencedEObject))
return true;
else
continue;

if (child instanceof EObject)
if ( EcoreUtil.equals((EObject)child, crossReferencedEObject))
return true;
else
continue;
try {
throw new Exception ("UnSupported object child:" + child.toString());
} catch (Exception e) {
e.printStackTrace();
}
}

}
return false;
}


Here is the last version of my crossReference function. Again I still did
not find the way to access to the getchildren method. I use mydonain to
access the Itemprovider of that item. Otherwise,
I tryed EcoreUtil.getAdapters or existingadapter but it didn't work. I
don't even know why. The only thing i notice using the debuger is that the
list of adapters retreive from the object in the function is right but
then there is a test, namely: IsfactoryFortype, that always return false
as the type is tested again the adapterfactory contained. Test that i
don't understand :-) either. SO if you have any idea over all of that,
please let me know. Otherwise thank already
Re: A RemoveChild Command: DELETING NON-CONTAINMENT CHILD Reference [message #427250 is a reply to message #427248] Sat, 07 February 2009 20:54 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
Maatari wrote:
> I think there was some mistake in my preceding code.
>
>
>
> protected boolean crossReference(EObject object, EReference
> reference, EObject crossReferencedEObject) {
Do you think you could just check if the reference is among children
features of the item provider?
>
>
> if (super.crossReference(object, reference,
> crossReferencedEObject)) {
>
> Collection<Object> children =
> mydomain.getChildren(object) == null ? Collections.EMPTY_LIST :
> mydomain.getChildren(object) ;
>
> for(Object child : children) {
> if (child instanceof IWrapperItemProvider)
> if
> (EcoreUtil.equals((EObject)((IWrapperItemProvider)child).get Value(),
> crossReferencedEObject))
> return true; else
> continue;
>
> if (child instanceof EObject)
> if ( EcoreUtil.equals((EObject)child,
> crossReferencedEObject))
> return true;
> else
> continue;
> try {
> throw new Exception ("UnSupported object
> child:" + child.toString());
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
>
> }
> return false;
> }
>
>
> Here is the last version of my crossReference function. Again I still
> did not find the way to access to the getchildren method. I use
> mydonain to access the Itemprovider of that item. Otherwise, I tryed
> EcoreUtil.getAdapters or existingadapter but it didn't work. I don't
> even know why. The only thing i notice using the debuger is that the
> list of adapters retreive from the object in the function is right but
> then there is a test, namely: IsfactoryFortype, that always return
> false as the type is tested again the adapterfactory contained. Test
> that i don't understand :-) either. SO if you have any idea over all
> of that, please let me know. Otherwise thank already
>
>
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:JETEmitter cannot resolve classpath
Next Topic:EMF and GMF
Goto Forum:
  


Current Time: Fri Apr 26 05:19:32 GMT 2024

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

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

Back to the top