Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Auto-create node's children
Auto-create node's children [message #425960] Sat, 13 December 2008 20:45 Go to next message
Eclipse UserFriend
Originally posted by: nuclearpsyche.gmail.com

Hi,

When a user adds a node to the tree I would like that node to
automatically create two children for itself.
Where do I put the code that does this?
I want this to only happens the first time the node is created.
Thank you,

tamer
Re: Auto-create node's children [message #425962 is a reply to message #425960] Sat, 13 December 2008 23:31 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30996
Registered: July 2009
Senior Member
Tamer,

Comments below.


Tamer wrote:
> Hi,
>
> When a user adds a node to the tree I would like that node to
> automatically create two children for itself.
> Where do I put the code that does this?
You can specialize the collectNewChildDescriptors of the parent; note
that there is no need to use commands to create the initial instance
being added, i.e., you can just create a new object, initialize it
however you want, and pass it along as the new child to be created.
> I want this to only happens the first time the node is created.
> Thank you,
>
> tamer
Re: Auto-create node's children [message #425963 is a reply to message #425962] Sun, 14 December 2008 04:34 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: nuclearpsyche.gmail.com

Hi Ed,

Thank you for your response. I had some trouble applying what you said.
It seems like the function you mentioned gets called each time the node
is selected -- not when it is created. I want to run the code that
creates the children when the parent is created.
Also I don't see exactly how to pass the created children instead of
using an AddCommand.

Thanks again for your help.

tamer

Ed Merks wrote:
> Tamer,
>
> Comments below.
>
>
> Tamer wrote:
>> Hi,
>>
>> When a user adds a node to the tree I would like that node to
>> automatically create two children for itself.
>> Where do I put the code that does this?
> You can specialize the collectNewChildDescriptors of the parent; note
> that there is no need to use commands to create the initial instance
> being added, i.e., you can just create a new object, initialize it
> however you want, and pass it along as the new child to be created.
>> I want this to only happens the first time the node is created.
>> Thank you,
>>
>> tamer
Re: Auto-create node's children [message #425964 is a reply to message #425963] Sun, 14 December 2008 08:51 Go to previous messageGo to next message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6487
Registered: July 2009
Senior Member
Tamer,

Please note also that it's usually a bad idea to add automatic child
creation to objects on the model level.
This causes problem when you deserialize a model resource (duplicated
nodes).
This has been discussed here several times, e.g.
http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg2 6718.html

Cheers
/Eike

----
http://thegordian.blogspot.com



Tamer schrieb:
> Hi Ed,
>
> Thank you for your response. I had some trouble applying what you
> said. It seems like the function you mentioned gets called each time
> the node is selected -- not when it is created. I want to run the code
> that creates the children when the parent is created.
> Also I don't see exactly how to pass the created children instead of
> using an AddCommand.
>
> Thanks again for your help.
>
> tamer
>
> Ed Merks wrote:
>> Tamer,
>>
>> Comments below.
>>
>>
>> Tamer wrote:
>>> Hi,
>>>
>>> When a user adds a node to the tree I would like that node to
>>> automatically create two children for itself.
>>> Where do I put the code that does this?
>> You can specialize the collectNewChildDescriptors of the parent; note
>> that there is no need to use commands to create the initial instance
>> being added, i.e., you can just create a new object, initialize it
>> however you want, and pass it along as the new child to be created.
>>> I want this to only happens the first time the node is created.
>>> Thank you,
>>>
>>> tamer


Re: Auto-create node's children [message #425965 is a reply to message #425964] Sun, 14 December 2008 13:50 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30996
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------090501050901000505040004
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Guys,

Comments below.

Eike Stepper wrote:
> Tamer,
>
> Please note also that it's usually a bad idea to add automatic child
> creation to objects on the model level.
Definitely that's going to cause problems.
> This causes problem when you deserialize a model resource (duplicated
> nodes).
Deserializers and copiers just aren't going to work.
> This has been discussed here several times, e.g.
> http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg2 6718.html
>
> Cheers
> /Eike
>
> ----
> http://thegordian.blogspot.com
>
>
>
> Tamer schrieb:
>> Hi Ed,
>>
>> Thank you for your response. I had some trouble applying what you
>> said. It seems like the function you mentioned gets called each time
>> the node is selected -- not when it is created.
Yes, it's done as part of preparing the command.
>> I want to run the code that creates the children when the parent is
>> created.
You have to go one level up then. The parent is created under the grand
parent. Evey time a grand parent is selected, the commands for creating
children for it will be created and tested for executability to populate
the menu. At that point you can create the "parent", the child of the
"grand parent", and populate it with "children", grand children of the
grand parent.
>> Also I don't see exactly how to pass the created children instead of
>> using an AddCommand.
Here's an example from EGenericTypeItemProvider where we want to allow
upper and lower bound children to be created only if this generic type
is type argument of another generic type and we want to allow type
arguments to be created only if this generic type has a classifier with
type parameters.

