We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Preventing placement of unconnected connectors

I wish to prevent the placement of unconnected connectors.  My best guess at how to achieve this is as shown below.

internal class MyLineConnectorTool : LineConnectorTool
    {
        public MyLineConnectorTool(DiagramController controller)
            : base(controller)
        {
            HeadDecorator.DecoratorShape = DecoratorShape.Filled45Arrow;
        }

        public override Tool ProcessMouseDown(MouseEventArgs evtArgs)
        {

            PointF mouseLocation = Controller.ConvertToModelCoordinates(evtArgs.Location);
            var nd = Controller.GetNodeAtPoint(mouseLocation);

            if(nd != null)
            {
                var cp = HandlesHitTesting.GetConnectionPointAtPoint(nd, mouseLocation);
                if(cp == null) return null;
                if(cp.ConnectionPointType == ConnectionPointType.Outgoing)
                    return base.ProcessMouseDown(evtArgs);
            }
            return null;
        }

        public override Tool ProcessMouseUp(MouseEventArgs evtArgs)
        {
            PointF mouseLocation = Controller.ConvertToModelCoordinates(evtArgs.Location);
            var nd = Controller.GetNodeAtPoint(mouseLocation);

            if(nd != null)
            {
                var cp = HandlesHitTesting.GetConnectionPointAtPoint(nd, mouseLocation);
                if(cp == null) return null;
                if(cp.ConnectionPointType == ConnectionPointType.Incoming)
                    return base.ProcessMouseUp(evtArgs);
            }
            return null;
        }
    }

This works for an invalid initial point but an invalid final point leaves the connection line drawn even though it seems clear that no connector has been placed.  As this code is mostly guesswork I am surprised that it works as well as it does :)
What is the official method to do this?

Thanks,
Andy

3 Replies

NG Naganathan Ganesh Babu Syncfusion Team May 12, 2015 07:15 AM UTC

Hi Andrew,

Thanks for using Syncfusion product.

We are glad to inform you that we have modified your code snippet to achieve your requirement. You were using Connector’s “ConnectionType” to identifying connection type is incoming/outgoing while connecting the nodes. We suggest you to use Node.CentralPort’s “ConnectionType” instead of Connector.ConnectionType while connecting the Nodes using connector tool to achieve your requirement. Please refer the below modified code snippet and sample for your references.

Here is the code snippet:

[C#]

public class MyLineConnectorTool : LineConnectorTool

{

public MyLineConnectorTool(DiagramController controller)

: base(controller)

{

HeadDecorator.DecoratorShape = DecoratorShape.Filled45Arrow;

Name = "LineConnectorTool";

}


public override Tool ProcessMouseDown(MouseEventArgs evtArgs)

{

PointF mouseLocation = Controller.ConvertToModelCoordinates(evtArgs.Location);

var nd = Controller.GetNodeAtPoint(mouseLocation);

if (nd != null && (nd.CentralPort.ConnectionPointType == ConnectionPointType.Outgoing

|| nd.CentralPort.ConnectionPointType == ConnectionPointType.IncomingOutgoing))

{

return base.ProcessMouseDown(evtArgs);

}

else

return null;

}


public override Tool ProcessMouseUp(MouseEventArgs evtArgs)

{

PointF mouseLocation = Controller.ConvertToModelCoordinates(evtArgs.Location);

var nd = Controller.GetNodeAtPoint(mouseLocation);

if (nd != null && (nd.CentralPort.ConnectionPointType == ConnectionPointType.Incoming

|| nd.CentralPort.ConnectionPointType == ConnectionPointType.IncomingOutgoing))

{

return base.ProcessMouseUp(evtArgs);

}

else

{

this.Controller.Model.BeginUpdate();

this.InAction = false;

this.Controller.Model.EndUpdate();

return null;

}

}
}


Here is the Sample:

Sample

Please let us know if any concerns.

Regards,

Naganathan K G



AL Andrew Le Couteur Bisson May 12, 2015 09:22 AM UTC

Thanks for your very quick and complete response. 
I am actually using custom symbols with distinct inputs and outputs so the centre port is disabled.
The important detail that I was unaware of is the else clause in ProcessMouseUp()
Adding these three lines to my code fixed it perfectly.

Thanks again,
Andy


NG Naganathan Ganesh Babu Syncfusion Team May 13, 2015 05:01 AM UTC

Hi Andrew,

Thanks for your update.

Please let us know if you need further assistance.

Regards,

Naganathan K G


Loader.
Live Chat Icon For mobile
Up arrow icon