Page Index Toggle Pages: [1] 2  Send TopicPrint
Hot Topic (More than 10 Replies) How to add a link beteewn two nodes (Read 10134 times)
Silvia88
Full Member
***
Offline


I Love MindFusion!

Posts: 121
Joined: Aug 31st, 2015
How to add a link beteewn two nodes
Sep 4th, 2015 at 2:25pm
Print Post  
Hi,
I have a problem, I create a link between two rectangles, then, if one of the two rectangles is already connected to another rectangle, I would like to remove one link and create a node at the end of the other link and then connect the rectangles to that node, I wrote this code but it doesn't work:
ShapeNode addedNode = new ShapeNode(diagram);
                DiagramLink newLink = new DiagramLink(diagram);
                ShapeNode originFirst = new ShapeNode(diagram);
                ShapeNode destFirst = new ShapeNode(diagram);
                float x = 0, y = 0;
                int position = 0, position2 = 0;
                if (origin.getShape() == Shape.fromId("Rectangle")) {
                    if (dest.getShape() == Shape.fromId("Rectangle")) {
                        Log.d("LinkCreated", "GENERALIZATION CREATED");
                        if (dest.getAllLinks().size() > 1) {
                            l.setText("NEW LINK");
                            newLink.setText("OLDLINK");
                            addedNode.setShape(Shape.fromId("Ellipse"));
                            addedNode.resize(2, 2);
                            DiagramNode dn = addedNode;
                            position2 = l.getOriginIndex();
                            PointList pl = l.getControlPoints();
                            PointF array[] = pl.getArray();
                            dn.moveTo(array[0].x, array[0].y);
                            Pen pen = new Pen();
                            pen.setColor(Color.GREEN);
                            addedNode.setPen(pen);
                            diagram.add(addedNode);
                            addedNode.attachTo(l, AttachToLink.Segment, position2);
                            diagram.getLinks().remove(position);
                            //DiagramLink addedLink = new DiagramLink(diagram);
                            DiagramLink addedLink = new DiagramLink(diagram, addedNode, originFirst);
                            /*addedLink.setOrigin(addedNode);
                            addedLink.setDestination(originFirst);*/
                            // diagram.add(addedLink);
                            l.setHeadShape(ArrowHeads.None);
                            addedLink.setVisible(true);
                            /*Pen dashedPen = new Pen(0, Color.black, DashStyle.Dash);
                            addedLink.setPen(dashedPen);*/
                            Pen pen2 = new Pen();
                            pen2.setColor(Color.RED);
                            addedLink.setPen(pen2);
                            //diagram.getLinks().add(position3, addedLink);
                            diagram.getLinks().add(addedLink);


                        }
                        l.setHeadShape(ArrowHeads.Triangle);
                        Pen penA = new Pen();
                        penA.setWidth(3);
                        penA.setColor(Color.BLACK);
                        l.setHeadPen(penA);
                        Pen pen = new Pen();
                        pen.setWidth(3);
                        pen.setColor(Color.BLACK);
                        l.setPen(pen);
                        originFirst = (ShapeNode) l.getOrigin();
                        destFirst = (ShapeNode) l.getDestination();
                        position = l.getOriginIndex();

                    }
                }
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: How to add a link beteewn two nodes
Reply #1 - Sep 4th, 2015 at 4:26pm
Print Post  
Quote:
I have a problem, I create a link between two rectangles, then, if one of the two rectangles is already connected to another rectangle, I would like to remove one link and create a node at the end of the other link and then connect the rectangles to that node, I wrote this code but it doesn't work


Sorry, I cannot parse this sentence Smiley Could you attach an image showing before- and after- connections?
  
Back to top
 
IP Logged
 
Silvia88
Full Member
***
Offline


I Love MindFusion!

Posts: 121
Joined: Aug 31st, 2015
Re: How to add a link beteewn two nodes
Reply #2 - Sep 4th, 2015 at 4:41pm
Print Post  
Sure..
In the attachment BeforeConnection.png there is the graph before when there is only one link between rectangle1 and rectangle2, while in the attachment afterConnection.png there is the graph when another rectangle is connected to rectangle 1
  

BeforeConnection.png ( 2 KB | 174 Downloads )
BeforeConnection.png
afterConnection.png ( 6 KB | 165 Downloads )
afterConnection.png
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: How to add a link beteewn two nodes
Reply #3 - Sep 4th, 2015 at 6:29pm
Print Post  
So starting from the first image, when you draw rectangle 3 and a link from rectangle 3 to rectangle 1, you want a new node added between the existing ones and the links moved to it to represent a three-way connection?
  
Back to top
 
IP Logged
 
Silvia88
Full Member
***
Offline


I Love MindFusion!

Posts: 121
Joined: Aug 31st, 2015
Re: How to add a link beteewn two nodes
Reply #4 - Sep 4th, 2015 at 6:42pm
Print Post  
Yes, exactly
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: How to add a link beteewn two nodes
Reply #5 - Sep 7th, 2015 at 6:03am
Print Post  
This works in my test project:

