Exemple #1
0
 public override void DeleteFromCanvas()
 {
     if (junction != null)
     {
         junction.DeleteFromCanvas();
     }
     if (primitiveJunction != null)
     {
         primitiveJunction.DeleteFromCanvas();
     }
     base.DeleteFromCanvas();
     this.CloseBindings();
 }
Exemple #2
0
        private void InitializeFromViewHelper()
        {
            if (ViewHelper.AssociationEndsViewHelpers.Count != Association.Ends.Count)
            {
                ViewHelper.CreateEndsViewHelpers(this.Association);
            }
            if (ViewHelper.ForceUseDiamond)
            {
                ViewHelper.UseDiamond = true;
            }
            ViewHelper.ForceUseDiamond = false;
            participantElements        = new Dictionary <AssociationEndViewHelper, IConnectable>();
            foreach (AssociationEndViewHelper endViewHelper in ViewHelper.AssociationEndsViewHelpers)
            {
                if (!(XCaseCanvas.ElementRepresentations[endViewHelper.AssociationEnd.Class] is IConnectable))
                {
                    throw new InvalidOperationException("One of the association ends is not visualized as IConnectable");
                }
                participantElements[endViewHelper]
                    = XCaseCanvas.ElementRepresentations[endViewHelper.AssociationEnd.Class] as IConnectable;
            }

            foreach (Control c in subControls)
            {
                if (c != null)
                {
                    c.MouseEnter -= junction_MouseEnter;
                    c.MouseLeave -= junction_MouseLeave;
                }
            }

            if (ViewHelper.UseDiamond)
            {
                // simpleAssociationJunction field won't be used if diamond is used
                if (simpleAssociationJunction != null)
                {
                    simpleAssociationJunction.DeleteFromCanvas();
                    ViewHelper.AssociationEndsViewHelpers[0].Points.Clear();
                }

                // prepare the diamond
                if (Diamond == null)
                {
                    Diamond = new AssociationDiamond(XCaseCanvas, this, ViewHelper);
                    XCaseCanvas.Children.Add(Diamond);
                }
                Rect r = RectExtensions.GetEncompassingRectangle(participantElements.Values);
                if (ViewHelper.X == 0 && ViewHelper.Y == 0)
                {
                    Diamond.X    = r.GetCenter().X - Diamond.ActualWidth / 2;
                    Diamond.Y    = r.GetCenter().Y - Diamond.ActualHeight / 2;
                    ViewHelper.X = Diamond.X;
                    ViewHelper.Y = Diamond.Y;
                }
                Diamond.UpdateLayout();

                /*
                 * for each association end there will be one junction between the diamond and the
                 * participating element. Start point of the junction will lay on the diamond, end
                 * point of the junction will lay on the participating element and will be bound to
                 * an association end.
                 */
                foreach (AssociationEndViewHelper endViewHelper in ViewHelper.AssociationEndsViewHelpers)
                {
                    //compute the start and end point of each junction if they are not already computed
                    if (endViewHelper.Points.Count < 2)
                    {
                        endViewHelper.Points.Clear();
                        endViewHelper.Points.AppendRange(JunctionGeometryHelper.ComputeOptimalConnection(Diamond, participantElements[endViewHelper]));
                        if (!Diamond.IsMeasureValid || !participantElements[endViewHelper].IsMeasureValid)
                        {
                            endViewHelper.Points.PointsInvalid = true;
                        }
                    }
                    XCaseJunction junction = new XCaseJunction(XCaseCanvas, this, endViewHelper.Points);
                    junction.NewConnection(Diamond, null, participantElements[endViewHelper], endViewHelper, endViewHelper.Points);
                    junction.StartPoint.Movable    = false;
                    junction.StartPoint.Visibility = Visibility.Hidden;
                    associationJunctions[endViewHelper.AssociationEnd] = junction;
                    XCaseCanvas.Children.Add(junction);
                }
            }
            else
            {
                foreach (XCaseJunction junction in associationJunctions.Values)
                {
                    junction.DeleteFromCanvas();
                }
                if (Diamond != null)
                {
                    Diamond.DeleteFromCanvas();
                    Diamond = null;
                }

                associationJunctions.Clear();

                IConnectable SourceElement = (IConnectable)XCaseCanvas.ElementRepresentations[ViewHelper.AssociationEndsViewHelpers[0].AssociationEnd.Class];
                IConnectable TargetElement;

                if (ViewHelper.AssociationEndsViewHelpers.Count == 2 &&
                    ViewHelper.AssociationEndsViewHelpers[0].AssociationEnd.Class == ViewHelper.AssociationEndsViewHelpers[1].AssociationEnd.Class)
                {
                    TargetElement = SourceElement;

                    if (ViewHelper.Points.Count == 0)
                    {
                        ViewHelper.Points.AppendRange(JunctionGeometryHelper.ComputePointsForSelfAssociation(SourceElement.GetBounds()));
                        if (!SourceElement.IsMeasureValid)
                        {
                            ViewHelper.Points.PointsInvalid = true;
                        }
                    }
                }

                /*
                 * two different classes are connected directly without using central diamond,
                 * association will consist of two points
                 * in optimal position to connect two rectangles.
                 */
                else if (ViewHelper.AssociationEndsViewHelpers.Count == 2)
                {
                    TargetElement = (IConnectable)XCaseCanvas.ElementRepresentations[ViewHelper.AssociationEndsViewHelpers[1].AssociationEnd.Class];
                    if (ViewHelper.Points.Count < 2)
                    {
                        ViewHelper.Points.Clear();
                        ViewHelper.Points.AppendRange(JunctionGeometryHelper.ComputeOptimalConnection(SourceElement, TargetElement));
                        if (!SourceElement.IsMeasureValid)
                        {
                            ViewHelper.Points.PointsInvalid = true;
                        }
                    }
                }
                else
                {
                    throw new InvalidOperationException("UseDiamond must be true if there are more association ends than 2");
                }

                /*
                 * diamond is not used, simpleAssociationJunction field will be used to
                 * represent the junction
                 */
                if (simpleAssociationJunction != null)
                {
                    simpleAssociationJunction.DeleteFromCanvas();
                }
                simpleAssociationJunction = new XCaseJunction(XCaseCanvas, this, ViewHelper.Points);
                if (Association.Ends.Count == 1)
                {
                    simpleAssociationJunction.NewConnection(SourceElement, null, TargetElement, ViewHelper.AssociationEndsViewHelpers[0], ViewHelper.Points);
                }
                else
                {
                    simpleAssociationJunction.NewConnection(SourceElement, ViewHelper.AssociationEndsViewHelpers[0], TargetElement, ViewHelper.AssociationEndsViewHelpers[1], ViewHelper.Points);
                }
                XCaseCanvas.Children.Add(simpleAssociationJunction);
            }

            foreach (Control c in subControls)
            {
                if (c != null)
                {
                    c.MouseEnter += junction_MouseEnter;
                    c.MouseLeave += junction_MouseLeave;
                }
            }
        }
