Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » How to create an EReference programmatically?
icon5.gif  How to create an EReference programmatically? [message #1220042] Mon, 09 December 2013 10:07 Go to next message
Xiaoying Hu is currently offline Xiaoying HuFriend
Messages: 2
Registered: June 2013
Location: Aachen Germany
Junior Member
Hi all,


I am trying to programmatically create a connection(an EReference) between two EClass on a GMF-based Editor. (cf. picture - an ERefference bewteen the Student and the Lecture)

I tried using CreateConnectionRequest, codes are as followed:

CreateConnectionRequest createConnectionReques = new CreateConnectionRequest();
		
createConnectionReques.setSourceEditPart(sourceEditPart);
		
createConnectionReques.setTargetEditPart(targetEditPart);
		
Command commandConnection = sourceEditPart.getCommand(createConnectionReques);
		
if(commandConnection.canExecute()){
    commandConnection.execute();
}


but a NullPointerException for the command is thrown, that means the command is not initiated correctly. I really don't know why Sad .

Could anyone help, how to build the command correctly?


Thanks in advance.


  • Attachment: Student.png
    (Size: 15.04KB, Downloaded 65 times)
Re: How to create an EReference programmatically? [message #1220070 is a reply to message #1220042] Mon, 09 December 2013 13:13 Go to previous messageGo to next message
Pierre-Charles David is currently offline Pierre-Charles DavidFriend
Messages: 171
Registered: July 2009
Senior Member
Le 09/12/2013 13:58, Xiaoying Hu a écrit :
> Hi all,
>
>
> I am trying to programmatically create a connection(an EReference) between two EClass on a GMF-based Editor. (cf. picture - an ERefference bewteen the Student and the Lecture)
>
> I tried using CreateConnectionRequest, codes are as followed:
>
>
> CreateConnectionRequest createConnectionReques = new CreateConnectionRequest();
>
> createConnectionReques.setSourceEditPart(sourceEditPart);
>
> createConnectionReques.setTargetEditPart(targetEditPart);
>
> Command commandConnection = sourceEditPart.getCommand(createConnectionReques);
>

You don't execute the commands yourself, but must pass through the
command stack (which is responsible for undo/redo behavior):

TransactionalEditingDomain ted = sourceEditPart.getEditingDomain();
ted.getCommandStack().execute(commandConnection);
Re: How to create an EReference programmatically? [message #1220093 is a reply to message #1220070] Mon, 09 December 2013 16:04 Go to previous messageGo to next message
Xiaoying Hu is currently offline Xiaoying HuFriend
Messages: 2
Registered: June 2013
Location: Aachen Germany
Junior Member
Hi Pierre-Charles,

thanks for your quick reply.

Quote:
You don't execute the commands yourself, but must pass through the
command stack (which is responsible for undo/redo behavior):

TransactionalEditingDomain ted = sourceEditPart.getEditingDomain();
ted.getCommandStack().execute(commandConnection);


There is a problem here, the execute method is not applicable for a GEF command (org.eclipse.gef.commands.Command), it works only for EMF command(org.eclipse.emf.common.command.Command).

To run the GEF command is the second step, the first step is to get the GEF command, in my case using the code:

Command commandConnection = sourceEditPart.getCommand(createConnectionReques);


This doesn't work. So the question is, how can I get the correct command to run the request?

Any idea will be appreciated. Smile
Re: How to create an EReference programmatically? [message #1692795 is a reply to message #1220093] Sat, 18 April 2015 21:00 Go to previous message
Nikos Margaritis is currently offline Nikos MargaritisFriend
Messages: 59
Registered: September 2014
Member

Hello there,

I encountered the same issue.

I came up with the following method which seems to solve the problem.

public void createAndExecuteConnectionRequestCommand(IElementType type, DiagramEditPart editPart, EditPart source, EditPart target) {
		CreateConnectionViewAndElementRequest request = new CreateConnectionViewAndElementRequest(type, ((IHintedType) type).getSemanticHint(), editPart.getDiagramPreferencesHint());
		Command cmd = CreateConnectionViewAndElementRequest.getCreateCommand(request, 
						source, target);
		System.out.println(cmd.getLabel());
		diagramEditPart.getDiagramEditDomain().getDiagramCommandStack().execute(cmd);
		//hack to refresh visuals and show the connection!
		diagramEditPart.addNotify();
}


Best, Nikos.
Previous Topic:GMF multidiagram editor - avoid data lost
Next Topic:org.eclipse.core.runtime.AssertionFailedException:
Goto Forum:
  


Current Time: Tue May 26 01:58:16 GMT 2015

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

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