/**
* This adds {@link
org.eclipse.emf.edit.command.CommandParameter}s describing the children
* that can be created under this object.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated NOT
*/
@Override
protected void collectNewChildDescriptors(Collection<Object>
newChildDescriptors, Object object)
{
super.collectNewChildDescriptors(newChildDescriptors, object);

EGenericType eGenericType = (EGenericType)object;

if (eGenericType.eContainmentFeature() ==
EcorePackage.Literals.EGENERIC_TYPE__ETYPE_ARGUMENTS &&
eGenericType.getEClassifier() == null &&
eGenericType.getETypeParameter() == null)
{
newChildDescriptors.add
(createChildParameter
(EcorePackage.Literals.EGENERIC_TYPE__EUPPER_BOUND,
EcoreFactory.eINSTANCE.createEGenericType()));
newChildDescriptors.add
(createChildParameter
(EcorePackage.Literals.EGENERIC_TYPE__ELOWER_BOUND,
EcoreFactory.eINSTANCE.createEGenericType()));
}

if (eGenericType.getEClassifier() != null &&
!eGenericType.getEClassifier().getETypeParameters().isEmpty( ))
{
newChildDescriptors.add
(createChildParameter
(EcorePackage.Literals.EGENERIC_TYPE__ETYPE_ARGUMENTS,
EcoreFactory.eINSTANCE.createEGenericType()));
}
}

It doesn't show prepopulation of the new object, but the new generic
type being created could have features set on it if that's what I'd
wanted...
>>
>> Thanks again for your help.
>>
>> tamer
>>
>> Ed Merks wrote:
>>> Tamer,
>>>
>>> Comments below.
>>>
>>>
>>> Tamer wrote:
>>>> Hi,
>>>>
>>>> When a user adds a node to the tree I would like that node to
>>>> automatically create two children for itself.
>>>> Where do I put the code that does this?
>>> You can specialize the collectNewChildDescriptors of the parent;
>>> note that there is no need to use commands to create the initial
>>> instance being added, i.e., you can just create a new object,
>>> initialize it however you want, and pass it along as the new child
>>> to be created.
>>>> I want this to only happens the first time the node is created.
>>>> Thank you,
>>>>
>>>> tamer