Code
Select All
@Override
public void linkCreated(LinkEvent e)
{
	DiagramLink link = e.getLink();
	ShapeNode origin = (ShapeNode)link.getOrigin();
	ShapeNode destination = (ShapeNode)link.getDestination();
	DiagramLinkList destLinks = destination.getIncomingLinks();

	Shape ellipse = Shape.fromId("Ellipse");
	Shape rectangle = Shape.fromId("Rectangle");

	if (origin.getShape() == rectangle &&
		destination.getShape() == rectangle &&
		destLinks.size() > 1)
	{
		ShapeNode connector = null;

		// check if intermediate node already exists
		DiagramLink testLink = destLinks.get(0);
		ShapeNode testNode = (ShapeNode)testLink.getOrigin();
		if (testNode.getShape() == ellipse)
		{
			connector = testNode;
		}
		else
		{
			// create intermediate connector node
			connector = diagram.getFactory().
				createShapeNode(0, 0, 10, 10, ellipse);
			diagram.getFactory().createDiagramLink(testNode, connector);
			testLink.setOrigin(connector);
		}

		link.setDestination(connector);

		// rearrange nodes under destination?
		// if not, set correct connector coordinates above
		TreeLayout layout = new TreeLayout();
		layout.setRoot(destination);
		layout.setReversedLinks(true);
		layout.arrange(diagram);
	}
} 



I haven' checked what your code does, but it doesn't seem to add some of the new nodes (e.g. originFirst) to the diagram, and then adding the new link connected to them will fail.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Silvia88
Full Member
***
Offline


I Love MindFusion!

Posts: 121
Joined: Aug 31st, 2015
Re: How to add a link beteewn two nodes
Reply #6 - Sep 7th, 2015 at 8:31am
Print Post  
Thank you very much, I'll try it Smiley
  
Back to top
 
IP Logged
 
Silvia88
Full Member
***
Offline


I Love MindFusion!

Posts: 121
Joined: Aug 31st, 2015
Re: How to add a link beteewn two nodes
Reply #7 - Sep 7th, 2015 at 3:38pm
Print Post  
Sorry, I tried the code you posted but if I put layout.arrange(diagram); it put what I did on the top left part of the screen while I want it to be where I drew it.
If i don't put it, the diagram is where I drew it but it is not very good the allignment of the nodes and links. How can I do?
  
Back to top
 
IP Logged
 
Silvia88
Full Member
***
Offline


I Love MindFusion!

Posts: 121
Joined: Aug 31st, 2015
Re: How to add a link beteewn two nodes
Reply #8 - Sep 7th, 2015 at 3:48pm
Print Post  
Sorry, I solved the problem  Smiley
  
Back to top
 
IP Logged
 
Silvia88
Full Member
***
Offline


I Love MindFusion!

Posts: 121
Joined: Aug 31st, 2015
Re: How to add a link beteewn two nodes
Reply #9 - Sep 7th, 2015 at 5:14pm
Print Post  
Sorry, is there, in LayeredLayout something like ayout.setKeepRootPosition(true); of TreeLayout? And is there a way to set the maximum number of layer and to use links without arrows?
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: How to add a link beteewn two nodes
Reply #10 - Sep 8th, 2015 at 8:41am
Print Post  
There is no LayeredLayout.KeepRootPosition property as general graphs do not have a well-defined root; e.g. there could be multiple source nodes, or none at all in cyclic graphs. If you need a specific node to retain its apparent position, you could find the distance from new to old position and offset all elements of the arranged graph by it.

LayeredLayout does not allow links between nodes on same layer, so you can't limit the number of layers, unless you remove links from the longest path in your graph.

You can hide arrowheads by calling link.setHeadShape(null);

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Silvia88
Full Member
***
Offline


I Love MindFusion!

Posts: 121
Joined: Aug 31st, 2015
Re: How to add a link beteewn two nodes
Reply #11 - Sep 8th, 2015 at 8:59am
Print Post  
Ok, thank you but then I decided to substitute it with OrthogonalLayout but when I call arrange(), it puts the diagram at the top left of the screen, is there a way to arrange the diagram but leave it at its position?
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: How to add a link beteewn two nodes
Reply #12 - Sep 8th, 2015 at 10:09am
Print Post  
Try to offset it using setMargins method, where the margins you specify correspond to minimum X/Y values of nodes before arranging them.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Silvia88
Full Member
***
Offline


I Love MindFusion!

Posts: 121
Joined: Aug 31st, 2015
Re: How to add a link beteewn two nodes
Reply #13 - Sep 8th, 2015 at 10:35am
Print Post  
Ok, thank you..
In the tree layout you suggested me yesterday, I have a problem when I add a forth rectangle, I want it to be connected to the same node of rectangle2 and rectangle3, while it adds another node, here is an example of what I want:
  

example_004.png ( 6 KB | 155 Downloads )
example_004.png
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: How to add a link beteewn two nodes
Reply #14 - Sep 8th, 2015 at 1:43pm
Print Post  
The sample code connects to existing intermediate node if it finds one; check if this condition is still valid if you are using a different shape:

if (testNode.getShape() == ellipse)
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: [1] 2 
Send TopicPrint