forked from icsharpcode/ILSpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SharpTreeNode.cs
681 lines (571 loc) · 15.3 KB
/
SharpTreeNode.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
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
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.ComponentModel;
using System.Collections.Specialized;
using System.Windows.Media;
namespace ICSharpCode.TreeView
{
public partial class SharpTreeNode : INotifyPropertyChanged
{
SharpTreeNodeCollection modelChildren;
internal SharpTreeNode modelParent;
bool isVisible = true;
void UpdateIsVisible(bool parentIsVisible, bool updateFlattener)
{
bool newIsVisible = parentIsVisible && !isHidden;
if (isVisible != newIsVisible) {
isVisible = newIsVisible;
// invalidate the augmented data
SharpTreeNode node = this;
while (node != null && node.totalListLength >= 0) {
node.totalListLength = -1;
node = node.listParent;
}
// Remember the removed nodes:
List<SharpTreeNode> removedNodes = null;
if (updateFlattener && !newIsVisible) {
removedNodes = VisibleDescendantsAndSelf().ToList();
}
// also update the model children:
UpdateChildIsVisible(false);
// Validate our invariants:
if (updateFlattener)
CheckRootInvariants();
// Tell the flattener about the removed nodes:
if (removedNodes != null) {
var flattener = GetListRoot().treeFlattener;
if (flattener != null) {
flattener.NodesRemoved(GetVisibleIndexForNode(this), removedNodes);
foreach (var n in removedNodes)
n.OnIsVisibleChanged();
}
}
// Tell the flattener about the new nodes:
if (updateFlattener && newIsVisible) {
var flattener = GetListRoot().treeFlattener;
if (flattener != null) {
flattener.NodesInserted(GetVisibleIndexForNode(this), VisibleDescendantsAndSelf());
foreach (var n in VisibleDescendantsAndSelf())
n.OnIsVisibleChanged();
}
}
}
}
protected virtual void OnIsVisibleChanged() {}
void UpdateChildIsVisible(bool updateFlattener)
{
if (modelChildren != null && modelChildren.Count > 0) {
bool showChildren = isVisible && isExpanded;
foreach (SharpTreeNode child in modelChildren) {
child.UpdateIsVisible(showChildren, updateFlattener);
}
}
}
#region Main
public SharpTreeNode()
{
}
public SharpTreeNodeCollection Children {
get {
if (modelChildren == null)
modelChildren = new SharpTreeNodeCollection(this);
return modelChildren;
}
}
public SharpTreeNode Parent {
get { return modelParent; }
}
public virtual object Text
{
get { return null; }
}
public virtual Brush Foreground {
get { return SystemColors.WindowTextBrush; }
}
public virtual object Icon
{
get { return null; }
}
public virtual object ToolTip
{
get { return null; }
}
public int Level
{
get { return Parent != null ? Parent.Level + 1 : 0; }
}
public bool IsRoot
{
get { return Parent == null; }
}
bool isHidden;
public bool IsHidden
{
get { return isHidden; }
set {
if (isHidden != value) {
isHidden = value;
if (modelParent != null)
UpdateIsVisible(modelParent.isVisible && modelParent.isExpanded, true);
RaisePropertyChanged("IsHidden");
if (Parent != null)
Parent.RaisePropertyChanged("ShowExpander");
}
}
}
/// <summary>
/// Return true when this node is not hidden and when all parent nodes are expanded and not hidden.
/// </summary>
public bool IsVisible {
get { return isVisible; }
}
bool isSelected;
public bool IsSelected {
get { return isSelected; }
set {
if (isSelected != value) {
isSelected = value;
RaisePropertyChanged("IsSelected");
}
}
}
#endregion
#region OnChildrenChanged
internal protected virtual void OnChildrenChanged(NotifyCollectionChangedEventArgs e)
{
if (e.OldItems != null) {
foreach (SharpTreeNode node in e.OldItems) {
Debug.Assert(node.modelParent == this);
node.modelParent = null;
Debug.WriteLine("Removing {0} from {1}", node, this);
SharpTreeNode removeEnd = node;
while (removeEnd.modelChildren != null && removeEnd.modelChildren.Count > 0)
removeEnd = removeEnd.modelChildren.Last();
List<SharpTreeNode> removedNodes = null;
int visibleIndexOfRemoval = 0;
if (node.isVisible) {
visibleIndexOfRemoval = GetVisibleIndexForNode(node);
removedNodes = node.VisibleDescendantsAndSelf().ToList();
}
RemoveNodes(node, removeEnd);
if (removedNodes != null) {
var flattener = GetListRoot().treeFlattener;
if (flattener != null) {
flattener.NodesRemoved(visibleIndexOfRemoval, removedNodes);
}
}
}
}
if (e.NewItems != null) {
SharpTreeNode insertionPos;
if (e.NewStartingIndex == 0)
insertionPos = null;
else
insertionPos = modelChildren[e.NewStartingIndex - 1];
foreach (SharpTreeNode node in e.NewItems) {
Debug.Assert(node.modelParent == null);
node.modelParent = this;
node.UpdateIsVisible(isVisible && isExpanded, false);
//Debug.WriteLine("Inserting {0} after {1}", node, insertionPos);
while (insertionPos != null && insertionPos.modelChildren != null && insertionPos.modelChildren.Count > 0) {
insertionPos = insertionPos.modelChildren.Last();
}
InsertNodeAfter(insertionPos ?? this, node);
insertionPos = node;
if (node.isVisible) {
var flattener = GetListRoot().treeFlattener;
if (flattener != null) {
flattener.NodesInserted(GetVisibleIndexForNode(node), node.VisibleDescendantsAndSelf());
}
}
}
}
RaisePropertyChanged("ShowExpander");
RaiseIsLastChangedIfNeeded(e);
}
#endregion
#region Expanding / LazyLoading
public virtual object ExpandedIcon
{
get { return Icon; }
}
public virtual bool ShowExpander
{
get { return LazyLoading || Children.Any(c => !c.isHidden); }
}
bool isExpanded;
public bool IsExpanded
{
get { return isExpanded; }
set
{
if (isExpanded != value) {
isExpanded = value;
if (isExpanded) {
EnsureLazyChildren();
OnExpanding();
} else {
OnCollapsing();
}
UpdateChildIsVisible(true);
RaisePropertyChanged("IsExpanded");
}
}
}
protected virtual void OnExpanding() {}
protected virtual void OnCollapsing() {}
bool lazyLoading;
public bool LazyLoading
{
get { return lazyLoading; }
set
{
lazyLoading = value;
if (lazyLoading) {
IsExpanded = false;
if (canExpandRecursively) {
canExpandRecursively = false;
RaisePropertyChanged("CanExpandRecursively");
}
}
RaisePropertyChanged("LazyLoading");
RaisePropertyChanged("ShowExpander");
}
}
bool canExpandRecursively = true;
/// <summary>
/// Gets whether this node can be expanded recursively.
/// If not overridden, this property returns false if the node is using lazy-loading, and true otherwise.
/// </summary>
public virtual bool CanExpandRecursively {
get { return canExpandRecursively; }
}
public virtual bool ShowIcon
{
get { return Icon != null; }
}
protected virtual void LoadChildren()
{
throw new NotSupportedException(GetType().Name + " does not support lazy loading");
}
/// <summary>
/// Ensures the children were initialized (loads children if lazy loading is enabled)
/// </summary>
public void EnsureLazyChildren()
{
if (LazyLoading) {
LazyLoading = false;
LoadChildren();
}
}
#endregion
#region Ancestors / Descendants
public IEnumerable<SharpTreeNode> Descendants()
{
return TreeTraversal.PreOrder(this.Children, n => n.Children);
}
public IEnumerable<SharpTreeNode> DescendantsAndSelf()
{
return TreeTraversal.PreOrder(this, n => n.Children);
}
internal IEnumerable<SharpTreeNode> VisibleDescendants()
{
return TreeTraversal.PreOrder(this.Children.Where(c => c.isVisible), n => n.Children.Where(c => c.isVisible));
}
internal IEnumerable<SharpTreeNode> VisibleDescendantsAndSelf()
{
return TreeTraversal.PreOrder(this, n => n.Children.Where(c => c.isVisible));
}
public IEnumerable<SharpTreeNode> Ancestors()
{
var node = this;
while (node.Parent != null) {
yield return node.Parent;
node = node.Parent;
}
}
public IEnumerable<SharpTreeNode> AncestorsAndSelf()
{
yield return this;
foreach (var node in Ancestors()) {
yield return node;
}
}
#endregion
#region Editing
public virtual bool IsEditable
{
get { return false; }
}
bool isEditing;
public bool IsEditing
{
get { return isEditing; }
set
{
if (isEditing != value) {
isEditing = value;
RaisePropertyChanged("IsEditing");
}
}
}
public virtual string LoadEditText()
{
return null;
}
public virtual bool SaveEditText(string value)
{
return true;
}
#endregion
#region Checkboxes
public virtual bool IsCheckable {
get { return false; }
}
bool? isChecked;
public bool? IsChecked {
get { return isChecked; }
set {
SetIsChecked(value, true);
}
}
void SetIsChecked(bool? value, bool update)
{
if (isChecked != value) {
isChecked = value;
if (update) {
if (IsChecked != null) {
foreach (var child in Descendants()) {
if (child.IsCheckable) {
child.SetIsChecked(IsChecked, false);
}
}
}
foreach (var parent in Ancestors()) {
if (parent.IsCheckable) {
if (!parent.TryValueForIsChecked(true)) {
if (!parent.TryValueForIsChecked(false)) {
parent.SetIsChecked(null, false);
}
}
}
}
}
RaisePropertyChanged("IsChecked");
}
}
bool TryValueForIsChecked(bool? value)
{
if (Children.Where(n => n.IsCheckable).All(n => n.IsChecked == value)) {
SetIsChecked(value, false);
return true;
}
return false;
}
#endregion
#region Cut / Copy / Paste / Delete
public bool IsCut { get { return false; } }
/*
static List<SharpTreeNode> cuttedNodes = new List<SharpTreeNode>();
static IDataObject cuttedData;
static EventHandler requerySuggestedHandler; // for weak event
static void StartCuttedDataWatcher()
{
requerySuggestedHandler = new EventHandler(CommandManager_RequerySuggested);
CommandManager.RequerySuggested += requerySuggestedHandler;
}
static void CommandManager_RequerySuggested(object sender, EventArgs e)
{
if (cuttedData != null && !Clipboard.IsCurrent(cuttedData)) {
ClearCuttedData();
}
}
static void ClearCuttedData()
{
foreach (var node in cuttedNodes) {
node.IsCut = false;
}
cuttedNodes.Clear();
cuttedData = null;
}
//static public IEnumerable<SharpTreeNode> PurifyNodes(IEnumerable<SharpTreeNode> nodes)
//{
// var list = nodes.ToList();
// var array = list.ToArray();
// foreach (var node1 in array) {
// foreach (var node2 in array) {
// if (node1.Descendants().Contains(node2)) {
// list.Remove(node2);
// }
// }
// }
// return list;
//}
bool isCut;
public bool IsCut
{
get { return isCut; }
private set
{
isCut = value;
RaisePropertyChanged("IsCut");
}
}
internal bool InternalCanCut()
{
return InternalCanCopy() && InternalCanDelete();
}
internal void InternalCut()
{
ClearCuttedData();
cuttedData = Copy(ActiveNodesArray);
Clipboard.SetDataObject(cuttedData);
foreach (var node in ActiveNodes) {
node.IsCut = true;
cuttedNodes.Add(node);
}
}
internal bool InternalCanCopy()
{
return CanCopy(ActiveNodesArray);
}
internal void InternalCopy()
{
Clipboard.SetDataObject(Copy(ActiveNodesArray));
}
internal bool InternalCanPaste()
{
return CanPaste(Clipboard.GetDataObject());
}
internal void InternalPaste()
{
Paste(Clipboard.GetDataObject());
if (cuttedData != null) {
DeleteCore(cuttedNodes.ToArray());
ClearCuttedData();
}
}
*/
public virtual bool CanDelete()
{
return false;
}
public virtual void Delete()
{
throw new NotSupportedException(GetType().Name + " does not support deletion");
}
public virtual void DeleteCore()
{
throw new NotSupportedException(GetType().Name + " does not support deletion");
}
public virtual IDataObject Copy(SharpTreeNode[] nodes)
{
throw new NotSupportedException(GetType().Name + " does not support copy/paste or drag'n'drop");
}
/*
public virtual bool CanCopy(SharpTreeNode[] nodes)
{
return false;
}
public virtual bool CanPaste(IDataObject data)
{
return false;
}
public virtual void Paste(IDataObject data)
{
EnsureLazyChildren();
Drop(data, Children.Count, DropEffect.Copy);
}
*/
#endregion
#region Drag and Drop
public virtual bool CanDrag(SharpTreeNode[] nodes)
{
return false;
}
public virtual void StartDrag(DependencyObject dragSource, SharpTreeNode[] nodes)
{
DragDropEffects effects = DragDropEffects.All;
if (!nodes.All(n => n.CanDelete()))
effects &= ~DragDropEffects.Move;
DragDropEffects result = DragDrop.DoDragDrop(dragSource, Copy(nodes), effects);
if (result == DragDropEffects.Move) {
foreach (SharpTreeNode node in nodes)
node.DeleteCore();
}
}
public virtual bool CanDrop(DragEventArgs e, int index)
{
return false;
}
internal void InternalDrop(DragEventArgs e, int index)
{
if (LazyLoading) {
EnsureLazyChildren();
index = Children.Count;
}
Drop(e, index);
}
public virtual void Drop(DragEventArgs e, int index)
{
throw new NotSupportedException(GetType().Name + " does not support Drop()");
}
#endregion
#region IsLast (for TreeView lines)
public bool IsLast
{
get
{
return Parent == null ||
Parent.Children[Parent.Children.Count - 1] == this;
}
}
void RaiseIsLastChangedIfNeeded(NotifyCollectionChangedEventArgs e)
{
switch (e.Action) {
case NotifyCollectionChangedAction.Add:
if (e.NewStartingIndex == Children.Count - 1) {
if (Children.Count > 1) {
Children[Children.Count - 2].RaisePropertyChanged("IsLast");
}
Children[Children.Count - 1].RaisePropertyChanged("IsLast");
}
break;
case NotifyCollectionChangedAction.Remove:
if (e.OldStartingIndex == Children.Count) {
if (Children.Count > 0) {
Children[Children.Count - 1].RaisePropertyChanged("IsLast");
}
}
break;
}
}
#endregion
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string name)
{
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
#endregion
/// <summary>
/// Gets called when the item is double-clicked.
/// </summary>
public virtual void ActivateItem(RoutedEventArgs e)
{
}
public override string ToString()
{
// used for keyboard navigation
object text = this.Text;
return text != null ? text.ToString() : string.Empty;
}
}
}