forked from dnSpy/ICSharpCode.TreeView
-
Notifications
You must be signed in to change notification settings - Fork 6
/
TreeFlattener.cs
158 lines (132 loc) · 4.05 KB
/
TreeFlattener.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
// 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;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
namespace ICSharpCode.TreeView
{
sealed class TreeFlattener : IList<SharpTreeNode>, INotifyCollectionChanged
{
/// <summary>
/// The root node of the flat list tree.
/// Tjis is not necessarily the root of the model!
/// </summary>
internal SharpTreeNode root;
readonly bool includeRoot;
public TreeFlattener(SharpTreeNode modelRoot, bool includeRoot)
{
this.root = modelRoot;
while (root.listParent != null)
root = root.listParent;
root.treeFlattener = this;
this.includeRoot = includeRoot;
}
public event NotifyCollectionChangedEventHandler CollectionChanged;
Dictionary<int, SharpTreeNode> nodeCache = new Dictionary<int, SharpTreeNode>();
void RaiseCollectionChanged(NotifyCollectionChangedEventArgs e)
{
nodeCache.Clear();
if (CollectionChanged != null)
CollectionChanged(this, e);
}
public void NodesInserted(int index, List<SharpTreeNode> nodes) {
if (!includeRoot) index--;
RaiseCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, nodes, index));
}
public void NodesInserted(int index, IEnumerable<SharpTreeNode> nodes)
{
if (!includeRoot) index--;
foreach (SharpTreeNode node in nodes) {
RaiseCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, node, index++));
}
}
public void NodesRemoved(int index, List<SharpTreeNode> nodes) {
if (!includeRoot) index--;
RaiseCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, nodes, index));
}
public void NodesRemoved(int index, IEnumerable<SharpTreeNode> nodes) {
if (!includeRoot) index--;
foreach (SharpTreeNode node in nodes) {
RaiseCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, node, index));
}
}
public void Stop()
{
Debug.Assert(root.treeFlattener == this);
root.treeFlattener = null;
}
public SharpTreeNode this[int index] {
get {
if (index < 0 || index >= this.Count)
throw new ArgumentOutOfRangeException();
SharpTreeNode node;
if (nodeCache.TryGetValue(index, out node))
return node;
node = SharpTreeNode.GetNodeByVisibleIndex(root, includeRoot ? index : index + 1);
nodeCache[index] = node;
return node;
}
set {
throw new NotSupportedException();
}
}
public int Count {
get {
return includeRoot ? root.GetTotalListLength() : root.GetTotalListLength() - 1;
}
}
public int IndexOf(SharpTreeNode node)
{
if (node != null && node.IsVisible && node.GetListRoot() == root) {
if (includeRoot)
return SharpTreeNode.GetVisibleIndexForNode(node);
else
return SharpTreeNode.GetVisibleIndexForNode(node) - 1;
} else {
return -1;
}
}
bool ICollection<SharpTreeNode>.IsReadOnly {
get { return true; }
}
void IList<SharpTreeNode>.Insert(int index, SharpTreeNode item)
{
throw new NotSupportedException();
}
void IList<SharpTreeNode>.RemoveAt(int index)
{
throw new NotSupportedException();
}
void ICollection<SharpTreeNode>.Add(SharpTreeNode item)
{
throw new NotSupportedException();
}
void ICollection<SharpTreeNode>.Clear()
{
throw new NotSupportedException();
}
public bool Contains(SharpTreeNode item)
{
return IndexOf(item) >= 0;
}
public void CopyTo(SharpTreeNode[] array, int arrayIndex)
{
for (int i = 0; i < this.Count; i++) {
array[arrayIndex++] = this[i];
}
}
bool ICollection<SharpTreeNode>.Remove(SharpTreeNode item)
{
throw new NotSupportedException();
}
public IEnumerator<SharpTreeNode> GetEnumerator()
{
for (int i = 0; i < this.Count; i++) {
yield return this[i];
}
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}