/// <summary> /// Iterate shapes associated with a given element /// </summary> /// <param name="modelElement">The parent element to iterate. Can be <see langword="null"/> if <paramref name="shapeElement"/> is specified.</param> /// <param name="shapeElement">The shape to reference. Can be <see langword="null"/> if <paramref name="modelElement"/> is specified.</param> /// <param name="onePerDiagram">True to filter the shapes to one per diagram</param> /// <param name="visitor">A <see cref="Predicate{ShapeElement}"/> callback. /// Return <see langword="true"/> to continue iteration.</param> public static void VisitAssociatedShapes(ModelElement modelElement, ShapeElement shapeElement, bool onePerDiagram, Predicate<ShapeElement> visitor) { if (modelElement == null && shapeElement != null) { modelElement = shapeElement.ModelElement; } if (modelElement != null) { LinkedElementCollection<PresentationElement> presentationElements = PresentationViewsSubject.GetPresentation(modelElement); int pelCount = presentationElements.Count; if (pelCount != 0) { List<Diagram> visitedDiagrams = null; Diagram visitedDiagram = null; Partition shapePartition = (shapeElement != null) ? shapeElement.Partition : modelElement.Partition; Type thisType = (shapeElement != null) ? shapeElement.GetType() : typeof(ShapeElement); for (int i = 0; i < pelCount; ++i) { PresentationElement pel = presentationElements[i]; if (shapePartition == pel.Partition && thisType.IsAssignableFrom(pel.GetType())) { ShapeElement sel = pel as ShapeElement; Diagram selDiagram = sel.Diagram; if (!onePerDiagram || (visitedDiagram != selDiagram && (visitedDiagrams == null || !visitedDiagrams.Contains(selDiagram)))) { if (!visitor(sel)) { return; } if (onePerDiagram) { if (visitedDiagram == null) { visitedDiagram = selDiagram; } else { if (visitedDiagrams == null) { visitedDiagrams = new List<Diagram>(); } visitedDiagrams.Add(selDiagram); } } } } } } } }
/// <summary> /// Determines if a the model element backing the shape element /// is represented by a shape of the same type elsewhere in the presentation /// layer. If a derived shape displays multiple presentations, they should /// also override the <see cref="DisplaysMultiplePresentations"/> property. /// </summary> public static bool ElementHasMultiplePresentations(ShapeElement shapeElement) { ModelElement modelElement; if (null != shapeElement && null != (modelElement = shapeElement.ModelElement)) { LinkedElementCollection<PresentationElement> presentationElements = PresentationViewsSubject.GetPresentation(modelElement); int pelCount = presentationElements.Count; if (pelCount != 0) { Partition shapePartition = shapeElement.Partition; Type thisType = shapeElement.GetType(); for (int i = 0; i < pelCount; ++i) { PresentationElement pel = presentationElements[i]; if (shapeElement != pel && shapePartition == pel.Partition && thisType.IsAssignableFrom(pel.GetType())) { return true; } } } } return false; }