-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enum.cs
291 lines (234 loc) · 8.33 KB
/
Enum.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
using System;
using System.Linq;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Input;
namespace HMIStudio.Shared.TreeView.Collections
{
public class TreeNode<T> : ObservableObject, ITreeNode<T>, IDisposable
where T : new()
{
public TreeNode()
{
// call property setters to trigger setup and event notifications
_Parent = null;
_ChildNodes = new TreeNodeList<T>(this);
}
public TreeNode(T Value)
{
// call property setters to trigger setup and event notifications
this.Value = Value;
_Parent = null;
_ChildNodes = new TreeNodeList<T>(this);
}
public TreeNode(T Value, TreeNode<T> Parent)
{
// call property setters to trigger setup and event notifications
this.Value = Value;
_Parent = Parent;
_ChildNodes = new TreeNodeList<T>(this);
}
public ITreeNode ParentNode
{
get { return _Parent; }
}
private ITreeNode<T> _Parent;
public ITreeNode<T> Parent
{
get { return _Parent; }
set { SetParent(value, true); }
}
public void SetParent(ITreeNode<T> node, bool updateChildNodes = true)
{
if (node == Parent)
return;
var oldParent = Parent;
var oldParentHeight = Parent != null ? Parent.Height : 0;
var oldDepth = Depth;
// if oldParent isn't null
// remove this node from its newly ex-parent's children
if (oldParent != null && oldParent.Children.Contains(this))
oldParent.Children.Remove(this, updateParent: false);
// update the backing field
_Parent = node;
// add this node to its new parent's children
if (_Parent != null && updateChildNodes)
_Parent.Children.Add(this, updateParent: false);
// signal the old parent that it has lost this child
if (oldParent != null)
oldParent.OnDescendantChanged(NodeChangeType.NodeRemoved, this);
if (oldDepth != Depth)
OnDepthChanged();
// if this operation has changed the height of any parent, initiate the bubble-up height changed event
if (Parent != null)
{
var newParentHeight = Parent != null ? Parent.Height : 0;
if (newParentHeight != oldParentHeight)
Parent.OnHeightChanged();
Parent.OnDescendantChanged(NodeChangeType.NodeAdded, this);
}
OnParentChanged(oldParent, Parent);
}
protected virtual void OnParentChanged(ITreeNode<T> oldValue, ITreeNode<T> newValue)
{
OnPropertyChanged("Parent");
}
// TODO: add property and event notifications that are missing from this set: DescendentsChanged, AnscestorsChanged, ChildrenChanged, ParentChanged
public ITreeNode<T> Root
{
get { return (Parent == null) ? this : Parent.Root; }
}
private TreeNodeList<T> _ChildNodes;
public TreeNodeList<T> Children
{
get { return _ChildNodes; }
}
// non-generic iterator for interface-based support (From TreeNodeView, for example)
public IEnumerable<ITreeNode> ChildNodes
{
get
{
foreach (ITreeNode node in Children)
yield return node;
yield break;
}
}
public IEnumerable<ITreeNode> Descendants
{
get
{
foreach (ITreeNode node in ChildNodes)
{
yield return node;
foreach (ITreeNode descendant in node.Descendants)
yield return descendant;
}
yield break;
}
}
public IEnumerable<ITreeNode> Subtree
{
get
{
yield return this;
foreach (ITreeNode node in Descendants)
yield return node;
yield break;
}
}
public IEnumerable<ITreeNode> Ancestors
{
get
{
if (Parent == null)
yield break;
yield return Parent;
foreach (ITreeNode node in Parent.Ancestors)
yield return node;
yield break;
}
}
public event Action<NodeChangeType, ITreeNode> AncestorChanged;
public virtual void OnAncestorChanged(NodeChangeType changeType, ITreeNode node)
{
if (AncestorChanged != null)
AncestorChanged(changeType, node);
foreach (ITreeNode<T> child in Children)
child.OnAncestorChanged(changeType, node);
}
public event Action<NodeChangeType, ITreeNode> DescendantChanged;
public virtual void OnDescendantChanged(NodeChangeType changeType, ITreeNode node)
{
if (DescendantChanged != null)
DescendantChanged(changeType, node);
if (Parent != null)
Parent.OnDescendantChanged(changeType, node);
}
// [recurse up] descending aggregate property
public int Height
{
get { return Children.Count == 0 ? 0 : Children.Max(n => n.Height) + 1; }
}
// [recurse down] descending-broadcasting event
public virtual void OnHeightChanged()
{
OnPropertyChanged("Height");
foreach (ITreeNode<T> child in Children)
child.OnHeightChanged();
}
private T _Value;
public T Value
{
get { return _Value; }
set
{
if (value == null && _Value == null)
return;
if (value != null && _Value != null && value.Equals(_Value))
return;
_Value = value;
OnPropertyChanged("Value");
// set Node if it's ITreeNodeAware
if (_Value != null && _Value is ITreeNodeAware<T>)
(_Value as ITreeNodeAware<T>).Node = this;
}
}
// [recurse up] bubble up aggregate property
public int Depth
{
get { return (Parent == null ? 0 : Parent.Depth + 1); }
}
// [recurse up] bubble up event
public virtual void OnDepthChanged()
{
OnPropertyChanged("Depth");
if (Parent != null)
Parent.OnDepthChanged();
}
private UpDownTraversalType _DisposeTraversal = UpDownTraversalType.BottomUp;
public UpDownTraversalType DisposeTraversal
{
get { return _DisposeTraversal; }
set { _DisposeTraversal = value; }
}
private bool _IsDisposed;
public bool IsDisposed
{
get { return _IsDisposed; }
}
public Action ExpandAction { get; set; }
public virtual void Dispose()
{
CheckDisposed();
OnDisposing();
// clean up contained objects (in Value property)
if (Value is IDisposable)
{
if (DisposeTraversal == UpDownTraversalType.BottomUp)
foreach (TreeNode<T> node in Children)
node.Dispose();
(Value as IDisposable).Dispose();
if (DisposeTraversal == UpDownTraversalType.TopDown)
foreach (TreeNode<T> node in Children)
node.Dispose();
}
_IsDisposed = true;
}
public event EventHandler Disposing;
protected void OnDisposing()
{
if (Disposing != null)
Disposing(this, EventArgs.Empty);
}
public void CheckDisposed()
{
if (IsDisposed)
throw new ObjectDisposedException(GetType().Name);
}
public override string ToString()
{
return "Depth=" + Depth + ", Height=" + Height + ", Children=" + Children.Count;
}
}
}