Exemple #1
0
        /// <summary>
        /// Builds the tree view.  Assumes the list is pre-sorted.
        /// </summary>
        private void BuildTreeView()
        {
            // Display a wait cursor while the TreeNodes are being created.
            Cursor.Current = Cursors.WaitCursor;

            this.treeView1.BeginUpdate();
            this.treeView1.Nodes.Clear();

            int      maxPaths = 20;        // Hopefully there are no paths more than 20 deep.
            TreeNode lastNode = null;

            // Hold the nodes from the previous record.
            // We save these so we do not need to search the treeview
            TreeNode[] lastNodes = new TreeNode[maxPaths];
            string     aggSubPath;

            string[] paths     = new string[maxPaths];
            string[] prevPaths = new string[maxPaths];

            string[] dataRecords;
            if (fileType == CompressedFileType.ArzFile)
            {
                dataRecords = arzFile.GetKeyTable();
            }
            else if (fileType == CompressedFileType.ArcFile)
            {
                dataRecords = arcFile.GetKeyTable();
            }
            else
            {
                return;
            }

            // We failed so return.
            if (dataRecords == null)
            {
                return;
            }

            foreach (string recordID in dataRecords)
            {
                // Holds the aggregate path
                aggSubPath = string.Empty;
                paths.Initialize();
                string[] subPaths = recordID.Split('\\');
                int      count    = 0;

                foreach (string subPath in subPaths)
                {
                    // We only add if not the last item in the path
                    // which should be the dbr.
                    if (count < subPaths.Length - 1)
                    {
                        aggSubPath  += subPath + '\\';
                        paths[count] = aggSubPath;

                        // See if the paths are still the same.
                        if (paths[count] != prevPaths[count])
                        {
                            if (lastNode == null || count == 0)
                            {
                                // The very top of the tree.
                                lastNode = this.treeView1.Nodes.Add(aggSubPath, subPath);
                            }
                            else
                            {
                                // Add a new node to the previous one in the tree.
                                lastNode = lastNode.Nodes.Add(aggSubPath, subPath);
                            }

                            // Save this guy so we do not need to search.
                            lastNodes[count] = lastNode;
                        }
                        else
                        {
                            // Use the previous TreeNode since the strings match.
                            // This saves the expensive lookup.
                            lastNode = lastNodes[count];
                        }
                    }
                    else
                    {
                        // This is the last thing so we just add it.
                        aggSubPath += subPath;

                        // There might not be any sub folders so we add the file to the root
                        if (lastNode == null || count == 0)
                        {
                            // We do not assign last node since we are still at the root.
                            this.treeView1.Nodes.Add(aggSubPath, subPath);
                            lastNode = null;
                        }
                        else
                        {
                            lastNode = lastNode.Nodes.Add(aggSubPath, subPath);
                        }

                        // Clear out and save the previous paths.
                        prevPaths.Initialize();
                        Array.Copy(paths, prevPaths, paths.Length);
                    }

                    count++;
                }
            }

            // Reset the cursor to the default for all controls.
            Cursor.Current = Cursors.Default;

            this.treeView1.EndUpdate();
        }