Example #1
0
        /// <summary>
        /// Creates connection between from and to according to Definition
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <returns>Returns null if connection cannot be created</returns>
        public ConnectionViewModel AddConnection(NodeBaseViewModel from, NodeBaseViewModel to)
        {
            var edge = Definition.CreateConnection(from, to);

            if (edge == null)
            {
                return(null);
            }
            // prevent showing attach descriptors  when their position is not calculated yet
            // this can be possible not necessary if calculation their location was done before data binding of AttachDescriptors collection
            // but then we would not have size of attach descriptors and size is necessary for calculating position
            if (edge.FromDescriptor != null)
            {
                edge.FromDescriptor.Location = new Point(-1000, -1000);
            }
            if (edge.ToDescriptor != null)
            {
                edge.ToDescriptor.Location = new Point(-1000, -1000);
            }

            edge.ParentDiagram = this;


            if (edge.FromDescriptor != null)
            {
                AttachDescriptors.Add(edge.FromDescriptor);

                edge.FromDescriptor.Name = "from";

                if (!IsInBatchMode)
                {
                    ForceRedraw();
                    edge.FromDescriptor.RaiseInitialize();
                }
            }
            if (edge.ToDescriptor != null)
            {
                AttachDescriptors.Add(edge.ToDescriptor);

                edge.ToDescriptor.Name = "to";
                if (!IsInBatchMode)
                {
                    ForceRedraw();
                    edge.ToDescriptor.RaiseInitialize();
                }
            }
            if (!IsInBatchMode)
            {
                ForceRedraw();
            }

            var sides = Definition.ConnectorSideStrategy.GetSidesForConnection(edge);

            edge.AttachPointFrom = from.Attach(sides.FromSide, edge, edge.FromDescriptor);
            edge.AttachPointTo   = to.Attach(sides.ToSide, edge, edge.ToDescriptor);

            Edges.Add(edge);

            if (!IsInBatchMode)
            {
                edge.UpdateConnection();
            }
            from.RaiseConnectionAdded(edge);
            to.RaiseConnectionAdded(edge);
            return(edge);
        }