Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Graphiti » Confusion about usage of generated model
Confusion about usage of generated model [message #1395130] Sun, 06 July 2014 12:11 Go to next message
Homer S is currently offline Homer SFriend
Messages: 13
Registered: June 2014
Junior Member
Hi,

I'm on my first Graphiti project and I'm a little confused about how to use my generated model classes.
The project is using the Pattern approach.

The creation of a node, when I drag and drop a node onto my diagram, is done via the following code:
	public Object[] create(ICreateContext context) {
		// ask user for node name
		String newName = ExampleUtil.askString(TITLE, USER_QUESTION, "");
		if (newName == null || newName.trim().length() == 0) {
			// TODO: default name with incrementing number?
			return EMPTY;
		}

		// get target from context
		EObject targetModel = (EObject) getBusinessObjectForPictogramElement(context
				.getTargetContainer());

		// create new model object
		Node newObject = NodeModelFactory.eINSTANCE.createNode();
		newObject.setName(newName);

		// add created model object to resource of targetModel
		targetModel.eResource().getContents().add(newObject);

		// add graphical representation to canvas
		addGraphicalRepresentation(context, newObject);

		// return newly created model object(s)
		return new Object[] { newObject };
	}


Now, for simplicity's sake, let's say the model is built so that a Node can have Nodes as children and I've a diagram with a Node that has several Nodes inside it:
When I call
Node node = (Node) context.getNewObject();
List<Node> nodeList = node.getNodes();

nodeList remains null.

What am I missing?

--
Homer

[Updated on: Sun, 27 July 2014 14:32]

Report message to a moderator

Re: Confusion about usage of my model [message #1396079 is a reply to message #1395130] Mon, 07 July 2014 21:13 Go to previous messageGo to next message
Soeren M is currently offline Soeren MFriend
Messages: 77
Registered: September 2011
Member
I am wondering, that you don't get any class cast exceptions.
If it works correct, this:

// get target from context
		EObject targetModel = (EObject) getBusinessObjectForPictogramElement(context
				.getTargetContainer());


Should return an object from type Node? But if it doesn't the target container is not a node.
Anyways, hence you've created your own model, in my opinion, you have to do the add by yourself (for the list).
Re: Confusion about usage of my model [message #1396643 is a reply to message #1396079] Tue, 08 July 2014 15:39 Go to previous messageGo to next message
Homer S is currently offline Homer SFriend
Messages: 13
Registered: June 2014
Junior Member
Soeren M wrote on Mon, 07 July 2014 21:13

// get target from context
		EObject targetModel = (EObject) getBusinessObjectForPictogramElement(context
				.getTargetContainer());


Should return an object from type Node? But if it doesn't the target container is not a node.


Well, it does return an object from type Node which in turn is a subclass of EObject. Hope this is what bugged you about the code.

Soeren M wrote on Mon, 07 July 2014 21:13

Anyways, hence you've created your own model, in my opinion, you have to do the add by yourself (for the list).


My model is created via ecore and genmodel files. Sorry if this didn't come through in my initial post. It is stored in the diagram file.

Do I still have to fill the model elements myself?
If so, how do I recreate the model at runtime when the editor or the diagram is closed and reopened?

I thought Graphiti will use the generated model to its fullest? Please tell me, I'm doing something wrong.

Thanks in advance.
Re: Confusion about usage of my model [message #1396840 is a reply to message #1396643] Tue, 08 July 2014 21:40 Go to previous messageGo to next message
Soeren M is currently offline Soeren MFriend
Messages: 77
Registered: September 2011
Member
Ah, yes, nevermind, that was my bad. Why are you casting it into an EObject?
If it's a Node, in my opinion you have to add it to the list. Graphiti is adding it to its resource, so you are able to ask for it's children.
But I don't think that Graphiti is able to add it to your customized model. I am sorry if I am wrong, that's just how I would do it.
Re: Confusion about usage of my model [message #1403792 is a reply to message #1396840] Sun, 27 July 2014 14:32 Go to previous message
Homer S is currently offline Homer SFriend
Messages: 13
Registered: June 2014
Junior Member
Ok, i've been stupid. Finally understood how to use the generated ecore model. One simply does have to use it.

Here's a simple sketch for the node example:

public Object[] create(ICreateContext context) {
		...
		// get target from context
		Node targetModel = (Node) getBusinessObjectForPictogramElement(context
				.getTargetContainer());

		// create new model object
		Node newObject = NodeModelFactory.eINSTANCE.createNode();
		newObject.setName(newName);

		// add created model object to resource of targetModel
		targetModel.getNodes().add(newObject);
		...
	}


The model will be actually useable via it's getter and setter methods, which will make it easier to fetch/search for certain business objects. At the same time the diagram file will be populated with the model objects according to it's model specification in the ecore file.

I can't believe I haven't come up with this myself earlier.

[Updated on: Sun, 27 July 2014 14:32]

Report message to a moderator

Previous Topic:Drag-n-drop from Project Explorer - invoking Create Feature
Next Topic:Add Connection automatik between two Anchor
Goto Forum:
  


Current Time: Tue Mar 19 11:24:19 GMT 2024

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

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

Back to the top