/// <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.º 2
0
        protected override void OnSelectionChanged(EventArgs e)
        {
            base.OnSelectionChanged(e);

            // select element in tree
            if (PrimarySelection != null && PrimarySelection is ModelElement element)
            {
                using (Transaction t = element.Store.TransactionManager.BeginTransaction("TreeSelectionChanged"))
                {
                    Diagram diagram = element.GetActiveDiagramView()?.Diagram;

                    switch (PrimarySelection)
                    {
                    case ModelDiagramData modelDiagramData:
                        // user selected a diagram. Open it.
                        EFModelDocData docData = (EFModelDocData)TreeContainer.ModelingDocData;
                        docData.OpenView(Constants.LogicalView, new Mexedge.VisualStudio.Modeling.ViewContext(modelDiagramData.Name, typeof(EFModelDiagram), docData.RootElement));

                        break;

                    case ModelClass modelClass:
                        // user selected a class. If it's in the current diagram, find it, center it and make it visible
                        ShapeElement primaryShapeElement = PresentationViewsSubject.GetPresentation(modelClass)
                                                           .OfType <ShapeElement>()
                                                           .FirstOrDefault(s => s.Diagram == diagram);

                        //if (primaryShapeElement == null || !primaryShapeElement.IsVisible)
                        //   break;

                        modelClass.LocateInDiagram(true);

                        // then fix up the compartments since they might need it
                        ModelElement[] classElements = { modelClass };
                        CompartmentItemAddRule.UpdateCompartments(classElements, typeof(ClassShape), "AttributesCompartment", false);
                        CompartmentItemAddRule.UpdateCompartments(classElements, typeof(ClassShape), "AssociationsCompartment", false);
                        CompartmentItemAddRule.UpdateCompartments(classElements, typeof(ClassShape), "SourcesCompartment", false);

                        // any associations to visible classes on this diagram need to be visible as well
                        foreach (NavigationProperty navigationProperty in modelClass.LocalNavigationProperties())
                        {
                            ModelClass other = navigationProperty.AssociationObject.Source == modelClass
                                                 ? navigationProperty.AssociationObject.Target
                                                 : navigationProperty.AssociationObject.Source;

                            // should never happen
                            if (other == null)
                            {
                                continue;
                            }

                            ShapeElement shapeElement = PresentationViewsSubject.GetPresentation(other)
                                                        .OfType <ShapeElement>()
                                                        .FirstOrDefault(s => s.Diagram == diagram);

                            if (shapeElement != null && shapeElement.IsVisible)
                            {
                                ShapeElement connectorElement = PresentationViewsSubject.GetPresentation(navigationProperty.AssociationObject)
                                                                .OfType <AssociationConnector>()
                                                                .FirstOrDefault(s => s.Diagram == diagram);
                                connectorElement?.Show();
                            }
                        }

                        // so do generalizations, as long as both classes are available
                        foreach (Generalization generalization in modelClass.Store.ElementDirectory.AllElements.OfType <Generalization>().Where(g => g.Superclass == modelClass || g.Subclass == modelClass))
                        {
                            ModelClass other = generalization.Superclass == modelClass
                                              ? generalization.Subclass
                                              : generalization.Superclass;

                            // should never happen
                            if (other == null)
                            {
                                continue;
                            }

                            ShapeElement shapeElement = PresentationViewsSubject.GetPresentation(other)
                                                        .OfType <ShapeElement>()
                                                        .FirstOrDefault(s => s.Diagram == diagram);

                            if (shapeElement != null && shapeElement.IsVisible)
                            {
                                ShapeElement connectorElement = PresentationViewsSubject.GetPresentation(generalization)
                                                                .OfType <GeneralizationConnector>()
                                                                .FirstOrDefault(s => s.Diagram == diagram);
                                connectorElement?.Show();
                            }
                        }

                        FixUpAllDiagrams.FixUp(diagram, modelClass.ModelRoot, modelClass);

                        break;

                    case ModelEnum modelEnum:
                        // user selected an enum. Find it in the current diagram, center it and make it visible
                        modelEnum.LocateInDiagram(true);

                        // then fix up the compartment since it might need it
                        ModelElement[] enumElements = { modelEnum };
                        CompartmentItemAddRule.UpdateCompartments(enumElements, typeof(EnumShape), "ValuesCompartment", false);
                        FixUpAllDiagrams.FixUp(diagram, modelEnum.ModelRoot, modelEnum);

                        break;
                    }

                    t.Commit();
                }
            }
        }
Ejemplo n.º 3
0
        public static bool SelectModelElement(this DiagramView diagramView, ModelElement modelElement, bool ensureVisible)
        {
            // Get the shape element that corresponds to the model element

            ShapeElement shapeElement = PresentationViewsSubject.GetPresentation(modelElement)
                                        .OfType <ShapeElement>()
                                        .FirstOrDefault(s => s.Diagram == diagramView.Diagram);

            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));

                if (ensureVisible)
                {
                    diagramView.Selection.EnsureVisible(DiagramClientView.EnsureVisiblePreferences.ScrollIntoViewCenter);
                    diagramView.ZoomAtViewCenter(1);
                }

                shapeElement.Invalidate();
                return(true);
            }

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

            if (modelElement is IModelElementInCompartment compartmentedModelElement)
            {
                // Get the parent
                IModelElementWithCompartments parentModelElement = compartmentedModelElement.ParentModelElement;

                if (parentModelElement != null)
                {
                    // Get the compartment that stores the model element
                    CompartmentShape parentShapeElement = PresentationViewsSubject.GetPresentation((ModelElement)parentModelElement)
                                                          .OfType <CompartmentShape>()
                                                          .FirstOrDefault(s => s.Diagram == diagramView.Diagram);

                    ElementListCompartment compartment = parentShapeElement?.GetCompartment(compartmentedModelElement.CompartmentName);

                    if (compartment != null)
                    {
                        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)));

                            if (ensureVisible)
                            {
                                diagramView.Selection.EnsureVisible(DiagramClientView.EnsureVisiblePreferences.ScrollIntoViewCenter);
                                diagramView.ZoomAtViewCenter(1);
                            }

                            compartment.Invalidate();
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }