/// <summary> /// Creates a new directory node. /// </summary> /// <param name="name">Name of the directory.</param> /// <param name="parent">Parent of the node.</param> /// <param name="mutable">If <c>true</c>, node can be modified after creation.</param> public DirectoryTreeNode(string name, DirectoryTreeNode <T> parent, bool mutable = true) : base(mutable) { _root = false; Name = name; Parent = parent; }
/// <summary> /// Create a new PayloadTree. /// </summary> /// <param name="seperator">Character that seperates directories and content in a path, e.g. <c>/</c>.</param> /// <param name="replaceSeperator">Character in a filesystem path to be replaced with <paramref name="seperator"/>.</param> public PayloadTree(char seperator = DefaultPathSeperator, char?replaceSeperator = null) { PathSeperator = seperator; PathSeperators = replaceSeperator == null ? new[] { PathSeperator } : new[] { PathSeperator, replaceSeperator.Value }; RootNode = new DirectoryTreeNode <PayloadItem>(); }
/// <summary> /// Add a subdirectory. /// </summary> /// <param name="name">Name of the directory to add.</param> /// <returns>Directory node.</returns> public DirectoryTreeNode <T> AddChildDirectory(string name) { ThrowIfImmutable(); var node = new DirectoryTreeNode <T>(name, this); _children.Add(node); return(node); }
/// <summary> /// Get the root of the tree. /// </summary> /// <returns>Root node.</returns> public TreeNode <T> GetRoot() { DirectoryTreeNode <T> node = (this as DirectoryTreeNode <T>) ?? Parent; while (node.IsRoot() == false) { node = node.Parent; } return(node); }
/// <summary> /// Get the sequence of nodes from the root. /// </summary> /// <returns>Sequence of nodes.</returns> public IEnumerable <TreeNode <T> > GetPath() { var pathStack = new Stack <TreeNode <T> >(); DirectoryTreeNode <T> node = (this as DirectoryTreeNode <T>) ?? Parent; while (node.IsRoot() == false) { pathStack.Push(node); node = node.Parent; } pathStack.Push(node); return(pathStack); }
/// <summary> /// Move node by reassigning its parent. /// </summary> /// <param name="newParent">Node to move this node to.</param> public void Move(DirectoryTreeNode <T> newParent) { ThrowIfImmutable(); if (newParent == null) { throw new ArgumentNullException("newParent"); } if (Parent == null) { throw new InvalidOperationException("Node has no current parent. Cannot move."); } Debug.Assert(Parent.HasChildren); Parent.RemoveChildNode(this, false); Parent = newParent; Parent.AddChildNode(this); }