1 using System;
2 using System.Collections;
3 using System.Windows;
4
5 namespace FileExplorer
6 {
7 public abstract class Node
8 {
9 public Node(const string& name_) : name(name_), parent(null), treeViewNode(null), listViewItem(null)
10 {
11 }
12 public virtual default ~Node();
13 public inline const string& Name() const
14 {
15 return name;
16 }
17 public inline Node* Parent() const
18 {
19 return parent;
20 }
21 public void SetParent(Node* parent_)
22 {
23 parent = parent_;
24 }
25 public virtual bool IsRootDirectory() const
26 {
27 return false;
28 }
29 public virtual MainWindow* GetMainWindow() const
30 {
31 if (parent != null)
32 {
33 return parent->GetMainWindow();
34 }
35 else
36 {
37 return null;
38 }
39 }
40 public inline TreeViewNode* GetTreeViewNode() const
41 {
42 return treeViewNode;
43 }
44 [nodiscard]
45 public Result<bool> SetTreeViewNode(TreeViewNode* treeViewNode_)
46 {
47 treeViewNode = treeViewNode_;
48 treeViewNode->SetData(this);
49 TreeView* treeView = treeViewNode->GetTreeView();
50 if (treeView != null)
51 {
52 ImageList* imageList = treeView->GetImageList();
53 if (imageList != null)
54 {
55 int imageIndex = imageList->GetImageIndex(ImageName());
56 if (imageIndex != -1)
57 {
58 auto result = treeViewNode->SetImageIndex(imageIndex);
59 if (result.Error()) return result;
60 }
61 }
62 }
63 return Result<bool>(true);
64 }
65 public virtual Result<UniquePtr<TreeViewNode>> ToTreeViewNode(bool createChildren, ImageList* imageList)
66 {
67 return Result<UniquePtr<TreeViewNode>>();
68 }
69 public abstract string ImageName() const;
70 public virtual string ExpandedImageName() const
71 {
72 return ImageName();
73 }
74 public virtual Result<Control*> CreateView(ImageList* imageList)
75 {
76 return Result<Control*>();
77 }
78 public virtual string DirectoryPath()
79 {
80 return string();
81 }
82 public virtual bool CanOpen() const
83 {
84 return true;
85 }
86 [nodiscard]
87 public Result<bool> AddMenuItems(ContextMenu* contextMenu, bool treeView)
88 {
89 if (CanOpen())
90 {
91 ClickAction* action = null;
92 UniquePtr<MenuItem> openMenuItem(new MenuItem("Open"));
93 if (openMenuItem->Error())
94 {
95 return Result<bool>(ErrorId(openMenuItem->GetErrorId()));
96 }
97 if (treeView)
98 {
99 action = new OpenAndExpandAction(this);
100 }
101 else
102 {
103 action = new OpenAction(this);
104 }
105 auto result = contextMenu->AddMenuItemAction(openMenuItem.Release(), action);
106 if (result.Error()) return result;
107 }
108 return Result<bool>(true);
109 }
110 [nodiscard]
111 public virtual Result<bool> SetData(ListViewItem* item, ImageList* imageList)
112 {
113 listViewItem = item;
114 item->SetData(this);
115 auto result = item->SetColumnValue(0, Name());
116 if (result.Error()) return result;
117 string imageName = ImageName();
118 if (!imageName.IsEmpty())
119 {
120 item->SetImageIndex(imageList->GetImageIndex(imageName));
121 }
122 return Result<bool>(true);
123 }
124 [nodiscard]
125 public Result<bool> Open()
126 {
127 auto result = Explore();
128 if (result.Error()) return result;
129 result = ViewContent();
130 if (result.Error()) return result;
131 result = SetCurrentPathNode();
132 if (result.Error()) return result;
133 return Result<bool>(true);
134 }
135 [nodiscard]
136 public Result<bool> OpenAndExpand()
137 {
138 auto result = Open();
139 if (result.Error()) return result;
140 TreeViewNode* treeViewNode = GetTreeViewNode();
141 if (treeViewNode != null && !treeViewNode->Children().IsEmpty())
142 {
143 result = treeViewNode->Expand();
144 if (result.Error()) return result;
145 }
146 return Result<bool>(true);
147 }
148 [nodiscard]
149 public Result<bool> ExecuteDefaultAction()
150 {
151 if (CanOpen())
152 {
153 auto result = Open();
154 if (result.Error()) return result;
155 }
156 return Result<bool>(true);
157 }
158 [nodiscard]
159 private Result<bool> Explore()
160 {
161 MainWindow* mainWindow = GetMainWindow();
162 if (mainWindow != null)
163 {
164 DirectoryTreeView* directoryTreeView = mainWindow->GetDirectoryTreeView();
165 if (directoryTreeView != null)
166 {
167 auto result = directoryTreeView->Open(this);
168 if (result.Error()) return result;
169 }
170 }
171 return Result<bool>(true);
172 }
173 [nodiscard]
174 private Result<bool> ViewContent()
175 {
176 MainWindow* mainWindow = GetMainWindow();
177 if (mainWindow != null)
178 {
179 ContentView* contentView = mainWindow->GetContentView();
180 if (contentView != null)
181 {
182 auto result = contentView->ViewContent(this);
183 if (result.Error()) return result;
184 }
185 }
186 return Result<bool>(true);
187 }
188 [nodiscard]
189 public Result<bool> SetCurrentPathNode()
190 {
191 MainWindow* mainWindow = GetMainWindow();
192 if (mainWindow != null)
193 {
194 PathBar* pathBar = mainWindow->GetPathBar();
195 if (pathBar != null)
196 {
197 auto result = pathBar->SetCurrentNode(this);
198 if (result.Error()) return result;
199 }
200 }
201 return Result<bool>(true);
202 }
203 private string name;
204 private Node* parent;
205 private TreeViewNode* treeViewNode;
206 private ListViewItem* listViewItem;
207 }
208 }