///--------------------------------------------------------------------------------
        /// <summary>This method removes an entity from the view model.</summary>
        ///
        /// <param name="diagramEntity">The diagram entity to remove.</param>
        ///--------------------------------------------------------------------------------
        public void RemoveEntity(DiagramEntityViewModel diagramEntity, bool isDeleted = false)
        {
            // remove diagram entity
            DiagramEntities.Remove(diagramEntity);
            Items.Remove(diagramEntity);
            //diagramEntity.Dispose();
            ClearSelectedItems();
            if (isDeleted == false)
            {
                EntityViewModel entity = EntitiesRemoved.Find("EntityID", diagramEntity.EntityViewModel.EntityID);
                if (entity != null)
                {
                    EntitiesRemoved.Remove(entity);
                    Entities.Add(entity);
                    Entities.Sort("DisplayName", SortDirection.Ascending);
                }
            }
            IList <DiagramRelationshipViewModel> relationshipsToRemove = new List <DiagramRelationshipViewModel>();

            foreach (DiagramRelationshipViewModel diagramRelationship in DiagramRelationships)
            {
                if (diagramRelationship.SinkDiagramEntityViewModel.EntityID == diagramEntity.EntityID || diagramRelationship.SourceDiagramEntityViewModel.EntityID == diagramEntity.EntityID)
                {
                    relationshipsToRemove.Add(diagramRelationship);
                }
            }
            foreach (DiagramRelationshipViewModel diagramRelationship in relationshipsToRemove)
            {
                DiagramRelationships.Remove(diagramRelationship);
                Items.Remove(diagramRelationship);
            }
            Refresh(false);
        }
        ///--------------------------------------------------------------------------------
        /// <summary>This method creates an entity and adds to the view model.</summary>
        ///
        /// <param name="entity">The entity to add.</param>
        /// <param name="position">The desired position to place the entity.</param>
        ///--------------------------------------------------------------------------------
        public DiagramEntityViewModel CreateEntity(EntityViewModel entity, Point position)
        {
            // create diagram entity and add to solution diagram
            DiagramEntity dropDiagram = new DiagramEntity();

            dropDiagram.DiagramEntityID = Guid.NewGuid();
            DiagramEntityViewModel diagramEntity = new DiagramEntityViewModel(dropDiagram, entity, this);

            diagramEntity.X      = Math.Max(0, position.X);
            diagramEntity.Y      = Math.Max(0, position.Y);
            diagramEntity.Width  = Double.NaN;
            diagramEntity.Height = Double.NaN;
            DiagramEntities.Add(diagramEntity);
            Items.Add(diagramEntity);
            ClearSelectedItems();
            diagramEntity.IsSelected = true;
            diagramEntity.ZIndex     = ++ZIndex;

            // add to diagram entites to add list
            ItemsToAdd.Add(diagramEntity);
            Refresh(false);
            return(diagramEntity);
        }
        ///--------------------------------------------------------------------------------
        /// <summary>This method adds an entity to the view model.</summary>
        ///
        /// <param name="entity">The entity to add.</param>
        /// <param name="position">The desired position to place the entity.</param>
        ///--------------------------------------------------------------------------------
        public DiagramEntityViewModel AddEntity(EntityViewModel entity, Point position, bool isNewEntity = false)
        {
            DiagramEntity dropDiagram = new DiagramEntity();

            dropDiagram.DiagramID       = DiagramID;
            dropDiagram.DiagramEntityID = Guid.NewGuid();
            EntityViewModel        entityView    = entity;
            DiagramEntityViewModel diagramEntity = new DiagramEntityViewModel(dropDiagram, entityView, this);

            diagramEntity.X        = Math.Max(0, position.X);
            diagramEntity.Y        = Math.Max(0, position.Y);
            diagramEntity.Width    = Double.NaN;
            diagramEntity.Height   = Double.NaN;
            diagramEntity.Updated += new EventHandler(Children_Updated);
            diagramEntity.DiagramEntity.Entity = diagramEntity.EntityViewModel.Entity;
            diagramEntity.ResetModified(false);
            diagramEntity.EditDiagramEntity.ResetModified(false);
            if (isNewEntity == true)
            {
                AddDiagramEntity(diagramEntity);
            }
            else
            {
                DiagramEntities.Add(diagramEntity);
                Items.Add(diagramEntity);
            }
            ClearSelectedItems();
            diagramEntity.IsSelected = true;
            diagramEntity.ZIndex     = ++ZIndex;
            Entities.Remove(entity);
            EntitiesRemoved.Add(entity);
            diagramEntity.PositionChanged += new EventHandler(diagramEntity_PositionChanged);

            AddRelationships(diagramEntity);

            return(diagramEntity);
        }
        ///--------------------------------------------------------------------------------
        /// <summary>This method loads a solution into the view model.</summary>
        ///
        /// <param name="solution">The solution to load.</param>
        ///--------------------------------------------------------------------------------
        public void LoadDiagram(Diagram solutionDiagram)
        {
            try
            {
                // attach the solution diagram
                Diagram = solutionDiagram;
                ItemID  = Diagram.DiagramID;

                // attach diagram entities
                DiagramRelationships.Clear();
                DiagramEntities.Clear();
                if (Entities.Count >= 0)
                {
                    IList <DiagramEntity> missingEntities = new List <DiagramEntity>();
                    foreach (DiagramEntity entity in solutionDiagram.DiagramEntityList)
                    {
                        if (entity.Entity == null)
                        {
                            entity.Entity = Solution.EntityList.FindByID(entity.EntityID);
                        }
                        if (entity.Entity != null)
                        {
                            EntityViewModel entityView = Entities.Find("EntityID", entity.EntityID);
                            if (entityView == null)
                            {
                                // entity may be removed from Entities already
                                Entity foundEntity = Solution.EntityList.FindByID(entity.EntityID);
                                if (foundEntity != null)
                                {
                                    entityView = new EntityViewModel(foundEntity, Solution, true);
                                }
                            }
                            if (entityView != null)
                            {
                                Point position = new Point();
                                position.X = entity.PositionX;
                                position.Y = entity.PositionY;
                                AddEntity(entityView, position, false);
                            }
                            else
                            {
                                missingEntities.Add(entity);
                            }
                        }
                        else
                        {
                            missingEntities.Add(entity);
                        }
                    }

                    // remove missing entities
                    if (missingEntities.Count > 0)
                    {
                        foreach (DiagramEntity entity in missingEntities)
                        {
                            solutionDiagram.DiagramEntityList.Remove(entity);
                            Solution.ResetModified(true);
                        }
                        ShowIssue(DisplayValues.Issue_MissingDiagramEntities);
                    }
                }

                Items.Clear();
                foreach (var item in DiagramEntities)
                {
                    Items.Add(item);
                }
                foreach (var item in DiagramRelationships)
                {
                    Items.Add(item);
                }
                Refresh(false);
                ResetModified(false);
            }
            catch (ApplicationException ex)
            {
                ShowException(ex);
            }
            catch (Exception ex)
            {
                ShowException(ex);
            }
        }