-
Notifications
You must be signed in to change notification settings - Fork 1
/
NodeCollection.cs
300 lines (268 loc) · 7.49 KB
/
NodeCollection.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
using System;
using System.Collections;
using System.ComponentModel;
namespace DevComponents.Tree
{
/// <summary>
/// Represents collection for Node objects.
/// </summary>
public class NodeCollection:CollectionBase
{
#region Private Variables
private Node m_ParentNode=null;
private TreeGX m_TreeControl=null;
private eTreeAction m_SourceAction=eTreeAction.Code;
#endregion
#region Internal Implementation
public NodeCollection()
{
}
/// <summary>
/// Gets or sets the node this collection is associated with.
/// </summary>
[Browsable(false),DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Node ParentNode
{
get {return m_ParentNode;}
}
/// <summary>
/// Sets the node collection belongs to.
/// </summary>
/// <param name="parent">Node that is parent of this collection.</param>
internal void SetParentNode(Node parent)
{
m_ParentNode=parent;
}
internal TreeGX TreeControl
{
get {return m_TreeControl;}
set {m_TreeControl=value;}
}
/// <summary>
/// Adds new object to the collection.
/// </summary>
/// <param name="node">Object to add.</param>
/// <returns>Index of newly added object.</returns>
public int Add(Node node)
{
return List.Add(node);
}
/// <summary>
/// Adds new object to the collection and provides information about the source of the command
/// </summary>
/// <param name="node">Node to add</param>
/// <param name="action">Source action</param>
/// <returns></returns>
public int Add(Node node, eTreeAction action)
{
m_SourceAction = action;
return List.Add(node);
}
/// <summary>
/// Adds an array of objects to the collection.
/// </summary>
/// <param name="nodes">Array of Node objects.</param>
public void AddRange(Node[] nodes)
{
foreach(Node node in nodes)
List.Add(node);
}
/// <summary>
/// Returns reference to the object in collection based on it's index.
/// </summary>
public Node this[int index]
{
get {return (Node)(List[index]);}
set {List[index] = value;}
}
/// <summary>
/// Inserts new object into the collection.
/// </summary>
/// <param name="index">Position of the object.</param>
/// <param name="value">Object to insert.</param>
public void Insert(int index, Node value)
{
List.Insert(index, value);
}
/// <summary>
/// Inserts new object into the collection.
/// </summary>
/// <param name="index">Position of the object.</param>
/// <param name="value">Object to insert.</param>
/// <param name="action">Action that is causing the event</param>
public void Insert(int index, Node value, eTreeAction action)
{
m_SourceAction = action;
List.Insert(index, value);
}
/// <summary>
/// Returns index of the object inside of the collection.
/// </summary>
/// <param name="value">Reference to the object.</param>
/// <returns>Index of the object.</returns>
public int IndexOf(Node value)
{
return List.IndexOf(value);
}
/// <summary>
/// Returns whether collection contains specified object.
/// </summary>
/// <param name="value">Object to look for.</param>
/// <returns>true if object is part of the collection, otherwise false.</returns>
public bool Contains(Node value)
{
return List.Contains(value);
}
/// <summary>
/// Removes specified object from the collection.
/// </summary>
/// <param name="value"></param>
public void Remove(Node value)
{
List.Remove(value);
}
/// <summary>
/// Removes specified object from the collection and provides information about source of the command
/// </summary>
/// <param name="node">Node to remove</param>
/// <param name="action">Source action</param>
public void Remove(Node node, eTreeAction action)
{
m_SourceAction = action;
List.Remove(node);
}
protected override void OnRemove(int index, object value)
{
TreeGX tree = GetTreeControl();
if(tree!=null)
tree.InvokeBeforeNodeRemove(m_SourceAction, value as Node, m_ParentNode);
base.OnRemove (index, value);
}
protected override void OnRemoveComplete(int index,object value)
{
base.OnRemoveComplete(index,value);
Node node=value as Node;
node.SetParent(null);
node.internalTreeControl=null;
if(m_ParentNode!=null) m_ParentNode.OnChildNodeRemoved(node);
TreeGX tree = GetTreeControl();
if(tree!=null)
{
tree.InvokeAfterNodeRemove(m_SourceAction, value as Node, m_ParentNode);
tree.NodeRemoved(m_SourceAction, value as Node, m_ParentNode, index);
}
}
protected override void OnInsert(int index, object value)
{
TreeGX tree = GetTreeControl();
if(tree!=null)
tree.InvokeBeforeNodeInsert(m_SourceAction, value as Node, m_ParentNode);
base.OnInsert (index, value);
}
protected override void OnInsertComplete(int index,object value)
{
base.OnInsertComplete(index,value);
Node node=value as Node;
if(m_ParentNode!=null)
{
if(node.Parent!=null && node.Parent!=m_ParentNode)
node.Remove();
node.SetParent(m_ParentNode);
}
node.internalTreeControl=m_TreeControl;
if(m_ParentNode!=null) m_ParentNode.OnChildNodeInserted(node);
TreeGX tree = GetTreeControl();
if(tree!=null)
tree.InvokeAfterNodeInsert(m_SourceAction, value as Node, m_ParentNode);
m_SourceAction = eTreeAction.Code;
}
/// <summary>
/// Copies collection into the specified array.
/// </summary>
/// <param name="array">Array to copy collection to.</param>
/// <param name="index">Starting index.</param>
public void CopyTo(Node[] array, int index)
{
List.CopyTo(array, index);
}
/// <summary>
/// Copies contained items to the Node array.
/// </summary>
/// <param name="array">Array to copy to.</param>
internal void CopyTo(Node[] array)
{
List.CopyTo(array,0);
}
protected override void OnClear()
{
foreach(Node node in this.List)
{
node.SetParent(null);
node.internalTreeControl=null;
}
base.OnClear();
}
protected override void OnClearComplete()
{
if (m_TreeControl != null && !PassiveCollection)
{
m_TreeControl.OnNodesCleared();
}
else if (m_ParentNode != null && !PassiveCollection)
m_ParentNode.OnNodesCleared();
TreeGX tree = GetTreeControl();
if (tree != null)
{
tree.ValidateSelectedNode();
}
base.OnClearComplete();
}
private TreeGX GetTreeControl()
{
if(m_TreeControl!=null)
return m_TreeControl;
if(m_ParentNode!=null)
return m_ParentNode.TreeControl;
return null;
}
private bool _PassiveCollection = false;
internal bool PassiveCollection
{
get { return _PassiveCollection; }
set
{
_PassiveCollection = value;
}
}
#endregion
}
#region NodeCollectionEditor
/// <summary>
/// Support for Node tabs design-time editor.
/// </summary>
public class NodeCollectionEditor:System.ComponentModel.Design.CollectionEditor
{
public NodeCollectionEditor(Type type):base(type)
{
}
protected override Type CreateCollectionItemType()
{
return typeof(Node);
}
protected override Type[] CreateNewItemTypes()
{
return new Type[] {typeof(Node)};
}
protected override object CreateInstance(Type itemType)
{
object item=base.CreateInstance(itemType);
if(item is Node)
{
Node node=item as Node;
node.Text=node.Name;
}
return item;
}
}
#endregion
}