--------------090501050901000505040004
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">
Guys,<br>
<br>
Comments below.<br>
<br>
Eike Stepper wrote:
<blockquote cite="mid:gi2hdp$d3o$1@build.eclipse.org" type="cite">Tamer,
<br>
<br>
Please note also that it's usually a bad idea to add automatic child
creation to objects on the model level.
<br>
</blockquote>
Definitely that's going to cause problems.<br>
<blockquote cite="mid:gi2hdp$d3o$1@build.eclipse.org" type="cite">This
causes problem when you deserialize a model resource (duplicated
nodes).
<br>
</blockquote>
Deserializers and copiers just aren't going to work.<br>
<blockquote cite="mid:gi2hdp$d3o$1@build.eclipse.org" type="cite">This
has been discussed here several times, e.g.
<a class="moz-txt-link-freetext" href=" http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg2 6718.html"> http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg2 6718.html</a>
<br>
<br>
Cheers
<br>
/Eike
<br>
<br>
----
<br>
<a class="moz-txt-link-freetext" href="http://thegordian.blogspot.com">http://thegordian.blogspot.com</a>
<br>
<br>
<br>
<br>
Tamer schrieb:
<br>
<blockquote type="cite">Hi Ed,
<br>
<br>
Thank you for your response. I had some trouble applying what you said.
It seems like the function you mentioned gets called each time the node
is selected -- not when it is created. </blockquote>
</blockquote>
Yes, it's done as part of preparing the command.<br>
<blockquote cite="mid:gi2hdp$d3o$1@build.eclipse.org" type="cite">
<blockquote type="cite">I want to run the code that creates the
children when the parent is created.
<br>
</blockquote>
</blockquote>
You have to go one level up then.&nbsp; The parent is created under the
grand parent.&nbsp; Evey time a grand parent is selected, the commands for
creating children for it will be created and tested for executability
to populate the menu.&nbsp; At that point you can create the "parent", the
child of the "grand parent", and populate it with "children", grand
children of the grand parent. <br>
<blockquote cite="mid:gi2hdp$d3o$1@build.eclipse.org" type="cite">
<blockquote type="cite">Also I don't see exactly how to pass the
created children instead of using an AddCommand.
<br>
</blockquote>
</blockquote>
Here's an example from EGenericTypeItemProvider where we want to allow
upper and lower bound children to be created only if this generic type
is type argument of another generic type and we want to allow type
arguments to be created only if this generic type has a classifier with
type parameters.<br>
<blockquote><small>&nbsp; /**</small><br>
<small>&nbsp;&nbsp; * This adds {@link
org.eclipse.emf.edit.command.CommandParameter}s describing the children</small><br>
<small>&nbsp;&nbsp; * that can be created under this object.</small><br>
<small>&nbsp;&nbsp; * &lt;!-- begin-user-doc --&gt;</small><br>
<small>&nbsp;&nbsp; * &lt;!-- end-user-doc --&gt;</small><br>
<small>&nbsp;&nbsp; * @generated NOT</small><br>
<small>&nbsp;&nbsp; */</small><br>
<small>&nbsp; @Override</small><br>
<small>&nbsp; protected void
collectNewChildDescriptors(Collection&lt;Object&gt;
newChildDescriptors, Object object)</small><br>
<small>&nbsp; {</small><br>
<small>&nbsp;&nbsp;&nbsp; super.collectNewChildDescriptors(newChildDescriptors,
object);</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; EGenericType eGenericType = (EGenericType)object;</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; if (eGenericType.eContainmentFeature() ==
EcorePackage.Literals.EGENERIC_TYPE__ETYPE_ARGUMENTS &amp;&amp;</small><br>
<small> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; eGenericType.getEClassifier() == null &amp;&amp; </small><br>
<small> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; eGenericType.getETypeParameter() == null)</small><br>
<small>&nbsp;&nbsp;&nbsp; {</small><br>
<small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newChildDescriptors.add</small><br>
<small> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; (createChildParameter</small><br>
<small> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (EcorePackage.Literals.EGENERIC_TYPE__EUPPER_BOUND,</small> <br>
<small> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; EcoreFactory.eINSTANCE.createEGenericType()));</small><br>
<small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newChildDescriptors.add</small><br>
<small> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; (createChildParameter</small><br>
<small> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (EcorePackage.Literals.EGENERIC_TYPE__ELOWER_BOUND,</small> <br>
<small> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; EcoreFactory.eINSTANCE.createEGenericType()));</small><br>
<small>&nbsp;&nbsp;&nbsp; }</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; if (eGenericType.getEClassifier() != null &amp;&amp;
!eGenericType.getEClassifier().getETypeParameters().isEmpty( )) </small><br>
<small>&nbsp;&nbsp;&nbsp; {</small><br>
<small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newChildDescriptors.add</small><br>
<small> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; (createChildParameter</small><br>
<small> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
(EcorePackage.Literals.EGENERIC_TYPE__ETYPE_ARGUMENTS,</small ><br>
<small> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; EcoreFactory.eINSTANCE.createEGenericType()));</small><br>
<small>&nbsp;&nbsp;&nbsp; }</small><br>
<small>&nbsp; }</small><br>
</blockquote>
It doesn't show prepopulation of the new object, but the new generic
type being created could have features set on it if that's what I'd
wanted...<br>
<blockquote cite="mid:gi2hdp$d3o$1@build.eclipse.org" type="cite">
<blockquote type="cite"><br>
Thanks again for your help.
<br>
<br>
tamer
<br>
<br>
Ed Merks wrote:
<br>
<blockquote type="cite">Tamer,
<br>
<br>
Comments below.
<br>
<br>
<br>
Tamer wrote:
<br>
<blockquote type="cite">Hi,
<br>
<br>
When a user adds a node to the tree I would like that node to
automatically create two children for itself.
<br>
Where do I put the code that does this?
<br>
</blockquote>
You can specialize the collectNewChildDescriptors of the parent; note
that there is no need to use commands to create the initial instance
being added, i.e., you can just create a new object, initialize it
however you want, and pass it along as the new child to be created.
<br>
<blockquote type="cite">I want this to only happens the first
time the node is created.
<br>
Thank you,
<br>
<br>
tamer
<br>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</body>
</html>