Exemple #3
0
            /// <summary>
            /// If mouse is over a connectable item, <see cref="DraggedConnectionProcessor"/>'s
            /// <see cref="IDraggedConnectionProcessor.DragConnectionCompleted"/> handler is called
            /// to finilize the operation. Otherwise nothing happens.
            /// </summary>
            /// <param name="e"></param>
            public override void OnMouseUp(MouseButtonEventArgs e)
            {
                if (e.ChangedButton == MouseButton.Right)
                {
                    Canvas.State = ECanvasState.Normal;
                    e.Handled    = true;
                    return;
                }
                base.OnMouseUp(e);

                Canvas.ReleaseMouseCapture();
                InConnectionDrag = false;
                Mouse.SetCursor(Cursors.Arrow);

                if (draggedConnection != null)
                {
                    if (draggedConnection.SourceElement != null)
                    {
                        ((IConnectable)draggedConnection.SourceElement).UnHighlight();
                    }
                    if (draggedConnection.TargetElement != null)
                    {
                        ((IConnectable)draggedConnection.TargetElement).UnHighlight();
                    }

                    draggedConnection.DeleteFromCanvas();

                    if (DraggedConnectionProcessor != null)
                    {
                        Element sourceElement = null;
                        Element targetElement = null;

                        if (draggedConnection.SourceElement is XCaseViewBase)
                        {
                            sourceElement = ((XCaseViewBase)draggedConnection.SourceElement).ModelElement;
                        }
                        if (draggedConnection.TargetElement is XCaseViewBase)
                        {
                            targetElement = ((XCaseViewBase)draggedConnection.TargetElement).ModelElement;
                        }
                        if (draggedConnection.SourceElement != null && ItemDraggedOver == draggedConnection.SourceElement)
                        {
                            targetElement = sourceElement;
                        }
                        if (draggedConnection.SourceElement is PIM_Class && draggedConnection.TargetElement is AssociationDiamond)
                        {
                            targetElement = ((PIM_Class)draggedConnection.SourceElement).ModelElement;
                            sourceElement = ((AssociationDiamond)draggedConnection.TargetElement).Association.Association;
                        }
                        if (draggedConnection.SourceElement is AssociationDiamond && draggedConnection.TargetElement is PIM_Class)
                        {
                            targetElement = ((PIM_Class)draggedConnection.TargetElement).ModelElement;
                            sourceElement = ((AssociationDiamond)draggedConnection.SourceElement).Association.Association;
                        }
                        DraggedConnectionProcessor.DragConnectionCompleted(sourceElement, targetElement);
                        ItemDraggedOver = (IConnectable)draggedConnection.TargetElement;
                    }
                }

                draggedConnection = null;
                Canvas.Children.Remove(draggedPoint);
                draggedPoint = null;
            }