Home » Modeling » GMF (Graphical Modeling Framework) » How to create a node from an EObject
How to create a node from an EObject [message #499365] |
Sat, 21 November 2009 07:55  |
Eclipse User |
|
|
|
Hi, everybody!
I wanted to ask if it is possible to create a node on the diagram from an instance of EObject that I have. E.g.,
I already have an object of MyType extending EObject which is an element from my EMF-metamodel, and its attributes (like name, children, etc) have been set. Now I wanted to programmatically create on my diagram a node which represents this object (i.e., with the same values of attributes). Or in one word, is there such method in GMF like createNodeOnDiagram(MyType obj) which generates EditPart for my EObject-object and the corresponding node on the diagram?
Could this be possible?
Thanks very much!
|
|
|
Re: How to create a node from an EObject [message #499626 is a reply to message #499365] |
Mon, 23 November 2009 10:02   |
Eclipse User |
|
|
|
Hi,
there isn't just a method to create that, but it is still quite easy to do so.
This method creates a CreateViewCommand for your eObject:
private Command createViewCommand(EObject template) {
CreateViewRequest.ViewDescriptor viewDescriptor = new CreateViewRequest.ViewDescriptor(new EObjectAdapter(
template), Node.class, ((IHintedType) XXXElementTypes
.getElementType(XXXEditPart.VISUAL_ID)).getSemanticHint(), true, CanvasEditPart
.getDiagramPreferencesHint());
viewDescriptor.setPersisted(true);
CreateViewRequest createViewRequest = new CreateViewRequest(viewDescriptor);
Command createViewCommand = CanvasEditPart.getCommand(createViewRequest);
return createViewCommand;
}
You could look at this earlier post where I talk about that probelm:
http://www.eclipse.org/forums/index.php?t=msg&th=157895& amp;start=0&S=ce633c2fab8a3bbe4d2c9575e449e52f
Basically you create a CreateViewRequest using your eObject to specify which thing to create and then you use the canvas you are working on to get the creation command returned. If you execute it, you have created your command.
Best regards,
Artur
Yuzhang Han wrote on Sat, 21 November 2009 07:55 | Hi, everybody!
I wanted to ask if it is possible to create a node on the diagram from an instance of EObject that I have. E.g.,
I already have an object of MyType extending EObject which is an element from my EMF-metamodel, and its attributes (like name, children, etc) have been set. Now I wanted to programmatically create on my diagram a node which represents this object (i.e., with the same values of attributes). Or in one word, is there such method in GMF like createNodeOnDiagram(MyType obj) which generates EditPart for my EObject-object and the corresponding node on the diagram?
Could this be possible?
Thanks very much!
|
|
|
| | |
Re: How to create a node from an EObject [message #500979 is a reply to message #499626] |
Mon, 30 November 2009 09:49   |
Eclipse User |
|
|
|
Artur Kronenberg wrote on Mon, 23 November 2009 10:02 | Hi,
there isn't just a method to create that, but it is still quite easy to do so.
This method creates a CreateViewCommand for your eObject:
private Command createViewCommand(EObject template) {
CreateViewRequest.ViewDescriptor viewDescriptor = new CreateViewRequest.ViewDescriptor(new EObjectAdapter(
template), Node.class, ((IHintedType) XXXElementTypes
.getElementType(XXXEditPart.VISUAL_ID)).getSemanticHint(), true, CanvasEditPart
.getDiagramPreferencesHint());
viewDescriptor.setPersisted(true);
CreateViewRequest createViewRequest = new CreateViewRequest(viewDescriptor);
Command createViewCommand = CanvasEditPart.getCommand(createViewRequest);
return createViewCommand;
}
You could look at this earlier post where I talk about that probelm:
http://www.eclipse.org/forums/index.php?t=msg&th=157895& amp; amp;start=0&S=ce633c2fab8a3bbe4d2c9575e449e52f
Basically you create a CreateViewRequest using your eObject to specify which thing to create and then you use the canvas you are working on to get the creation command returned. If you execute it, you have created your command.
Best regards,
Artur
Yuzhang Han wrote on Sat, 21 November 2009 07:55 | Hi, everybody!
I wanted to ask if it is possible to create a node on the diagram from an instance of EObject that I have. E.g.,
I already have an object of MyType extending EObject which is an element from my EMF-metamodel, and its attributes (like name, children, etc) have been set. Now I wanted to programmatically create on my diagram a node which represents this object (i.e., with the same values of attributes). Or in one word, is there such method in GMF like createNodeOnDiagram(MyType obj) which generates EditPart for my EObject-object and the corresponding node on the diagram?
Could this be possible?
Thanks very much!
|
|
Hello Arthur,
thanks very much for your reply. That method really worked generating the corresponding child in my diagram-file and displaying the shape of the node. But it didn't add anything to the model - the domain-model file was not changed. Do you think there is any way to do the two things in one go, i mean to make a shape and add the child in my model at one time?
|
|
|
Re: How to create a node from an EObject [message #500999 is a reply to message #500979] |
Mon, 30 November 2009 11:20  |
Eclipse User |
|
|
|
Hi,
well I had the same problem. The method create the eObject on the diagram, but doesn't actually add it to the model.
Since unfrotunatelly I couldn't figure out how to create and add at the same time I used a SetCommand with the according SetRequest to set a new List of EObjects to my diagram model. There I simply added the new object to all the old ones and executed the command.
I used a CompoundCommand to bundle both commands in one and returned that so both are executed at the same time.
If you don't need to return a command, I think the ViewService.createNode() would work.
The way I think you have to call it is in a static way:
ViewService.createNode(container, eObject, type, preferencesHint)
The container is the View of your diagram editpart. You can get it by calling:
DiagramEditPart.getNotationView()
The eObject is the object you want to create. The type would be Node and is a static reference to the editPart you want to create. The preferenceHint is the DiagramPreferenceHint of your diagram. You can get it by calling getPreferenceHint on your DiagramEditPart.
Yuzhang Han wrote on Mon, 30 November 2009 09:49 | Artur Kronenberg wrote on Mon, 23 November 2009 10:02 | Hi,
there isn't just a method to create that, but it is still quite easy to do so.
This method creates a CreateViewCommand for your eObject:
private Command createViewCommand(EObject template) {
CreateViewRequest.ViewDescriptor viewDescriptor = new CreateViewRequest.ViewDescriptor(new EObjectAdapter(
template), Node.class, ((IHintedType) XXXElementTypes
.getElementType(XXXEditPart.VISUAL_ID)).getSemanticHint(), true, CanvasEditPart
.getDiagramPreferencesHint());
viewDescriptor.setPersisted(true);
CreateViewRequest createViewRequest = new CreateViewRequest(viewDescriptor);
Command createViewCommand = CanvasEditPart.getCommand(createViewRequest);
return createViewCommand;
}
You could look at this earlier post where I talk about that probelm:
http://www.eclipse.org/forums/index.php?t=msg&th=157895& amp; amp; amp;start=0&S=ce633c2fab8a3bbe4d2c9575e449e52f
Basically you create a CreateViewRequest using your eObject to specify which thing to create and then you use the canvas you are working on to get the creation command returned. If you execute it, you have created your command.
Best regards,
Artur
Yuzhang Han wrote on Sat, 21 November 2009 07:55 | Hi, everybody!
I wanted to ask if it is possible to create a node on the diagram from an instance of EObject that I have. E.g.,
I already have an object of MyType extending EObject which is an element from my EMF-metamodel, and its attributes (like name, children, etc) have been set. Now I wanted to programmatically create on my diagram a node which represents this object (i.e., with the same values of attributes). Or in one word, is there such method in GMF like createNodeOnDiagram(MyType obj) which generates EditPart for my EObject-object and the corresponding node on the diagram?
Could this be possible?
Thanks very much!
|
|
Hello Arthur,
thanks very much for your reply. That method really worked generating the corresponding child in my diagram-file and displaying the shape of the node. But it didn't add anything to the model - the domain-model file was not changed. Do you think there is any way to do the two things in one go, i mean to make a shape and add the child in my model at one time?
|
|
|
|
Goto Forum:
Current Time: Thu Jul 03 19:46:43 EDT 2025
Powered by FUDForum. Page generated in 0.03578 seconds
|