Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Graphiti » Create Diagram Model Programatically
Create Diagram Model Programatically [message #708892] Wed, 03 August 2011 05:06 Go to next message
Murthy Bhat is currently offline Murthy Bhat
Messages: 159
Registered: July 2009
Senior Member
Hello,

I have a graphiti editor which i use to create/edit certain domain model. The diagram information is all stored in the same file when saved. However there are some old domain models which do not have the Diagram model within them. So when I load the old files, I want to create corresponding pictogram elements and other graphics programmatically.

I have created the diagram using:
Diagram diagram = Graphiti.getPeCreateService().createDiagram(diagramTypeId, diagramName, true);

Is there any way to create pictogram elements and graphics algorithms programmatically or should I create each element using the create features only?

Thanks in advance for help.

Best Regards,
Murthy

[Updated on: Wed, 03 August 2011 05:11]

Report message to a moderator

Re: Create Diagram Model Programatically [message #708915 is a reply to message #708892] Wed, 03 August 2011 05:28 Go to previous messageGo to next message
Joerg Reichert is currently offline Joerg Reichert
Messages: 78
Registered: July 2009
Location: Leipzig
Member
Hi Murthy,

sure it is. Economically you can reuse the logic of your add feature implementation:

	private void createExampleStructure(
			final IDiagramTypeProvider ddtp,
			final Diagram diagram, DomainObject domainObject) {
		AddDomainObjectFeature add = new AddDomainObjectFeature(
				ddtp.getFeatureProvider()) {

			public IFeatureProvider getFeatureProvider() {
				return ddtp.getFeatureProvider();
			}

			protected Diagram getDiagram() {
				return diagram;
			}
		};
		AddContext context = new AddContext();
		context.setNewObject(domainObject);
		context.setTargetContainer(diagram);
		add.add(context);
	}


Depending in which context you want to execute the creation you have to provide the containing diagram and feature provider.

Regards,
Joerg


--
Need professional support for Xtext and EMF?
Go to: http://xtext.itemis.com
Re: Create Diagram Model Programatically [message #715653 is a reply to message #708892] Mon, 15 August 2011 00:03 Go to previous messageGo to next message
K Kumar is currently offline K Kumar
Messages: 18
Registered: March 2011
Junior Member
I had similar question and Viet has given another way this can be achieved.

www.eclipse.org/forums/index.php/t/227034

Hi Santosh,

you have to implement AddFeatures for the business elements you want to 
show and build an UpdateFeature for the root business feature (the one 
that is represented by the diagram figure) that delegates to the 
AddFeatures for each element that should be added.

So in your UpdateFeature identify the business objects to add and do 
something like this for each of them:

EObject eobject = <<the element to add>>
AddContext context2 = new AddContext();
context2.setNewObject(eobject);
context2.setLocation(x, y);
context2.setSize(width, height);
context2.setTargetContainer((ContainerShape) pictogramElement);
getFeatureProvider().addIfPossible(context2);

Moreover if you want the update to happen automatically you need to 
overwrite isAutoUpdateAtStartup() to return true in your 
DiagramTypeProvider class

Cheers,

Veit
Re: Create Diagram Model Programatically [message #717719 is a reply to message #715653] Mon, 22 August 2011 03:33 Go to previous messageGo to next message
Murthy Bhat is currently offline Murthy Bhat
Messages: 159
Registered: July 2009
Senior Member
Hi Joerg,Hi Santhosh,

Thanks for your suggestions, it helped.

But my usecase is slightly more complicated, I have to create diagrams without using a IFeature. The scenario is, when I load and open my model file, I have to create the diagram model on the fly. So I am in my loader outside the UI plugins and since the FeatureProvider is not accesible outside, i am unable to call the add feature. Any suggestions ?

Thanks,
Murthy
Re: Create Diagram Model Programatically [message #717737 is a reply to message #717719] Mon, 22 August 2011 04:26 Go to previous messageGo to next message
Joerg Reichert is currently offline Joerg Reichert
Messages: 78
Registered: July 2009
Location: Leipzig
Member
Hi Murthy,

I just looked into my code once again and noticed that only depend on the feature provider as I use the direct editing support. In any other case you should not depend on that.

I also had two use cases


  1. Create Diagram file (and content) for existing domain model
  2. Create initial domain model and associated initial diagram file


My notion was just to reuse the logic of the PictogramElement add(IAddContext context), but you may redefine such logic also outside the add feature class. You only need your loaded domain model and a diagram object as starting point, this can be easily created by

Diagram diagram = Graphiti.getPeCreateService().createDiagram(yourDiagramTypeId, diagramName, true);


The you have the create services:

IPeService peService = Graphiti.getPeService();
IPeCreateService peCreateService = Graphiti.getPeCreateService();
IGaService gaService = Graphiti.getGaService();


and you can as usual create your graphical elements and link them with the corresponding domain model objects.

Regards
Joerg


--
Need professional support for Xtext and EMF?
Go to: http://xtext.itemis.com
Re: Create Diagram Model Programatically [message #719802 is a reply to message #717737] Mon, 29 August 2011 01:01 Go to previous messageGo to next message
Murthy Bhat is currently offline Murthy Bhat
Messages: 159
Registered: July 2009
Senior Member
Thanks Joerg,Santhosh. It works now.

I have a little more to ask. IS it possible to create a connection programmatically ? I have tried adding one using the method below. But i run into exceptions at "getConnectionPointAt(GefService.java:214)"

	private PictogramElement createMyTransition(ContainerShape srcState,ContainerShape destState,MyTransition transition) {

		ChopboxAnchor srcAnchor=GraphitiUi.getPeService().createChopboxAnchor(srcState);
		ChopboxAnchor destAnchor=GraphitiUi.getPeService().createChopboxAnchor(destState);

		AddConnectionContext context = new AddConnectionContext(srcAnchor, destAnchor);
		context.setNewObject(transition);
		return getFeatureProvider().addIfPossible(context);
	}


Any ideas anyone ?

Thanks,
Murthy
Re: Create Diagram Model Programatically [message #719841 is a reply to message #719802] Mon, 29 August 2011 03:56 Go to previous messageGo to next message
Joerg Reichert is currently offline Joerg Reichert
Messages: 78
Registered: July 2009
Location: Leipzig
Member
Hi Murthy,

I think this is due to the fact, that you had created the anchors without associating them without an org.eclipse.graphiti.mm.pictograms.AnchorContainer and without a referenced GA (see setReferencedGraphicsAlgorithm). An anchor have to belong to a graphical element, that should be an connection end. The anchor itself is only the concept of a connection point of that element (as far as I understand). So e.g. a rectangle has a list of anchors (AnchorContainer.getAnchors()).

Regards,
Joerg


--
Need professional support for Xtext and EMF?
Go to: http://xtext.itemis.com
Re: Create Diagram Model Programatically [message #722898 is a reply to message #719841] Wed, 07 September 2011 02:13 Go to previous messageGo to next message
Murthy Bhat is currently offline Murthy Bhat
Messages: 159
Registered: July 2009
Senior Member
Thanks Joerg....that put me in the right direction, but am still unable to create the connection. I have changed my code like below.

	

	private PictogramElement createTransition(ContainerShape srcState,ContainerShape destState,MyTransition myTransition) {

		ChopboxAnchor srcAnchor=GraphitiUi.getPeService().createChopboxAnchor(srcState);
		srcAnchor.setReferencedGraphicsAlgorithm(srcState.getGraphicsAlgorithm());
		srcState.getAnchors().add(srcAnchor);

		ChopboxAnchor destAnchor=GraphitiUi.getPeService().createChopboxAnchor(destState);
		destAnchor.setReferencedGraphicsAlgorithm(destState.getGraphicsAlgorithm());
		destState.getAnchors().add(destAnchor);

		AddConnectionContext context = new AddConnectionContext(srcAnchor, destAnchor);
		context.setNewObject(myTransition);
		PictogramElement connection = iFeatureProvider.addIfPossible(context);

		return connection;
	}


It appears that that the connection is created because all the other associated Decorators are visible (like the connection label,image etc).Any idea what point I am missing ?

Thanks in advance for help.

Regards,
Murthy
Re: Create Diagram Model Programatically [message #722908 is a reply to message #722898] Wed, 07 September 2011 03:17 Go to previous messageGo to next message
Joerg Reichert is currently offline Joerg Reichert
Messages: 78
Registered: July 2009
Location: Leipzig
Member
Hi Murthy,

the decorators of the connection are visible, but the polyline itself is not?

I have had an issue when not setting the source and target pictogram elements at the add connection context beside the the anchors.

		Create[DomainObject]AssociationFeature createLink = new Create[DomainObject]AssociationFeature(
				getFeatureProvider());
		Anchor startAnchor = ...
		Anchor endAnchor = ...
		if(startAnchor != null && endAnchor != null) {
			CreateConnectionContext createContext = new CreateConnectionContext();
			createContext.setSourcePictogramElement(startAnchor.getParent());
			createContext.setSourceAnchor(startAnchor);
			createContext.setTargetPictogramElement(endAnchor.getParent());
			createContext.setTargetAnchor(endAnchor);
			if(createLink.canCreate(createContext)) {
				createLink.create(createContext);
			}
		}


I used the create connection feature as the domain object for the connection should be created, too. Inside the create connection feature the add connection feature is called. And this code looks identical to yours.

Joerg


--
Need professional support for Xtext and EMF?
Go to: http://xtext.itemis.com
Re: Create Diagram Model Programatically [message #722946 is a reply to message #722908] Wed, 07 September 2011 05:32 Go to previous messageGo to next message
Murthy Bhat is currently offline Murthy Bhat
Messages: 159
Registered: July 2009
Senior Member
Yes, the decorators are visible but the polyline is not. I am unable to see the anchors as well. any ideas?
Re: Create Diagram Model Programatically [message #723088 is a reply to message #722946] Wed, 07 September 2011 11:29 Go to previous messageGo to next message
Joerg Reichert is currently offline Joerg Reichert
Messages: 78
Registered: July 2009
Location: Leipzig
Member
Maybe the call context.setTargetContainer(targetContainer) is missing. The target container is in that case getDiagram()

--
Need professional support for Xtext and EMF?
Go to: http://xtext.itemis.com
Re: Create Diagram Model Programatically [message #723208 is a reply to message #723088] Thu, 08 September 2011 00:14 Go to previous messageGo to next message
Murthy Bhat is currently offline Murthy Bhat
Messages: 159
Registered: July 2009
Senior Member
The addConnectionFeature will add the container as the current Diagram. I am trying to simulate the manual creation of the connections vs creation via code. So all the object values are the same except for new objects in both the cases.
Re: Create Diagram Model Programatically [message #875708 is a reply to message #717737] Wed, 23 May 2012 04:01 Go to previous message
Joyce Liao is currently offline Joyce Liao
Messages: 1
Registered: May 2012
Junior Member
Hi Joerg, Hi Murthy

I have the same question now. I am a bigger at Graphiti.

Could you please give the code example about "you can as usual create your graphical elements and link them with the corresponding domain model objects"

Thank you very much


Joerg Reichert wrote on Mon, 22 August 2011 04:26
Hi Murthy,

I just looked into my code once again and noticed that only depend on the feature provider as I use the direct editing support. In any other case you should not depend on that.

I also had two use cases


  1. Create Diagram file (and content) for existing domain model
  2. Create initial domain model and associated initial diagram file


My notion was just to reuse the logic of the PictogramElement add(IAddContext context), but you may redefine such logic also outside the add feature class. You only need your loaded domain model and a diagram object as starting point, this can be easily created by

Diagram diagram = Graphiti.getPeCreateService().createDiagram(yourDiagramTypeId, diagramName, true);


The you have the create services:

IPeService peService = Graphiti.getPeService();
IPeCreateService peCreateService = Graphiti.getPeCreateService();
IGaService gaService = Graphiti.getGaService();


and you can as usual create your graphical elements and link them with the corresponding domain model objects.

Regards
Joerg

Previous Topic:New Project form Ecore Model
Next Topic:DiagramEditorFactory changed to what in 0.9.0?
Goto Forum:
  


Current Time: Wed Jul 30 19:30:27 EDT 2014

Powered by FUDForum. Page generated in 0.01995 seconds