This repository has been archived by the owner on Nov 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathTreeFlattener.cs
165 lines (139 loc) · 3.74 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
159
160
161
162
163
164
165
// 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.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace ICSharpCode.TreeView
{
sealed class TreeFlattener : IList, 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;
readonly object syncRoot = new object();
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, IEnumerable<SharpTreeNode> nodes)
{
if (!includeRoot) index--;
foreach (SharpTreeNode node in nodes) {
RaiseCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, node, 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 object 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(object item)
{
SharpTreeNode node = item as SharpTreeNode;
if (node != null && node.IsVisible && node.GetListRoot() == root) {
if (includeRoot)
return SharpTreeNode.GetVisibleIndexForNode(node);
else
return SharpTreeNode.GetVisibleIndexForNode(node) - 1;
} else {
return -1;
}
}
bool IList.IsReadOnly {
get { return true; }
}
bool IList.IsFixedSize {
get { return false; }
}
bool ICollection.IsSynchronized {
get { return false; }
}
object ICollection.SyncRoot {
get {
return syncRoot;
}
}
void IList.Insert(int index, object item)
{
throw new NotSupportedException();
}
void IList.RemoveAt(int index)
{
throw new NotSupportedException();
}
int IList.Add(object item)
{
throw new NotSupportedException();
}
void IList.Clear()
{
throw new NotSupportedException();
}
public bool Contains(object item)
{
return IndexOf(item) >= 0;
}
public void CopyTo(Array array, int arrayIndex)
{
foreach (object item in this)
array.SetValue(item, arrayIndex++);
}
void IList.Remove(object item)
{
throw new NotSupportedException();
}
public IEnumerator GetEnumerator()
{
for (int i = 0; i < this.Count; i++) {
yield return this[i];
}
}
}
}