Ejemplo n.º 1
0
 public static ElementListCompartment GetCompartment(this ModelElement element, string compartmentName)
 {
     return(element.GetFirstShapeElement()
            .NestedChildShapes
            .OfType <ElementListCompartment>()
            .FirstOrDefault(s => s.Name == compartmentName));
 }
Ejemplo n.º 2
0
        private static ShapeElement GetShapeElement(this ModelElement element)
        {
            // Get the first shape
            // If the model element is in a compartment the result will be null

            return(element.GetFirstShapeElement() ?? element.GetCompartmentElementFirstParentElement()?.GetFirstShapeElement());
        }
        /// <summary>
        /// Sets the selection state of a shape element that represents the modelElement parameter on the given DiagramView
        /// </summary>
        /// <param name="diagramView">Object containing the shape element to select</param>
        /// <param name="modelElement">Model element the shape represents</param>
        /// <returns>True if the shape is present, visible and now selected, false otherwise</returns>
        public static bool SelectModelElement(this DiagramView diagramView, ModelElement modelElement)
        {
            // Get the shape element that corresponds to the model element

            ShapeElement shapeElement = modelElement.GetFirstShapeElement();

            if (shapeElement != null)
            {
                // Make sure the shape element is visible (because connectors can be hidden)

                if (!shapeElement.IsVisible)
                {
                    shapeElement.Show();
                }

                // Create a diagram item for this shape element and select it
                diagramView.Selection.Set(new DiagramItem(shapeElement));

                return(true);
            }

            // If the model element does not have a shape, try to cast it IModelElementCompartmented

            if (modelElement is IModelElementInCompartment compartmentedModelElement && compartmentedModelElement.ParentModelElement is ModelElement parentModelElement)
            {
                // Get the compartment that stores the model element

                ElementListCompartment compartment = parentModelElement.GetCompartment(compartmentedModelElement.CompartmentName);

                if (compartment == null)
                {
                    throw new InvalidOperationException($"Can't find compartment {compartmentedModelElement.CompartmentName}");
                }

                // Expand the compartment
                if (!compartment.IsExpanded)
                {
                    using (Transaction trans = modelElement.Store.TransactionManager.BeginTransaction("IsExpanded"))
                    {
                        compartment.IsExpanded = true;
                        trans.Commit();
                    }
                }

                // Find the model element in the compartment

                int index = compartment.Items.IndexOf(modelElement);

                if (index >= 0)
                {
                    // Create a diagram item and select it

                    diagramView.Selection.Set(new DiagramItem(compartment, compartment.ListField, new ListItemSubField(index)));

                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 4
0
        private void AddElementsToActiveDiagram(List <ModelElement> newElements)
        {
            // TODO: Needs sped up
            int elementCount = newElements.Count;
            List <ShapeElement> newShapes = new List <ShapeElement>();

            using (Transaction t = Store.TransactionManager.BeginTransaction("adding diagram elements"))
            {
                for (int index = 0; index < elementCount; index++)
                {
                    ModelElement newElement = newElements[index];
                    StatusDisplay.Show($"Adding element {index + 1} of {elementCount}");

                    ForceAddShape = true;
                    FixUpAllDiagrams.FixUp(this, newElement);
                    newShapes.Add(newElement.GetFirstShapeElement());
                    ForceAddShape = false;
                }

                t.Commit();
            }

            using (Transaction t = Store.TransactionManager.BeginTransaction("adding diagram links"))
            {
                for (int index = 0; index < elementCount; index++)
                {
                    ModelElement newElement = newElements[index];
                    StatusDisplay.Show($"Linking {index + 1} of {elementCount}");

                    // find all element links that are attached to our element where the ends are in the diagram but the link isn't already in the diagram
                    List <ElementLink> elementLinks = Store.GetAll <ElementLink>()
                                                      .Where(link => link.LinkedElements.Contains(newElement) &&
                                                             link.LinkedElements.All(linkedElement => DisplayedElements.Contains(linkedElement)) &&
                                                             !DisplayedElements.Contains(link))
                                                      .ToList();

                    foreach (ElementLink elementLink in elementLinks)
                    {
                        BinaryLinkShape linkShape = CreateChildShape(elementLink) as BinaryLinkShape;
                        newShapes.Add(linkShape);
                        NestedChildShapes.Add(linkShape);

                        switch (elementLink)
                        {
                        case Association a:
                            linkShape.FromShape = a.Source.GetFirstShapeElement() as NodeShape;
                            linkShape.ToShape   = a.Target.GetFirstShapeElement() as NodeShape;

                            break;

                        case Generalization g:
                            linkShape.FromShape = g.Subclass.GetFirstShapeElement() as NodeShape;
                            linkShape.ToShape   = g.Superclass.GetFirstShapeElement() as NodeShape;

                            break;
                        }
                    }
                }

                AutoLayoutShapeElements(newShapes);
                t.Commit();
            }
        }