private void RestoreExpandedNodes(string searchText, TreeSearchBox.Mode searchMode)
        {
            if ((searchText != _lastSearchText && searchText.Length > 0) || (searchMode == TreeSearchBox.Mode.SavedSearch && _lastSearchMode != TreeSearchBox.Mode.SavedSearch))
            {
                // expand all nodes if there's a search and the search has changed.
                _treeView.ExpandAll();
            }

            if (searchText.Length == 0 && searchMode != TreeSearchBox.Mode.SavedSearch)
            {
                // if there isn't a search persist the user's expanded nodes.
                var list = new List<VirtualTreeNode.PersistenceInfo>(searchMode == TreeSearchBox.Mode.OrgView ? _orgViewExpandedTags : _serverViewExpandedTags);

                var allNodes = new List<VirtualTreeNode>(_treeView.AllNodes);

                foreach (VirtualTreeNode.PersistenceInfo info in list)
                {
                    VirtualTreeNode match;

                    // First, look for the object in the same position
                    if (_treeView.TryExactMatch(info.Path, out match) >= info.Path.Count)
                    {
                        if (!match.IsExpanded)
                        {
                            match.Expand();
                        }
                        allNodes.Remove(match);
                        continue;
                    }

                    // Second, find the object in the maximal sub tree where it appeared only once
                    if (_treeView.TryExactMatch(info.PathToMaximalSubTree, out match) >= info.PathToMaximalSubTree.Count)
                    {
                        match = _treeView.FindNodeIn(match, info.Tag);
                        if (match != null)
                        {
                            if (!match.IsExpanded)
                            {
                                match.Expand();
                            }
                            allNodes.Remove(match);
                        }
                    }
                }

                // collapse everything else
                foreach (VirtualTreeNode n in allNodes)
                {
                    if (n.Tag != null && n.IsExpanded)
                    {
                        n.Collapse();
                    }
                }

                // special case for root node
                if (_rootExpanded)
                {
                    _treeView.Nodes[0].Expand();
                }
            }
        }
 private void AssignList(ref List<VirtualTreeNode.PersistenceInfo> list, TreeSearchBox.Mode mode)
 {
     switch (mode)
     {
         case TreeSearchBox.Mode.Objects:
             list = _objectViewExpandedTags;
             break;
         case TreeSearchBox.Mode.Organization:
             list = _organizationViewExpandedTags;
             break;
         default:
             list = _infrastructureViewExpandedTags;
             break;
     }
 }
        /// <summary>
        /// Updates the <see cref="TreeView"/> with the specified new root node. This is done by merging the specified
        /// root node with the existing root node to minimize updates to the treeview to reduce flicker.
        /// </summary>
        /// <param name="newRootNode">The new root node.</param>
        /// <param name="searchText">The search text for the currently active search.</param>
        public void RefreshTreeView(VirtualTreeNode newRootNode, string searchText, TreeSearchBox.Mode searchMode)
        {
            Util.ThrowIfParameterNull(newRootNode, "newRootNode");
            Util.ThrowIfParameterNull(searchText, "searchText");

            Program.AssertOnEventThread();

            _treeView.BeginUpdate();

            PersistExpandedNodes(searchText);

            _treeView.UpdateRootNodes(new[] { newRootNode });

            RestoreExpandedNodes(searchText, searchMode);

            bool searchTextCleared = (searchText.Length == 0 && searchText != _lastSearchText);

            _lastSearchText = searchText;
            _lastSearchMode = searchMode;
            _treeView.EndUpdate();

            // ensure that the selected nodes are visible when search text is cleared (CA-102127)
            if (searchTextCleared)
            {
                ExpandSelection();
            }
        }
        public VirtualTreeNode CreateNewRootNode(Search search, TreeSearchBox.Mode mode)
        {
            VirtualTreeNode newRootNode;
            MainWindowTreeNodeGroupAcceptor groupAcceptor;

            switch (mode)
            {
                case TreeSearchBox.Mode.Objects:
                    newRootNode = new VirtualTreeNode(Messages.VIEW_OBJECTS);
                    groupAcceptor = CreateGroupAcceptor(_highlightedDragTarget, newRootNode);
                    OrganizationalView.PopulateObjectView(groupAcceptor, search);
                    break;
                case TreeSearchBox.Mode.Organization:
                    newRootNode = new VirtualTreeNode(Messages.VIEW_ORGANIZATION);
                    groupAcceptor = CreateGroupAcceptor(_highlightedDragTarget, newRootNode);
                    OrganizationalView.PopulateOrganizationView(groupAcceptor, search);
                    break;
                default:
                    Util.ThrowIfParameterNull(search, "search");
                    newRootNode = new VirtualTreeNode("XenCenter");
                    groupAcceptor = CreateGroupAcceptor(_highlightedDragTarget, newRootNode);
                    search.PopulateAdapters(groupAcceptor);
                    break;
            }

            return newRootNode;
        }