public MainModelTreeViewModel(ViewModelStore viewModelStore)
            : base(viewModelStore)
        {
            contextMenu = defaultContextMenu;

            this.EventManager.GetEvent<SelectionChangedEvent>().Subscribe(new Action<SelectionChangedEventArgs>(ReactOnViewSelection));
        }
        /// <summary>
        /// Clear resources.
        /// </summary>
        protected override void OnDispose()
        {
            if (contextMenu != null)
                contextMenu.Dispose();
            contextMenu = null;

            base.OnDispose();

            this.IsSelected = false;

            if (this.Parent != null)
                if( Parent.Children != null )
                    this.Parent.DeleteElementFromCollection(this);

            if (this.childrenCollectionStorage != null)
            {
                for (var i = childrenCollectionStorage.Count - 1; i >= 0; i--)
                {
                    childrenCollectionStorage[i].Dispose();
                }
            }

            this.parent = null;
            this.childrenCollectionStorage = null;
            this.childrenCollectionStorageReadOnly = null;
            //this.localContextMenuProvider = null;
            //this.localSortingProvider = null;
        }
        /// <summary>
        /// Creates context menus to add child items as children of the specified menu item vm.
        /// </summary>
        /// <param name="menuItemAdd">Menu item vm to hold newly created context menu items.</param>
        protected virtual void CreateAddElementsContextMenu(MenuItemViewModel menuItemAdd)
        {

        }
        /// <summary>
        /// Constuctor.
        /// </summary>
        /// <param name="viewModelStore">The store this view model belongs to.</param>
        /// <param name="bSubscribeToEvents">True, if changes in the model need to be reflected on this view model.</param>
        public DependenciesViewModel(ViewModelStore viewModelStore, bool bSubscribeToEvents)
            : base(viewModelStore)
        {
            this.allDependencies = new ObservableCollection<DependencyItemViewModel>();
            this.displayedDependencies = new ObservableCollection<DependencyItemViewModel>();
            this.displayedDependenciesRO = new ReadOnlyObservableCollection<DependencyItemViewModel>(displayedDependencies);

            this.sortOrder = DependenciesSortOrder.DependencyCategory;
            this.isAscending = true;

            this.doSubscribeToEvents = bSubscribeToEvents;
            this.hiddenCategories = new List<DependencyItemCategory>();
            this.hiddenCategories.Add(DependencyItemCategory.Embedded);
            this.hiddenCategories.Add(DependencyItemCategory.Embedding);

            toggleEmbeddedCategoryCommand = new DelegateCommand(ToggleEmbeddedCategoryCommand_Executed);
            toggleEmbeddingCategoryCommand = new DelegateCommand(ToggleEmbeddingCategoryCommand_Executed);
            toggleReferencedCategoryCommand = new DelegateCommand(ToggleReferencedCategoryCommand_Executed);
            toggleReferencingCategoryCommand = new DelegateCommand(ToggleReferencingCategoryCommand_Executed);

            sortByNumberCommand = new DelegateCommand(SortByNumberCommand_Executed);
            sortByCategoryCommand = new DelegateCommand(SortByCategoryCommand_Executed);
            sortBySourceModelElementCommand = new DelegateCommand(SortBySourceModelElementCommand_Executed);
            sortByTargetModelElementCommand = new DelegateCommand(SortByTargetModelElementCommand_Executed);
            sortByLinkElementCommand = new DelegateCommand(SortByLinkElementCommand_Executed);

            navigateToSourceCommand = new DelegateCommand(NavigateToSourceCommand_Executed);
            navigateToTargetCommand = new DelegateCommand(NavigateToTargetCommand_Executed);

            // create menu
            menuOptions = new ObservableCollection<MenuItemViewModel>();

            navigateToSourceMenuItem = new MenuItemViewModel(viewModelStore, "Navigate to source element");
            navigateToSourceMenuItem.Command = this.NavigateToSourceCommand;
            menuOptions.Add(navigateToSourceMenuItem);

            navigateToTargetMenuItem = new MenuItemViewModel(viewModelStore, "Navigate to target element");
            navigateToTargetMenuItem.Command = this.NavigateToTargetCommand;
            menuOptions.Add(navigateToTargetMenuItem);
        }
        /// <summary>
        /// Reset selection.
        /// </summary>
        protected override void OnReset()
        {
            if (this.selectedItemViewModel != null)
                this.selectedItemViewModel.IsSelected = false;

            this.clickedItemViewModel = null;
            this.selectedItemViewModel = null;

            this.contextMenu = null;
            this.contextMenu = this.defaultContextMenu;

            if (rootViewModels != null)
                rootViewModels.Clear();
            rootViewModels = null;

            if (rootTreeViewItemViewModelStorage != null)
                rootTreeViewItemViewModelStorage.Dispose();
            rootTreeViewItemViewModelStorage = null;

            base.OnReset();
        }
 /// <summary>
 /// Modify the context menu view model by adding custom menu items.
 /// </summary>
 /// <param name="contextMenu">Context menu view model containing automatically added menu items.</param>
 /// <param name="element">Host element.</param>
 public void ProcessContextMenu(MenuItemViewModel contextMenu, ModelElementTreeViewModel element)
 {
     this.ProcessContextMenu(contextMenu, element, new List<IModelTreeContextMenuProvider>());
 }
        public MainErrorListViewModel(ViewModelStore viewModelStore)
            : base(viewModelStore)
        {
            filteredErrorListData = new FilteredErrorListData();

            toggleErrorCategory = new DelegateCommand(ToggleErrorCategory_Executed);
            toggleMessageCategory = new DelegateCommand(ToggleMessageCategory_Executed);
            toggleWarningCategory = new DelegateCommand(ToggleWarningCategory_Executed);
            toggleFilteredItems = new DelegateCommand(ToggleFilteredItems_Executed);

            navigateCommand = new DelegateCommand(NavigateCommand_Executed);
            filterCommand = new DelegateCommand(FilterCommand_Executed);
            unfilterCommand = new DelegateCommand(UnfilterCommand_Executed);

            sortByDescriptionCommand = new DelegateCommand(SortByDescriptionCommand_Executed);
            sortByCategoryCommand = new DelegateCommand(SortByCategoryCommand_Executed);
            sortByNumberCommand = new DelegateCommand(SortByNumberCommand_Executed);
            sortBySourceDisplayNameCommand = new DelegateCommand(SortBySourceDisplayNameCommand_Executed);

            selectedErrorListItems = new ObservableCollection<BaseErrorListItemViewModel>();
            selectedErrorListItems.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(selectedErrorListItems_CollectionChanged);

            unfilteredErrorListItems = new ObservableCollection<BaseErrorListItemViewModel>();
            filteredErrorListItems = new ObservableCollection<BaseErrorListItemViewModel>();
            displayedErrorListItems = new ObservableCollection<BaseErrorListItemViewModel>();
            displayedErrorListItemsRO = new ReadOnlyObservableCollection<BaseErrorListItemViewModel>(displayedErrorListItems);

            hiddenCategories = new List<ErrorListItemCategory>();

            if (this.ViewModelStore.Options != null)
                if (!this.ViewModelStore.Options.ErrorCategoryVisible)
                    hiddenCategories.Add(ErrorListItemCategory.Error);

            if (this.ViewModelStore.Options != null)
                if (!this.ViewModelStore.Options.InfoCategoryVisible)
                    hiddenCategories.Add(ErrorListItemCategory.Message);

            if (this.ViewModelStore.Options != null)
                if (!this.ViewModelStore.Options.WarningCategoryVisible)
                    hiddenCategories.Add(ErrorListItemCategory.Warning);

            if (this.ViewModelStore.Options != null)
                if (this.ViewModelStore.Options.FilteredCategoryVisible)
                    showFilteredItems = true;

            // create menu
            menuOptions = new ObservableCollection<MenuItemViewModel>();

            navigateMenuItem = new MenuItemViewModel(viewModelStore, "Navigate");
            navigateMenuItem.IconUrl = "pack://application:,,,/Tum.PDE.ToolFramework.Images;component/Views/Navigate-16.png";
            navigateMenuItem.Command = this.NavigateCommand;
            menuOptions.Add(navigateMenuItem);
            menuOptions.Add(new SeparatorMenuItemViewModel(viewModelStore));
            
            copyMenuItem = new MenuItemViewModel(viewModelStore, "Copy");
            copyMenuItem.IconUrl = "pack://application:,,,/Tum.PDE.ToolFramework.Images;component/Views/Copy-16.png";
            copyMenuItem.Command = this.CopyCommand;
            menuOptions.Add(copyMenuItem);

            separatorBeforeFilterItem = new SeparatorMenuItemViewModel(viewModelStore);
            filterMenuItem = new MenuItemViewModel(viewModelStore, "Filter");
            filterMenuItem.Command = this.FilterCommand;

            unFilterMenuItem = new MenuItemViewModel(viewModelStore, "Unfilter");
            unFilterMenuItem.Command = this.UnfilterCommand;

            this.EventManager.GetEvent<ErrorListClearItems>().Subscribe(new Action<BaseViewModel>(ClearItems));
            this.EventManager.GetEvent<ErrorListAddItem>().Subscribe(new Action<BaseErrorListItemViewModel>(AddItem));
            this.EventManager.GetEvent<ErrorListAddItems>().Subscribe(new Action<List<BaseErrorListItemViewModel>>(AddItems));
            this.EventManager.GetEvent<ErrorListRemoveItem>().Subscribe(new Action<Guid>(RemoveItem));

            this.EventManager.GetEvent<DocumentSavedEvent>().Subscribe(OnDocumentSaved);
            this.EventManager.GetEvent<DocumentClosingEvent>().Subscribe(OnDocumentClosing);
        }
        /// <summary>
        /// Searches for a MenuItemViewModel, which has its user data field set to the specified key.
        /// (Only the top level children of the context menu are searched).
        /// </summary>
        /// <param name="contextMenu">Context menu.</param>
        /// <param name="userDataKey">User data key.</param>
        /// <returns>MenuItemViewModel if found. Null otherwise.</returns>
        public MenuItemViewModel FindMenuViewModel(MenuItemViewModel contextMenu, string userDataKey)
        {
            foreach (MenuItemViewModel m in contextMenu.Children)
                if (m.UserData != null)
                    if (m.UserData.ToString() == userDataKey)
                        return m;

            return null;
        }
        /// <summary>
        /// Modify the context menu view model by adding custom menu items.
        /// </summary>
        /// <param name="contextMenu">Context menu view model containing automatically added menu items.</param>
        /// <param name="element">Host element.</param>
        /// <param name="processedProviders">Providers that have already been visited.</param>
        public virtual void ProcessContextMenu(MenuItemViewModel contextMenu, ModelElementTreeViewModel element, List<IModelTreeContextMenuProvider> processedProviders)
        {
            processedProviders.Add(this);

            foreach (IModelTreeContextMenuProvider p in this.InjectedContextMenuProviders)
                if( !processedProviders.Contains(p) )
                    p.ProcessContextMenu(contextMenu, element, processedProviders);
        }
        private void ProcessRoleForContextMenu(IModelElementParentProvider parentProvider, MenuItemViewModel menuItemAdd, DomainRoleInfo roleInfo)
        {
            bool bShouldAdd = true;
            if (this.ContextMenuProvider != null)
                bShouldAdd = this.ContextMenuProvider.ShouldCreateMenuItem(ModelTreeContextMenuItemType.AddElementMenuItem,
                    roleInfo.OppositeDomainRole.RolePlayer.GetType(), this.Element, null);

            if( this.Store.DomainDataAdvDirectory.IsAbstractRelationship(roleInfo.DomainRelationship.Id) )
                bShouldAdd = false;
            else if (roleInfo.Multiplicity == Multiplicity.One ||
                roleInfo.Multiplicity == Multiplicity.ZeroOne)
            {
                global::System.Collections.Generic.IList<ElementLink> links = DomainRoleInfo.GetElementLinks<ElementLink>(this.Element, roleInfo.Id);
                if (links.Count == 1)
                    bShouldAdd = false;
            }

            if (bShouldAdd)
            {
                DomainClassInfo rolePlayer = roleInfo.OppositeDomainRole.RolePlayer;
                if (!this.Store.DomainDataAdvDirectory.IsAbstractClass(rolePlayer.Id))
                {                    
                    MenuItemViewModel<ContextMenuCreationHelper> item = new MenuItemViewModel<ContextMenuCreationHelper>(this.ViewModelStore);
                    item.Text = "Add new " + (this.Element as IDomainModelOwnable).GetDomainModelServices().ElementTypeProvider.GetTypeDisplayName(this.Store, rolePlayer.Id);
                    item.Command = new DelegateCommand<ContextMenuCreationHelper>(AddNewElement);
                    //item.CommandParameter = roleInfo;
                    item.CommandParameter = new ContextMenuCreationHelper(roleInfo, rolePlayer);
                    menuItemAdd.Children.Add(item);
                }

                foreach (DomainClassInfo r in rolePlayer.AllDescendants)
                {
                    if (!this.Store.DomainDataAdvDirectory.IsAbstractClass(r.Id))
                    {
                        MenuItemViewModel<ContextMenuCreationHelper> item = new MenuItemViewModel<ContextMenuCreationHelper>(this.ViewModelStore);
                        item.Text = "Add new " + (this.Element as IDomainModelOwnable).GetDomainModelServices().ElementTypeProvider.GetTypeDisplayName(this.Store, r.Id);
                        item.Command = new DelegateCommand<ContextMenuCreationHelper>(AddNewElement);
                        //item.CommandParameter = roleInfo;
                        item.CommandParameter = new ContextMenuCreationHelper(roleInfo, r);
                        menuItemAdd.Children.Add(item);
                    }
                }
            }            
        }
        protected override void CreateAddElementsContextMenu(MenuItemViewModel menuItemAdd)
        {
            base.CreateAddElementsContextMenu(menuItemAdd);

            List<Guid> omittedItems;
            ParentChildrenCMMapping[this.ModelData.CurrentModelContext.ModelContextId].TryGetValue(this.ElementInfo.Id, out omittedItems);

            IModelElementParentProvider parentProvider = (this.Element as IDomainModelOwnable).GetDomainModelServices().ElementParentProvider;
            DomainClassInfo info = this.Element.GetDomainClass();
            foreach (DomainRoleInfo roleInfo in info.AllDomainRolesPlayed)
            {
                if (roleInfo.IsSource)
                {
                    DomainRelationshipAdvancedInfo infoAdv = this.Store.DomainDataAdvDirectory.FindRelationshipInfo(roleInfo.DomainRelationship.Id);
                    if (infoAdv is EmbeddingRelationshipAdvancedInfo)
                        if (!infoAdv.SourceRoleIsUIReadOnly && infoAdv.SourceRoleIsUIBrowsable)
                        //if (!infoAdv.SourceRoleIsUIReadOnly && infoAdv.SourceRoleIsUIBrowsable && infoAdv.SourceRoleIsGenerated)
                        //if (!infoAdv.TargetRoleIsUIReadOnly && infoAdv.TargetRoleIsUIBrowsable && infoAdv.TargetRoleIsGenerated)
                        {
                            if (omittedItems != null)
                                if (omittedItems.Contains(roleInfo.DomainRelationship.Id))
                                    continue;

                            ProcessRoleForContextMenu(parentProvider, menuItemAdd, roleInfo);
                        }
                }
            }
        }
        protected override MenuItemViewModel CreateContextMenu()
        {
            if (!this.DoCreateContextMenus)
                return null;

            MenuItemViewModel contextMenu = new MenuItemViewModel(this.ViewModelStore);

            MenuItemViewModel menuItemAdd = new MenuItemViewModel(this.ViewModelStore);
            menuItemAdd.Text = "Add";
            menuItemAdd.IconUrl = "pack://application:,,,/Tum.PDE.ToolFramework.Images;component/Views/Add-16.png";

            CreateAddElementsContextMenu(menuItemAdd);

            if (menuItemAdd.Children.Count > 0)
            {
                // sort children context menu

                // add to the main context menu
                contextMenu.Children.Add(menuItemAdd);
            }

            if (this.CanMoveElements)
            {
                #region MoveUp and MoveDown
                if (contextMenu.Children.Count > 0)
                    if (!(contextMenu.Children[contextMenu.Children.Count - 1] is SeparatorMenuItemViewModel))
                        contextMenu.Children.Add(new SeparatorMenuItemViewModel(this.ViewModelStore));

                MenuItemViewModel itemMoveUp = new MenuItemViewModel(this.ViewModelStore);
                itemMoveUp.Text = "Move Up";
                itemMoveUp.IconUrl = "pack://application:,,,/Tum.PDE.ToolFramework.Images;component/Views/Up-16.png";
                itemMoveUp.IsEnabled = CanMoveUp();
                itemMoveUp.Command = new DelegateCommand(MoveUp);
                contextMenu.Children.Add(itemMoveUp);

                MenuItemViewModel itemMoveDown = new MenuItemViewModel(this.ViewModelStore);
                itemMoveDown.Text = "Move Down";
                itemMoveDown.IsEnabled = CanMoveDown();
                itemMoveDown.IconUrl = "pack://application:,,,/Tum.PDE.ToolFramework.Images;component/Views/Down-16.png";
                itemMoveDown.Command = new DelegateCommand(MoveDown);
                contextMenu.Children.Add(itemMoveDown);
                #endregion
            }

            #region Cut, Copy, Paste
            if (contextMenu.Children.Count > 0)
                if (!(contextMenu.Children[contextMenu.Children.Count - 1] is SeparatorMenuItemViewModel))
                    contextMenu.Children.Add(new SeparatorMenuItemViewModel(this.ViewModelStore));

            MenuItemViewModel menuItemCut = new MenuItemViewModel(this.ViewModelStore);
            menuItemCut.Text = "Cut";
            menuItemCut.IconUrl = "pack://application:,,,/Tum.PDE.ToolFramework.Images;component/Ribbon/Cut-16.png";
            menuItemCut.IsEnabled = OnCutCommandCanExecute();
            menuItemCut.Command = new DelegateCommand(OnCutCommandExecuted);
            contextMenu.Children.Add(menuItemCut);

            MenuItemViewModel menuItemCopy = new MenuItemViewModel(this.ViewModelStore);
            menuItemCopy.Text = "Copy";
            menuItemCopy.IconUrl = "pack://application:,,,/Tum.PDE.ToolFramework.Images;component/Ribbon/Copy-16.png";
            menuItemCopy.IsEnabled = OnCopyCommandCanExecute();
            menuItemCopy.Command = new DelegateCommand(OnCopyCommandExecuted);
            contextMenu.Children.Add(menuItemCopy);

            MenuItemViewModel menuItemPaste = new MenuItemViewModel(this.ViewModelStore);
            menuItemPaste.Text = "Paste";
            menuItemPaste.IconUrl = "pack://application:,,,/Tum.PDE.ToolFramework.Images;component/Ribbon/Paste-16.png";
            menuItemPaste.IsEnabled = OnPasteCommandCanExecute();
            menuItemPaste.Command = new DelegateCommand(OnPasteCommandExecuted);
            contextMenu.Children.Add(menuItemPaste);
            #endregion

            if (!this.IsDomainModel)
            {
                #region Delete menu item
                if (!this.AdvancedRelationshipInfo.TargetRoleIsUIReadOnly)
                {
                    bool bShouldAdd0 = true;
                    if (this.ContextMenuProvider != null)
                        bShouldAdd0 = this.ContextMenuProvider.ShouldCreateMenuItem(ModelTreeContextMenuItemType.DeleteElementMenuItem,
                            this.Element.GetType(), this.Parent.Element, this.Element);

                    if (bShouldAdd0)
                    {
                        if (contextMenu.Children.Count > 0)
                            if (!(contextMenu.Children[contextMenu.Children.Count - 1] is SeparatorMenuItemViewModel))
                                contextMenu.Children.Add(new SeparatorMenuItemViewModel(this.ViewModelStore));

                        MenuItemViewModel item0 = new MenuItemViewModel(this.ViewModelStore);
                        item0.Text = "Delete";
                        item0.IconUrl = "pack://application:,,,/Tum.PDE.ToolFramework.Images;component/Views/Delete2-16.png";
                        item0.Command = new DelegateCommand(Delete);

                        contextMenu.Children.Add(item0);
                    }
                }
                #endregion
            }

            if (this.ContextMenuProvider != null)
                this.ContextMenuProvider.ProcessContextMenu(contextMenu, this);

            if (contextMenu.Children.Count == 0)
                return null;
            else
                return contextMenu;
        }