private int GetAttributeIndex(SerializationClassViewModel viewModel, SerializationAttributeElementViewModel attrVM)
        {
            // get index of attrVM in the collection of attributes of viewModel
            for (int i = 0; i < viewModel.Attributes.Count; i++)
            {
                if (viewModel.Attributes[i] == attrVM)
                {
                    return(i);
                }
            }

            return(-1);
        }
        private int GetAttributeIndex(SerializationClassViewModel viewModel, SerializationAttributeElementViewModel attrVM)
        {
            // get index of attrVM in the collection of attributes of viewModel
            for (int i = 0; i < viewModel.Attributes.Count; i++)
                if (viewModel.Attributes[i] == attrVM)
                {
                    return i;
                }

            return -1;
        }
        /// <summary>
        /// Moves the selected elements in the serialization model up or down.
        /// </summary>
        /// <param name="bUp"></param>
        private void MoveElementInSerilizationTree(bool bUp)
        {
            Dictionary <SerializationClassViewModel, SortedDictionary <int, SerializationElementViewModel> >          dictionaryChildren   = new Dictionary <SerializationClassViewModel, SortedDictionary <int, SerializationElementViewModel> >();
            Dictionary <SerializationClassViewModel, SortedDictionary <int, SerializationAttributeElementViewModel> > dictionaryAttributes = new Dictionary <SerializationClassViewModel, SortedDictionary <int, SerializationAttributeElementViewModel> >();

            // gather information about selected elements, that are to be moved
            foreach (SerializationElementViewModel vm in this.SelectedItems)
            {
                if (vm is SerializationAttributeElementViewModel)
                {
                    SerializationAttributeElementViewModel attrVM = vm as SerializationAttributeElementViewModel;
                    if (attrVM.Parent.Children.Contains(attrVM))
                    {
                        if (!dictionaryChildren.ContainsKey(attrVM.Parent))
                        {
                            dictionaryChildren.Add(attrVM.Parent, new SortedDictionary <int, SerializationElementViewModel>());
                        }

                        int index = GetChildIndex(attrVM.Parent, attrVM);
                        if (index != -1)
                        {
                            dictionaryChildren[attrVM.Parent].Add(index, attrVM);
                        }
                    }
                    else if (attrVM.Parent.Attributes.Contains(attrVM))
                    {
                        if (!dictionaryAttributes.ContainsKey(attrVM.Parent))
                        {
                            dictionaryAttributes.Add(attrVM.Parent, new SortedDictionary <int, SerializationAttributeElementViewModel>());
                        }

                        int index = GetAttributeIndex(attrVM.Parent, attrVM);
                        if (index != -1)
                        {
                            dictionaryAttributes[attrVM.Parent].Add(index, attrVM);
                        }
                    }
                }
                else if (vm is SerializedDomainClassViewModel)
                {
                    SerializedDomainClassViewModel dcVM = vm as SerializedDomainClassViewModel;
                    if (dcVM.Parent == null)
                    {
                        continue;
                    }

                    if (dcVM.Parent.SerializationElement is SerializedRelationship)
                    {
                        if ((dcVM.Parent.SerializationElement as SerializedRelationship).IsInFullSerialization)
                        {
                            continue;
                        }
                    }

                    if (dcVM.Parent.Parent == null)
                    {
                        continue;
                    }

                    if (!dictionaryChildren.ContainsKey(dcVM.Parent.Parent))
                    {
                        dictionaryChildren.Add(dcVM.Parent.Parent, new SortedDictionary <int, SerializationElementViewModel>());
                    }

                    int index = GetChildIndex(dcVM.Parent.Parent, dcVM.Parent);
                    if (index != -1)
                    {
                        dictionaryChildren[dcVM.Parent.Parent].Add(index, dcVM.Parent);
                    }
                }
                else if (vm is SerializedEmbeddingRelationshipViewModel)
                {
                    SerializedEmbeddingRelationshipViewModel dcVM = vm as SerializedEmbeddingRelationshipViewModel;
                    if (dcVM.Parent == null)
                    {
                        continue;
                    }

                    if (!dictionaryChildren.ContainsKey(dcVM.Parent))
                    {
                        dictionaryChildren.Add(dcVM.Parent, new SortedDictionary <int, SerializationElementViewModel>());
                    }

                    int index = GetChildIndex(dcVM.Parent, dcVM);
                    if (index != -1)
                    {
                        dictionaryChildren[dcVM.Parent].Add(index, dcVM);
                    }
                }
                else if (vm is SerializedReferenceRelationshipViewModel)
                {
                    SerializedReferenceRelationshipViewModel dcVM = vm as SerializedReferenceRelationshipViewModel;
                    if (dcVM.Parent == null)
                    {
                        continue;
                    }

                    if (!dictionaryChildren.ContainsKey(dcVM.Parent))
                    {
                        dictionaryChildren.Add(dcVM.Parent, new SortedDictionary <int, SerializationElementViewModel>());
                    }

                    int index = GetChildIndex(dcVM.Parent, dcVM);
                    if (index != -1)
                    {
                        dictionaryChildren[dcVM.Parent].Add(index, dcVM);
                    }
                }
            }

            // move elements
            using (Transaction transaction = this.Store.TransactionManager.BeginTransaction("Move serialization elements up/down"))
            {
                foreach (SerializationClassViewModel key in dictionaryChildren.Keys)
                {
                    SortedDictionary <int, SerializationElementViewModel> sortedDictionary = dictionaryChildren[key];
                    if (bUp)
                    {
                        for (int i = 0; i < sortedDictionary.Keys.Count; ++i)
                        {
                            key.MoveChildren(sortedDictionary.Keys.ElementAt(i), sortedDictionary.Keys.ElementAt(i) - 1);
                            //key.Children.Move(sortedDictionary.Keys.ElementAt(i), sortedDictionary.Keys.ElementAt(i) - 1);
                        }
                    }
                    else
                    {
                        for (int i = sortedDictionary.Keys.Count - 1; i >= 0; --i)
                        {
                            key.MoveChildren(sortedDictionary.Keys.ElementAt(i), sortedDictionary.Keys.ElementAt(i) + 1);
                            //key.Children.Move(sortedDictionary.Keys.ElementAt(i), sortedDictionary.Keys.ElementAt(i) + 1);
                        }
                    }
                }
                foreach (SerializationClassViewModel key in dictionaryAttributes.Keys)
                {
                    SortedDictionary <int, SerializationAttributeElementViewModel> sortedDictionary = dictionaryAttributes[key];
                    if (bUp)
                    {
                        for (int i = 0; i < sortedDictionary.Keys.Count; ++i)
                        {
                            key.MoveAttributes(sortedDictionary.Keys.ElementAt(i), sortedDictionary.Keys.ElementAt(i) - 1);
                            //key.Attributes.Move(sortedDictionary.Keys.ElementAt(i), sortedDictionary.Keys.ElementAt(i) - 1);
                        }
                    }
                    else
                    {
                        for (int i = sortedDictionary.Keys.Count - 1; i >= 0; --i)
                        {
                            key.MoveAttributes(sortedDictionary.Keys.ElementAt(i), sortedDictionary.Keys.ElementAt(i) + 1);
                            //key.Attributes.Move(sortedDictionary.Keys.ElementAt(i), sortedDictionary.Keys.ElementAt(i) + 1);
                        }
                    }
                }

                transaction.Commit();
            }
        }
        /// <summary>
        /// Determines whether the selected elements in the serialization model can be moved up or down.
        /// </summary>
        /// <param name="bUp"></param>
        /// <returns></returns>
        private bool CanMoveElementInSerializationTree(bool bUp)
        {
            foreach (SerializationElementViewModel vm in this.SelectedItems)
            {
                if (vm is SerializedDomainModelViewModel)
                {
                    return(false);
                }
                else if (vm is SerializationAttributeElementViewModel)
                {
                    SerializationAttributeElementViewModel attrVM = vm as SerializationAttributeElementViewModel;
                    if (ImmutabilityExtensionMethods.GetLocks(attrVM.Parent.Element) == Locks.All)
                    {
                        return(false);
                    }

                    if (attrVM.Parent.Children.Contains(attrVM))
                    {
                        if (bUp)
                        {
                            if (attrVM.Parent.Children.Count == 0)
                            {
                                return(false);
                            }

                            if (attrVM.Parent.Children[0] == attrVM)
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            if (attrVM.Parent.Children.Count == 0)
                            {
                                return(false);
                            }

                            if (attrVM.Parent.Children[attrVM.Parent.Children.Count - 1] == attrVM)
                            {
                                return(false);
                            }
                        }
                    }
                    else if (attrVM.Parent.Attributes.Contains(attrVM))
                    {
                        if (bUp)
                        {
                            if (attrVM.Parent.Attributes.Count == 0)
                            {
                                return(false);
                            }

                            if (attrVM.Parent.Attributes[0] == attrVM)
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            if (attrVM.Parent.Attributes.Count == 0)
                            {
                                return(false);
                            }

                            if (attrVM.Parent.Attributes[attrVM.Parent.Attributes.Count - 1] == attrVM)
                            {
                                return(false);
                            }
                        }
                    }
                }
                else if (vm is SerializedDomainClassViewModel)
                {
                    SerializedDomainClassViewModel dcVM = vm as SerializedDomainClassViewModel;
                    if (dcVM.Parent == null)
                    {
                        return(false);
                    }

                    if (ImmutabilityExtensionMethods.GetLocks(dcVM.Parent.Element) == Locks.All)
                    {
                        return(false);
                    }

                    if (dcVM.Parent.SerializationElement is SerializedRelationship)
                    {
                        if ((dcVM.Parent.SerializationElement as SerializedRelationship).IsInFullSerialization)
                        {
                            return(false);
                        }
                    }

                    if (dcVM.Parent.Parent == null)
                    {
                        return(false);
                    }

                    if (bUp)
                    {
                        if (dcVM.Parent.Parent.Children.Count == 0)
                        {
                            return(false);
                        }

                        if (dcVM.Parent.Parent.Children[0] == dcVM.Parent)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        if (dcVM.Parent.Parent.Children.Count == 0)
                        {
                            return(false);
                        }

                        if (dcVM.Parent.Parent.Children[dcVM.Parent.Parent.Children.Count - 1] == dcVM.Parent)
                        {
                            return(false);
                        }
                    }
                }
                else if (vm is SerializedDomainRoleViewModel)
                {
                    return(false);
                }
                else if (vm is SerializedEmbeddingRelationshipViewModel)
                {
                    SerializedEmbeddingRelationshipViewModel dcVM = vm as SerializedEmbeddingRelationshipViewModel;
                    if (dcVM.Parent == null)
                    {
                        return(false);
                    }


                    if (ImmutabilityExtensionMethods.GetLocks(dcVM.Parent.Element) == Locks.All)
                    {
                        return(false);
                    }

                    if (bUp)
                    {
                        if (dcVM.Parent.Children.Count == 0)
                        {
                            return(false);
                        }

                        if (dcVM.Parent.Children[0] == dcVM)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        if (dcVM.Parent.Children.Count == 0)
                        {
                            return(false);
                        }

                        if (dcVM.Parent.Children[dcVM.Parent.Children.Count - 1] == dcVM)
                        {
                            return(false);
                        }
                    }
                }
                else if (vm is SerializedReferenceRelationshipViewModel)
                {
                    SerializedReferenceRelationshipViewModel dcVM = vm as SerializedReferenceRelationshipViewModel;
                    if (dcVM.Parent == null)
                    {
                        return(false);
                    }

                    if (ImmutabilityExtensionMethods.GetLocks(dcVM.Parent.Element) == Locks.All)
                    {
                        return(false);
                    }

                    if (bUp)
                    {
                        if (dcVM.Parent.Children.Count == 0)
                        {
                            return(false);
                        }

                        if (dcVM.Parent.Children[0] == dcVM)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        if (dcVM.Parent.Children.Count == 0)
                        {
                            return(false);
                        }

                        if (dcVM.Parent.Children[dcVM.Parent.Children.Count - 1] == dcVM)
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }