Exemplo n.º 1
0
        /// <summary>
        /// News the connection.
        /// </summary>
        /// <param name="sourceElement">source element</param>
        /// <param name="sourceViewHelper">view helper for the association end that is related to <paramref name="sourceElement"/>,
        /// can be set to null if <paramref name="sourceElement"/> has not an association end for this connection.</param>
        /// <param name="targetElement">target element</param>
        /// <param name="targetViewHelper">view helper for the association end that is related to <paramref name="targetElement"/>,
        /// can be set to null if <paramref name="targetElement"/> has not an association end for this connection.</param>
        /// <param name="points">collection of points for the junction (usually part of some ViewHelper), the junction reflects
        /// all changes in the collection</param>
        public void NewConnection(
            IConnectable sourceElement,
            AssociationEndViewHelper sourceViewHelper,
            IConnectable targetElement,
            AssociationEndViewHelper targetViewHelper,
            ObservablePointCollection points)
        {
            foreach (JunctionPoint point in Points)
            {
                point.DeleteFromCanvas();
            }
            Points.Clear();
            if (points == null)
            {
                throw new ArgumentNullException("points");
            }
            if (points.Count < 2)
            {
                throw new ArgumentException("There must be at last two points to set a connection.", "points");
            }

            SourceElement = (Control)sourceElement;
            TargetElement = (Control)targetElement;

            JunctionPoint startPoint;

            if (sourceViewHelper != null)
            {
                startPoint = ((ICreatesAssociationEnd)sourceElement).CreateAssociationEnd(points.First(), sourceViewHelper, sourceViewHelper.AssociationEnd);
                ((PIM_AssociationEnd)startPoint).Association = Association;
            }
            else
            {
                startPoint = (sourceElement).CreateJunctionEnd(points.First());
            }

            startPoint.ContextMenu     = null;
            startPoint.Junction        = this;
            startPoint.OrderInJunction = 0;
            Points.Add(startPoint);

            JunctionPoint endPoint;

            if (targetViewHelper != null)
            {
                endPoint = ((ICreatesAssociationEnd)targetElement).CreateAssociationEnd(points.Last(), targetViewHelper, targetViewHelper.AssociationEnd);
                ((PIM_AssociationEnd)endPoint).Association = Association;
            }
            else
            {
                endPoint = targetElement.CreateJunctionEnd(points.Last());
            }

            endPoint.ContextMenu     = null;
            endPoint.Junction        = this;
            endPoint.OrderInJunction = 1;
            Points.Add(endPoint);

            if (startPoint is PIM_AssociationEnd)
            {
                ((PIM_AssociationEnd)startPoint).StartBindings();
            }
            if (endPoint is PIM_AssociationEnd)
            {
                ((PIM_AssociationEnd)endPoint).StartBindings();
            }

            int zindex = Math.Min(Panel.GetZIndex(SourceElement), Panel.GetZIndex(TargetElement));

            zindex--;
            Canvas.SetZIndex(this, zindex - 1);

            UpdateLayout();
            for (int i = 1; i < points.Count - 1; i++)
            {
                Point point = points[i];
                BreakLine(point, i);
            }

            SourceElement.SizeChanged += SourceElement_SizeChanged;
            TargetElement.SizeChanged += TargetElement_SizeChanged;
            AutoPosModeOnly            = AutoPosModeOnly;
            InvalidateGeometry();
        }