-
Notifications
You must be signed in to change notification settings - Fork 4
/
DirectoryDiffTreeView.cs
352 lines (294 loc) · 6.46 KB
/
DirectoryDiffTreeView.cs
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
namespace Menees.Diffs.Windows.Forms
{
#region Using Directives
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Windows.Forms;
using Menees;
using Menees.Windows.Forms;
#endregion
internal sealed class DirectoryDiffTreeView : TreeView
{
#region Private Data Members
private const int FileIndex = 3;
private const int FileErrorIndex = 5;
private const int FileMissingIndex = 4;
private const int FolderClosedIndex = 0;
private const int FolderMissingIndex = 2;
private const int FolderOpenIndex = 1;
private bool useA;
private DirectoryDiffResults? results;
#endregion
#region Constructors
public DirectoryDiffTreeView()
{
this.HideSelection = false;
this.ShowLines = false;
this.FullRowSelect = true;
DiffOptions.OptionsChanged += this.DiffOptionsChanged;
}
#endregion
#region Internal Events
internal event EventHandler? MouseWheelMsg;
internal event EventHandler? VScroll;
#endregion
#region Public Properties
public bool CanView
{
get
{
bool result = false;
TreeNode node = this.SelectedNode;
if (node != null)
{
DirectoryDiffEntry? entry = GetEntryForNode(node);
if (entry != null)
{
result = (this.useA && entry.InA) || (!this.useA && entry.InB);
}
}
return result;
}
}
#endregion
#region Public Methods
public static DirectoryDiffEntry? GetEntryForNode(TreeNode? node) => node?.Tag as DirectoryDiffEntry;
public string GetFullNameForNode(TreeNode node)
{
string result = node.FullPath;
if (this.results != null)
{
string basePath = this.useA ? this.results.DirectoryA.FullName : this.results.DirectoryB.FullName;
if (!basePath.EndsWith("\\"))
{
basePath += "\\";
}
result = basePath + result;
}
return result;
}
public void SetData(DirectoryDiffResults results, bool useA)
{
this.results = results;
this.useA = useA;
this.PopulateTree();
}
public void View()
{
if (this.CanView)
{
TreeNode node = this.SelectedNode;
string fullName = this.GetFullNameForNode(node);
WindowsUtility.ShellExecute(this, fullName);
}
}
#endregion
#region Protected Methods
protected override void Dispose(bool disposing)
{
if (disposing)
{
DiffOptions.OptionsChanged -= this.DiffOptionsChanged;
}
base.Dispose(disposing);
}
protected override void OnAfterCollapse(TreeViewEventArgs e)
{
TreeNode? node = e.Node;
DirectoryDiffEntry? entry = GetEntryForNode(node);
if (node != null && entry != null)
{
this.SetNodeImage(node, entry);
}
base.OnAfterCollapse(e);
}
protected override void OnAfterExpand(TreeViewEventArgs e)
{
TreeNode? node = e.Node;
DirectoryDiffEntry? entry = GetEntryForNode(node);
if (node != null && entry != null)
{
this.SetNodeImage(node, entry);
}
base.OnAfterExpand(e);
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
TreeNode node = this.GetNodeAt(e.X, e.Y);
if (node != null)
{
this.SelectedNode = node;
}
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == NativeMethods.WM_VSCROLL && this.VScroll != null)
{
this.VScroll(this, EventArgs.Empty);
}
else if (m.Msg == NativeMethods.WM_MOUSEWHEEL && this.MouseWheelMsg != null)
{
this.MouseWheelMsg(this, EventArgs.Empty);
}
}
#endregion
#region Private Methods
private void AddEntry(DirectoryDiffEntry entry, TreeNode? parentNode)
{
TreeNode node = new()
{
Tag = entry,
};
if (this.useA)
{
entry.TagA = node;
}
else
{
entry.TagB = node;
}
node.Expand();
this.SetNodeText(node, entry);
this.SetNodeImage(node, entry);
this.SetNodeColor(node, entry);
if (parentNode != null)
{
parentNode.Nodes.Add(node);
}
else
{
this.Nodes.Add(node);
}
if (!entry.IsFile)
{
foreach (DirectoryDiffEntry subEntry in entry.Subentries!)
{
this.AddEntry(subEntry, node);
}
}
}
private void DiffOptionsChanged(object? sender, EventArgs e)
{
this.SetNodesColors(this.Nodes);
}
private void PopulateTree()
{
this.BeginUpdate();
try
{
this.Nodes.Clear();
if (this.results != null)
{
foreach (DirectoryDiffEntry entry in this.results.Entries)
{
this.AddEntry(entry, null);
}
}
this.ExpandAll();
}
finally
{
this.EndUpdate();
}
}
private void SetNodeColor(TreeNode node, DirectoryDiffEntry entry)
{
if (entry.InA && entry.InB)
{
if (entry.Different)
{
node.BackColor = DiffOptions.ChangedColor;
}
else
{
node.BackColor = this.BackColor;
}
}
else
{
if (entry.InA)
{
node.BackColor = DiffOptions.DeletedColor;
}
else
{
node.BackColor = DiffOptions.InsertedColor;
}
}
}
private void SetNodeImage(TreeNode node, DirectoryDiffEntry entry)
{
bool present = (this.useA && entry.InA) || (!this.useA && entry.InB);
int index;
if (entry.Error != null)
{
index = FileErrorIndex;
}
else if (entry.IsFile)
{
if (present)
{
index = FileIndex;
}
else
{
index = FileMissingIndex;
}
}
else if (present)
{
// If a folder is only present on one side, then
// we should always show it closed since we haven't
// actually recursed into it.
//
// Also, we should only show a folder open if we're
// showing recursive differences.
if (this.results != null && this.results.Recursive && node.IsExpanded && entry.InA && entry.InB)
{
index = FolderOpenIndex;
}
else
{
index = FolderClosedIndex;
}
}
else
{
index = FolderMissingIndex;
}
node.ImageIndex = index;
node.SelectedImageIndex = index;
}
private void SetNodesColors(TreeNodeCollection nodes)
{
foreach (TreeNode node in nodes)
{
DirectoryDiffEntry? entry = GetEntryForNode(node);
if (entry != null)
{
this.SetNodeColor(node, entry);
}
this.SetNodesColors(node.Nodes);
}
}
private void SetNodeText(TreeNode node, DirectoryDiffEntry entry)
{
if ((entry.InA && entry.InB) || (this.useA && entry.InA) || (!this.useA && entry.InB))
{
if (entry.Error == null)
{
node.Text = entry.Name;
}
else
{
node.Text = string.Format("{0}: {1}", entry.Name, entry.Error);
}
}
}
#endregion
}
}