forked from icsharpcode/ILSpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SharpTreeNodeCollection.cs
208 lines (184 loc) · 5.24 KB
/
SharpTreeNodeCollection.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
// 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.Collections.Specialized;
namespace ICSharpCode.TreeView
{
/// <summary>
/// Collection that validates that inserted nodes do not have another parent.
/// </summary>
public sealed class SharpTreeNodeCollection : IList<SharpTreeNode>, INotifyCollectionChanged
{
readonly SharpTreeNode parent;
List<SharpTreeNode> list = new List<SharpTreeNode>();
bool isRaisingEvent;
public SharpTreeNodeCollection(SharpTreeNode parent)
{
this.parent = parent;
}
public event NotifyCollectionChangedEventHandler CollectionChanged;
void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
Debug.Assert(!isRaisingEvent);
isRaisingEvent = true;
try {
parent.OnChildrenChanged(e);
if (CollectionChanged != null)
CollectionChanged(this, e);
} finally {
isRaisingEvent = false;
}
}
void ThrowOnReentrancy()
{
if (isRaisingEvent)
throw new InvalidOperationException();
}
void ThrowIfValueIsNullOrHasParent(SharpTreeNode node)
{
if (node == null)
throw new ArgumentNullException("node");
if (node.modelParent != null)
throw new ArgumentException("The node already has a parent", "node");
}
public SharpTreeNode this[int index] {
get {
return list[index];
}
set {
ThrowOnReentrancy();
var oldItem = list[index];
if (oldItem == value)
return;
ThrowIfValueIsNullOrHasParent(value);
list[index] = value;
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, value, oldItem, index));
}
}
public int Count {
get { return list.Count; }
}
bool ICollection<SharpTreeNode>.IsReadOnly {
get { return false; }
}
public int IndexOf(SharpTreeNode node)
{
if (node == null || node.modelParent != parent)
return -1;
else
return list.IndexOf(node);
}
public void Insert(int index, SharpTreeNode node)
{
ThrowOnReentrancy();
ThrowIfValueIsNullOrHasParent(node);
list.Insert(index, node);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, node, index));
}
public void InsertRange(int index, IEnumerable<SharpTreeNode> nodes)
{
if (nodes == null)
throw new ArgumentNullException("nodes");
ThrowOnReentrancy();
List<SharpTreeNode> newNodes = nodes.ToList();
if (newNodes.Count == 0)
return;
foreach (SharpTreeNode node in newNodes) {
ThrowIfValueIsNullOrHasParent(node);
}
list.InsertRange(index, newNodes);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, newNodes, index));
}
public void RemoveAt(int index)
{
ThrowOnReentrancy();
var oldItem = list[index];
list.RemoveAt(index);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, oldItem, index));
}
public void RemoveRange(int index, int count)
{
ThrowOnReentrancy();
if (count == 0)
return;
var oldItems = list.GetRange(index, count);
list.RemoveRange(index, count);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, oldItems, index));
}
public void Add(SharpTreeNode node)
{
ThrowOnReentrancy();
ThrowIfValueIsNullOrHasParent(node);
list.Add(node);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, node, list.Count - 1));
}
public void AddRange(IEnumerable<SharpTreeNode> nodes)
{
InsertRange(this.Count, nodes);
}
public void Clear()
{
ThrowOnReentrancy();
var oldList = list;
list = new List<SharpTreeNode>();
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, oldList, 0));
}
public bool Contains(SharpTreeNode node)
{
return IndexOf(node) >= 0;
}
public void CopyTo(SharpTreeNode[] array, int arrayIndex)
{
list.CopyTo(array, arrayIndex);
}
public bool Remove(SharpTreeNode item)
{
int pos = IndexOf(item);
if (pos >= 0) {
RemoveAt(pos);
return true;
} else {
return false;
}
}
public IEnumerator<SharpTreeNode> GetEnumerator()
{
return list.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return list.GetEnumerator();
}
public void RemoveAll(Predicate<SharpTreeNode> match)
{
if (match == null)
throw new ArgumentNullException("match");
ThrowOnReentrancy();
int firstToRemove = 0;
for (int i = 0; i < list.Count; i++) {
bool removeNode;
isRaisingEvent = true;
try {
removeNode = match(list[i]);
} finally {
isRaisingEvent = false;
}
if (!removeNode) {
if (firstToRemove < i) {
RemoveRange(firstToRemove, i - firstToRemove);
i = firstToRemove - 1;
} else {
firstToRemove = i + 1;
}
Debug.Assert(firstToRemove == i + 1);
}
}
if (firstToRemove < list.Count) {
RemoveRange(firstToRemove, list.Count - firstToRemove);
}
}
}
}