/// <summary> /// Removes the element from <see cref="XCaseCanvas"/>. /// </summary> public void DeleteFromCanvas() { AssociationName = null; foreach (KeyValuePair <AssociationEnd, XCaseJunction> pair in associationJunctions) { pair.Value.DeleteFromCanvas(); } if (simpleAssociationJunction != null) { simpleAssociationJunction.DeleteFromCanvas(); } if (Diamond != null) { Diamond.DeleteFromCanvas(); } this.CloseBindings(); Controller.Association.Ends.CollectionChanged -= AssociationEnds_CollectionChanged; }
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; } } }