src/Misc/EFTools/EntityDesignEntityDesigner/CustomCode/Diagram/EntityDesignerDiagram.cs
/// <summary>
///     Event Handler when selections in the Diagram are changed.
///     When the selection changes in diagram, we build the list of the diagram items that will be emphasized and we ask the diagram item's shapes to be redrawn.
///     When the shape is redrawn, each shape determines whether it is in the list; if yes, it will draw the emphasis shape.
/// </summary>
Event Handler when selections in the Diagram are changed.
When the selection changes in diagram, we build the list of the diagram items that will be emphasized and we ask the diagram item's shapes to be redrawn.
When the shape is redrawn, each shape determines whether it is in the list; if yes, it will draw the emphasis shape.
Event Handler when selections in the Diagram are changed . When the selection changes in diagram we build the list of the diagram items that will be emphasized and we ask the diagram item ' s shapes to be redrawn . When the shape is redrawn each shape determines whether it is in the list ; if yes it will draw the emphasis shape .
private void diagramView_OnShapeSelectionChanged(object sender, EventArgs e)
        {
            if (ActiveDiagramView != null
                && ActiveDiagramView.Selection != null)
            {
                var shapesToBeEmphasized = new DiagramItemCollection();
                var selectedShapes = ActiveDiagramView.Selection;
                foreach (DiagramItem diagramItem in selectedShapes)
                {
                    IContainRelatedElementsToEmphasizeWhenSelected relatedModelElementToEmphasize = null;
                    if (diagramItem.Shape != null
                        && diagramItem.Shape.ModelElement != null)
                    {
                        relatedModelElementToEmphasize = diagramItem.Shape.ModelElement as IContainRelatedElementsToEmphasizeWhenSelected;
                    }
                    else
                    {
                        relatedModelElementToEmphasize =
                            diagramItem.RepresentedElements.OfType<ModelElement>().FirstOrDefault() as
                            IContainRelatedElementsToEmphasizeWhenSelected;
                    }
                    if (relatedModelElementToEmphasize != null)
                    {
                        foreach (var emphasizedModelElement in relatedModelElementToEmphasize.RelatedElementsToEmphasizeOnSelected)
                        {
                            DiagramItem emphasizedDiagramItem = null;
                            var propertyBase = emphasizedModelElement as ViewModelPropertyBase;
                            if (propertyBase != null)
                            {
                                ViewModelEntityType et = null;
                                var navigationProperty = propertyBase as ViewModelNavigationProperty;
                                var property = propertyBase as ViewModelProperty;
                                if (navigationProperty != null)
                                {
                                    et = navigationProperty.EntityType;
                                }
                                else if (property != null)
                                {
                                    et = property.EntityType;
                                }
                                else
                                {
                                    Debug.Fail("Unexpected property type. Type name:" + propertyBase.GetType().Name);
                                }
                                Debug.Assert(et != null, "Could not get EntityType for property: " + propertyBase.Name);
                                if (et != null)
                                {
                                    Debug.Assert(
                                        PresentationViewsSubject.GetPresentation(et).Count() <= 1,
                                        "There should be at most 1 EntityTypeShape for EntityType:" + et.Name);
                                    var ets = PresentationViewsSubject.GetPresentation(et).FirstOrDefault() as EntityTypeShape;
                                    emphasizedDiagramItem = ets.GetDiagramItemForProperty(propertyBase);
                                }
                            }
                            else
                            {
                                var relatedPresentationElementToEmphasize =
                                    PresentationViewsSubject.GetPresentation(emphasizedModelElement).FirstOrDefault();
                                if (relatedPresentationElementToEmphasize != null)
                                {
                                    var relatedShapeElementToEmphasize = relatedPresentationElementToEmphasize as ShapeElement;
                                    if (relatedShapeElementToEmphasize != null)
                                    {
                                        emphasizedDiagramItem = new DiagramItem(relatedShapeElementToEmphasize);
                                    }
                                }
                            }
                            if (emphasizedDiagramItem != null
                                && shapesToBeEmphasized.Contains(emphasizedDiagramItem) == false
                                && selectedShapes.Contains(emphasizedDiagramItem) == false)
                            {
                                shapesToBeEmphasized.Add(emphasizedDiagramItem);
                            }
                        }
                    }
                }
                EmphasizedShapes.Set(shapesToBeEmphasized);
            }
        }
private void diagramView_OnShapeSelectionChanged ( object sender , EventArgs e ) { if ( ActiveDiagramView != null && ActiveDiagramView . Selection != null ) { var shapesToBeEmphasized = new DiagramItemCollection ( ) ; var selectedShapes = ActiveDiagramView . Selection ; foreach ( DiagramItem diagramItem in selectedShapes ) { IContainRelatedElementsToEmphasizeWhenSelected relatedModelElementToEmphasize = null ; if ( diagramItem . Shape != null && diagramItem . Shape . ModelElement != null ) { relatedModelElementToEmphasize = diagramItem . Shape . ModelElement as IContainRelatedElementsToEmphasizeWhenSelected ; } else { relatedModelElementToEmphasize = diagramItem . RepresentedElements . OfType < ModelElement > ( ) . FirstOrDefault ( ) as IContainRelatedElementsToEmphasizeWhenSelected ; } if ( relatedModelElementToEmphasize != null ) { foreach ( var emphasizedModelElement in relatedModelElementToEmphasize . RelatedElementsToEmphasizeOnSelected ) { DiagramItem emphasizedDiagramItem = null ; var propertyBase = emphasizedModelElement as ViewModelPropertyBase ; if ( propertyBase != null ) { ViewModelEntityType et = null ; var navigationProperty = propertyBase as ViewModelNavigationProperty ; var property = propertyBase as ViewModelProperty ; if ( navigationProperty != null ) { et = navigationProperty . EntityType ; } else if ( property != null ) { et = property . EntityType ; } else { Debug . Fail ( " " + propertyBase . GetType ( ) . Name ) ; } Debug . Assert ( et != null , " " + propertyBase . Name ) ; if ( et != null ) { Debug . Assert ( PresentationViewsSubject . GetPresentation ( et ) . Count ( ) <= 1 , " " + et . Name ) ; var ets = PresentationViewsSubject . GetPresentation ( et ) . FirstOrDefault ( ) as EntityTypeShape ; emphasizedDiagramItem = ets . GetDiagramItemForProperty ( propertyBase ) ; } } else { var relatedPresentationElementToEmphasize = PresentationViewsSubject . GetPresentation ( emphasizedModelElement ) . FirstOrDefault ( ) ; if ( relatedPresentationElementToEmphasize != null ) { var relatedShapeElementToEmphasize = relatedPresentationElementToEmphasize as ShapeElement ; if ( relatedShapeElementToEmphasize != null ) { emphasizedDiagramItem = new DiagramItem ( relatedShapeElementToEmphasize ) ; } } } if ( emphasizedDiagramItem != null && shapesToBeEmphasized . Contains ( emphasizedDiagramItem ) == false && selectedShapes . Contains ( emphasizedDiagramItem ) == false ) { shapesToBeEmphasized . Add ( emphasizedDiagramItem ) ; } } } } EmphasizedShapes . Set ( shapesToBeEmphasized ) ; } }
Event Handler when selections in the Diagram are changed.
Event Handler when selections in the Diagram are changed .
// For each DiagramItem in the Selection // - Get the corresponding model element. // - See if model element implement IContainRelatedElementsToEmphasizeWhenSelected. // - Get related model elements. // - For each related model elements, instantiates DiagramItem for the ModelElement. // DiagramItem.Shape.ModelElement is null when the selected item is not a ShapeElement(for example: Property/NavigationProperty). // Fortunately, we can retrieve the information from RepresentedElements property. // For each ModelElement get the corresponding diagram item. // if ModelElement is a Property, we could not just instantiate a DiagramItem and pass in the property's PresentationElement to the constructor // since property's PresentationElement is not a ShapeElement. // Only add if the DiagramItem hasn't been added to the list and the diagram item is not a member of selected diagram item list.
sender
object
e
EventArgs