/// <summary>
        /// Applies a SearchCriteria to the index.
        /// </summary>
        /// <param name="crit">The SearchCriteria to apply.</param>
        /// <returns>A ResultsTable that contains the result of applying the criteria.</returns>
        public ResultsTable GenerateResultsTable(SearchCriteria crit)
        {
            Debug.Assert(crit != null);

            CodeElementWrapperArrayIndexTable idxTable = null;

            // If using all criteria, perform a copy.
            if (crit.ElementFilter == CodeElementType.All)
            {
                idxTable = (CodeElementWrapperArrayIndexTable)indexTable.Clone();
            }
            else
            {
                idxTable = new CodeElementWrapperArrayIndexTable(_codeElementArray);

                for (int i = 0; i < _codeElementArray.Count; i++)
                {
                    if (_codeElementArray[i].ElementType == crit.ElementFilter)
                    {
                        idxTable.Add(i);
                    }

                }

                idxTable.Sort(new SortCriteria(SortCriteria.SortType.ALPHA));
            }

            ResultsTable ret = new ResultsTable(idxTable);
            return (ret);
        }
        /// <summary>
        /// Decrements the transaction counter.
        /// </summary>
        /// <remarks>
        /// The counter determines if nested operations are running,
        /// and when it reaches zero, the filter is rebuilt to reflect the operations.
        /// </remarks>
        private void EndTransaction()
        {
            if (_transactionCounter == 0)
            {
                Debug.Assert(false, "Invalid call to EndTransaction");

                return;
            }

            _transactionCounter--;
            if (_transactionCounter == 0)
            {
                // Recompute current results.
                _codeElementWrapperIndexTable = new CodeElementWrapperIndexTable(_codeElementWrapperArray);
                _currentResults = _codeElementWrapperIndexTable.GenerateResultsTable(_searchCriteria);
                this.FilterText = _currentFilterText;
            }
        }
        /// <summary>
        /// Completes loading the source outline.
        /// </summary>
        internal void FinishLoading()
        {
            _codeElementWrapperIndexTable = new CodeElementWrapperIndexTable(_codeElementWrapperArray);
            _currentResults = _codeElementWrapperIndexTable.GenerateResultsTable(_searchCriteria);

            if ((_control.filterStringTextBox.Text != "<Filter>") && (_control.filterStringTextBox.Text != ""))
            {
                _control.filterStringTextBox.Text = "<Filter>";
                if (_viewType == ViewType.TreeView)
                {
                    TreeView.ExpandAll();
                }
            }
            else
            {
                switch (_viewType)
                {
                    case ViewType.FlatView:
                        LoadFlatView();
                        break;

                    case ViewType.TreeView:
                        TreeView.ExpandAll();
                        break;
                }
            }
            State = OutlineFileManagerState.WaitToStartOver;
        }
        /// <summary>
        /// Clears the filters and displays the hierarchical TreeView.
        /// </summary>
        public void ResetFilters()
        {
            _searchCriteria = new SearchCriteria(CodeElementType.All);

            if (_currentDocument != null)
            {
                _currentFilterText = "";
                _currentResults = _codeElementWrapperIndexTable.GenerateResultsTable(_searchCriteria);
                ViewType = ViewType.TreeView;
                _control.Reset();
            }
        }
        /// <summary>
        /// Resets the state of the TreeView and the filters.
        /// </summary>
        public void Reset()
        {
            _transactionCounter = 0;
            _codeElementWrapperArray = new CodeElementWrapperArray();
            _codeElementWrapperIndexTable = new CodeElementWrapperIndexTable(_codeElementWrapperArray);
            _mapElementIDToTreeViewCodeElementWrapper = null;
            _mapElementIDToFilterViewCodeElementWrapper = null;
            _currentResults = null;

            ResetFilters();
        }
        /// <summary>
        /// Occurs when a new code element is added to the text window,
        /// and adds the new element to the appropriate place in the outline.
        /// </summary>
        /// <param name="newElement">The new code element.</param>
        public void OnCodeModelElementAdd(CodeElement newElement)
        {
            if ((newElement == null) || !CodeModelHelpers.IsInterestingKind(newElement.Kind))
            {
                return;
            }

            try
            {
                int line = newElement.StartPoint.Line;
            }
            catch
            {
                // An exception can be thrown here when an element is being edited, so ignore it.
                return;
            }

            // Update the tree.
            if (newElement.Kind == vsCMElement.vsCMElementParameter)
            {
                FindBestMatchCodeElementToRefreshFrom(newElement);
                return;
            }

            // Get the start point from the wrapper object and not from the CodeElement directly.
            CodeElementWrapper temp = new CodeElementWrapper(newElement);
            TextPoint tp = temp.StartPoint;
            CodeElementWrapper cew = GetClosestCodeElementInTreeView(TreeView, tp);

            if (cew == null)
            {
                // Nothing found, this must be the first element drawn.
                ReadCodeModelElementsRecursive(newElement, TreeView.Nodes, false);
            }
            else
            {
                CodeElementWrapper newNode = new CodeElementWrapper(newElement);

                // Note that the add could result from a paste, and the editor only informs
                // regarding the outer element that was added if the language is C# or VB;
                // the C++ language raises an event for each element.
                AddNodeToInternalStructures(newNode);
                ReadCodeModelChildrenElementsRecursive(newNode, false);

                newNode.ExpandAll();

                int index = 0;

                // Insert this element in the correct place.
                foreach (CodeElementWrapper n in cew.Nodes)
                {
                    if (n.Location > newNode.Location)
                    {
                        // Insert prior to n.
                        cew.Nodes.Insert(index, newNode);
                        newNode = null;
                        break;
                    }
                    else
                    {
                        index++;
                    }
                }

                // If it was not inserted, append it.

                if (newNode != null)
                {
                    cew.Nodes.Add(newNode);
                }
            }

            // Update the filter view.
            _codeElementWrapperIndexTable = new CodeElementWrapperIndexTable(_codeElementWrapperArray);
            _currentResults = _codeElementWrapperIndexTable.GenerateResultsTable(_searchCriteria);
            this.FilterText = _currentFilterText;
        }