Dragging connections from a port

I have a problem with ports. I created input and output ports on my custom shape. Also there can only be one connection on a port. This all works fine. Now I want the user to be able to move connections to other ports just like he adds a new connection. So the user should hold down the mouse button on a port and drag or create a connection.



I tried to override the DraggingObject method within my port implementation and returning the link if there is one. But this doesn’t seem to work. I also tried to use the OnSingleClick method but it’s only called after the mouse button is released. How would I implement such a behavior? Maybe writing my own custom tool?

So are you saying that a mouse-down-mouse-move should start drawing a new link if there is no link already connected at a port, and when there is a link connected to a port it should start reconnecting that link?
If so, then you need to change the behaviors of both linking tools by inheriting from GoToolLinkingNew and GoToolRelinking and replacing the standard linking tools in your GoView. I think you just need to change their CanStart predicates to handle the additional cases that you want:
// only draw new link when there are no links already at the port
[Serializable]
public class TestLinkingNewTool : GoToolLinkingNew {
public TestLinkingNewTool(GoView view) : base(view) {}
public override bool CanStart() {
bool result = base.CanStart();
if (!result) return false;
// also false when there are any links already at the port
return (this.OriginalStartPort != null && this.OriginalStartPort.LinksCount == 0);
}
}
// dragging from a port with one link will start relinking
[Serializable]
public class TestRelinkingTool : GoToolRelinking {
public TestRelinkingTool(GoView view) : base(view) {}
public override bool CanStart() {
bool result = base.CanStart();
if (result) return true;
// also true when dragging from port with exactly one link
IGoPort port = PickPort(this.FirstInput.DocPoint);
if (port != null && port.LinksCount == 1) {
IGoLink link = null;
foreach (IGoLink l in port.Links) {
link = l; // get first and only link connected to this port
break;
}
if (link != null) {
this.Link = link;
this.Forwards = (link.ToPort == port);
return true;
}
}
return false;
}
}

Thanks, that’s what I am looking for.