Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » Create objects programmatically
Create objects programmatically [message #1064159] Mon, 17 June 2013 21:20 Go to next message
Missing name Mising name is currently offline Missing name Mising name
Messages: 8
Registered: July 2011
Junior Member
Hello GMF-Forum,

so i want to create a GMF-Editor with the usual components like Palette and Canvas to enable the user to create and modify a model instance in a graphical manner. Also the editor should have a button by that the user can create several model elements in one step (instead of using drag&drop multiple times).

This is what Ive got so far to create an object programmatically:
import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.diagram.core.edithelpers.CreateElementRequestAdapter;			
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequest;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequestFactory;
...
IWorkbenchPart activePart = PlatformUI.getWorkbench()
					.getActiveWorkbenchWindow().getActivePage().getActivePart();
			WorkflowDiagramEditor diagramEditor = (WorkflowDiagramEditor) activePart;
			DiagramEditPart diagramEditPart = diagramEditor
					.getDiagramEditPart();

			ComposedAppWorkflowFactory factory = ComposedAppWorkflowFactory.eINSTANCE;
			Action firstAction = factory.createAction();
			firstAction.setName("TEST");

			CreateViewRequest actionRequest = CreateViewRequestFactory
					.getCreateShapeRequest(
							ComposedAppWorkflow.diagram.providers.WorkflowElementTypes.Action_2007,
							PreferencesHint.USE_DEFAULTS);

			ViewAndElementDescriptor descriptor = (ViewAndElementDescriptor) ((List) actionRequest
					.getNewObject()).get(0);
			CreateElementRequestAdapter adapter = descriptor
					.getCreateElementRequestAdapter();
			
			adapter.setNewElement(firstAction);

			Command command = diagramEditPart.getCommand(actionRequest);
			command.execute();
...

ComposedAppWorkflowFactory is an EFactory, Action is a CDOObject.
The code above doesn't work because the setNewElement methode got called again by command.execute(). But with a different EObject than firstAction. So i create a figure on the canvas but it is not related to firstAction.

I got inspired by this: http://wiki.eclipse.org/GMF_Tutorial_Part_3#Custom_Actions But here I dont understand how i can modifiy the related EObject by only having an IAdaptable. Also how to get form the IAdaptable to the just created figure? Eg to set the position?
IAdaptable topicViewAdapter = (IAdaptable) ((List) topicRequest.getNewObject()).get(0);


Am I on the wrong track? What is the best way to archive what i have in mind?
Re: Create objects programmatically [message #1064363 is a reply to message #1064159] Tue, 18 June 2013 21:27 Go to previous messageGo to next message
Missing name Mising name is currently offline Missing name Mising name
Messages: 8
Registered: July 2011
Junior Member
Ok, I have made some progress. I now can modify the model elements after the execution of the initial CreateViewRequest:

IWorkbenchPart activePart = PlatformUI.getWorkbench()
					.getActiveWorkbenchWindow().getActivePage().getActivePart();
			WorkflowDiagramEditor diagramEditor = (WorkflowDiagramEditor) activePart;
			DiagramEditPart diagramEditPart = diagramEditor
					.getDiagramEditPart();

			CreateViewRequest actionRequest = CreateViewRequestFactory
					.getCreateShapeRequest(
							ComposedAppWorkflow.diagram.providers.WorkflowElementTypes.Action_2007,
							PreferencesHint.USE_DEFAULTS);

			org.eclipse.gef.commands.Command command = diagramEditPart.getCommand(actionRequest);
			command.execute();

			ComposedAppRequest parentElement = (ComposedAppRequest) diagramEditPart.resolveSemanticElement();
			Action fromParent =  (Action) parentElement.getOwnedNode().get(parentElement.getOwnedNode().size()-1);
			
			org.eclipse.emf.common.command.Command setCmd = SetCommand.create(
					diagramEditPart.getEditingDomain(),
					fromParent,
					ComposedAppWorkflowPackage.eINSTANCE.getAction_Name(), "fromParent");
			diagramEditPart.getEditingDomain().getCommandStack().execute(setCmd);


As you see this is some kind of workaround, since I just get the last created child form the parent node by: parentElement.getOwnedNode().get(parentElement.getOwnedNode().size()-1);

I dont think this is the GMF way to do it...Starting from the CreateViewRequest I also try this to get the last created child:
			ViewAndElementDescriptor descriptor = (ViewAndElementDescriptor) ((List) actionRequest
					.getNewObject()).get(0);
			CreateElementRequestAdapter adapter = descriptor
					.getCreateElementRequestAdapter();

			EObject fromResolve = (EObject) adapter.resolve();
			//Dont give me the right object...


My workaround has the weakness that last child of the parent node is not necessarily the object I just created... So i need to fix that sooner or later. Is there anything i can do with this: (IAdaptable) ((List) actionRequest.getNewObject()).get(0); ?

Re: Create objects programmatically [message #1064407 is a reply to message #1064363] Wed, 19 June 2013 06:40 Go to previous messageGo to next message
Ralph Gerbig is currently offline Ralph Gerbig
Messages: 697
Registered: November 2009
Senior Member
Hi,

you should take a look at CreateUnspecifiedTypeRequest (http://publib.boulder.ibm.com/infocenter/rsmhelp/v7r0m0/index.jsp?topic=/org.eclipse.gmf.doc/reference/api/runtime/org/eclipse/gmf/runtime/diagram/ui/requests/CreateUnspecifiedTypeRequest.html) this should make live much easier. You can put your code for setting up the created model element into GMFMap. There is something called FeatureSeqInitializer which you can add to your node mappings.

Ralph
Re: Create objects programmatically [message #1064457 is a reply to message #1064407] Wed, 19 June 2013 11:44 Go to previous messageGo to next message
Missing name Mising name is currently offline Missing name Mising name
Messages: 8
Registered: July 2011
Junior Member
The values of the model element are not fix/only available during runtime. I dont know how the GMFMap approach could help me in that regard.
Re: Create objects programmatically [message #1064488 is a reply to message #1064457] Wed, 19 June 2013 13:25 Go to previous messageGo to next message
Ralph Gerbig is currently offline Ralph Gerbig
Messages: 697
Registered: November 2009
Senior Member
Hi,

the values do get calculated during creation, right? If this is the case you can use OCL constraints in gmfmap to calculate these values.

Ralph
Re: Create objects programmatically [message #1064501 is a reply to message #1064488] Wed, 19 June 2013 14:04 Go to previous messageGo to next message
Missing name Mising name is currently offline Missing name Mising name
Messages: 8
Registered: July 2011
Junior Member
Yes, i dont know the value until the user enters it. To what i should refer with a ocl constraint?
Re: Create objects programmatically [message #1064585 is a reply to message #1064501] Thu, 20 June 2013 06:43 Go to previous messageGo to next message
Ralph Gerbig is currently offline Ralph Gerbig
Messages: 697
Registered: November 2009
Senior Member
Hi,

so you want to display a dialog on element creation?

Ralph
Re: Create objects programmatically [message #1065088 is a reply to message #1064585] Mon, 24 June 2013 10:05 Go to previous messageGo to next message
Missing name Mising name is currently offline Missing name Mising name
Messages: 8
Registered: July 2011
Junior Member
Yes, but it is not related to a special element from the model. The dialog is to speed up certain parts of the model instance creation. So the result from a this dialog should be one to many prearranged model elements. The dialog can be called multiple times... so not only during the instantiation of a new model instance.
Re: Create objects programmatically [message #1065116 is a reply to message #1065088] Mon, 24 June 2013 12:34 Go to previous message
Ralph Gerbig is currently offline Ralph Gerbig
Messages: 697
Registered: November 2009
Senior Member
Hi,

then simply call the set command of EMF to put the result of your command into the model.

Ralph
Previous Topic:GMF Editor hasn't "Connection Creation Assistants" in MultiPageEditor
Next Topic:How to register selection changed event.
Goto Forum:
  


Current Time: Wed Sep 17 09:38:10 GMT 2014

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

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