Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » How to add EClass objects programmatically to a diagram?(or how can update the diagram from the model?)
How to add EClass objects programmatically to a diagram? [message #900541] Tue, 07 August 2012 08:58 Go to next message
andrej dyck is currently offline andrej dyck
Messages: 14
Registered: May 2012
Junior Member
Hello,

for about a week, I am trying to create "new" elements on a GMF diagram without success. Now you are my hope to finally get it done.

This is what I have as "requirements": Modelling with "Eclipse Modelling Tools" one creates a EMF Project and then Ecore Model (.ecore) and an Ecore Diagram (.ecorediag). Opening the .ecorediag file in the default editor (EcoreDiagramEditor) containing a GMFResource. No I want my plug-in to add (and remove) objects to this diagram and its underlying model. Concrete, I want to be able to add EClass, EDataType, EEnum, EOperation, EAttribute, EReference, Inheritance objects, i.e., more or less the default Objects and Connections from the "Palette". Further, I already get, e.g., EClass, objects. These objects are created programmatically and should be added to the diagram.

This is what I have found so far:


First approach: So I found this CreateViewAndElementRequest and looked up where it is used and how. The following code snippet works partially for a new EClass object:

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecoretools.diagram.part.EcoreDiagramEditor;
import org.eclipse.emf.ecoretools.diagram.providers.EcoreElementTypes;
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.CreateViewAndElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.IHintedType;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.gmf.runtime.notation.Node;
IWorkbenchPart activePart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart();
EcoreDiagramEditor diagramEditor = (EcoreDiagramEditor) activePart;
DiagramEditPart diagramEditPart = diagramEditor.getDiagramEditPart();

EClass myCreatedClass = ...;
CreateElementRequest createElementRequest = new CreateElementRequest(EcoreElementTypes.EClass_1001);
createElementRequest.setNewElement(myCreatedClass);

CreateElementRequestAdapter createElementRequestAdapter = new 
        CreateElementRequestAdapter(createElementRequest);
			
CreateViewAndElementRequest.ViewAndElementDescriptor veDescriptor = new 
        CreateViewAndElementRequest.ViewAndElementDescriptor(
            createElementRequestAdapter, Node.class,
	    ((IHintedType) EcoreElementTypes.EClass_1001).getSemanticHint(),
	    diagramEditPart.getDiagramPreferencesHint());
			
CreateViewAndElementRequest veRequest = new
        CreateViewAndElementRequest(veDescriptor);

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


This command execution creates a new EClass object with the name "EClass0" in both the .ecore and .ecorediag file and views the element in the diagram. Yay! But it completely ignores myCreatedClass. And I do not have a clue how (1) I get the newly created element so I can modify it or (2) how to make this command clear, that I really want my EClass object to be inserted.

Second approach: The next thing I have found is the DropObjectsRequest. But all I get is an Exception while executing the corresponding command and I cannot find examples how to use it right. This is how I have tried this:

DropObjectsRequest dropObjectsRequest = new DropObjectsRequest();
dropObjectsRequest.setObjects(Collections.singletonList(myCreatedClass));
Command command = diagramEditPart.getCommand(dropObjectsRequest);
command.execute();


But maybe it is a wrong approach anyway.

Third approach: Since I can create, remove and edit EObjects with org.eclipse.emf.common.command.* in a Resource, another idea of mine was to update the model of the diagram [((Diagram) diagramEditor.getEditingDomain().getResourceSet().getResources().get(0).getContents().get(0)).getElement()] and then "reinitialize" the diagram. I have found InitializeAndLayoutDiagramCommand which use EcoreDiagramContentInitializer to initialize diagram content. Well since it is generated code, I cannot overwrite functionality Sad But this one way would be cool and easy for me. I already tried to use this command without the use of LayoutService and it (partially) worked. But obviously it had duplicated all existing elements since it gets all elements from the model resource and puts them into the diagram resource. Is there another (similar) class I can use to update the diagram from the model?

Regards,
Andrej
Re: How to add EClass objects programmatically to a diagram? [message #900689 is a reply to message #900541] Wed, 08 August 2012 03:53 Go to previous messageGo to next message
Ralph Gerbig is currently offline Ralph Gerbig
Messages: 697
Registered: November 2009
Senior Member
Hi,

did you try out this request: 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 ? It executes the same code that is executed when creating a model element via the palette.

Ralph
Re: How to add EClass objects programmatically to a diagram? [message #900732 is a reply to message #900689] Wed, 08 August 2012 06:44 Go to previous messageGo to next message
andrej dyck is currently offline andrej dyck
Messages: 14
Registered: May 2012
Junior Member
Hello Ralph,

thank you for the reply! With the CreateUnspecifiedTypeRequest the code shrinks a lot. And I figured that I can get the newly created objects with the getNewObject() method. If I am right, I should then be able to alter these objects. Do I create relationships, e.g., inheritance, the same way?

Now, trying to alter the objects. Coming back with an update. For everyone else, here is my previous code using CreateUnspecifiedTypeRequest.

CreateUnspecifiedTypeRequest request = new
        CreateUnspecifiedTypeRequest(
            Collections.singletonList(EcoreElementTypes.EClass_1001),
            diagramEditPart.getDiagramPreferencesHint());

Command command = diagramEditPart.getCommand(request);
command.execute();
			
Object newObject = request.getNewObject(); // these are the newly created objects


Regards,
Andrej
Re: How to add EClass objects programmatically to a diagram? [message #900746 is a reply to message #900732] Wed, 08 August 2012 07:37 Go to previous messageGo to next message
Ralph Gerbig is currently offline Ralph Gerbig
Messages: 697
Registered: November 2009
Senior Member
Hi,

to change your object you should use the command framework provided by gmf. You should also consider to execute your commands through the command stack of your edit/editing domain. I never tried with connection but there is a request 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/CreateUnspecifiedTypeConnectionRequest.html that you could try.

Ralph
Re: How to add EClass objects programmatically to a diagram? [message #900749 is a reply to message #900746] Wed, 08 August 2012 07:48 Go to previous messageGo to next message
andrej dyck is currently offline andrej dyck
Messages: 14
Registered: May 2012
Junior Member
Hey Ralph,

thank you for the tip with CreateUnspecifiedTypeConnectionRequest. I will try this. Do you know which commands I can use to copy all attributes of an existing EClass object to this newly created objects by the request? The other thing with the command stack is that I have tried to do
editingDomain.getCommandStack().execute(command);
but the execute method do not accept GEF Commands. Any ideas?

Cheers,
Andrej
Re: How to add EClass objects programmatically to a diagram? [message #900763 is a reply to message #900749] Wed, 08 August 2012 08:24 Go to previous messageGo to next message
Ralph Gerbig is currently offline Ralph Gerbig
Messages: 697
Registered: November 2009
Senior Member
Hi,

there is a difference between edit and editing domain. Try getEditDomain. I am not aware of a command for copying something. You should try to use the SetCommand. There is a create method for this http://download.eclipse.org/modeling/emf/emf/javadoc/2.4.2/org/eclipse/emf/edit/command/SetCommand.html#create(org.eclipse.emf.edit.domain.EditingDomain, java.lang.Object, java.lang.Object, java.lang.Object). You can put multiple commands into one compound command. This way you do not need to execute them seperatly.

Ralph
Re: How to add EClass objects programmatically to a diagram? [message #900793 is a reply to message #900763] Wed, 08 August 2012 09:45 Go to previous messageGo to next message
andrej dyck is currently offline andrej dyck
Messages: 14
Registered: May 2012
Junior Member
Hi Ralph,

currently, I am facing two problems:

First: I still cannot get the created EClass object. Maybe I don't have to but CreateUnspecifiedTypeRequest.getNewObject() returns (in my case) a List of CreateViewAndElementRequest.ViewAndElementDescriptor objects which encapsulate CreateElementRequestAdapter. And in that Adapter is a CreateElementRequest object with the newElement. But I do not see any getNewElement() method...

Second: The SetCommand you have mentioned is an EMF command. And I tried these commands before. They work on the underlying model, but the diagram (view) is not updated.

Do you know the DropObjectsRequest? Maybe this can can help?

Andrej
Re: How to add EClass objects programmatically to a diagram? [message #900800 is a reply to message #900793] Wed, 08 August 2012 10:11 Go to previous messageGo to next message
andrej dyck is currently offline andrej dyck
Messages: 14
Registered: May 2012
Junior Member
Hi,

OK. I just tried DropObjectsRequest and it worked for me. Here is the quick & dirty code for those who are interested:

EClass myCreatedClass = ...
EPackage package = ((Diagram) editingDomain.getResourceSet().getResources().get(0).getContents().get(0)).getElement();

editingDomain.getCommandStack().execute(AddCommand.create(editingDomain, package, null, myCreatedClass);

DropObjectsRequest dropObjectsRequest = new DropObjectsRequest();
dropObjectsRequest.setObjects(Collections.singletonList(myCreatedClass));
dropObjectsRequest.setLocation(new Point(15, 15));
diagramEditor.getDiagramEditDomain().getDiagramCommandStack().execute(diagramEditPart.getCommand(dropObjectsRequest));


Of course, now I have to figure out how to "show up" references and delete stuff. For those who are interested in arranging the elements, there is an ArrangeRequest which I have not tried yet, so I cannot give you any code snippet.

Cheers,
Andrej
Re: How to add EClass objects programmatically to a diagram? [message #901009 is a reply to message #900800] Thu, 09 August 2012 07:53 Go to previous messageGo to next message
andrej dyck is currently offline andrej dyck
Messages: 14
Registered: May 2012
Junior Member
Hi,

I do not know what happened, but the code does not work any more Sad The drop command cannot be executed, since no provider for the CreateView part of the command is found. As far as I can tell, to choose the right provider the creation of the CreateViewCommand needs a "semanticHint" but null is given. Any ideas?

Cheers,
Andrej
Re: How to add EClass objects programmatically to a diagram? [message #901028 is a reply to message #901009] Thu, 09 August 2012 09:12 Go to previous messageGo to next message
andrej dyck is currently offline andrej dyck
Messages: 14
Registered: May 2012
Junior Member
OK... I am confused. Well, good news is that the code work. BUT only after one creates a new element, e.g., EClass, from the palette manually. Is there an initialization process going on? Does someone know which method is called first when someone clicks on an element in the palette?

Thanks,
Andrej
Re: How to add EClass objects programmatically to a diagram? [message #901038 is a reply to message #901028] Thu, 09 August 2012 10:02 Go to previous messageGo to next message
Ralph Gerbig is currently offline Ralph Gerbig
Messages: 697
Registered: November 2009
Senior Member
Hi,

if you want to change the position of your model element you should try out SetBoundsCommand or change the view in the notation model.

Ralph
Re: How to add EClass objects programmatically to a diagram? [message #901041 is a reply to message #901038] Thu, 09 August 2012 10:06 Go to previous messageGo to next message
andrej dyck is currently offline andrej dyck
Messages: 14
Registered: May 2012
Junior Member
Hi Ralph,

thank you. But I am fine with the auto arrangement of objects. My current problem is the "why do I have to first create an element manually before I the drop command works". Would be great if someone has an idea what this is. Do I have to trigger the registration of providers or something like this?

Andrej
Re: How to add EClass objects programmatically to a diagram? [message #1046734 is a reply to message #901041] Mon, 22 April 2013 05:43 Go to previous messageGo to next message
Blazo Nastov is currently offline Blazo Nastov
Messages: 1
Registered: April 2013
Junior Member
Hi Andrej,

I am wondering if you solved the problem with the first drag and drop request ? I am trying to dynamicly add annotations to the diagram with the drag and drop request, but it is not working for me neither.

Blazo
Re: How to add EClass objects programmatically to a diagram? [message #1046847 is a reply to message #1046734] Mon, 22 April 2013 09:06 Go to previous message
andrej dyck is currently offline andrej dyck
Messages: 14
Registered: May 2012
Junior Member
Hi Blazo,

what are you referring to with "problem with the first drag and drop request"? What is your setting?

Cheers,
Andrej

[Updated on: Mon, 22 April 2013 10:30]

Report message to a moderator

Previous Topic:Can't find genClass for class 'MyClass' in package myPckage
Next Topic:How to create multiple figures within a single "FigureDescriptor" ?
Goto Forum:
  


Current Time: Thu Jul 31 01:25:37 EDT 2014

Powered by FUDForum. Page generated in 0.11243 seconds