Create objects programmatically [message #1064159] |
Mon, 17 June 2013 21:20  |
Eclipse User |
|
|
|
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   |
Eclipse User |
|
|
|
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 #1065088 is a reply to message #1064585] |
Mon, 24 June 2013 10:05   |
Eclipse User |
|
|
|
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.
|
|
|
|
Powered by
FUDForum. Page generated in 0.05205 seconds