--------------090501050901000505040004--
Re: Auto-create node's children [message #425968 is a reply to message #425965] Sun, 14 December 2008 18:34 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: nuclearpsyche.gmail.com

Thank you both for your response. I appreciate your input/help.

Would it be a better idea to customize notifyChanged() method of the
parent to check for
if(notification.getEventType()==Notification.ADD)
And then create the children then? Or is that problematic?

Thank you!

tamer

Ed Merks wrote:
> Guys,
>
> Comments below.
>
> Eike Stepper wrote:
>> Tamer,
>>
>> Please note also that it's usually a bad idea to add automatic child
>> creation to objects on the model level.
> Definitely that's going to cause problems.
>> This causes problem when you deserialize a model resource (duplicated
>> nodes).
> Deserializers and copiers just aren't going to work.
>> This has been discussed here several times, e.g.
>> http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg2 6718.html
>>
>> Cheers
>> /Eike
>>
>> ----
>> http://thegordian.blogspot.com
>>
>>
>>
>> Tamer schrieb:
>>> Hi Ed,
>>>
>>> Thank you for your response. I had some trouble applying what you
>>> said. It seems like the function you mentioned gets called each time
>>> the node is selected -- not when it is created.
> Yes, it's done as part of preparing the command.
>>> I want to run the code that creates the children when the parent is
>>> created.
> You have to go one level up then. The parent is created under the grand
> parent. Evey time a grand parent is selected, the commands for creating
> children for it will be created and tested for executability to populate
> the menu. At that point you can create the "parent", the child of the
> "grand parent", and populate it with "children", grand children of the
> grand parent.
>>> Also I don't see exactly how to pass the created children instead of
>>> using an AddCommand.
> Here's an example from EGenericTypeItemProvider where we want to allow
> upper and lower bound children to be created only if this generic type
> is type argument of another generic type and we want to allow type
> arguments to be created only if this generic type has a classifier with
> type parameters.
>
> /**
> * This adds {@link
> org.eclipse.emf.edit.command.CommandParameter}s describing the children
> * that can be created under this object.
> * <!-- begin-user-doc -->
> * <!-- end-user-doc -->
> * @generated NOT
> */
> @Override
> protected void collectNewChildDescriptors(Collection<Object>
> newChildDescriptors, Object object)
> {
> super.collectNewChildDescriptors(newChildDescriptors, object);
>
> EGenericType eGenericType = (EGenericType)object;
>
> if (eGenericType.eContainmentFeature() ==
> EcorePackage.Literals.EGENERIC_TYPE__ETYPE_ARGUMENTS &&
> eGenericType.getEClassifier() == null &&
> eGenericType.getETypeParameter() == null)
> {
> newChildDescriptors.add
> (createChildParameter
> (EcorePackage.Literals.EGENERIC_TYPE__EUPPER_BOUND,
> EcoreFactory.eINSTANCE.createEGenericType()));
> newChildDescriptors.add
> (createChildParameter
> (EcorePackage.Literals.EGENERIC_TYPE__ELOWER_BOUND,
> EcoreFactory.eINSTANCE.createEGenericType()));
> }
>
> if (eGenericType.getEClassifier() != null &&
> !eGenericType.getEClassifier().getETypeParameters().isEmpty( ))
> {
> newChildDescriptors.add
> (createChildParameter
> (EcorePackage.Literals.EGENERIC_TYPE__ETYPE_ARGUMENTS,
> EcoreFactory.eINSTANCE.createEGenericType()));
> }
> }
>
> It doesn't show prepopulation of the new object, but the new generic
> type being created could have features set on it if that's what I'd
> wanted...
>>>
>>> Thanks again for your help.
>>>
>>> tamer
>>>
>>> Ed Merks wrote:
>>>> Tamer,
>>>>
>>>> Comments below.
>>>>
>>>>
>>>> Tamer wrote:
>>>>> Hi,
>>>>>
>>>>> When a user adds a node to the tree I would like that node to
>>>>> automatically create two children for itself.
>>>>> Where do I put the code that does this?
>>>> You can specialize the collectNewChildDescriptors of the parent;
>>>> note that there is no need to use commands to create the initial
>>>> instance being added, i.e., you can just create a new object,
>>>> initialize it however you want, and pass it along as the new child
>>>> to be created.
>>>>> I want this to only happens the first time the node is created.
>>>>> Thank you,
>>>>>
>>>>> tamer
Re: Auto-create node's children [message #425970 is a reply to message #425968] Sun, 14 December 2008 19:52 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30996
Registered: July 2009
Senior Member
Tamer,

Comments below.

Tamer wrote:
> Thank you both for your response. I appreciate your input/help.
>
> Would it be a better idea to customize notifyChanged() method of the
> parent to check for
> if(notification.getEventType()==Notification.ADD)
> And then create the children then? Or is that problematic?
Don't do anything directly in the model in this regard. The item
provider is the place where new children are created and that's exactly
the one and only place you should be creating prepopulated additions.
I'm not quite sure why you don't just try what's already been suggested...
>
> Thank you!
>
> tamer
>
> Ed Merks wrote:
>> Guys,
>>
>> Comments below.
>>
>> Eike Stepper wrote:
>>> Tamer,
>>>
>>> Please note also that it's usually a bad idea to add automatic child
>>> creation to objects on the model level.
>> Definitely that's going to cause problems.
>>> This causes problem when you deserialize a model resource
>>> (duplicated nodes).
>> Deserializers and copiers just aren't going to work.
>>> This has been discussed here several times, e.g.
>>> http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg2 6718.html
>>>
>>> Cheers
>>> /Eike
>>>
>>> ----
>>> http://thegordian.blogspot.com
>>>
>>>
>>>
>>> Tamer schrieb:
>>>> Hi Ed,
>>>>
>>>> Thank you for your response. I had some trouble applying what you
>>>> said. It seems like the function you mentioned gets called each
>>>> time the node is selected -- not when it is created.
>> Yes, it's done as part of preparing the command.
>>>> I want to run the code that creates the children when the parent is
>>>> created.
>> You have to go one level up then. The parent is created under the
>> grand parent. Evey time a grand parent is selected, the commands for
>> creating children for it will be created and tested for executability
>> to populate the menu. At that point you can create the "parent", the
>> child of the "grand parent", and populate it with "children", grand
>> children of the grand parent.
>>>> Also I don't see exactly how to pass the created children instead
>>>> of using an AddCommand.
>> Here's an example from EGenericTypeItemProvider where we want to
>> allow upper and lower bound children to be created only if this
>> generic type is type argument of another generic type and we want to
>> allow type arguments to be created only if this generic type has a
>> classifier with type parameters.
>>
>> /**
>> * This adds {@link
>> org.eclipse.emf.edit.command.CommandParameter}s describing the
>> children
>> * that can be created under this object.
>> * <!-- begin-user-doc -->
>> * <!-- end-user-doc -->
>> * @generated NOT
>> */
>> @Override
>> protected void collectNewChildDescriptors(Collection<Object>
>> newChildDescriptors, Object object)
>> {
>> super.collectNewChildDescriptors(newChildDescriptors, object);
>>
>> EGenericType eGenericType = (EGenericType)object;
>>
>> if (eGenericType.eContainmentFeature() ==
>> EcorePackage.Literals.EGENERIC_TYPE__ETYPE_ARGUMENTS &&
>> eGenericType.getEClassifier() == null &&
>> eGenericType.getETypeParameter() == null)
>> {
>> newChildDescriptors.add
>> (createChildParameter
>> (EcorePackage.Literals.EGENERIC_TYPE__EUPPER_BOUND,
>> EcoreFactory.eINSTANCE.createEGenericType()));
>> newChildDescriptors.add
>> (createChildParameter
>> (EcorePackage.Literals.EGENERIC_TYPE__ELOWER_BOUND,
>> EcoreFactory.eINSTANCE.createEGenericType()));
>> }
>>
>> if (eGenericType.getEClassifier() != null &&
>> !eGenericType.getEClassifier().getETypeParameters().isEmpty( ))
>> {
>> newChildDescriptors.add
>> (createChildParameter
>> (EcorePackage.Literals.EGENERIC_TYPE__ETYPE_ARGUMENTS,
>> EcoreFactory.eINSTANCE.createEGenericType()));
>> }
>> }
>>
>> It doesn't show prepopulation of the new object, but the new generic
>> type being created could have features set on it if that's what I'd
>> wanted...
>>>>
>>>> Thanks again for your help.
>>>>
>>>> tamer
>>>>
>>>> Ed Merks wrote:
>>>>> Tamer,
>>>>>
>>>>> Comments below.
>>>>>
>>>>>
>>>>> Tamer wrote:
>>>>>> Hi,
>>>>>>
>>>>>> When a user adds a node to the tree I would like that node to
>>>>>> automatically create two children for itself.
>>>>>> Where do I put the code that does this?
>>>>> You can specialize the collectNewChildDescriptors of the parent;
>>>>> note that there is no need to use commands to create the initial
>>>>> instance being added, i.e., you can just create a new object,
>>>>> initialize it however you want, and pass it along as the new child
>>>>> to be created.
>>>>>> I want this to only happens the first time the node is created.
>>>>>> Thank you,
>>>>>>
>>>>>> tamer
Re: Auto-create node's children [message #425971 is a reply to message #425970] Sun, 14 December 2008 20:16 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: nuclearpsyche.gmail.com

