forked from jaysonragasa/MultiRDPClient.NET
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTreeListNode.cs
More file actions
1017 lines (990 loc) · 22.8 KB
/
TreeListNode.cs
File metadata and controls
1017 lines (990 loc) · 22.8 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace CommonTools
{
public class Node
{
NodeCollection m_owner = null;
Node m_prevSibling = null;
Node m_nextSibling = null;
NodeCollection m_children = null;
bool m_hasChildren = false;
bool m_expanded = false;
int m_imageId = -1;
int m_id = -1;
object m_tag = null;
string m_key = string.Empty;
bool m_selected = false;
public Node Parent
{
get
{
if (m_owner != null)
return m_owner.Owner;
return null;
}
}
public Node PrevSibling
{
get { return m_prevSibling; }
}
public Node NextSibling
{
get { return m_nextSibling; }
}
public bool HasChildren
{
get
{
if (m_children != null && m_children.IsEmpty() == false)
return true;
return m_hasChildren;
}
set
{
m_hasChildren = value;
}
}
public int ImageId
{
get { return m_imageId; }
set { m_imageId = value; }
}
public virtual NodeCollection Owner
{
get { return m_owner; }
}
public virtual NodeCollection Nodes
{
get
{
if (m_children == null)
m_children = new NodeCollection(this);
return m_children;
}
}
public bool Expanded
{
get { return m_expanded && HasChildren; }
set
{
if (m_expanded == value)
return;
NodeCollection root = GetRootCollection();
if (root != null)
root.NodetifyBeforeExpand(this, value);
int oldcount = VisibleNodeCount;
m_expanded = value;
if (m_expanded)
UpdateOwnerTotalCount(1, VisibleNodeCount);
else
UpdateOwnerTotalCount(oldcount, 1);
if (root != null)
root.NodetifyAfterExpand(this, value);
}
}
public void Collapse()
{
Expanded = false;
}
public void Expand()
{
Expanded = true;
}
public void ExpandAll()
{
Expanded = true;
if (HasChildren)
{
foreach (Node node in Nodes)
node.ExpandAll();
}
}
public object Tag
{
get { return m_tag; }
set { m_tag = value; }
}
public string Key
{
set
{
m_key = value;
}
get
{
return m_key;
}
}
public bool Selected
{
set
{
m_selected = value;
}
get
{
return m_selected;
}
}
public Node()
{
m_data = new object[1];
}
public Node(string text)
{
m_data = new object[1] {text};
}
public Node(object[] fields)
{
SetData(fields);
}
object[] m_data = null;
public object this [string fieldname]
{
get
{
return this[Owner.FieldIndex(fieldname)];
}
set
{
this[Owner.FieldIndex(fieldname)] = value;
}
}
public object this [int index]
{
get
{
if (index < 0 || index >= m_data.Length)
return null;
return m_data[index];
}
set
{
if (index >= 0 && index < 100) // within this range just increase
{
if (m_data == null || index >= m_data.Length)
{
object[] newdata = new object[index+1];
if (m_data != null)
m_data.CopyTo(newdata, 0);
m_data = newdata;
}
}
AssertData(index);
m_data[index] = value;
}
}
public void SetData(object[] fields)
{
m_data = new object[fields.Length];
fields.CopyTo(m_data, 0);
}
public int VisibleNodeCount
{
get
{
// can not use Expanded property here as it returns false node has no children
if (m_expanded)
return m_childVisibleCount + 1;
return 1;
}
}
/// <summary>
/// MakeVisible will expand all the parents up the tree.
/// </summary>
public void MakeVisible()
{
Node parent = Parent;
while (parent != null)
{
parent.Expanded = true;
parent = parent.Parent;
}
}
/// <summary>
/// IsVisible returns true if all parents are expanded, else false
/// </summary>
public bool IsVisible()
{
Node parent = Parent;
while (parent != null)
{
// parent not expanded, so this node is not visible
if (parent.Expanded == false)
return false;
// parent not hooked up to a collection, so this node is not visible
if (parent.Owner == null)
return false;
parent = parent.Parent;
}
return true;
}
public Node GetRoot()
{
Node parent = this;
while (parent.Parent != null)
parent = parent.Parent;
return parent;
}
public NodeCollection GetRootCollection()
{
return GetRoot().Owner;
}
public string GetId()
{
StringBuilder sb = new StringBuilder(32);
Node node = this;
while (node != null)
{
node.Owner.UpdateChildIds(false);
if (node.Parent != null)
sb.Insert(0, "." + node.Id.ToString());
else
sb.Insert(0, node.Id.ToString());
node = node.Parent;
}
return sb.ToString();
}
internal void InsertBefore(Node insertBefore, NodeCollection owner)
{
this.m_owner = owner;
Node next = insertBefore;
Node prev = null;
if (next != null)
{
prev = insertBefore.PrevSibling;
next.m_prevSibling = this;
}
if (prev != null)
prev.m_nextSibling = this;
this.m_nextSibling = next;
this.m_prevSibling = prev;
UpdateOwnerTotalCount(0, VisibleNodeCount);
}
internal void InsertAfter(Node insertAfter, NodeCollection owner)
{
this.m_owner = owner;
Node prev = insertAfter;
Node next = null;
if (prev != null)
{
next = prev.NextSibling;
prev.m_nextSibling = this;
this.m_prevSibling = prev;
}
if (next != null)
next.m_prevSibling = this;
this.m_nextSibling = next;
UpdateOwnerTotalCount(0, VisibleNodeCount);
}
internal void Remove()
{
Node prev = this.PrevSibling;
Node next = this.NextSibling;
if (prev != null)
prev.m_nextSibling = next;
if (next != null)
next.m_prevSibling = prev;
this.m_nextSibling = null;
this.m_prevSibling = null;
UpdateOwnerTotalCount(VisibleNodeCount, 0);
this.m_owner = null;
this.m_id = -1;
}
internal static void SetHasChildren(Node node, bool hasChildren)
{
if (node != null)
node.m_hasChildren = hasChildren;
}
public int NodeIndex
{
get { return Id;}
}
internal int Id
{
get
{
if (m_owner == null)
return -1;
m_owner.UpdateChildIds(false);
return m_id;
}
set { m_id = value; }
}
int m_childVisibleCount = 0;
void UpdateTotalCount(int oldValue, int newValue)
{
int old = VisibleNodeCount;
m_childVisibleCount += (newValue - oldValue);
UpdateOwnerTotalCount(old, VisibleNodeCount);
}
void UpdateOwnerTotalCount(int oldValue, int newValue)
{
if (Owner != null)
Owner.internalUpdateNodeCount(oldValue, newValue);
if (Parent != null)
Parent.UpdateTotalCount(oldValue, newValue);
}
void AssertData(int index)
{
Debug.Assert(index >= 0, "index >= 0");
Debug.Assert(index < m_data.Length, "index < m_data.Length");
}
}
public class NodeCollection : IEnumerable
{
internal int m_version = 0;
int m_nextId = 0;
int m_IdDirty = 0;
Node[] m_nodesInternal = null;
Node m_owner = null;
Node m_firstNode = null;
Node m_lastNode = null;
int m_count = 0;
public Node Owner
{
get { return m_owner; }
}
public Node FirstNode
{
get { return m_firstNode; }
}
public Node LastNode
{
get { return m_lastNode; }
}
public bool IsEmpty()
{
return m_firstNode == null;
}
public int Count
{
get { return m_count; }
}
public NodeCollection(Node owner)
{
m_owner = owner;
}
public virtual void Clear()
{
m_version++;
while (m_firstNode != null)
{
Node node = m_firstNode;
m_firstNode = node.NextSibling;
node.Remove();
}
m_firstNode = null;
m_lastNode = null;
m_count = 0;
m_totalNodeCount = 0;
m_IdDirty = 0;
m_nextId = 0;
ClearInternalArray();
Node.SetHasChildren(m_owner, m_count != 0);
}
public Node Add(string key, string text)
{
Node n = new Node(text);
n.Key = key;
return Add(key, new Node(text));
}
public Node Add(string key, Node newnode)
{
m_version++;
ClearInternalArray();
Debug.Assert(newnode != null && newnode.Owner == null, "Add(Node newnode)");
newnode.InsertAfter(m_lastNode, this);
m_lastNode = newnode;
if (m_firstNode == null)
{
m_firstNode = newnode;
}
newnode.Key = key;
newnode.Id = m_nextId++;
m_count++;
return newnode;
}
public void Remove(Node node)
{
if (m_lastNode == null)
return;
m_version++;
ClearInternalArray();
Debug.Assert(node != null && object.ReferenceEquals(node.Owner, this), "Remove(Node node)");
Node prev = node.PrevSibling;
Node next = node.NextSibling;
node.Remove();
if (prev == null) // first node
m_firstNode = next;
if (next == null) // last node
m_lastNode = prev;
m_IdDirty++;
m_count--;
Node.SetHasChildren(m_owner, m_count != 0);
}
public void InsertAfter(Node node, Node insertAfter)
{
m_version++;
ClearInternalArray();
Debug.Assert(node.Owner == null, "node.Owner == null");
if (insertAfter == null)
{
node.InsertBefore(m_firstNode, this);
m_firstNode = node;
}
else
{
node.InsertAfter(insertAfter, this);
}
if (m_lastNode == insertAfter)
{
m_lastNode = node;
node.Id = m_nextId++;
}
else
m_IdDirty++;
m_count++;
}
public Node this [int index]
{
get
{
Debug.Assert(index >= 0 && index < Count, "Index out of range");
if (index >= Count)
throw new IndexOutOfRangeException(string.Format("Node this [{0}], Collection Count {1}", index, Count));
EnsureInternalArray();
return m_nodesInternal[index];
}
}
public Node this[string key]
{
get
{
EnsureInternalArray();
Node n = null;
foreach (Node xnode in this)
{
if (xnode.Key == key)
{
n = xnode;
break;
}
}
return n;
}
}
public Node NodeAtIndex(int index)
{
Node node = FirstNode;
while (index-- > 0 && node != null)
node = node.NextSibling;
return node;
}
public int GetNodeIndex(Node node)
{
int index = 0;
Node tmp = FirstNode;
while (tmp != null && tmp != node)
{
tmp = tmp.NextSibling;
index++;
}
if (tmp == null)
return -1;
return index;
}
public virtual int FieldIndex(string fieldname)
{
NodeCollection rootCollection = this;
while (rootCollection.Owner != null && rootCollection.Owner.Owner != null)
rootCollection = rootCollection.Owner.Owner;
return rootCollection.GetFieldIndex(fieldname);
}
public Node FirstVisibleNode()
{
return FirstNode;
}
public Node LastVisibleNode(bool recursive)
{
if (recursive)
return FindNodesBottomLeaf(LastNode, true);
return LastNode;
}
public virtual void NodetifyBeforeExpand(Node nodeToExpand, bool expanding)
{
}
public virtual void NodetifyAfterExpand(Node nodeToExpand, bool expanding)
{
}
internal void UpdateChildIds(bool recursive)
{
if (recursive == false && m_IdDirty == 0)
return;
m_IdDirty = 0;
m_nextId = 0;
foreach (Node node in this)
{
node.Id = m_nextId++;
if (node.HasChildren && recursive)
node.Nodes.UpdateChildIds(true);
}
}
protected virtual int GetFieldIndex(string fieldname)
{
return -1;
}
public Node slowGetNodeFromVisibleIndex(int index)
{
int startindex = index;
CommonTools.Tracing.StartTrack(0);
RecursiveNodesEnumerator iterator = new RecursiveNodesEnumerator(m_firstNode, true);
while (iterator.MoveNext())
{
index--;
if (index < 0)
{
CommonTools.Tracing.EndTrack(0, "slowGetNodeFromVisibleIndex({0})", startindex);
return iterator.Current as Node;
}
}
CommonTools.Tracing.EndTrack(0, "slowGetNodeFromVisibleIndex (null)");
return null;
}
public IEnumerator GetEnumerator()
{
return new NodesEnumerator(m_firstNode);
}
/// <summary>
/// TotalRowCount returns the total number of 'visible' nodes. Visible meaning visible for the
/// tree view, This is used to determine the size of the scroll bar
/// If 10 nodes each has 10 children and 9 of them are expanded, then 100 will be returned.
/// </summary>
/// <returns></returns>
public virtual int slowTotalRowCount(bool mustBeVisible)
{
int cnt = 0;
RecursiveNodesEnumerator iterator = new RecursiveNodesEnumerator(this, mustBeVisible);
while (iterator.MoveNext())
cnt++;
//Debug.Assert(cnt == m_totalNodeCount);
return cnt;
}
public virtual int VisibleNodeCount
{
get { return m_totalNodeCount; }
}
/// <summary>
/// Returns the number of pixels required to show all visible nodes
/// </summary>
void EnsureInternalArray()
{
if (m_nodesInternal != null)
{
Debug.Assert(m_nodesInternal.Length == Count, "m_nodesInternal.Length == Count");
return;
}
m_nodesInternal = new Node[Count];
int index = 0;
foreach (Node xnode in this)
m_nodesInternal[index++] = xnode;
}
void ClearInternalArray()
{
m_nodesInternal = null;
}
int m_totalNodeCount = 0;
protected virtual void UpdateNodeCount(int oldvalue, int newvalue)
{
m_totalNodeCount += (newvalue - oldvalue);
}
internal void internalUpdateNodeCount(int oldvalue, int newvalue)
{
UpdateNodeCount(oldvalue, newvalue);
}
internal class NodesEnumerator : IEnumerator
{
Node m_firstNode;
Node m_current = null;
public NodesEnumerator(Node firstNode)
{
m_firstNode = firstNode;
}
public object Current
{
get { return m_current; }
}
public bool MoveNext()
{
if (m_firstNode == null)
return false;
if (m_current == null)
{
m_current = m_firstNode;
return true;
}
m_current = m_current.NextSibling;
return m_current != null;
}
public void Reset()
{
m_current = null;
}
}
internal class RecursiveNodesEnumerator : IEnumerator<Node>
{
class NodeCollIterator : IEnumerator<Node>
{
Node m_firstNode;
Node m_current;
bool m_visible;
public NodeCollIterator(NodeCollection collection, bool mustBeVisible)
{
m_firstNode = collection.FirstNode;
m_visible = mustBeVisible;
}
public Node Current
{
get { return m_current; }
}
object IEnumerator.Current
{
get { return m_current; }
}
public bool MoveNext()
{
if (m_firstNode == null)
return false;
if (m_current == null)
{
m_current = m_firstNode;
return true;
}
if (m_current.HasChildren && m_current.Nodes.FirstNode != null)
{
if (m_visible == false || m_current.Expanded)
{
m_current = m_current.Nodes.FirstNode;
return true;
}
}
if (m_current == m_firstNode)
{
m_firstNode = m_firstNode.NextSibling;
m_current = m_firstNode;
return m_current != null;
}
if (m_current.NextSibling != null)
{
m_current = m_current.NextSibling;
return true;
}
// search up the parent tree
while (m_current.Parent != null)
{
m_current = m_current.Parent;
// back at collection level, now go to next sibling
if (m_current == m_firstNode)
{
m_firstNode = m_firstNode.NextSibling;
m_current = m_firstNode;
return m_current != null;
}
if (m_current.NextSibling != null)
{
m_current = m_current.NextSibling;
return true;
}
}
m_current = null;
return false;
}
public void Reset()
{
m_current = null;
}
public void Dispose()
{
throw new Exception("The method or operation is not implemented.");
}
}
IEnumerator<Node> m_enumerator = null;
public RecursiveNodesEnumerator(Node firstNode, bool mustBeVisible)
{
m_enumerator = new ForwardNodeEnumerator(firstNode, mustBeVisible);
}
public RecursiveNodesEnumerator(NodeCollection collection, bool mustBeVisible)
{
m_enumerator = new NodeCollIterator(collection, mustBeVisible);
}
public Node Current
{
get { return m_enumerator.Current; }
}
object IEnumerator.Current
{
get { return m_enumerator.Current; }
}
public bool MoveNext()
{
return m_enumerator.MoveNext();
}
public void Reset()
{
m_enumerator.Reset();
}
public void Dispose()
{
m_enumerator.Dispose();
}
}
internal class ForwardNodeEnumerator : IEnumerator<Node>
{
Node m_firstNode;
Node m_current;
bool m_visible;
public ForwardNodeEnumerator(Node firstNode, bool mustBeVisible)
{
m_firstNode = firstNode;
m_visible = mustBeVisible;
}
public Node Current
{
get { return m_current; }
}
public void Dispose()
{
}
object IEnumerator.Current
{
get { return m_current; }
}
public bool MoveNext()
{
if (m_firstNode == null)
return false;
if (m_current == null)
{
m_current = m_firstNode;
return true;
}
if (m_current.HasChildren && m_current.Nodes.FirstNode != null)
{
if (m_visible == false || m_current.Expanded)
{
m_current = m_current.Nodes.FirstNode;
return true;
}
}
if (m_current.NextSibling != null)
{
m_current = m_current.NextSibling;
return true;
}
// search up the paret tree until we find a parent with a sibling
while (m_current.Parent != null && m_current.Parent.NextSibling == null)
{
m_current = m_current.Parent;
}
if (m_current.Parent != null && m_current.Parent.NextSibling != null)
{
m_current = m_current.Parent.NextSibling;
return true;
}
m_current = null;
return false;
}
public void Reset()
{
m_current = m_firstNode;
}
}
internal class ReverseNodeEnumerator : IEnumerator<Node>
{
Node m_firstNode;
Node m_current;
bool m_visible;
public ReverseNodeEnumerator(Node firstNode, bool mustBeVisible)
{
m_firstNode = firstNode;
m_visible = mustBeVisible;
}
public Node Current
{
get { return m_current; }
}
public void Dispose()
{
}
object IEnumerator.Current
{
get { return m_current; }
}
public bool MoveNext()
{
if (m_firstNode == null)
return false;
if (m_current == null)
{
m_current = m_firstNode;
return true;
}
if (m_current.PrevSibling != null)
{
m_current = FindNodesBottomLeaf(m_current.PrevSibling, m_visible);
return true;
}
if (m_current.Parent != null)
{
m_current = m_current.Parent;
return true;
}
m_current = null;
return false;
}
public void Reset()
{
m_current = m_firstNode;
}
}
public static Node GetNextNode(Node startingNode, int searchOffset)
{
if (searchOffset == 0)
return startingNode;
if (searchOffset > 0)
{
ForwardNodeEnumerator iterator = new ForwardNodeEnumerator(startingNode, true);
while (searchOffset-- >= 0 && iterator.MoveNext());
return iterator.Current;
}
if (searchOffset < 0)
{
ReverseNodeEnumerator iterator = new ReverseNodeEnumerator(startingNode, true);
while (searchOffset++ <= 0 && iterator.MoveNext());
return iterator.Current;
}
return null;
}
public static IEnumerable ReverseNodeIterator(Node firstNode, Node lastNode, bool mustBeVisible)
{
bool m_done = false;
ReverseNodeEnumerator iterator = new ReverseNodeEnumerator(firstNode, mustBeVisible);
while (iterator.MoveNext())
{
if (m_done)
break;
if (iterator.Current == lastNode)
m_done = true;
yield return iterator.Current;
}
}
public static IEnumerable ForwardNodeIterator(Node firstNode, Node lastNode, bool mustBeVisible)
{
bool m_done = false;
ForwardNodeEnumerator iterator = new ForwardNodeEnumerator(firstNode, mustBeVisible);
while (iterator.MoveNext())
{
if (m_done)
break;
if (iterator.Current == lastNode)
m_done = true;
yield return iterator.Current;
}
}
public static IEnumerable ForwardNodeIterator(Node firstNode, bool mustBeVisible)
{
ForwardNodeEnumerator iterator = new ForwardNodeEnumerator(firstNode, mustBeVisible);
while (iterator.MoveNext())
yield return iterator.Current;
}
public static int GetVisibleNodeIndex(Node node)
{
if (node == null || node.IsVisible() == false || node.GetRootCollection() == null)
return -1;
// Finding the node index is done by searching up the tree and use the visible node count from each node.
// First all previous siblings are searched, then when first sibling in the node collection is reached
// the node is switch to the parent node and the again a search is done up the sibling list.
// This way only higher up the tree are being iterated while nodes at the same level are skipped.
// Worst case scenario is if all nodes are at the same level. In that case the search is a linear search.
// adjust count for the visible count of the current node.
int count = -node.VisibleNodeCount;
while (node != null)
{
count += node.VisibleNodeCount;
if (node.PrevSibling != null)
node = node.PrevSibling;
else
{
node = node.Parent;
if (node != null)
count -= node.VisibleNodeCount - 1; // -1 is for the node itself
}
}
return count;
}
public static Node FindNodesBottomLeaf(Node node, bool mustBeVisible)
{
if (mustBeVisible && node.Expanded == false)
return node;
if (node.HasChildren == false || node.Nodes.LastNode == null)
return node;
node = node.Nodes.LastNode;
return FindNodesBottomLeaf(node, mustBeVisible);
}
}
public class NodesSelection : IEnumerable
{
List<Node> m_nodes = new List<Node>();
Dictionary<Node, int> m_nodesMap = new Dictionary<Node,int>();
public void Clear()
{
m_nodes.Clear();
m_nodesMap.Clear();
}
public IEnumerator GetEnumerator()
{
return m_nodes.GetEnumerator();
}
public Node this[int index]
{
get { return m_nodes[index]; }
}
public int Count
{
get { return m_nodes.Count; }
}
public void Add(Node node)
{
m_nodes.Add(node);
m_nodesMap.Add(node, 0);
}
public void Remove(Node node)
{
m_nodes.Remove(node);
m_nodesMap.Remove(node);
}
public bool Contains(Node node)
{
return m_nodesMap.ContainsKey(node);
}
public IList<Node> GetSortedNodes()