/// <summary>
        /// SelectRelationshipCommand executed;
        /// </summary>
        public void SelectRelationshipCommand_Executed()
        {
            if (this.SelectedItems.Count == 1)
            {
                SerializedDomainClassViewModel classViewModel = this.SelectedItems[0] as SerializedDomainClassViewModel;
                if (classViewModel != null)
                {
                    if (classViewModel.Parent != null)
                    {
                        SelectedItemsCollection col = new SelectedItemsCollection();
                        col.Add(classViewModel.Parent.GetHostedElement());

                        EventManager.GetEvent <SelectionChangedEvent>().Publish(new SelectionChangedEventArgs(this, col));
                    }
                }

                SerializedReferenceRelationshipViewModel refRelViewModel = this.SelectedItems[0] as SerializedReferenceRelationshipViewModel;
                if (refRelViewModel != null)
                {
                    if (!refRelViewModel.IsInFullSerialization)
                    {
                        SelectedItemsCollection col = new SelectedItemsCollection();
                        col.Add(refRelViewModel.SerializationElement);
                        EventManager.GetEvent <SelectionChangedEvent>().Publish(new SelectionChangedEventArgs(this, col));
                    }
                }
            }
        }
        /// <summary>
        /// Updates operation collection.
        /// </summary>
        private void UpdateOperations()
        {
            this.contextMenuOperations = new Collection <MenuItemViewModel>();

            if (this.SelectedItems.Count == 1)
            {
                SerializedDomainClassViewModel classViewModel = this.SelectedItems[0] as SerializedDomainClassViewModel;
                if (classViewModel != null)
                {
                    if (classViewModel.Parent != null)
                    {
                        MenuItemViewModel mv = new MenuItemViewModel(this.ViewModelStore, "Select Relationship");
                        mv.Command = SelectRelationshipCommand;
                        this.contextMenuOperations.Add(mv);
                    }
                }

                SerializedReferenceRelationshipViewModel refRelViewModel = this.SelectedItems[0] as SerializedReferenceRelationshipViewModel;
                if (refRelViewModel != null)
                {
                    if (!refRelViewModel.IsInFullSerialization)
                    {
                        MenuItemViewModel mv = new MenuItemViewModel(this.ViewModelStore, "Select Relationship");
                        mv.Command = SelectRelationshipCommand;
                        this.contextMenuOperations.Add(mv);
                    }
                }
            }

            if (this.SelectedItems.Count > 0)
            {
                AddSeparator();

                MenuItemViewModel mvMoveUp = new MenuItemViewModel(this.ViewModelStore, "Move up", "pack://application:,,,/Tum.PDE.LanguageDSL.Visualization;component/Resources/Images/Up-16.png");
                mvMoveUp.Command = MoveUpCommand;
                if (!MoveUpCommand.CanExecute())
                {
                    mvMoveUp.IsEnabled = false;
                }
                this.contextMenuOperations.Add(mvMoveUp);

                MenuItemViewModel mvMoveDown = new MenuItemViewModel(this.ViewModelStore, "Move down", "pack://application:,,,/Tum.PDE.LanguageDSL.Visualization;component/Resources/Images/Down-16.png");
                mvMoveDown.Command = MoveDownCommand;
                if (!MoveDownCommand.CanExecute())
                {
                    mvMoveDown.IsEnabled = false;
                }
                this.contextMenuOperations.Add(mvMoveDown);
            }

            OnPropertyChanged("Operations");
        }
Example #3
0
        /// <summary>
        /// Adds a new element view model for the given element.
        /// </summary>
        /// <param name="element">Element.</param>
        public void AddChild(SerializationElement element)
        {
            if (!HasLoadedChildren)
            {
                if (!HasDummyChild)
                {
                    this.childrenVMs.Add(DummyChild);
                }

                return;
            }

            // verify that node hasnt been added yet
            foreach (SerializationElementViewModel viewModel in this.childrenVMs)
            {
                if (viewModel.SerializationElement.Id == element.Id)
                {
                    return;
                }
            }

            if (element is SerializedDomainClass)
            {
                SerializedDomainClassViewModel vm = new SerializedDomainClassViewModel(this.ViewModelStore, element as SerializedDomainClass, this);
                this.childrenVMs.Add(vm);
            }
            else if (element is SerializedEmbeddingRelationship)
            {
                SerializedEmbeddingRelationshipViewModel vm = new SerializedEmbeddingRelationshipViewModel(this.ViewModelStore, element as SerializedEmbeddingRelationship, this);
                this.childrenVMs.Add(vm);
            }
            else if (element is SerializedReferenceRelationship)
            {
                SerializedReferenceRelationshipViewModel vm = new SerializedReferenceRelationshipViewModel(this.ViewModelStore, element as SerializedReferenceRelationship, this);
                this.childrenVMs.Add(vm);
            }
            else if (element is SerializedDomainProperty)
            {
                SerializedDomainPropertyViewModel vm = new SerializedDomainPropertyViewModel(this.ViewModelStore, element as SerializedDomainProperty, this);
                this.childrenVMs.Add(vm);
            }

            OnPropertyChanged("HasChildren");
        }
        /// <summary>
        /// Adds a new element view model for the given element.
        /// </summary>
        /// <param name="element">Element.</param>
        public void AddChild(SerializationElement element)
        {
            if (!HasLoadedChildren)
            {
                if (!HasDummyChild)
                    this.childrenVMs.Add(DummyChild);

                return;
            }

            // verify that node hasnt been added yet
            foreach (SerializationElementViewModel viewModel in this.childrenVMs)
                if (viewModel.SerializationElement.Id == element.Id)
                    return;

            if (element is SerializedDomainClass)
            {
                SerializedDomainClassViewModel vm = new SerializedDomainClassViewModel(this.ViewModelStore, element as SerializedDomainClass, this);
                this.childrenVMs.Add(vm);
            }
            else if (element is SerializedEmbeddingRelationship)
            {
                SerializedEmbeddingRelationshipViewModel vm = new SerializedEmbeddingRelationshipViewModel(this.ViewModelStore, element as SerializedEmbeddingRelationship, this);
                this.childrenVMs.Add(vm);
            }
            else if (element is SerializedReferenceRelationship)
            {
                SerializedReferenceRelationshipViewModel vm = new SerializedReferenceRelationshipViewModel(this.ViewModelStore, element as SerializedReferenceRelationship, this);
                this.childrenVMs.Add(vm);
            }
            else if (element is SerializedDomainProperty)
            {
                SerializedDomainPropertyViewModel vm = new SerializedDomainPropertyViewModel(this.ViewModelStore, element as SerializedDomainProperty, this);
                this.childrenVMs.Add(vm);
            }

            OnPropertyChanged("HasChildren");
        }
        /// <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);
        }