Hi Ed,

I tried what you suggested but I actually use an EMF/GMF combined
editor. So the solution works from the tab of my editor where I have the
model displayed as a tree but it doesn't work from the diagram tab.
I'm also using a transactional editing domain so maybe that's why I have
to use AddCommands to populate the node's children.
Best regards,

tamer

Ed Merks wrote:
> Tamer,
>
> Comments below.
>
> Tamer wrote:
>> Thank you both for your response. I appreciate your input/help.
>>
>> Would it be a better idea to customize notifyChanged() method of the
>> parent to check for
>> if(notification.getEventType()==Notification.ADD)
>> And then create the children then? Or is that problematic?
> Don't do anything directly in the model in this regard. The item
> provider is the place where new children are created and that's exactly
> the one and only place you should be creating prepopulated additions.
> I'm not quite sure why you don't just try what's already been suggested...
>>
>> Thank you!
>>
>> tamer
>>
>> Ed Merks wrote:
>>> Guys,
>>>
>>> Comments below.
>>>
>>> Eike Stepper wrote:
>>>> Tamer,
>>>>
>>>> Please note also that it's usually a bad idea to add automatic child
>>>> creation to objects on the model level.
>>> Definitely that's going to cause problems.
>>>> This causes problem when you deserialize a model resource
>>>> (duplicated nodes).
>>> Deserializers and copiers just aren't going to work.
>>>> This has been discussed here several times, e.g.
>>>> http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg2 6718.html
>>>>
>>>> Cheers
>>>> /Eike
>>>>
>>>> ----
>>>> http://thegordian.blogspot.com
>>>>
>>>>
>>>>
>>>> Tamer schrieb:
>>>>> Hi Ed,
>>>>>
>>>>> Thank you for your response. I had some trouble applying what you
>>>>> said. It seems like the function you mentioned gets called each
>>>>> time the node is selected -- not when it is created.
>>> Yes, it's done as part of preparing the command.
>>>>> I want to run the code that creates the children when the parent is
>>>>> created.
>>> You have to go one level up then. The parent is created under the
>>> grand parent. Evey time a grand parent is selected, the commands for
>>> creating children for it will be created and tested for executability
>>> to populate the menu. At that point you can create the "parent", the
>>> child of the "grand parent", and populate it with "children", grand
>>> children of the grand parent.
>>>>> Also I don't see exactly how to pass the created children instead
>>>>> of using an AddCommand.
>>> Here's an example from EGenericTypeItemProvider where we want to
>>> allow upper and lower bound children to be created only if this
>>> generic type is type argument of another generic type and we want to
>>> allow type arguments to be created only if this generic type has a
>>> classifier with type parameters.
>>>
>>> /**
>>> * This adds {@link
>>> org.eclipse.emf.edit.command.CommandParameter}s describing the
>>> children
>>> * that can be created under this object.
>>> * <!-- begin-user-doc -->
>>> * <!-- end-user-doc -->
>>> * @generated NOT
>>> */
>>> @Override
>>> protected void collectNewChildDescriptors(Collection<Object>
>>> newChildDescriptors, Object object)
>>> {
>>> super.collectNewChildDescriptors(newChildDescriptors, object);
>>>
>>> EGenericType eGenericType = (EGenericType)object;
>>>
>>> if (eGenericType.eContainmentFeature() ==
>>> EcorePackage.Literals.EGENERIC_TYPE__ETYPE_ARGUMENTS &&
>>> eGenericType.getEClassifier() == null &&
>>> eGenericType.getETypeParameter() == null)
>>> {
>>> newChildDescriptors.add
>>> (createChildParameter
>>> (EcorePackage.Literals.EGENERIC_TYPE__EUPPER_BOUND,
>>> EcoreFactory.eINSTANCE.createEGenericType()));
>>> newChildDescriptors.add
>>> (createChildParameter
>>> (EcorePackage.Literals.EGENERIC_TYPE__ELOWER_BOUND,
>>> EcoreFactory.eINSTANCE.createEGenericType()));
>>> }
>>>
>>> if (eGenericType.getEClassifier() != null &&
>>> !eGenericType.getEClassifier().getETypeParameters().isEmpty( ))
>>> {
>>> newChildDescriptors.add
>>> (createChildParameter
>>> (EcorePackage.Literals.EGENERIC_TYPE__ETYPE_ARGUMENTS,
>>> EcoreFactory.eINSTANCE.createEGenericType()));
>>> }
>>> }
>>>
>>> It doesn't show prepopulation of the new object, but the new generic
>>> type being created could have features set on it if that's what I'd
>>> wanted...
>>>>>
>>>>> Thanks again for your help.
>>>>>
>>>>> tamer
>>>>>
>>>>> Ed Merks wrote:
>>>>>> Tamer,
>>>>>>
>>>>>> Comments below.
>>>>>>
>>>>>>
>>>>>> Tamer wrote:
>>>>>>> Hi,
>>>>>>>
>>>>>>> When a user adds a node to the tree I would like that node to
>>>>>>> automatically create two children for itself.
>>>>>>> Where do I put the code that does this?
>>>>>> You can specialize the collectNewChildDescriptors of the parent;
>>>>>> note that there is no need to use commands to create the initial
>>>>>> instance being added, i.e., you can just create a new object,
>>>>>> initialize it however you want, and pass it along as the new child
>>>>>> to be created.
>>>>>>> I want this to only happens the first time the node is created.
>>>>>>> Thank you,
>>>>>>>
>>>>>>> tamer
Re: Auto-create node's children [message #425973 is a reply to message #425971] Mon, 15 December 2008 00:57 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 30996
Registered: July 2009
Senior Member
Tamer,

