/// <summary> /// Removes the specified nodes from the tree view. /// </summary> /// <remarks> /// The user is asked to confirm the removal. /// </remarks> /// <param name="p_lstNodes">The nodes to remove.</param> public void RemoveNodes(IList <TreeNode> p_lstNodes) { //make a copy in case removing nodes changes the given p_lstNodes List <TreeNode> lstNodesToRemove = new List <TreeNode>(p_lstNodes); if (lstNodesToRemove.IsNullOrEmpty()) { return; } string strMessage = null; if (lstNodesToRemove.Count == 1) { strMessage = String.Format("Are you sure you want to removed '{0}?'", lstNodesToRemove[0].Text); } else { strMessage = String.Format("Are you sure you want to removed the {0} selected nodes?", lstNodesToRemove.Count); } if (MessageBox.Show(this.FindForm(), strMessage, "Confirm Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { FileSystemTreeNode tndNode = null; for (Int32 i = lstNodesToRemove.Count - 1; i >= 0; i--) { tndNode = (FileSystemTreeNode)lstNodesToRemove[i]; tndNode.Remove(); OnNodeRemoved(new NodesChangedEventArgs(tndNode)); } } }
/// <summary> /// Adds a virtual file system item to the tree view. /// </summary> /// <param name="p_tndRoot">The node to which to add the new item.</param> /// <param name="p_strName">The name of the new virtual item to add.</param> /// <returns>The new tree node representing the new virtual file system item.</returns> public FileSystemTreeNode AddVirtualNode(FileSystemTreeNode p_tndRoot, string p_strName) { if ((p_tndRoot != null) && !p_tndRoot.IsDirectory) { return(AddVirtualNode(p_tndRoot.Parent, p_strName)); } FileSystemTreeNode tndFile = null; System.Windows.Forms.TreeNodeCollection tncSiblings = (p_tndRoot == null) ? this.Nodes : p_tndRoot.Nodes; if (!tncSiblings.ContainsKey(p_strName)) { tndFile = (FileSystemTreeNode)tncSiblings[p_strName]; } else { string strName = Path.GetFileName(p_strName); if (String.IsNullOrEmpty(strName)) { strName = p_strName; } tndFile = new FileSystemTreeNode(strName, null); tndFile.Name = p_strName; tncSiblings.Add(tndFile); OnNodeAdded(new NodesChangedEventArgs(tndFile)); } return(tndFile); }
/// <summary> /// Addes the specified virtual path to the source tree. /// </summary> /// <param name="p_tndRoot">The node to which to add the path.</param> /// <param name="p_vfiPath">The path to add to the source tree.</param> public void AddVirtualPath(FileSystemTreeNode p_tndRoot, VirtualFileSystemItem p_vfiPath) { Cursor crsOldCursor = Cursor; Cursor = Cursors.WaitCursor; Queue <string> queRemainingPath = null; FileSystemTreeNode tndNode = FindNearestAncestor(p_tndRoot, p_vfiPath.Path, out queRemainingPath); string strCurrentPath = null; while (queRemainingPath.Count > 0) { strCurrentPath = queRemainingPath.Dequeue(); if ((queRemainingPath.Count > 0) || p_vfiPath.IsDirectory) { FileSystemTreeNode tndChild = new FileSystemTreeNode(strCurrentPath, null); ((tndNode == null) ? Nodes : tndNode.Nodes).Add(tndChild); OnNodeAdded(new NodesChangedEventArgs(tndChild)); tndNode = tndChild; } else { tndNode = AddPath(tndNode, p_vfiPath.Source); } } tndNode.AddSource(p_vfiPath.Source, true); Cursor = crsOldCursor; }
/// <summary> /// Raises the <see cref="Control.DragEnter"/>. /// </summary> /// <remarks> /// This determines if the item being dragged can be dropped at the current location. /// </remarks> /// <param name="e">A <see cref="DragEventArgs"/> that describes the event arguments.</param> protected override void OnDragOver(DragEventArgs e) { base.OnDragOver(e); e.Effect = DragDropEffects.None; if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] strFileNames = (string[])e.Data.GetData(DataFormats.FileDrop); foreach (string strFile in strFileNames) { if ((Directory.Exists(strFile) || (File.Exists(strFile) && !".lnk".Equals(Path.GetExtension(strFile).ToLowerInvariant())))) { e.Effect = DragDropEffects.Copy; break; } } } else if (e.Data.GetDataPresent(typeof(List <FileSystemTreeNode>))) { e.Effect = DragDropEffects.Move; } if (e.Effect != DragDropEffects.None) { FileSystemTreeNode tndFolder = (FileSystemTreeNode)GetNodeAt(PointToClient(new Point(e.X, e.Y))); if ((tndFolder != null) && !tndFolder.IsDirectory) { tndFolder = tndFolder.Parent; } SelectedNode = tndFolder; } }
/// <summary> /// Handles the <see cref="Control.Click"/> event of the new folder button. /// </summary> /// <param name="sender">The object that raised the event.</param> /// <param name="e">An <see cref="EventArgs"/> describing the event properties.</param> private void butNewFolder_Click(object sender, EventArgs e) { FileSystemTreeNode tndRoot = (FileSystemTreeNode)ftvFileSystem.SelectedNode; if (tndRoot == null) { tndRoot = (FileSystemTreeNode)ftvFileSystem.Nodes[0]; } string strNewFolderName = Path.Combine(tndRoot.LastSource, "New Folder"); for (Int32 i = 0; i < Int32.MaxValue && Directory.Exists(strNewFolderName); i++) { strNewFolderName = Path.Combine(tndRoot.LastSource, "New Folder " + i); } Directory.CreateDirectory(strNewFolderName); FileSystemTreeNode tndNewNode = ftvFileSystem.AddPath(tndRoot, strNewFolderName); if (tndRoot != null) { tndRoot.Expand(); } //make sure the node being edited is the only one selected ftvFileSystem.SelectedNode = tndNewNode; tndNewNode.BeginEdit(); }
/// <summary> /// Sets the image of the given node. /// </summary> /// <param name="p_fsnItem">The node whose image shuld be set.</param> protected virtual void SetNodeImage(FileSystemTreeNode p_fsnItem) { if (p_fsnItem.IsDirectory) { p_fsnItem.ImageKey = "folder"; p_fsnItem.SelectedImageKey = "folder"; } else { string strExtension = Path.GetExtension(p_fsnItem.LastSource).ToLowerInvariant(); if (!ImageList.Images.ContainsKey(strExtension)) { //this method should work, and it should be faster; however, it seems to // always return the generic file icon - no idea why /*if (!Archive.IsArchivePath(p_fsnItem.LastSource)) * ImageList.Images.Add(strExtension, System.Drawing.Icon.ExtractAssociatedIcon(p_fsnItem.LastSource)); * else*/ { string strIconPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()) + strExtension; File.CreateText(strIconPath).Close(); ImageList.Images.Add(strExtension, System.Drawing.Icon.ExtractAssociatedIcon(strIconPath)); File.Delete(strIconPath); } } p_fsnItem.ImageKey = strExtension; p_fsnItem.SelectedImageKey = strExtension; } }
/// <summary> /// Finds the specified node, or the specified node's nearest ancestor if the specified /// node does not exist. /// </summary> /// <param name="p_tndRoot">The node under which to search for the specified node.</param> /// <param name="p_strPath">The path to the node to be found.</param> /// <param name="p_queMissingPath">An out parameter that returns the path parts that were not found.</param> /// <returns>The specified node, it if exists. If it does not exist, then the specified node's /// neasrest ancestor. If no ancestor exists, then <c>null</c> is returned.</returns> protected FileSystemTreeNode FindNearestAncestor(FileSystemTreeNode p_tndRoot, string p_strPath, out Queue <string> p_queMissingPath) { string[] strPaths = p_strPath.Split(new char[] { Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); if (strPaths[0].EndsWith(Path.VolumeSeparatorChar.ToString(), StringComparison.OrdinalIgnoreCase)) { strPaths[0] = strPaths[0] + Path.DirectorySeparatorChar; } Queue <string> quePath = new Queue <string>(strPaths); FileSystemTreeNode tndSelectedNode = null; System.Windows.Forms.TreeNodeCollection tncCurrentLevel = (p_tndRoot == null) ? Nodes : p_tndRoot.Nodes; string strCurrentPath = null; while (quePath.Count > 0) { strCurrentPath = quePath.Peek(); foreach (FileSystemTreeNode tndCurrent in tncCurrentLevel) { if (tndCurrent.Text.Equals(strCurrentPath, StringComparison.OrdinalIgnoreCase)) { tndSelectedNode = tndCurrent; break; } } if ((tndSelectedNode == null) || (tncCurrentLevel == tndSelectedNode.Nodes)) { break; } tncCurrentLevel = tndSelectedNode.Nodes; quePath.Dequeue(); } p_queMissingPath = quePath; return(tndSelectedNode); }
/// <summary> /// Addes the specified virtual paths to the source tree. /// </summary> /// <param name="p_tndRoot">The node to which to add the paths.</param> /// <param name="p_vfiPaths">The paths to add to the source tree.</param> public void AddVirtualPaths(FileSystemTreeNode p_tndRoot, VirtualFileSystemItem[] p_vfiPaths) { Cursor crsOldCursor = Cursor; Cursor = Cursors.WaitCursor; foreach (VirtualFileSystemItem vfiPath in p_vfiPaths) { AddVirtualPath(p_tndRoot, vfiPath); } Cursor = crsOldCursor; }
/// <summary> /// Addes the specified files to the source tree. /// </summary> /// <param name="p_tndRoot">The node to which to add the file/folder.</param> /// <param name="p_strFileNames">The paths to add to the source tree.</param> public void AddPaths(FileSystemTreeNode p_tndRoot, string[] p_strFileNames) { Cursor crsOldCursor = Cursor; Cursor = Cursors.WaitCursor; foreach (string strFile in p_strFileNames) { AddPath(p_tndRoot, strFile); } Cursor = crsOldCursor; }
/// <summary> /// Sets the image of the given node. /// </summary> /// <param name="p_fsnItem">The node whose image shuld be set.</param> protected override void SetNodeImage(FileSystemTreeNode p_fsnItem) { if (p_fsnItem.IsDrive) { p_fsnItem.ImageKey = "drive"; p_fsnItem.SelectedImageKey = "drive"; } else { base.SetNodeImage(p_fsnItem); } }
/// <summary> /// Raises the <see cref="Control.DragDrop"/>. /// </summary> /// <remarks> /// This handles adding the dropped file/folder to the source tree. /// </remarks> /// <param name="e">A <see cref="DragEventArgs"/> that describes the event arguments.</param> protected override void OnDragDrop(DragEventArgs e) { base.OnDragDrop(e); Cursor crsOldCursor = Cursor; Cursor = Cursors.WaitCursor; BeginUpdate(); FileSystemTreeNode tndFolder = (FileSystemTreeNode)GetNodeAt(PointToClient(new Point(e.X, e.Y))); if ((tndFolder != null) && !tndFolder.IsDirectory) { tndFolder = tndFolder.Parent; } if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] strFileNames = (string[])e.Data.GetData(DataFormats.FileDrop); if (strFileNames != null) { if (tndFolder != null) { AddPaths(tndFolder, strFileNames); tndFolder.Expand(); } else { AddPaths(null, strFileNames); } } } else if (e.Data.GetDataPresent(typeof(List <FileSystemTreeNode>))) { List <FileSystemTreeNode> lstNodes = ((List <FileSystemTreeNode>)e.Data.GetData(typeof(List <FileSystemTreeNode>))); System.Windows.Forms.TreeNodeCollection tncSiblings = null; if (tndFolder == null) { tncSiblings = Nodes; } else { tncSiblings = tndFolder.Nodes; tndFolder.Expand(); } for (Int32 i = 0; i < lstNodes.Count; i++) { lstNodes[i].Remove(); tncSiblings.Add(lstNodes[i]); } } EndUpdate(); Cursor = crsOldCursor; }
/// <summary> /// Loads the grand children of the given node. /// </summary> /// <param name="p_tndFolder">The node whose grand children are to be loaded.</param> protected void LoadGrandChildren(FileSystemTreeNode p_tndFolder) { Cursor crsOldCursor = Cursor; Cursor = Cursors.WaitCursor; try { foreach (FileSystemTreeNode tndFolder in p_tndFolder.Nodes) { LoadChildren(tndFolder); } } finally { Cursor = crsOldCursor; } }
/// <summary> /// Determines if the children of the given node should be loaded. /// </summary> /// <param name="p_fsnItem">The node for which it is to be determined if the children should be loaded.</param> /// <returns><c>true</c> if teh children should be loaded; /// <c>false</c> otherwise.</returns> protected override bool ShouldLoadChildren(FileSystemTreeNode p_fsnItem) { return((p_fsnItem.Parent == null) || (p_fsnItem.Parent.IsExpanded)); }
/// <summary> /// A simple constructor that initializes the object with the given values. /// </summary> /// <param name="p_tndNode">The node that has changed.</param> public NodesChangedEventArgs(FileSystemTreeNode p_tndNode) { ChangedNode = p_tndNode; }
/// <summary> /// Loads the children of the given node. /// </summary> /// <param name="p_tndFolder">The node whose children are to be loaded.</param> protected void LoadChildren(FileSystemTreeNode p_tndFolder) { if (p_tndFolder.LastSource.IsLoaded || !p_tndFolder.IsDirectory && (!BrowseIntoArchives || !p_tndFolder.IsArchive)) { return; } Cursor crsOldCursor = Cursor; Cursor = Cursors.WaitCursor; try { p_tndFolder.LastSource.IsLoaded = true; string strPath = p_tndFolder.LastSource; if (BrowseIntoArchives && (Archive.IsArchivePath(strPath) || p_tndFolder.IsArchive)) { KeyValuePair <string, string> kvpPath = new KeyValuePair <string, string>(p_tndFolder.LastSource, Path.DirectorySeparatorChar.ToString()); if (Archive.IsArchivePath(strPath)) { kvpPath = Archive.ParseArchivePath(strPath); } Archive arcArchive = new Archive(kvpPath.Key); string[] strFolders = arcArchive.GetDirectories(kvpPath.Value); for (Int32 i = 0; i < strFolders.Length; i++) { AddPath(p_tndFolder, Archive.GenerateArchivePath(kvpPath.Key, strFolders[i])); } if (ShowFiles) { string[] strFiles = arcArchive.GetFiles(kvpPath.Value, false); for (Int32 i = 0; i < strFiles.Length; i++) { AddPath(p_tndFolder, Archive.GenerateArchivePath(kvpPath.Key, strFiles[i])); } } } else { try { string[] strFolders = Directory.GetDirectories(strPath); for (Int32 i = 0; i < strFolders.Length; i++) { AddPath(p_tndFolder, strFolders[i]); } if (ShowFiles) { string[] strFiles = Directory.GetFiles(strPath); for (Int32 i = 0; i < strFiles.Length; i++) { AddPath(p_tndFolder, strFiles[i]); } } } catch (UnauthorizedAccessException) { } } } finally { Cursor = crsOldCursor; } }
/// <summary> /// Determines if the children of the given node should be loaded. /// </summary> /// <param name="p_fsnItem">The node for which it is to be determined if the children should be loaded.</param> /// <returns><c>true</c> if teh children should be loaded; /// <c>false</c> otherwise.</returns> protected virtual bool ShouldLoadChildren(FileSystemTreeNode p_fsnItem) { return(true); }
/// <summary> /// This adds a file/folder to the source file structure. /// </summary> /// <param name="p_tndRoot">The node to which to add the file/folder.</param> /// <param name="p_strFile">The path to add to the source file structure.</param> public FileSystemTreeNode AddPath(FileSystemTreeNode p_tndRoot, string p_strFile) { if ((p_tndRoot != null) && !p_tndRoot.IsDirectory && (!BrowseIntoArchives || !p_tndRoot.IsArchive)) { return(AddPath(p_tndRoot.Parent, p_strFile)); } Cursor crsOldCursor = Cursor; Cursor = Cursors.WaitCursor; try { if (!Archive.IsArchivePath(p_strFile)) { FileSystemInfo fsiInfo = null; if (Directory.Exists(p_strFile)) { fsiInfo = new DirectoryInfo(p_strFile); } else if (ShowFiles && File.Exists(p_strFile) && !".lnk".Equals(Path.GetExtension(p_strFile), StringComparison.OrdinalIgnoreCase)) { fsiInfo = new FileInfo(p_strFile); } else { return(null); } if (!FileUtil.IsDrivePath(p_strFile) && ((fsiInfo.Attributes & FileAttributes.System) > 0)) { return(null); } } FileSystemTreeNode tndFile = null; System.Windows.Forms.TreeNodeCollection tncSiblings = (p_tndRoot == null) ? this.Nodes : p_tndRoot.Nodes; string strName = Path.GetFileName(p_strFile); if (String.IsNullOrEmpty(strName)) { strName = p_strFile; } if (tncSiblings.ContainsKey(strName)) { tndFile = (FileSystemTreeNode)tncSiblings[strName]; tndFile.AddSource(p_strFile, false); } else { tndFile = new FileSystemTreeNode(strName, p_strFile); tndFile.Name = strName; tncSiblings.Add(tndFile); OnNodeAdded(new NodesChangedEventArgs(tndFile)); } SetNodeImage(tndFile); if (tndFile.IsDirectory) { if (ShouldLoadChildren(tndFile)) { LoadChildren(tndFile); } } else { if (BrowseIntoArchives && tndFile.IsArchive) { if (ShouldLoadChildren(tndFile)) { LoadChildren(tndFile); } } else { tndFile.Sources[p_strFile].IsLoaded = true; } } return(tndFile); } finally { Cursor = crsOldCursor; } }