Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 10 Replies) How to label links to achieve my aims (Read 7901 times)
AR
Junior Member
**
Offline


I Love MindFusion!

Posts: 65
Joined: Jan 23rd, 2015
How to label links to achieve my aims
Jan 23rd, 2015 at 11:20am
Print Post  
We are planning to use JDiagram to add the ability to draw simple diagrams to our product. These diagrams will have a small number of nodes (probably between 2 and 5) joined by one or two links. These links will all be labelled.

What we need the labels to do (ideally) is:
1. be movable by the user
2. be editable by the user
3. move with the link if it is moved (by 'bending' the link or moving one or both of the nodes it is linking)
4. have 'snap to my link' functionality so if the user moves it and forgets which link it associates with they can do something on the label (or the diagram) which causes it to 'snap' back to a default position relative to the link it is the label for.

Using the setText() or addLink() methods on the link adds labels that follow the link, but the text can 'cross' over the link and be difficult to read, and these can not be moved in relation to the link by the user.

Following a suggestion from a forum, I tried adding a 'ShapeNode' attached to the link. This allows moving of the label, but the label does not move when the link does and can be placed by any link not just the one it is 'attached' to.
Also I'm not sure how to find the instance of ShapeNode attached to a particular link so that a user can edit it.

The code I've used is:
Code (Java)
Select All
public void onLinkDoubleClicked( LinkEvent event )
	{
		DiagramLink link = event.getLink();
		// Edit the label
		//link.setText( JOptionPane.showInputDialog( this, "Edit the label", link.getText()) ); // one label
		//LinkLabel theLabel = link.addLabel( JOptionPane.showInputDialog( this, "Edit the label", link.getText()) ); // many labels but overwrite each other!
		PointList pointsOnLink = link.getControlPoints();
		Float[] points = pointsOnLink.getArray();
		Rectangle2D rectangle2D = link.getEditRect(points[points.length/2]);
		ShapeNode label = diagram.getFactory().createShapeNode(rectangle2D);
		label.attachTo(link, AttachToLink.Point, points.length);
		label.setTransparent(true);
		label.setText(JOptionPane.showInputDialog( this, "Edit the label", ""));
		label.setIgnoreLayout(true);
		label.setAllowIncomingLinks( false );
		label.setAllowOutgoingLinks( false );
	} 



Have you any suggestions how I can achieve my aims regarding labels on links?

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


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: How to label links to achieve my aims
Reply #1 - Jan 23rd, 2015 at 11:48am
Print Post  
Quote:
Using the setText() or addLink() methods on the link adds labels that follow the link, but the text can 'cross' over the link and be difficult to read, and these can not be moved in relation to the link by the user.


If you use a LinkLabel for this, you could set its Brush property to add background color and make the text legible even if over the link graphics, and/or set its AutoArrange property to avoid the label overlapping with its link and nearby nodes.

Labels cannot be moved interactively at this time. You could probably use a temporary ShapeNode to implement label-editing mode that lets users specify new label position by moving the node.

Quote:
Following a suggestion from a forum, I tried adding a 'ShapeNode' attached to the link. This allows moving of the label, but the label does not move when the link does and can be placed by any link not just the one it is 'attached' to.


Whether the node moves will depend on which link element it is attached to. E.g. if you attach to first control point, the node will move only when users move that point. Try different attach modes supported by the AttachToLink enum:
http://www.mindfusion.eu/onlinehelp/flowchartnet/T_MindFusion_Diagramming_Attach...

You could implement auto-arrange functionality for such attached nodes as shown here:
http://mindfusion.eu/Forum/YaBB.pl?num=1334837045/2#2

Quote:
Also I'm not sure how to find the instance of ShapeNode attached to a particular link so that a user can edit it.


You can find nodes attached to a link by calling link.getSubordinateGroup().getAttachedNodes(); To get the node's master link call node.getMasterGroup().getMainItem();

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
AR
Junior Member
**
Offline


I Love MindFusion!

Posts: 65
Joined: Jan 23rd, 2015
Re: How to label links to achieve my aims
Reply #2 - Jan 23rd, 2015 at 3:21pm
Print Post  
Thanks that looks very help full - I will try your suggestions in my 'demo' application to see which looks best.
  
Back to top
 
IP Logged
 
AR
Junior Member
**
Offline


I Love MindFusion!

Posts: 65
Joined: Jan 23rd, 2015
Re: How to label links to achieve my aims
Reply #3 - Jan 26th, 2015 at 5:59pm
Print Post  
Have been looking into this further, and the Java API doesn't have 'FixedDistance' or 'RelativeDistance' for the AttachToLink enum so I tried some of the others.

However, after I've called attachToLink() on the node (see code in original post), the link.getSubordinateGroup().getAttachedNodes() returns an empty instance of DiagramNodesList (and if I look at the label node itself it's MasterGroup is null) - are you sure this functionality has been implemented in the Java version?
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: How to label links to achieve my aims
Reply #4 - Jan 26th, 2015 at 7:34pm
Print Post  
If you meant attaching label to last point, pass points.length - 1 to index parameter instead of points.length:

Code
Select All
label.attachTo(link, AttachToLink.Point, points.length - 1); 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
AR
Junior Member
**
Offline


I Love MindFusion!

Posts: 65
Joined: Jan 23rd, 2015
Re: How to label links to achieve my aims
Reply #5 - Jan 27th, 2015 at 11:00am
Print Post  
Unfortunately using other values such as points.length -1 or even just 1 make no difference - the subordinateGroup.attachedNodes remains empty on the link, and the masterGroup on the node being used as a label is still null.

Before I make the attachTo() call on the ShapeNode for the link, the subordinateGroup on the link is null, so something is happening to create the 'Group' item, but it is not being populated, and nothing is added to the label itself.

Attached is a minimal application (based on your IconNodes example) to reproduce this problem.
  

MinimalLinkProblemDemo.zip ( 3 KB | 271 Downloads )
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: How to label links to achieve my aims
Reply #6 - Jan 27th, 2015 at 12:22pm
Print Post  
You are now using the AttachToLink.Segment attachment type; note that there are even fewer segments than control points especially for Bezier links, so the 4 control points you are seeing correspond to just one segment, and only 0 is accepted as a valid index. So replacing

Code
Select All
label.attachTo( link, AttachToLink.Segment,points.length -1 );

// with

label.attachTo( link, AttachToLink.Segment, 0); 



works for me and next double-clicks show the edit dialog.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
AR
Junior Member
**
Offline


I Love MindFusion!

Posts: 65
Joined: Jan 23rd, 2015
Re: How to label links to achieve my aims
Reply #7 - Jan 27th, 2015 at 2:32pm
Print Post  
Thanks, that works beautifully and when the link is moved the label moves with it now - just what I wanted Smiley
  
Back to top
 
IP Logged
 
AR
Junior Member
**
Offline


I Love MindFusion!

Posts: 65
Joined: Jan 23rd, 2015
Re: How to label links to achieve my aims
Reply #8 - Feb 26th, 2015 at 5:28pm
Print Post  
Just one more query to do with this - is there any way to prevent the user selecting and deleting the 'shapenode' being used as a label?

I do want them to be able to move it relative to the link, but I only want it deleted if the link itself is deleted.

Is this possible?

(I already have working code which deletes the label node when the link is deleted.)
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: How to label links to achieve my aims
Reply #9 - Feb 27th, 2015 at 8:57am
Print Post  
You can prevent this by handling the nodeDeleting validation event:

Code
Select All
diagram.addDiagramListener(new DiagramAdapter() {

	@Override
	public void nodeDeleting(NodeValidationEvent e)
	{
		Group group = e.getNode().getMasterGroup();
		if (group != null && group.getMainItem() instanceof DiagramLink)
			e.setCancel(true);
	}
... 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
AR
Junior Member
**
Offline


I Love MindFusion!

Posts: 65
Joined: Jan 23rd, 2015
Re: How to label links to achieve my aims
Reply #10 - Mar 3rd, 2015 at 11:21am
Print Post  
Thanks that works exactly as I needed it to Smiley
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint