Loop link, not avoiding node

Hi.

The 'avoid nodes' property is working nicely for me except if I have nodes with loop links. For instance I have a custom JGoNode that have input ports on the left and output ports on the right. If a create a loop link on such a node, in other words, a link from one of the node's output ports to one of the input ports on the same node, then the resulting link does not avoid the node - it is drawn straight through the node and its hard to visually pick up the link and its flow. Why is the link not avoiding the owning node?
The only time when it avoids itself is when the orthogonal property is set, but when we have more than one loop link on the same node they overlap and it becomes hard to pick up which link is which is which. Also the convention is that all links are cubic, thus I would like to keep the curvy feel on all the links.
Any ideas?
I'm using JGo4.1

Actually, the AvoidsNodes property only works when the Orthogonal property is also true. And it’s also true that orthogonal links can have segments that overlap.

If you stay with cubic links, you could detect the case where you are linking to the same node and route the link yourself. You might want to do this by overriding JGoLink.calculateStroke() so this will occur whenever the route needs to be recalculated.

Thanks for the tip ssmith. I will try that.

How’s about some sample code maybe? Please? The overidden calculateStroke() method will look like this, but I need some help with the actual recalculation of the link.

public void calculateStroke()
{
if(this.getFromPort().getParent() == this.getToPort().getParent())
{
//Recalculation of link to avoid node done here...
}
else
{
super.calculateStroke();
}
}

The following code is on the JGoDocumentChanged event when a link is added to the document rather than an override of calculateStroke(), but it seems to work fine.

void jGoView1_documentChanged(JGoDocumentEvent e) {
switch (e.getHint()) {
case JGoDocumentEvent.INSERTED:
if (e.getJGoObject()instanceof JGoLink) {
JGoLink link = (JGoLink) e.getJGoObject();
link.setArrowHeads(false, true);
link.setCubic(true);
JGoPort from = link.getFromPort();
JGoPort to = link.getToPort();
int existingSelfLinkCount = 0;
for (JGoListPosition pos = from.getFirstLinkPos(); pos != null; pos = from.getNextLinkPos(pos)) {
JGoLink l = from.getLinkAtPos(pos);
if ((from == l.getFromPort()) && (to == l.getToPort()) && (link != l)) {
existingSelfLinkCount++;
}
}
link.setCurviness(existingSelfLinkCount * 10);
link.calculateStroke();
}
break;
}
}

Thanks, I have thought about such a solution, where one sets the curviness on a self link based on the amount of existing self links on that specific node, but the problem is I’m running version 4.1, which does not have the cool setCurviness method

Any other ideas? Except upgrading

Actually, the upgrading is a really good idea. Version 4.1 is quite old now.
However, if you really want to stay with the old version, you could do something like the following:
void jGoView1_documentChanged(JGoDocumentEvent e) {
switch (e.getHint()) {
case JGoDocumentEvent.INSERTED:
if (e.getJGoObject()instanceof JGoLink) {
JGoLink link = (JGoLink) e.getJGoObject();
link.setArrowHeads(false, true);
link.setCubic(true);
JGoPort from = link.getFromPort();
JGoPort to = link.getToPort();
int existingSelfLinkCount = 0;
for (JGoListPosition pos = from.getFirstLinkPos(); pos != null; pos = from.getNextLinkPos(pos)) {
JGoLink l = from.getLinkAtPos(pos);
if ((from == l.getFromPort()) && (to == l.getToPort()) && (link != l)) {
existingSelfLinkCount++;
}
}
if (from == to) {
Point fromPoint = link.getStartPoint();
Point endPoint = link.getEndPoint();
link.removeAllPoints();
link.addPoint(fromPoint);
link.addPoint(from.getLocation().x + (existingSelfLinkCount + 1) * 40, from.getLocation().y);
link.addPoint(from.getLocation().x , from.getLocation().y + (existingSelfLinkCount + 1) * 40);
link.addPoint(endPoint);
}
}
break;
}
}

You'll probably want to fiddle with the intermediate points to get something that looks nice.

Thanks a lot man, I haven’t tried the code but reading it makes sense. Thanks again. Yes, I want to upgrade to the latest, but I first have to convince management… Thus far we have been able to do almost everything we need to with 4.1, its probably going to take a big halt point, which is solvable out of the box with 5.1.2 to convince them.