In GMF there is some type of "creation tool". I'm not sure how that
works, but it's probably something similar that you can specialize.

I don't think the editing domain can know about newly created objects
until you attach them to a parent, so I doubt editing domains are an
issue. Keep in mind that an AddCommand is used when you do, for
example, a copy and paste, or drag and drop to move something to a
different parent in the tree, so it's highly unlikely that specializing
AddCommand will not result in desirable side-effects.


Tamer wrote:
> Hi Ed,
>
> I tried what you suggested but I actually use an EMF/GMF combined
> editor. So the solution works from the tab of my editor where I have
> the model displayed as a tree but it doesn't work from the diagram tab.
> I'm also using a transactional editing domain so maybe that's why I
> have to use AddCommands to populate the node's children.
> Best regards,
>
> tamer
>
> Ed Merks wrote:
>> Tamer,
>>
>> Comments below.
>>
>> Tamer wrote:
>>> Thank you both for your response. I appreciate your input/help.
>>>
>>> Would it be a better idea to customize notifyChanged() method of the
>>> parent to check for
>>> if(notification.getEventType()==Notification.ADD)
>>> And then create the children then? Or is that problematic?
>> Don't do anything directly in the model in this regard. The item
>> provider is the place where new children are created and that's
>> exactly the one and only place you should be creating prepopulated
>> additions. I'm not quite sure why you don't just try what's already
>> been suggested...
>>>
>>> Thank you!
>>>
>>> tamer
>>>
>>> Ed Merks wrote:
>>>> Guys,
>>>>
>>>> Comments below.
>>>>
>>>> Eike Stepper wrote:
>>>>> Tamer,
>>>>>
>>>>> Please note also that it's usually a bad idea to add automatic
>>>>> child creation to objects on the model level.
>>>> Definitely that's going to cause problems.
>>>>> This causes problem when you deserialize a model resource
>>>>> (duplicated nodes).
>>>> Deserializers and copiers just aren't going to work.
>>>>> This has been discussed here several times, e.g.
>>>>> http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg2 6718.html
>>>>>
>>>>> Cheers
>>>>> /Eike
>>>>>
>>>>> ----
>>>>> http://thegordian.blogspot.com
>>>>>
>>>>>
>>>>>
>>>>> Tamer schrieb:
>>>>>> Hi Ed,
>>>>>>
>>>>>> Thank you for your response. I had some trouble applying what you
>>>>>> said. It seems like the function you mentioned gets called each
>>>>>> time the node is selected -- not when it is created.
>>>> Yes, it's done as part of preparing the command.
>>>>>> I want to run the code that creates the children when the parent
>>>>>> is created.
>>>> You have to go one level up then. The parent is created under the
>>>> grand parent. Evey time a grand parent is selected, the commands
>>>> for creating children for it will be created and tested for
>>>> executability to populate the menu. At that point you can create
>>>> the "parent", the child of the "grand parent", and populate it with
>>>> "children", grand children of the grand parent.
>>>>>> Also I don't see exactly how to pass the created children instead
>>>>>> of using an AddCommand.
>>>> Here's an example from EGenericTypeItemProvider where we want to
>>>> allow upper and lower bound children to be created only if this
>>>> generic type is type argument of another generic type and we want
>>>> to allow type arguments to be created only if this generic type has
>>>> a classifier with type parameters.
>>>>
>>>> /**
>>>> * This adds {@link
>>>> org.eclipse.emf.edit.command.CommandParameter}s describing the
>>>> children
>>>> * that can be created under this object.
>>>> * <!-- begin-user-doc -->
>>>> * <!-- end-user-doc -->
>>>> * @generated NOT
>>>> */
>>>> @Override
>>>> protected void collectNewChildDescriptors(Collection<Object>
>>>> newChildDescriptors, Object object)
>>>> {
>>>> super.collectNewChildDescriptors(newChildDescriptors, object);
>>>>
>>>> EGenericType eGenericType = (EGenericType)object;
>>>>
>>>> if (eGenericType.eContainmentFeature() ==
>>>> EcorePackage.Literals.EGENERIC_TYPE__ETYPE_ARGUMENTS &&
>>>> eGenericType.getEClassifier() == null &&
>>>> eGenericType.getETypeParameter() == null)
>>>> {
>>>> newChildDescriptors.add
>>>> (createChildParameter
>>>> (EcorePackage.Literals.EGENERIC_TYPE__EUPPER_BOUND,
>>>> EcoreFactory.eINSTANCE.createEGenericType()));
>>>> newChildDescriptors.add
>>>> (createChildParameter
>>>> (EcorePackage.Literals.EGENERIC_TYPE__ELOWER_BOUND,
>>>> EcoreFactory.eINSTANCE.createEGenericType()));
>>>> }
>>>>
>>>> if (eGenericType.getEClassifier() != null &&
>>>> !eGenericType.getEClassifier().getETypeParameters().isEmpty( ))
>>>> {
>>>> newChildDescriptors.add
>>>> (createChildParameter
>>>> (EcorePackage.Literals.EGENERIC_TYPE__ETYPE_ARGUMENTS,
>>>> EcoreFactory.eINSTANCE.createEGenericType()));
>>>> }
>>>> }
>>>>
>>>> It doesn't show prepopulation of the new object, but the new
>>>> generic type being created could have features set on it if that's
>>>> what I'd wanted...
>>>>>>
>>>>>> Thanks again for your help.
>>>>>>
>>>>>> tamer
>>>>>>
>>>>>> Ed Merks wrote:
>>>>>>> Tamer,
>>>>>>>
>>>>>>> Comments below.
>>>>>>>
>>>>>>>
>>>>>>> Tamer wrote:
>>>>>>>> Hi,
>>>>>>>>
>>>>>>>> When a user adds a node to the tree I would like that node to
>>>>>>>> automatically create two children for itself.
>>>>>>>> Where do I put the code that does this?
>>>>>>> You can specialize the collectNewChildDescriptors of the parent;
>>>>>>> note that there is no need to use commands to create the initial
>>>>>>> instance being added, i.e., you can just create a new object,
>>>>>>> initialize it however you want, and pass it along as the new
>>>>>>> child to be created.
>>>>>>>> I want this to only happens the first time the node is created.
>>>>>>>> Thank you,
>>>>>>>>
>>>>>>>> tamer
Previous Topic:ECore Editor not displaying as model tree
Next Topic:[CDO] CDOObject containment problems
Goto Forum:
  


Current Time: Tue Mar 31 01:21:41 GMT 2020

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

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

Back to the top