-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathWrappedCollection.cs
More file actions
321 lines (266 loc) · 11.3 KB
/
Copy pathWrappedCollection.cs
File metadata and controls
321 lines (266 loc) · 11.3 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using JetBrains.Annotations;
namespace FirstFloor.ModernUI {
public class WrappedCollection {
public static WrappedCollection<TSource, TWrapper> Create<TSource, TWrapper>(IReadOnlyList<TSource> collection,
Func<TSource, TWrapper> wrap) {
return new DelegateWrappedCollection<TSource, TWrapper>(collection, wrap);
}
}
public class DelegateWrappedCollection<TSource, TWrapper> : WrappedCollection<TSource, TWrapper> {
private readonly Func<TSource, TWrapper> _wrap;
public DelegateWrappedCollection(IReadOnlyList<TSource> collection, Func<TSource, TWrapper> wrap) : base(collection) {
_wrap = wrap;
}
protected override TWrapper Wrap(TSource source) {
return _wrap(source);
}
}
public abstract class WrappedCollection<TSource, TWrapper> : WrappedCollection, IList<TWrapper>, IList, IReadOnlyList<TWrapper>, INotifyCollectionChanged,
INotifyPropertyChanged {
[NotNull]
private readonly IReadOnlyList<TSource> _source;
[CanBeNull]
private List<TWrapper> _items;
protected WrappedCollection([NotNull] IReadOnlyList<TSource> collection) {
_source = collection ?? throw new ArgumentNullException(nameof(collection));
var notify = collection as INotifyCollectionChanged;
if (notify != null) {
WeakEventManager<INotifyCollectionChanged, NotifyCollectionChangedEventArgs>.AddHandler(notify, nameof(notify.CollectionChanged),
OnItemsSourceCollectionChanged);
}
}
[NotNull]
private List<TWrapper> Rebuild() {
return _source.Select(Wrap).ToList();
}
private void Reset() {
Debug.WriteLine(@"WrappedCollection.Reset()");
_items = null;
OnCountAndIndexerChanged();
OnCollectionReset();
}
private int IndexOf(TSource value) {
for (var i = 0; i < _source.Count; i++) {
if (Equals(_source[i], value)) return i;
}
return -1;
}
public void Refresh(TSource source) {
var index = IndexOf(source);
if (index == -1) return;
var items = _items;
if (items == null) {
Reset();
return;
}
var oldItem = items[index];
var newItem = Wrap(source);
if (!Equals(oldItem, newItem)) {
items[index] = newItem;
OnIndexerChanged();
OnCollectionChanged(NotifyCollectionChangedAction.Replace, oldItem, newItem, index);
}
}
private void OnItemsSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
var items = _items;
if (items == null) {
Reset();
return;
}
switch (e.Action) {
case NotifyCollectionChangedAction.Add:
if (e.NewItems == null || e.NewItems.Count != 1 || e.OldItems != null && e.OldItems.Count > 0) {
Reset();
} else {
var item = Wrap((TSource)e.NewItems[0]);
items.Insert(e.NewStartingIndex, item);
OnCountAndIndexerChanged();
OnCollectionChanged(NotifyCollectionChangedAction.Add, item, e.NewStartingIndex);
}
break;
case NotifyCollectionChangedAction.Remove:
if (e.OldItems == null || e.OldItems.Count != 1 || e.NewItems != null && e.NewItems.Count > 0 ||
e.OldStartingIndex >= items.Count) {
Reset();
} else {
var item = items[e.OldStartingIndex];
items.RemoveAt(e.OldStartingIndex);
OnCountAndIndexerChanged();
OnCollectionChanged(NotifyCollectionChangedAction.Remove, item, e.OldStartingIndex);
}
break;
case NotifyCollectionChangedAction.Replace:
if (e.OldItems != null && e.OldItems.Count != 1 || e.NewItems == null || e.NewItems.Count != 1 ||
e.OldStartingIndex >= items.Count) {
Reset();
} else {
var oldItem = items[e.OldStartingIndex];
var newItem = Wrap((TSource)e.NewItems[0]);
if (!Equals(oldItem, newItem)) {
items[e.OldStartingIndex] = newItem;
OnIndexerChanged();
OnCollectionChanged(NotifyCollectionChangedAction.Replace, oldItem, newItem, e.OldStartingIndex);
}
}
break;
case NotifyCollectionChangedAction.Move:
if (e.OldItems != null && e.OldItems.Count != 1 || e.NewItems != null && e.NewItems.Count != 1 ||
e.OldStartingIndex >= items.Count) {
Reset();
} else {
var item = items[e.OldStartingIndex];
items.RemoveAt(e.OldStartingIndex);
items.Insert(e.NewStartingIndex, item);
OnIndexerChanged();
OnCollectionChanged(NotifyCollectionChangedAction.Move, item, e.NewStartingIndex, e.OldStartingIndex);
}
break;
case NotifyCollectionChangedAction.Reset:
Reset();
break;
default:
throw new ArgumentOutOfRangeException();
}
}
protected abstract TWrapper Wrap(TSource source);
#region IList, IReadOnlyList methods
public IEnumerator<TWrapper> GetEnumerator() {
if (_items == null) _items = Rebuild();
return _items.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
public void Add(TWrapper item) {
throw new NotSupportedException();
}
int IList.Add(object value) {
throw new NotSupportedException();
}
private static bool IsCompatibleObject(object value) {
return value is TWrapper || value == null && default(TWrapper) == null;
}
bool IList.Contains(object value) {
return IsCompatibleObject(value) && Contains((TWrapper)value);
}
public void Clear() {
throw new NotSupportedException();
}
int IList.IndexOf(object value) {
return IsCompatibleObject(value) ? IndexOf((TWrapper)value) : -1;
}
void IList.Insert(int index, object value) {
throw new NotSupportedException();
}
void IList.Remove(object value) {
throw new NotSupportedException();
}
public bool Contains(TWrapper item) {
if (_items == null) _items = Rebuild();
return _items.Contains(item);
}
public void CopyTo(TWrapper[] array, int arrayIndex) {
if (_items == null) _items = Rebuild();
_items.CopyTo(array, arrayIndex);
}
public bool Remove(TWrapper item) {
throw new NotSupportedException();
}
public void CopyTo(Array array, int index) {
if (_items == null) _items = Rebuild();
var wrappers = array as TWrapper[];
if (wrappers != null) {
_items.CopyTo(wrappers, index);
} else {
var objects = (object[])array;
var count = _items.Count;
for (int i = 0; i < count; i++) {
objects[index++] = _items[i];
}
}
}
public int Count => _source.Count;
[NonSerialized]
private object _syncRoot;
public object SyncRoot {
get {
if (_syncRoot == null) {
ICollection c = _items;
if (c != null) {
_syncRoot = c.SyncRoot;
} else {
System.Threading.Interlocked.CompareExchange<object>(ref _syncRoot, new object(), null);
}
}
return _syncRoot;
}
}
public bool IsSynchronized => false;
public bool IsReadOnly => true;
public bool IsFixedSize => false;
public int IndexOf(TWrapper item) {
if (_items == null) _items = Rebuild();
return _items.IndexOf(item);
}
public void Insert(int index, TWrapper item) {
throw new NotSupportedException();
}
public void RemoveAt(int index) {
throw new NotSupportedException();
}
object IList.this[int index] {
get => this[index];
set => throw new NotSupportedException();
}
public TWrapper this[int index] {
get {
if (_items == null) _items = Rebuild();
return _items[index];
}
set => throw new NotSupportedException();
}
#endregion
#region INotifyCollectionChanged, INotifyPropertyChanged events
public event NotifyCollectionChangedEventHandler CollectionChanged;
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region On-Methods
private void OnIndexerChanged() {
OnPropertyChanged(@"Item[]");
}
private void OnCountAndIndexerChanged() {
OnPropertyChanged(nameof(Count));
OnIndexerChanged();
}
protected void OnPropertyChanged(PropertyChangedEventArgs e) {
PropertyChanged?.Invoke(this, e);
}
protected void OnCollectionChanged(NotifyCollectionChangedEventArgs e) {
CollectionChanged?.Invoke(this, e);
}
private void OnPropertyChanged(string propertyName) {
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
private void OnCollectionChanged(NotifyCollectionChangedAction action, object item, int index) {
OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, item, index));
}
private void OnCollectionChanged(NotifyCollectionChangedAction action, object item, int index, int oldIndex) {
OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, item, index, oldIndex));
}
private void OnCollectionChanged(NotifyCollectionChangedAction action, object oldItem, object newItem, int index) {
OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, newItem, oldItem, index));
}
private void OnCollectionReset() {
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
#endregion Private Methods
}
}