Repository navigation
Expand file tree
/
Copy pathTreeItem.cs
More file actions
67 lines (58 loc) · 1.85 KB
/
Copy pathTreeItem.cs
File metadata and controls
67 lines (58 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
namespace NoteWorthy;
internal class TreeItem
{
/// <summary>
/// The path of the file or directory.
/// </summary>
public string FilePath { get; set; }
/// <summary>
/// The name of the TreeItem / file.
/// </summary>
public string Name { get => Path.GetFileName(FilePath); }
/// <summary>
/// True if the TreeItem represents a directory,
/// Flase if it represents a file.
/// </summary>
public bool IsDir { get; set; }
/// <summary>
/// The children of the TreeItem.
/// The TreeItem will only have children if it is a directory.
/// </summary>
public List<TreeItem> Children { get; set; } = new();
/// <summary>
/// The parent of the TreeItem.
/// This will be null if the TreeItem is the root directory.
/// </summary>
public TreeItem? Parent { get; set; }
/// <summary>
/// Will create a TreeItem that represents a file.
/// Call <see cref="SetDirectory(List{TreeItem})"/> to make it a directory.
/// </summary>
/// <param name="path"></param>
public TreeItem(string path, TreeItem? parent)
{
FilePath = path;
Parent = parent;
IsDir = false;
}
/// <summary>
/// Make the TreeItem a directory.
/// This must be called if the TreeItem is a directory as TreeItems represent files by default.
/// </summary>
/// <param name="children">The children of this TreeItem</param>
public void SetDirectory(List<TreeItem> children)
{
IsDir = true;
Children = children;
}
public void AddChild(TreeItem child)
{
if (!IsDir) throw new Exception("Cannot add a child to a file.");
Children.Add(child);
}
public void RemoveChild(TreeItem child)
{
if (!IsDir) throw new Exception("Cannot remove a child from a file.");
Children.Remove(child);
}
}