private void ShowContextMenu(HierarchicalViewModelBase selected, FrameworkElement control)
        {
            ContextMenuBuilder builder = new ContextMenuBuilder(null);

            if (_content.CanAddNewItem)
            {
                builder.New("Add new").Handler(() => { AddNewItem(selected); }).End();
            }

            if (_content.CanRenameItem)
            {
                builder.Separator();
                builder.New("Rename").Handler(() => { RenameItem(selected); }).End();
            }

            if (_content.CanDeleteItem)
            {
                builder.Separator();
                builder.New("Delete").Handler(() => { DeleteItem(selected); }).End();
            }

            if (builder.HasItems)
            {
                control.ContextMenu = builder.ContextMenu;
            }
        }
        private void DoSelect()
        {
            HierarchicalViewModelBase selected = null;

            if (tvwExplorer.IsVisible)
            {
                if (tvwExplorer.SelectedItem != null)
                {
                    selected = tvwExplorer.SelectedItem as HierarchicalViewModelBase;
                }
            }
            else if (tvwSearchResults.IsVisible)
            {
                if (tvwSearchResults.SelectedItem != null)
                {
                    selected = tvwSearchResults.SelectedItem as HierarchicalViewModelBase;
                }
            }

            if (selected != null && Select(selected))
            {
                this.DialogResult = true;
                this.Close();
            }
        }
        private ObservableCollection <HierarchicalViewModelBase> LoadModel(HierarchicalViewModelBase parent)
        {
            var model = new ObservableCollection <HierarchicalViewModelBase>();

            if (_content != null)
            {
                var list = _content.LoadModel(parent);

                foreach (HierarchicalViewModelBase vm in list)
                {
                    vm.Children.Add(new ViewModelPlaceholder("Loading..."));
                    vm.LazyLoadChildren += new HierarchicalViewModelAction((p) => {
                        using (new OverrideCursor(Cursors.Wait)) {
                            p.Children.Clear();
                            var children = LoadModel(p);
                            foreach (HierarchicalViewModelBase child in children)
                            {
                                p.Children.Add(child);
                            }
                        }
                    });
                }
                model = new ObservableCollection <HierarchicalViewModelBase>(list);
            }

            return(model);
        }
        private void ValidateSelection()
        {
            HierarchicalViewModelBase selected = null;

            if (tvwExplorer.IsVisible)
            {
                if (tvwExplorer.SelectedItem != null)
                {
                    selected = tvwExplorer.SelectedItem as HierarchicalViewModelBase;
                }
            }
            else if (tvwSearchResults.IsVisible)
            {
                if (tvwSearchResults.SelectedItem != null)
                {
                    selected = tvwSearchResults.SelectedItem as HierarchicalViewModelBase;
                }
            }

            btnSelect.IsEnabled = false;
            if (selected != null)
            {
                btnSelect.IsEnabled = _content.CanSelectItem(selected);
            }
        }
 private void RenameItem(HierarchicalViewModelBase item)
 {
     if (item != null)
     {
         // This starts the rename, and the change is registered when the edit is complete. See EditableTextBlock_EditingComplete
         item.IsRenaming = true;
     }
 }
 private string GetParentage(HierarchicalViewModelBase item)
 {
     if (item != null)
     {
         return(GetParentage(item.Parent) + "/" + _content.GetElementIDForViewModel(item));
     }
     return("");
 }
        private void AddNewItem(HierarchicalViewModelBase parent)
        {
            var action = _content.AddNewItem(parent);

            if (action != null)
            {
                RegisterPendingChange(action, this);
            }
        }
        private void RestoreLastExpandedNodes()
        {
            var findstr = Config.GetProfile(User, "HierarchicalSelector." + _content.GetType().Name + ".LastSearch", "");
            var model   = _model;

            if (!string.IsNullOrWhiteSpace(findstr))
            {
                txtFind.TimerDisabled = true;
                txtFind.Text          = findstr;
                txtFind.TimerDisabled = false;

                DoFind(findstr);
                model = tvwSearchResults.ItemsSource as ObservableCollection <HierarchicalViewModelBase>;
            }

            List <string> expanded = Config.GetProfile <List <string> >(User, "HierarchicalSelector." + _content.GetType().Name + ".ExpandedParentages", null);

            if (expanded != null)
            {
                ExpandParentages(model, expanded);
            }

            var lastSelectedId = Config.GetProfile <int>(User, "HierarchicalSelector." + _content.GetType().Name + ".LastSelected", -1);

            if (lastSelectedId >= 0)
            {
                HierarchicalViewModelBase selected = null;
                foreach (HierarchicalViewModelBase root in model)
                {
                    if (root.ObjectID == lastSelectedId)
                    {
                        selected = root;
                    }
                    else
                    {
                        root.Traverse((vm) => {
                            if (vm.ObjectID == lastSelectedId)
                            {
                                selected = vm;
                            }
                        });
                    }

                    if (selected != null)
                    {
                        break;
                    }
                }

                if (selected != null)
                {
                    selected.IsSelected = true;
                    tvwExplorer.BringModelToView(selected);
                }
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Traverses from this node to each successive parent until a node with parent is reached.
        /// </summary>
        /// <param name="func"></param>
        public void TraverseToTop(HierarchicalViewModelAction func)
        {
            HierarchicalViewModelBase p = this;

            while (p != null)
            {
                func(p);
                p = p.Parent;
            }
        }
        private void DeleteItem(HierarchicalViewModelBase item)
        {
            var action = _content.DeleteItem(item);

            if (action != null)
            {
                RegisterPendingChange(action, this);
                item.IsDeleted = true;
            }
        }
Ejemplo n.º 11
0
 private void DecorateChildViewModels(HierarchicalViewModelBase parent, List <HierarchicalViewModelBase> list)
 {
     parent.Children.Clear();
     foreach (HierarchicalViewModelBase child in list)
     {
         if (child.NumChildren > 0)
         {
             child.LazyLoadChildren += new HierarchicalViewModelAction(viewModel_LazyLoadChildren);
             child.Children.Add(new ViewModelPlaceholder("Loading..."));
         }
         parent.Children.Add(child);
     }
 }
        private bool Select(HierarchicalViewModelBase selected)
        {
            SaveExpandedNodes();

            if (_content != null)
            {
                var result = _content.CreateSelectionResult(selected);
                if (SelectedAction != null)
                {
                    SelectedAction(result);
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 13
0
        public bool IsAncestorOf(HierarchicalViewModelBase item)
        {
            if (item == null)
            {
                return(false);
            }
            HierarchicalViewModelBase p = item.Parent;

            while (p != null)
            {
                if (p == this)
                {
                    return(true);
                }
                p = p.Parent;
            }
            return(false);
        }
Ejemplo n.º 14
0
        private void BuildFavoritesModel(HierarchicalViewModelBase root, bool global)
        {
            var list = Provider.GetTopFavorites(global);

            foreach (T item in list)
            {
                var viewModel = Provider.CreateViewModel(item);
                viewModel.Parent = root;
                if (item.NumChildren > 0)
                {
                    viewModel.LazyLoadChildren += new HierarchicalViewModelAction(viewModel_LazyLoadChildren);
                    viewModel.Children.Add(new ViewModelPlaceholder("Loading..."));
                }
                root.Children.Add(viewModel);
                root.IsExpanded = true;
                root.Tag        = global;
            }
        }
Ejemplo n.º 15
0
        internal void AddFavoriteGroup(HierarchicalViewModelBase parent)
        {
            int parentGroupID = 0;

            if (parent == null)
            {
                return;
            }

            bool?isGlobal = false;

            if (parent is ViewModelPlaceholder)
            {
                var pvm = parent as ViewModelPlaceholder;
                if (pvm.Tag != null)
                {
                    isGlobal = (bool?)(pvm).Tag;
                }
            }
            else if (parent is FavoriteViewModel <T> )
            {
                var parentViewModel = parent as FavoriteViewModel <T>;
                isGlobal      = parentViewModel.IsGlobal;
                parentGroupID = parentViewModel.FavoriteID;
            }

            T model = new T();

            model.IsGroup          = true;
            model.GroupName        = "<New Folder>";
            model.IsGlobal         = isGlobal.HasValue && isGlobal.Value;
            model.FavoriteParentID = parentGroupID;

            FavoriteViewModel <T> viewModel = Provider.CreateFavoriteViewModel(model);

            viewModel.Parent     = parent;
            viewModel.IsSelected = true;

            parent.Children.Add(viewModel);


            RegisterUniquePendingChange(new InsertFavoriteGroupCommand(model));
            viewModel.IsRenaming = true;
        }
Ejemplo n.º 16
0
        public void LoadFavorites()
        {
            _model      = new ObservableCollection <HierarchicalViewModelBase>();
            _userRoot   = new ViewModelPlaceholder("User Favorites");
            _globalRoot = new ViewModelPlaceholder("Global Favorites");

            BuildFavoritesModel(_userRoot, false);
            BuildFavoritesModel(_globalRoot, true);

            _model.Add(_userRoot);
            _model.Add(_globalRoot);

            _globalRoot.IsExpanded = true;
            _userRoot.IsExpanded   = true;

            Provider.FavoritesTree.ItemsSource = _model;

            IsPopulated = true;
        }
        private void EditableTextBlock_EditingComplete(object sender, string text)
        {
            HierarchicalViewModelBase selected = null;

            if (tvwExplorer.IsVisible)
            {
                selected = tvwExplorer.SelectedItem as HierarchicalViewModelBase;
            }
            else
            {
                selected = tvwSearchResults.SelectedItem as HierarchicalViewModelBase;
            }

            if (_content.CanRenameItem && selected != null)
            {
                var action = _content.RenameItem(selected, text);
                if (action != null)
                {
                    RegisterUniquePendingChange(action, this);
                }
            }
        }
Ejemplo n.º 18
0
 void viewModel_LazyLoadChildren(HierarchicalViewModelBase item)
 {
     using (new OverrideCursor(Cursors.Wait)) {
         var vm = item as FavoriteViewModel <T>;
         if (vm != null)
         {
             if (vm.IsGroup)
             {
                 // Load the children of this favorites group...
                 var list = Provider.GetFavoritesForParent(vm.FavoriteID, vm.IsGlobal);
                 vm.Children.Clear();
                 list.ForEach((model) => {
                     var viewModel    = Provider.CreateViewModel(model);
                     viewModel.Parent = item;
                     if (model.NumChildren > 0)
                     {
                         viewModel.LazyLoadChildren += new HierarchicalViewModelAction(viewModel_LazyLoadChildren);
                         viewModel.Children.Add(new ViewModelPlaceholder("Loading..."));
                     }
                     vm.Children.Add(viewModel);
                 });
             }
             else
             {
                 DecorateChildViewModels(item, Provider.GetChildViewModels(vm));
             }
         }
         else
         {
             if (item is V)
             {
                 DecorateChildViewModels(item, Provider.GetChildViewModels((V)item));
             }
         }
     }
 }
Ejemplo n.º 19
0
        public static void BringModelToView(this TreeView tvw, HierarchicalViewModelBase item, ItemsControl rootItemsControl = null)
        {
            ItemsControl itemsControl = rootItemsControl != null ? rootItemsControl : tvw;

            // Get the stack of parentages...
            var stack = item.GetParentStack();

            // Descend through the levels until the desired TreeViewItem is found.
            while (stack.Count > 0)
            {
                HierarchicalViewModelBase model = stack.Pop();

                if (!model.IsExpanded)
                {
                    model.IsExpanded = true;
                }

                int index = 0;
                if (model.Parent == null)
                {
                    var itemSource = itemsControl.ItemsSource as System.Collections.IList;
                    if (itemSource != null)
                    {
                        index = itemSource.IndexOf(model);
                    }
                }
                else
                {
                    index = model.Parent.Children.IndexOf(model);
                }

                // Access the custom VSP that exposes BringIntoView
                bool foundContainer = false;
                if (index >= 0)
                {
                    BLVirtualizingStackPanel itemsHost = FindVisualChild <BLVirtualizingStackPanel>(itemsControl);
                    if (itemsHost != null)
                    {
                        // Due to virtualization, BringIntoView may not predict the offset correctly the first time.
                        ItemsControl nextItemsControl = null;
                        int          iterCount        = 0;
                        while (nextItemsControl == null && iterCount < 1000)
                        {
                            foundContainer = true;
                            itemsHost.UpdateLayout();
                            itemsHost.BringIntoView(index);
                            tvw.Dispatcher.Invoke(DispatcherPriority.Background, (DispatcherOperationCallback) delegate(object unused) {
                                nextItemsControl = (ItemsControl)itemsControl.ItemContainerGenerator.ContainerFromIndex(index);
                                return(null);
                            }, null);
                            iterCount++;    // Sometimes this thing can spin on forever...
                        }

                        if (nextItemsControl != null)
                        {
                            nextItemsControl.Focus();
                        }

                        itemsControl = nextItemsControl;
                    }
                }

                if (!foundContainer || (itemsControl == null))
                {
                    // Abort the operation
                    return;
                }
            }
        }