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:
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?