/// <summary>
        /// Clean up.
        /// </summary>
        protected override void OnDispose()
        {
            if (this.ShapeElement != null)
            {
                Unsubscribe();
            }

            for (int i = this.nestedChildItems.Count - 1; i >= 0; i--)
            {
                BaseDiagramItemElementViewModel vm = nestedChildItems[i];
                this.RemoveNestedChild(vm);
                vm.Dispose();
            }


            for (int i = this.relativeChildItems.Count - 1; i >= 0; i--)
            {
                BaseDiagramItemElementViewModel vm = relativeChildItems[i];
                this.RemoveRelativeChild(vm);
                vm.Dispose();
            }

            this.ShapeElement = null;
            this.parentItem   = null;

            base.OnDispose();
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="source">View model holding the source element.</param>
        /// <param name="target">View model holding the target element.</param>
        public ViewModelRelationshipCreationInfo(BaseDiagramItemElementViewModel source, BaseDiagramItemElementViewModel target)
        {
            this.Source = source;
            this.Target = target;

            this.ProposedSourcePoint = null;
            this.ProposedTargetPoint = null;
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="source">View model holding the source element.</param>
        /// <param name="target">View model holding the target element.</param>
        public ViewModelRelationshipCreationInfo(BaseDiagramItemElementViewModel source, BaseDiagramItemElementViewModel target)
        {
            this.Source = source;
            this.Target = target;

            this.ProposedSourcePoint = null;
            this.ProposedTargetPoint = null;
        }
        /// <summary>
        /// Assigns a parent item to the current item.
        /// </summary>
        /// <param name="parent">Parent item to be assigned.</param>
        /// <param name="bNestedChildItem">True if the child item is a nested item. False for relative item.</param>
        public void SetParent(BaseDiagramItemElementViewModel parent, bool bNestedChildItem)
        {
            this.parentItem = parent;

            if (bNestedChildItem && !parent.NestedChildren.Contains(this))
            {
                parent.AddNestedChild(this);
            }
            else if (!bNestedChildItem && !parent.RelativeChildren.Contains(this))
            {
                parent.AddRelativeChild(this);
            }
        }
        /// <summary>
        /// Removes this element from its parents collection.
        /// </summary>
        /// <param name="bNestedChildItem">True if the child item is a nested item. False for relative item.</param>
        public void RemoveParent(bool bNestedChildItem)
        {
            if (this.parentItem != null)
            {
                if (bNestedChildItem && this.parentItem.NestedChildren.Contains(this))
                {
                    this.parentItem.RemoveNestedChild(this);
                }
                else if (!bNestedChildItem && this.parentItem.RelativeChildren.Contains(this))
                {
                    this.parentItem.RemoveRelativeChild(this);
                }
            }

            this.parentItem = null;
        }
        protected BaseDiagramItemElementViewModel(ViewModelStore viewModelStore, DiagramSurfaceViewModel diagram, NodeShape shapeElement)
            : base(viewModelStore, diagram, shapeElement)
        {
            this.itemLocation = shapeElement.Location;
            this.itemSize     = shapeElement.Size;

            this.parentItem = null;

            this.nestedChildItems   = new ObservableCollection <BaseDiagramItemElementViewModel>();
            this.nestedChildItemsRO = new ReadOnlyObservableCollection <BaseDiagramItemElementViewModel>(nestedChildItems);

            this.relativeChildItems   = new ObservableCollection <BaseDiagramItemElementViewModel>();
            this.relativeChildItemsRO = new ReadOnlyObservableCollection <BaseDiagramItemElementViewModel>(relativeChildItems);

            Subscribe();
        }
        protected BaseDiagramItemElementViewModel(ViewModelStore viewModelStore, DiagramSurfaceViewModel diagram, NodeShape shapeElement)
            : base(viewModelStore, diagram, shapeElement)
        {
            this.itemLocation = shapeElement.Location;
            this.itemSize = shapeElement.Size;
            
            this.parentItem = null;

            this.nestedChildItems = new ObservableCollection<BaseDiagramItemElementViewModel>();
            this.nestedChildItemsRO = new ReadOnlyObservableCollection<BaseDiagramItemElementViewModel>(nestedChildItems);

            this.relativeChildItems = new ObservableCollection<BaseDiagramItemElementViewModel>();
            this.relativeChildItemsRO = new ReadOnlyObservableCollection<BaseDiagramItemElementViewModel>(relativeChildItems);

            Subscribe();
        }
        /// <summary>
        /// Called whenever a relationship of type NodeShapeReferencesRelativeChildren is removed and
        /// the element hosted by this model is the source.
        /// </summary>
        /// <param name="args"></param>
        protected virtual void OnRelativeChildShapeElementRemoved(ElementDeletedEventArgs args)
        {
            NodeShapeReferencesRelativeChildren con = args.ModelElement as NodeShapeReferencesRelativeChildren;
            NodeShape nodeShape = con.ChildShape;

            if (nodeShape != null)
            {
                for (int i = this.relativeChildItems.Count - 1; i >= 0; i--)
                {
                    if (this.relativeChildItems[i].ShapeElement.Id == nodeShape.Id)
                    {
                        BaseDiagramItemElementViewModel vm = this.relativeChildItems[i];
                        this.RemoveRelativeChild(vm);
                        vm.Dispose();
                    }
                }
            }
        }
        private void RemoveChild(BaseDiagramItemElementViewModel item, bool bNested)
        {
            if (bNested && this.nestedChildItems.Contains(item))
            {
                this.nestedChildItems.Remove(item);
            }
            else if (!bNested && this.relativeChildItems.Contains(item))
            {
                this.relativeChildItems.Remove(item);
            }

            if (item.Parent != null)
            {
                item.RemoveParent(bNested);
            }

            if (this.Diagram != null)
            {
                this.Diagram.RemoveElement(item);
            }
        }
        private void AddChild(BaseDiagramItemElementViewModel item, bool bNested)
        {
            if (bNested)
            {
                this.nestedChildItems.Add(item);
            }
            else
            {
                this.relativeChildItems.Add(item);
            }

            if (item.Parent != this)
            {
                item.SetParent(this, bNested);
            }

            if (this.Diagram != null)
            {
                this.Diagram.AddElement(item);
            }
        }
 /// <summary>
 /// Removes the given item from the relative children collection.
 /// </summary>
 /// <param name="item">Child item to be removed.</param>
 public void RemoveRelativeChild(BaseDiagramItemElementViewModel item)
 {
     RemoveChild(item, false);
 }
        private void AddChild(BaseDiagramItemElementViewModel item, bool bNested)
        {
            if (bNested)
                this.nestedChildItems.Add(item);
            else
                this.relativeChildItems.Add(item);

            if (item.Parent != this)
                item.SetParent(this, bNested);

            if (this.Diagram != null)
                this.Diagram.AddElement(item);
        }
        /// <summary>
        /// Clean up.
        /// </summary>
        protected override void OnDispose()
        {
            if (this.ShapeElement != null)
                Unsubscribe();

            for (int i = this.nestedChildItems.Count - 1; i >= 0; i--)
            {
                BaseDiagramItemElementViewModel vm = nestedChildItems[i];
                this.RemoveNestedChild(vm);
                vm.Dispose();
            }


            for (int i = this.relativeChildItems.Count - 1; i >= 0; i--)
            {
                BaseDiagramItemElementViewModel vm = relativeChildItems[i];
                this.RemoveRelativeChild(vm);
                vm.Dispose();
            }

            this.ShapeElement = null;
            this.parentItem = null;

            base.OnDispose();
        }
        /// <summary>
        /// Removes this element from its parents collection.
        /// </summary>
        /// <param name="bNestedChildItem">True if the child item is a nested item. False for relative item.</param>
        public void RemoveParent(bool bNestedChildItem)
        {
            if (this.parentItem != null)
            {
                if (bNestedChildItem && this.parentItem.NestedChildren.Contains(this))
                    this.parentItem.RemoveNestedChild(this);
                else if (!bNestedChildItem && this.parentItem.RelativeChildren.Contains(this))
                    this.parentItem.RemoveRelativeChild(this);
            }

            this.parentItem = null;
        }
 /// <summary>
 /// Assigns a parent item to the current item.
 /// </summary>
 /// <param name="parent">Parent item to be assigned.</param>
 /// <param name="bNestedChildItem">True if the child item is a nested item. False for relative item.</param>
 public void SetParent(BaseDiagramItemElementViewModel parent, bool bNestedChildItem)
 {
     this.parentItem = parent;
     
     if (bNestedChildItem && !parent.NestedChildren.Contains(this))
     {
         parent.AddNestedChild(this);
     }
     else if (!bNestedChildItem && !parent.RelativeChildren.Contains(this))
         parent.AddRelativeChild(this);
 }
        private void RemoveChild(BaseDiagramItemElementViewModel item, bool bNested)
        {
            if (bNested && this.nestedChildItems.Contains(item))
                this.nestedChildItems.Remove(item);
            else if (!bNested && this.relativeChildItems.Contains(item))
                this.relativeChildItems.Remove(item);

            if (item.Parent != null)
                item.RemoveParent(bNested);

            if( this.Diagram != null )
                this.Diagram.RemoveElement(item);
        }
 /// <summary>
 /// Removes the given item from the nested children collection.
 /// </summary>
 /// <param name="item">Child item to be removed.</param>
 public void RemoveNestedChild(BaseDiagramItemElementViewModel item)
 {
     RemoveChild(item, true);
 }
 /// <summary>
 /// Adds a nested child item to the current item.
 /// </summary>
 /// <param name="item">Child item to be added.</param>
 public void AddNestedChild(BaseDiagramItemElementViewModel item)
 {
     AddChild(item, true);
 }
 /// <summary>
 /// Removes the given item from the relative children collection.
 /// </summary>
 /// <param name="item">Child item to be removed.</param>
 public void RemoveRelativeChild(BaseDiagramItemElementViewModel item)
 {
     RemoveChild(item, false);
 }
 /// <summary>
 /// Removes the given item from the nested children collection.
 /// </summary>
 /// <param name="item">Child item to be removed.</param>
 public void RemoveNestedChild(BaseDiagramItemElementViewModel item)
 {
     RemoveChild(item, true);
 }
 /// <summary>
 /// Adds a nested child item to the current item.
 /// </summary>
 /// <param name="item">Child item to be added.</param>
 public void AddNestedChild(BaseDiagramItemElementViewModel item)
 {
     AddChild(item, true);
 }