Re: Proggrammaticaly add an element for the "root" model element (access the roo [message #123840] |
Thu, 03 May 2007 08:17  |
Eclipse User |
|
|
|
Originally posted by: ben.renwart.skynet.be
Hi Khai,
Thanks for your fast reply. I'd like to be able to do the first example
you gave me and the last one :
"Is it the case that you have a Diagram opened in an editor then by some
code (an action maybe) you want to add an element to your root model?"
My root model element contains a list (EList) of objects. When I create
a new object on my diagram, it's added in the list. I'd like to add
programatically an another object in this list (when the new one is
created with the editor). The programatically-created object (which will
be invisible in the visual editor) will be linked with the created
object in the diagram which is visible.
"Or is it the simplest case where you have a root element (as example a
Car) that contains other elements (as example Doors) and you've used the
Factory to create Door but does not know how to add the Door to the Car?"
The example that I give you above will probably need the access the an
element and use the factory to create a new element and finally add this
element to another one. I want too to access to attributes of a model
element to modify programatically some attributes ?
Could you give me pieces of code to do these steps ?
Thanks,
Ben.
> Is it the case that you have a Diagram opened in an editor then by some
> code (an action maybe) you want to add an element to your root model?
>
> Or is it the case that when ever you create a new Diagram you want to
> automatically create an element and add it to the root model element?
>
> Or is it the simplest case where you have a root element (as example a
> Car) that contains other elements (as example Doors) and you've used the
> Factory to create Door but does not know how to add the Door to the Car?
>
>
> Each of the three above are very different so can you provide some more
> detail on which scenario matches what you're trying to do?
>
>
> -- Khai --
>
>
>
>
> Ben Renwart wrote:
>
>> Hi everybody,
>>
>> I'm new with GMF and I'm facing lots of trouble these times :
>>
>> I'd like programatically modify my "root" model element. I've already
>> post this request but I don't find how to proceed really... I'm using
>> the XXXFactory singleton to create a new instance of the model element
>> I want to add to my model. The problem is that I don't know the right
>> way to access the element where to add the freshly created one. Please
>> note that I don't want to display on the editor the new element : It's
>> only an internal element which is aimed to stay invisible.
>>
>> Note that the fact of creating a new element appears in the "Impl"
>> model classes located in the package "project_name".impl
>>
>> Could you provide me a piece of code that show me the way to do that ?
>>
>> Regards,
>> benren.
|
|
|
Re: Proggrammaticaly add an element for the "root" model element (access the roo [message #123916 is a reply to message #123840] |
Thu, 03 May 2007 12:44   |
Eclipse User |
|
|
|
Originally posted by: khai.n.situvista.com
To do all of what you need you would have to override a method in the
generated code.
In this example let assume these:
The root element is an instance of RootElement
The child element is an instance of ChildElement
The project is called MyProject
Locate the RootElementItemSemanticEditPolicy in the
....diagram.edit.policies package of your generated code.
In this class look for a private class named
CreateChildElement_xxxCommand where xxx is some number.
In this private class you need to implement/override the method
doDefaultElementCreation()
To see the code and comments better just copy and paste the code from
below into this private class. You will, of course, need to replace
the the appropriate names with your specific names
Depending on your actual model you may have more than one
CreateChildElement_xxxCommand private classes. If that's the case then
you would need to do the same for all of them.
I hope this helps
-- Khai --
Also, can you use the 'reply' feature of your newsreader so that the
messages are attached to the same thread? This last message of yours
started a new thread which makes it difficult to follow later.
===============copy the code below==================
protected EObject doDefaultElementCreation() {
/*
* First get the default created element
*/
EObject defaultElement = super.doDefaultElementCreation();
if (defaultElement instanceof ChildElement) {
/*
* If you need to set some attributes of the newly created
* element you can cast it into the appropriate type and set/get
* the attributes as normal
*/
ChildElement createdElement = (ChildElement) defaultElement;
/*
* If you want to add an extra element of the SAME type to the
* same parent then use this code to create it. Once created you
* can cast it and use get/set like normal for its attributes.
*
* HINT: EMFCoreUtil.create() has already added the element to
* the parent automatically.
*/
EObject elementToEdit = getElementToEdit();
EReference containment = getContainmentFeature();
EClass eClass = getElementType().getEClass();
ChildElement extraElement = (ChildElement)
EMFCoreUtil.create(elementToEdit, containment, eClass);
/*
* If the extra element you want to create is a different type
* and is contained in a different feature (but the same
* parent) then use this code
*/
//EObject elementToEdit = getElementToEdit();
//EReference containment =
MyProjectPackage.Literals.ROOT_ELEMENT__DIFFERENT_CHILD_ELEM ENTS;
//EClass eClass = MyProjectPackage.Literals.DIFFERENT_CHILD;
//DifferentChildElement extraElement = (DifferentChildElement)
EMFCoreUtil.create(elementToEdit, containment, eClass);
}
/*
* Return the default created element
*/
return defaultElement;
}
========================================================
Ben Renwart wrote:
> Hi Khai,
>
> Thanks for your fast reply. I'd like to be able to do the first example
> you gave me and the last one :
>
> "Is it the case that you have a Diagram opened in an editor then by some
> code (an action maybe) you want to add an element to your root model?"
>
> My root model element contains a list (EList) of objects. When I create
> a new object on my diagram, it's added in the list. I'd like to add
> programatically an another object in this list (when the new one is
> created with the editor). The programatically-created object (which will
> be invisible in the visual editor) will be linked with the created
> object in the diagram which is visible.
>
> "Or is it the simplest case where you have a root element (as example a
> Car) that contains other elements (as example Doors) and you've used the
> Factory to create Door but does not know how to add the Door to the Car?"
>
> The example that I give you above will probably need the access the an
> element and use the factory to create a new element and finally add this
> element to another one. I want too to access to attributes of a model
> element to modify programatically some attributes ?
>
> Could you give me pieces of code to do these steps ?
>
> Thanks,
>
> Ben.
>
>> Is it the case that you have a Diagram opened in an editor then by some
>> code (an action maybe) you want to add an element to your root model?
>>
>> Or is it the case that when ever you create a new Diagram you want to
>> automatically create an element and add it to the root model element?
>>
>> Or is it the simplest case where you have a root element (as example a
>> Car) that contains other elements (as example Doors) and you've used the
>> Factory to create Door but does not know how to add the Door to the Car?
>>
>>
>> Each of the three above are very different so can you provide some more
>> detail on which scenario matches what you're trying to do?
>>
>>
>> -- Khai --
>>
>>
>>
>>
>> Ben Renwart wrote:
>>
>>> Hi everybody,
>>>
>>> I'm new with GMF and I'm facing lots of trouble these times :
>>>
>>> I'd like programatically modify my "root" model element. I've already
>>> post this request but I don't find how to proceed really... I'm using
>>> the XXXFactory singleton to create a new instance of the model
>>> element I want to add to my model. The problem is that I don't know
>>> the right way to access the element where to add the freshly created
>>> one. Please note that I don't want to display on the editor the new
>>> element : It's only an internal element which is aimed to stay
>>> invisible.
>>>
>>> Note that the fact of creating a new element appears in the "Impl"
>>> model classes located in the package "project_name".impl
>>>
>>> Could you provide me a piece of code that show me the way to do that ?
>>>
>>> Regards,
>>> benren.
>
|
|
|
|
Re: Proggrammaticaly add an element for the "root" model element (access the roo [message #124022 is a reply to message #123983] |
Fri, 04 May 2007 05:25   |
Eclipse User |
|
|
|
Originally posted by: khai.n.situvista.com
You are getting this behavior because the 'invisible' element you
created is the same type as the visible element created by the action
but GMF does not know that some should be 'invisible'
Let me explain further.
Assume your RootElement has a list containing ChildElement's. When you
open a GMF editor the framework will ask for this list. Then for each
ChildElement in the list it will check if a graphical View definition
already exist for it; if none exist then it will automatically create
the default View for it. The editor does not know that you want some of
the ChildElement's to be 'invisible' so we have to somehow let it know
this. This is where the next part comes in.
In your RootElementEditPart you need to implement/override the
getModelChildren() method. This method returns the list that the editor
uses to instantiate existing graphical Views and also create any
'missing' Views. What you want to do is to provide a filtered list...
LEAVING OUT the ChildElement's that you want to stay 'invisible'
Keep in mind that super.getModelChildren() will return a complete list
of all children (of all types) so make sure your code does proper type
checking so you don't run into ClassCastException.
Also remember that we are essentially 'hiding' some ChildElement's from
the GMF editor but the user can still see them in the tree editor of the
generated EMF editor.
============copy code below============
protected List getModelChildren() {
List filteredChildren = new ArrayList();
for (Iterator iter = super.getModelChildren().iterator();
iter.hasNext();) {
View view = (View) iter.next();
if (view.getElement() instanceof ChildElement) {
ChildElement element = (ChildElement) view.getElement();
/*
* You can implement any logic here
* <P>
* NOTE: element.isVisible() is only used as an example
* <P>
*/
if (true == element.isVisible()) {
filteredChildren .add(view);
}
} else {
filteredChildren .add(view);
}
}
return filteredChildren ;
}
=====================================
Up to now we've handle the creation and rendering of the ChildElements
now we have to also deal with delete. If you recall, when a
ChildElement is created in the editor you 'secretly' created an
additional ChildElement. So when you delete the 'visible' ChildElement
in the editor... the editor does not know about the extra ChildElement
you created... and so that child will become almost like an 'orphan' and
might give you 'dangling reference' errors. To properly perform the
cleanup there is more code to implement/override. I'm assuming that
when the 'visible' ChildElement is deleted in the editor you also want
to delete the 'invisible' ChildElement too.
Find your ChildElementEditHelper in the generated ...diagram.edit.helper
package. You will need to implement/override two methods
getDestroyDependentsCommand() and getDestroyElementCommand().
============copy code below========
protected ICommand getDestroyDependentsCommand(DestroyDependentsRequest
req) {
EObject element = req.getElementToDestroy();
if (element instanceof ChildElement) {
ChildElement visibleChild = (ChildElement) element;
ChildElement invisibleChild = [retrieve the invisible ChildElement]
if (null != invisibleChild ) {
return req.getDestroyDependentCommand(invisibleChild);
}
}
return super.getDestroyDependentsCommand(req);
}
/**
* Delegating to AbstractEditHelper so that
getDestroyDependentsCommand() is
* subsequently called
*
* @generated NOT
*/
protected ICommand getDestroyElementCommand(DestroyElementRequest req) {
return super.getDestroyElementCommand(req);
}
=====================================
So now you should have a complete solution from creation to rendering,
and finally to delete.
Let me know if it all work out for you
-- Khai --
Ben Renwart wrote:
> Hi Khai,
>
> Thanks for your precious help... That's very well explained and it works
> fine. I have only one problem :
> When I create an element, everything is ok. I save my diagram, close the
> editor , and just after that re-open it (without restarting the eclipse
> workbench). The element is duplicated each time I'm doing this step.
> For example :
> I add 1 element and save my diagram. I close it and re-open it, 2
> element are displayed now. If I close the diagram once again and re-open
> it once again, 3 elements are now displayed .
>
> I think it's because the same root element is always used when I open
> the diagram. It seems like my root element is shared while executing the
> eclipse workbench.
>
> What do you think about that ? Is there a simple solution ?
>
> Regards,
>
> Ben.
>
|
|
|
Re: Proggrammaticaly add an element for the "root" model element (access the roo [message #125840 is a reply to message #124022] |
Thu, 10 May 2007 17:27  |
Eclipse User |
|
|
|
Originally posted by: khai.n.situvista.com
Just a correction for the getDestroyElementCommand()... you would need
to override it in your YOUR_PROJECT_BaseEditHelper NOT in
ChildElementEditHelper as previously stated.
-- Khai --
Khai M Nguyen wrote:
> You are getting this behavior because the 'invisible' element you
> created is the same type as the visible element created by the action
> but GMF does not know that some should be 'invisible'
>
> Let me explain further.
>
> Assume your RootElement has a list containing ChildElement's. When you
> open a GMF editor the framework will ask for this list. Then for each
> ChildElement in the list it will check if a graphical View definition
> already exist for it; if none exist then it will automatically create
> the default View for it. The editor does not know that you want some of
> the ChildElement's to be 'invisible' so we have to somehow let it know
> this. This is where the next part comes in.
>
> In your RootElementEditPart you need to implement/override the
> getModelChildren() method. This method returns the list that the editor
> uses to instantiate existing graphical Views and also create any
> 'missing' Views. What you want to do is to provide a filtered list...
> LEAVING OUT the ChildElement's that you want to stay 'invisible'
>
> Keep in mind that super.getModelChildren() will return a complete list
> of all children (of all types) so make sure your code does proper type
> checking so you don't run into ClassCastException.
>
> Also remember that we are essentially 'hiding' some ChildElement's from
> the GMF editor but the user can still see them in the tree editor of the
> generated EMF editor.
>
>
> ============copy code below============
>
> protected List getModelChildren() {
> List filteredChildren = new ArrayList();
>
> for (Iterator iter = super.getModelChildren().iterator();
> iter.hasNext();) {
> View view = (View) iter.next();
> if (view.getElement() instanceof ChildElement) {
> ChildElement element = (ChildElement) view.getElement();
>
> /*
> * You can implement any logic here
> * <P>
> * NOTE: element.isVisible() is only used as an example
> * <P>
> */
> if (true == element.isVisible()) {
> filteredChildren .add(view);
> }
> } else {
> filteredChildren .add(view);
> }
>
> }
> return filteredChildren ;
> }
> =====================================
>
>
> Up to now we've handle the creation and rendering of the ChildElements
> now we have to also deal with delete. If you recall, when a
> ChildElement is created in the editor you 'secretly' created an
> additional ChildElement. So when you delete the 'visible' ChildElement
> in the editor... the editor does not know about the extra ChildElement
> you created... and so that child will become almost like an 'orphan' and
> might give you 'dangling reference' errors. To properly perform the
> cleanup there is more code to implement/override. I'm assuming that
> when the 'visible' ChildElement is deleted in the editor you also want
> to delete the 'invisible' ChildElement too.
>
>
> Find your ChildElementEditHelper in the generated ...diagram.edit.helper
> package. You will need to implement/override two methods
> getDestroyDependentsCommand() and getDestroyElementCommand().
>
>
> ============copy code below========
>
> protected ICommand getDestroyDependentsCommand(DestroyDependentsRequest
> req) {
>
> EObject element = req.getElementToDestroy();
>
> if (element instanceof ChildElement) {
> ChildElement visibleChild = (ChildElement) element;
>
> ChildElement invisibleChild = [retrieve the invisible ChildElement]
>
> if (null != invisibleChild ) {
> return req.getDestroyDependentCommand(invisibleChild);
> }
> }
>
> return super.getDestroyDependentsCommand(req);
> }
>
> /**
> * Delegating to AbstractEditHelper so that
> getDestroyDependentsCommand() is
> * subsequently called
> *
> * @generated NOT
> */
> protected ICommand getDestroyElementCommand(DestroyElementRequest req) {
> return super.getDestroyElementCommand(req);
> }
>
> =====================================
>
>
>
> So now you should have a complete solution from creation to rendering,
> and finally to delete.
>
>
> Let me know if it all work out for you
>
>
> -- Khai --
>
>
>
>
> Ben Renwart wrote:
>> Hi Khai,
>>
>> Thanks for your precious help... That's very well explained and it
>> works fine. I have only one problem :
>> When I create an element, everything is ok. I save my diagram, close
>> the editor , and just after that re-open it (without restarting the
>> eclipse workbench). The element is duplicated each time I'm doing this
>> step.
>> For example :
>> I add 1 element and save my diagram. I close it and re-open it, 2
>> element are displayed now. If I close the diagram once again and
>> re-open it once again, 3 elements are now displayed .
>>
>> I think it's because the same root element is always used when I open
>> the diagram. It seems like my root element is shared while executing
>> the eclipse workbench.
>>
>> What do you think about that ? Is there a simple solution ?
>>
>> Regards,
>>
>> Ben.
>>
|
|
|
Powered by
FUDForum. Page generated in 0.04264 seconds