Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
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 12:55 Go to next message
Yuzhang Han is currently offline Yuzhang HanFriend
Messages: 19
Registered: September 2009
Junior Member
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 15:02 Go to previous messageGo to next message
Artur Kronenberg is currently offline Artur KronenbergFriend
Messages: 159
Registered: August 2009
Senior Member
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 #500829 is a reply to message #499365] Sun, 29 November 2009 11:58 Go to previous messageGo to next message
Alex Shatalin is currently offline Alex ShatalinFriend
Messages: 141
Registered: July 2009
Senior Member
Hello Yuzhang,

See ViewService.createNode() method


-----------------
Alex Shatalin
Re: How to create a node from an EObject [message #500978 is a reply to message #499365] Mon, 30 November 2009 14:48 Go to previous messageGo to next message
Yuzhang Han is currently offline Yuzhang HanFriend
Messages: 19
Registered: September 2009
Junior Member
Hello Alex,
thanks very much for your reply!

>See ViewService.createNode() method
I think you mean the method

static Node createNode(View container, EObject eObject, java.lang.String type, PreferencesHint preferencesHint)
"Creates a node for a given eObject and with a given type and inserts it into a given container"

As I'm a really nubie in GMF, could you be so kind to provide me with some further information, like how to use the generated "Node"-instance so as to make the node displayed and added into the domain-model? I really appreciate it!

(I think that the above generates a node in the notation model, so my question is how to use this node to produce a node on the diagram and in my domain model?)

[Updated on: Mon, 30 November 2009 15:00]

Report message to a moderator

Re: How to create a node from an EObject [message #500979 is a reply to message #499626] Mon, 30 November 2009 14:49 Go to previous messageGo to next message
Yuzhang Han is currently offline Yuzhang HanFriend
Messages: 19
Registered: September 2009
Junior Member
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 16:20 Go to previous message
Artur Kronenberg is currently offline Artur KronenbergFriend
Messages: 159
Registered: August 2009
Senior Member
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?

Previous Topic:Generate GMF diagram editor dynamically with diagram
Next Topic:Icons in Creation Tools
Goto Forum:
  


Current Time: Fri Apr 23 09:31:21 GMT 2021

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

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

Back to the top