Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Sirius » Programmatically create nodes
Programmatically create nodes [message #1728862] Fri, 08 April 2016 05:49 Go to next message
Martin Jedlicka is currently offline Martin JedlickaFriend
Messages: 122
Registered: January 2016
Senior Member
Hi,

How to programmatically on display (in the Sirius) to create a node and set the location and size?

Thanks.

Martin
Re: Programmatically create nodes [message #1728908 is a reply to message #1728862] Fri, 08 April 2016 12:31 Go to previous messageGo to next message
Florian Barbin is currently offline Florian BarbinFriend
Messages: 270
Registered: August 2010
Senior Member
On 04/08/2016 07:49 AM, Martin Jedlicka wrote:
> Hi,

Hi Martin,

What is your need behind this ?

Sirius is based on GMF but it is no really intended for adding graphical
elements out of the classic mechanism "odesign file + semantic model =
diagram's graphical elements "

Nodes and their bounds are defined in the GMF model but in a Sirius
diagram displaying elements defined in a odesign it's more complicated.
With the semantic model and with what is defined in the odesign
(NodeMappings, containerMappings etc), Sirius produces a model composed
of DNode, DNodeContainer, DEdge etc.

Next, Sirius builds the GMF model based on that Sirius model.

You can take a look in
org.eclipse.sirius.diagram.business.internal.sync.DDiagramSynchronizer.refresh(IProgressMonitor).
This action refreshes the Sirius model according to the semantic model
and the odesign.
The GMF model is created using the CanonicalSynchronizer (see
org.eclipse.sirius.diagram.ui.internal.refresh.diagram.DDiagramCanonicalSynchronizer.synchronize())


>
> How to programmatically on display (in the Sirius) to create a node and
> set the location and size?
>
> Thanks.
>
> Martin

Regards,

--
Florian - Obeo

Need training or professional services for Sirius?
http://www.obeodesigner.com/sirius
Re: Programmatically create nodes [message #1729363 is a reply to message #1728908] Wed, 13 April 2016 11:53 Go to previous messageGo to next message
Martin Jedlicka is currently offline Martin JedlickaFriend
Messages: 122
Registered: January 2016
Senior Member
My main problem is that I do not know how to programmatically set the location of the node.
DNode interface has methods to set the size (height and width), but not the location setting.

I need to implement custom layout provider (class GridLayoutProvider), but I do not know how distribute the nodes in the diagram. I need specific nodes put on specific location.

An example?

Thank you very much.
Re: Programmatically create nodes [message #1729364 is a reply to message #1729363] Wed, 13 April 2016 12:08 Go to previous messageGo to next message
Florian Barbin is currently offline Florian BarbinFriend
Messages: 270
Registered: August 2010
Senior Member
You can retrieve the GMF Node related to the DNode and set its Bounds
(node.getLayoutConstraint()). All the layout constraints are stored in
the GMF model.

On 04/13/2016 01:53 PM, Martin Jedlicka wrote:
> My main problem is that I do not know how to programmatically set the
> location of the node.
> DNode interface has methods to set the size (height and width), but not
> the location setting.
>
> I need to implement custom layout provider (class GridLayoutProvider),
> but I do not know how distribute the nodes in the diagram. I need
> specific nodes put on specific location.
>
> An example?
>
> Thank you very much.


--
Florian - Obeo

Need training or professional services for Sirius?
http://www.obeodesigner.com/sirius
Re: Programmatically create nodes [message #1729368 is a reply to message #1729364] Wed, 13 April 2016 12:52 Go to previous messageGo to next message
Martin Jedlicka is currently offline Martin JedlickaFriend
Messages: 122
Registered: January 2016
Senior Member
Thanks but I don't know how to do it.

My code:
package com.ge.eufo.poc.design.services;

import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.services.layout.AbstractLayoutEditPartProvider;
import org.eclipse.gmf.runtime.notation.Bounds;
import org.eclipse.gmf.runtime.notation.LayoutConstraint;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.sirius.diagram.DDiagram;
import org.eclipse.sirius.diagram.DNode;
import org.eclipse.sirius.diagram.description.DiagramDescription;
import org.eclipse.sirius.diagram.ui.edit.api.part.AbstractDDiagramEditPart;
import org.eclipse.sirius.diagram.ui.tools.api.layout.provider.GridLayoutProvider;
import org.eclipse.sirius.diagram.ui.tools.api.layout.provider.LayoutProvider;

public class CustomLayoutProvider implements LayoutProvider {

	/** The process diagram layout provider. */
       private GridLayoutProvider processDiagramLayoutProvider;


	@Override
	public AbstractLayoutEditPartProvider getLayoutNodeProvider(IGraphicalEditPart container) {
		if (isProcessDiagram(container)) {
			return getProcessDiagramLayoutProvider(container);
		}

		return null;
	}

	@Override
	public boolean isDiagramLayoutProvider() {
		return false;
	}

	@Override
	public boolean provides(IGraphicalEditPart container) {
		return isProcessDiagram(container);
	}

	private boolean isProcessDiagram(IGraphicalEditPart container) {

		if (container instanceof AbstractDDiagramEditPart) {
            AbstractDDiagramEditPart editPart = (AbstractDDiagramEditPart) container;
            if (editPart.resolveSemanticElement() instanceof DDiagram) {
                DDiagram diagram = (DDiagram) editPart.resolveSemanticElement();
                if (diagram.getDescription() != null) {
                    DiagramDescription diagramDescription = diagram.getDescription();
                    String name = diagramDescription.getName();
                    if (name.equals("Process diagram")) {
                    	return true;
                    }
                }
            }
        }
        return false;
	}

	 public GridLayoutProvider getProcessDiagramLayoutProvider(IGraphicalEditPart container) {
		 if (processDiagramLayoutProvider == null) {
			 processDiagramLayoutProvider = new GridLayoutProvider();
			 processDiagramLayoutProvider.setColumnSizeMode(GridLayoutProvider.DIMENSION_BY_LINE_OR_COLUMN);
			 processDiagramLayoutProvider.setLineSizeMode(GridLayoutProvider.DIMENSION_BY_LINE_OR_COLUMN);
			 processDiagramLayoutProvider.getPadding().top = 20;
			 processDiagramLayoutProvider.getPadding().bottom = 20;
			 processDiagramLayoutProvider.getPadding().left = 30;
			 processDiagramLayoutProvider.getPadding().right = 30;

			 List<EditPart> editParts = processDiagramLayoutProvider.getAllEditParts(container);

			 for (EditPart editPart : editParts) {
				 Object object = editPart.getModel();
				 if (object instanceof Node) {
					 Node node = (Node) object;

                                         // settings position X and Y - BAD
					 LayoutConstraint layoutConstraint = node.getLayoutConstraint();
					 if (layoutConstraint instanceof Bounds) {
						 Bounds bounds = (Bounds) layoutConstraint;
						 bounds.setHeight(7); // doesn't work
						 bounds.setWidth(15); // doesn't work
						 bounds.setX(100);    // doesn't work
						 bounds.setY(100);    // doesn't work
					 }

                                         // settings height and width - OK
					 EObject eobject = node.getElement();
					 if (eobject instanceof DNode) {
						 DNode dNode = (DNode) eobject;
						 dNode.setHeight(7); // OK
						 dNode.setWidth(15); // OK
					 }
				 }
			}
		 }
		 return processDiagramLayoutProvider;
	 }

}
Re: Programmatically create nodes [message #1729682 is a reply to message #1729368] Mon, 18 April 2016 05:57 Go to previous messageGo to next message
Martin Jedlicka is currently offline Martin JedlickaFriend
Messages: 122
Registered: January 2016
Senior Member
Hi Florian,

can you please write how to modify my code to set location of the nodes (previous post)?

Thank you very much.

Martin
Re: Programmatically create nodes [message #1729692 is a reply to message #1729682] Mon, 18 April 2016 07:38 Go to previous messageGo to next message
Florian Barbin is currently offline Florian BarbinFriend
Messages: 270
Registered: August 2010
Senior Member
Hi Martin,

It seems that you change the GMF Node bounds within the
getLayoutNodeProvider method. This method is not intended to execute the
layout and your code is probably called outside of a Transactional
Command (which is not authorized in the Sirius context).

Take a look on
org.eclipse.sirius.diagram.ui.tools.internal.layout.provider.CompositeDownTopProvider
for instance. getLayoutNodeProvider returns a
AbstractLayoutEditPartProvider and this last has to provides the Command
that will be executed to perform the layout.

On 04/18/2016 07:57 AM, Martin Jedlicka wrote:
> Hi Florian,
>
> can you please write how to modify my code to set location of the nodes
> (previous post)?
>
> Thank you very much.
>
> Martin


Regards,


--
Florian - Obeo

Need training or professional services for Sirius?
http://www.obeodesigner.com/sirius
Re: Programmatically create nodes [message #1729720 is a reply to message #1729692] Mon, 18 April 2016 13:12 Go to previous messageGo to next message
Martin Jedlicka is currently offline Martin JedlickaFriend
Messages: 122
Registered: January 2016
Senior Member
Hi Florian,

I understand, but how do you implement a custom layout provider (Arrange All), where I can set the location of nodes?

I'm sorry, but I still do not understand. Perhaps I need a concrete example

Thanks.

Martin
Re: Programmatically create nodes [message #1729726 is a reply to message #1729720] Mon, 18 April 2016 13:24 Go to previous messageGo to next message
Florian Barbin is currently offline Florian BarbinFriend
Messages: 270
Registered: August 2010
Senior Member
You can take a look on the documentation [1] or this similar thread [2].
You can also look at the existing implementations of
org.eclipse.sirius.diagram.ui.tools.api.layout.provider.LayoutProvider
in the Sirius code.

[1]
http://www.eclipse.org/sirius/doc/developer/extensions-provide_custom-arrange-all.html

https://www.eclipse.org/forums/index.php/mv/msg/639946/1234795/#msg_1234795

On 04/18/2016 03:12 PM, Martin Jedlicka wrote:
> Hi Florian,
>
> I understand, but how do you implement a custom layout provider (Arrange
> All), where I can set the location of nodes?
>
> I'm sorry, but I still do not understand. Perhaps I need a concrete example
>
> Thanks.
>
> Martin

Regards,

--
Florian - Obeo

Need training or professional services for Sirius?
http://www.obeodesigner.com/sirius
Re: Programmatically create nodes [message #1729828 is a reply to message #1729726] Tue, 19 April 2016 11:56 Go to previous messageGo to next message
Martin Jedlicka is currently offline Martin JedlickaFriend
Messages: 122
Registered: January 2016
Senior Member
Hi Florian,

My code method getLayoutNodeProvider():

public AbstractLayoutEditPartProvider getLayoutNodeProvider(IGraphicalEditPart container) {
	if (isProcessDiagram(container)) {
		if (this.layoutProvider == null) {

			this.layoutProvider = new CompoundLayoutProvider();
			this.processDiagramLayoutProvider = getProcessDiagramLayoutProvider(container);
			this.layoutProvider.addProvider(this.processDiagramLayoutProvider);

			List<EditPart> editParts = this.layoutProvider.getAllEditParts(container);

			 for (int i=0; i<editParts.size(); i++) {
				 EditPart editPart = editParts.get(i);
				 Object object = editPart.getModel();
				 if (object instanceof Node) {
					 Node node = (Node) object;

					 LayoutConstraint layoutConstraint = node.getLayoutConstraint();

					 if (layoutConstraint instanceof Bounds) {
						 Bounds bounds = (Bounds) layoutConstraint;
						 bounds.setHeight(7); // doesn't work
						 bounds.setWidth(15); // doesn't work
						 bounds.setX(100);    // doesn't work
						 bounds.setY(10*i);    // doesn't work
					 }

					 EObject eobject = node.getElement();
					 if (eobject instanceof DNode) {
						 DNode dNode = (DNode) eobject;
						 dNode.setHeight(7); // OK
						 dNode.setWidth(15); // OK
					 }
				 }
			}
		}
	}

	return this.layoutProvider;
}


In my code I see nothing else than the examples you referenced (https://www.eclipse.org/sirius/doc/developer/extensions-provide_custom-arrange-all.html or CompositeDownTopProvider.java).
So how do I set the location of the elements, if this does not work?

 LayoutConstraint layoutConstraint = node.getLayoutConstraint();

 if (layoutConstraint instanceof Bounds) {
	 Bounds bounds = (Bounds) layoutConstraint;
	 bounds.setX(100);    // doesn't work
	 bounds.setY(10*i);    // doesn't work
 }


I also tried the following code, but still nothing.

CompoundLayoutProvider clp = new CompoundLayoutProvider();
processDiagramLayoutProvider = getProcessDiagramLayoutProvider(container);
clp.addProvider(processDiagramLayoutProvider);
this.layoutProvider = new ArrangeAllOnlyLayoutProvider(clp);


Where is the mistake?
Sorry for the questions, but I'll still get it.

Thanks.

Martin
Re: Programmatically create nodes [message #1729832 is a reply to message #1729726] Tue, 19 April 2016 12:15 Go to previous messageGo to next message
Rahul Bhogavkar is currently offline Rahul BhogavkarFriend
Messages: 13
Registered: April 2016
Junior Member
Hello Guys,
Even I'm also facing the same problem that Martin has.
Martin if you have code regarding placing elements as per our demand then please send it.
If you solved out your problem please help me to solve the same.
Thank You.
Re: Programmatically create nodes [message #1729903 is a reply to message #1729828] Wed, 20 April 2016 07:53 Go to previous messageGo to next message
Florian Barbin is currently offline Florian BarbinFriend
Messages: 270
Registered: August 2010
Senior Member
On 04/19/2016 01:56 PM, Martin Jedlicka wrote:
> Hi Florian,

Hi Martin,

In your code you are still modifying the GMF Bounds into the
getLayoutNodeProvider method. This method returns an
AbstractLayoutEditPartProvider that should do this job. In the
org.eclipse.sirius.diagram.ui.tools.internal.layout.provider.CompositeDownTopProvider.getLayoutNodeProvider(IGraphicalEditPart)
method, there is not direct modification of the GMF Bounds.


> I also tried the following code, but still nothing.
>
>
> CompoundLayoutProvider clp = new CompoundLayoutProvider();
> processDiagramLayoutProvider = getProcessDiagramLayoutProvider(container);
> clp.addProvider(processDiagramLayoutProvider);
> this.layoutProvider = new ArrangeAllOnlyLayoutProvider(clp);
>

Try to set a breakpoint to make sure this code is properly called.

>
> Where is the mistake?
> Sorry for the questions, but I'll still get it.

Providing custom layouts is not an easy task which is unfortunately not
very well documented.
The best thing to do is still observing the existing code in debug mode
to understand how it works.

>
> Thanks.
>
> Martin

Regards,

--
Florian - Obeo

Need training or professional services for Sirius?
http://www.obeodesigner.com/sirius
Re: Programmatically create nodes [message #1730041 is a reply to message #1729903] Thu, 21 April 2016 07:06 Go to previous messageGo to next message
Martin Jedlicka is currently offline Martin JedlickaFriend
Messages: 122
Registered: January 2016
Senior Member
Hi,

so I solved it (code below work for me)

Thanks.

Martin

My code:
public class CustomLayoutProvider implements LayoutProvider {

	/** The GMF layout provider. */
	private AbstractLayoutEditPartProvider layoutProvider;

	@Override
	public AbstractLayoutEditPartProvider getLayoutNodeProvider(IGraphicalEditPart container) {
		if (isProcessDiagram(container)) {
			if (this.layoutProvider == null) {
				this.layoutProvider = new CustomLayout();
			}
		}

		return this.layoutProvider;
	}

	@Override
	public boolean isDiagramLayoutProvider() {
		return false;
	}

	@Override
	public boolean provides(IGraphicalEditPart container) {
		return isProcessDiagram(container);
	}

	private boolean isProcessDiagram(IGraphicalEditPart container) {

		if (container instanceof AbstractDDiagramEditPart) {
			AbstractDDiagramEditPart editPart = (AbstractDDiagramEditPart) container;
			if (editPart.resolveSemanticElement() instanceof DDiagram) {
				DDiagram diagram = (DDiagram) editPart.resolveSemanticElement();
				if (diagram.getDescription() != null) {
					DiagramDescription diagramDescription = diagram.getDescription();
					String name = diagramDescription.getName();
					if (name.equals("Process diagram")) {
						return true;
					}
				}
			}
		}
		return false;
	}

	class CustomLayout extends AbstractLayoutProvider {
		@Override
		public Command layoutEditParts(List selectedObjects, IAdaptable layoutHint) {

			if (selectedObjects.isEmpty()) {
	                  return UnexecutableCommand.INSTANCE;
	                }

			int index = 0;
			for (IGraphicalEditPart graphicalEditPart : Iterables.filter(selectedObjects, IGraphicalEditPart.class)) {

				EObject semanticElement = graphicalEditPart.resolveSemanticElement();

				if (semanticElement instanceof DDiagramElement) {
					View notationView = graphicalEditPart.getNotationView();
					if (notationView instanceof Node) {
						final Node node = (Node) notationView;
						final LayoutConstraint layoutConstraint = node.getLayoutConstraint();
						if (layoutConstraint instanceof Bounds) {
							final Bounds bounds = (Bounds) layoutConstraint;
							bounds.setHeight(50);
							bounds.setWidth(130);
							bounds.setX(index * 100);
							bounds.setY(index * 100);
						}
					}
				}
				index++;
				// refresh for size of nodes
				graphicalEditPart.refresh();
			}

			return new CompoundCommand();
		}
	}

}
Re: Programmatically create nodes [message #1730173 is a reply to message #1730041] Fri, 22 April 2016 06:06 Go to previous messageGo to next message
Rahul Bhogavkar is currently offline Rahul BhogavkarFriend
Messages: 13
Registered: April 2016
Junior Member
Hello Martin Jedlicka,
Can you please specify what other things we need to do for running this code because this code is not running for me.
can you send me whole detail and code along with their import section.

Thank You.
Re: Programmatically create nodes [message #1736169 is a reply to message #1730041] Mon, 27 June 2016 09:10 Go to previous message
getty inn is currently offline getty innFriend
Messages: 1
Registered: June 2016
Junior Member
Hi martin,
I know this is a bit old, but was hoping you might be able to help me,
I have spent many many hours now trying to get this to work, and i cant see, to change diagram nodes location

i have done exactly as you in example below,
While debugging i see the code does enter the getLayoutNodeProvider
but it does not invoke any of the CustomLayout class methods.

any ideas why this is ?

also when you want to activate the code, do you simply press the arrange all button ?

appreciate the help !!
thank you.
Previous Topic:[Properties view]
Next Topic:[PropertiesView] Has anybody a working example for the select option in the new Properties View?
Goto Forum:
  


Current Time: Thu Apr 25 15:10:16 GMT 2024

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

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

Back to the top