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
|
using System;
using System.Collections.Generic;
using System.Text;
namespace Pulse
{
/// <summary>
///
/// </summary>
public enum ListChangedType
{
/// <summary>
///
/// </summary>
Unknown = 0,
/// <summary>
///
/// </summary>
Inserted = 1,
/// <summary>
///
/// </summary>
Updated = 2,
/// <summary>
///
/// </summary>
Deleted = 3,
/// <summary>
///
/// </summary>
Cleared = 4
}
/// <summary>
///
/// </summary>
public sealed class ListEventArgs : EventArgs
{
/// <summary>
///
/// </summary>
/// <param name="ListChangedType"></param>
/// <param name="Index"></param>
/// <param name="Reference"></param>
internal ListEventArgs(ListChangedType ListChangedType, int Index, object Reference)
{
this.DataStore = new object[3];
this.DataStore[0] = ListChangedType;
this.DataStore[1] = Index;
this.DataStore[2] = Reference;
}
/// <summary>
/// Enthält die durchgeführte Aktion des Ereignisses
/// </summary>
public ListChangedType ListChangedType
{
get
{
return (ListChangedType)this.DataStore[0];
}
}
/// <summary>
/// Enthält den Index des zum Event gehörenden Elements
/// -1 bei Clear
/// </summary>
public int Index
{
get
{
return Pulse.Converter.ToInt(this.DataStore[1]);
}
}
/// <summary>
/// Enthält die Referenz auf das bei der Änderung betroffene Objekt
/// </summary>
public object Reference
{
get
{
return this.DataStore[2];
}
}
/// <summary>
/// Enthält die Daten
/// </summary>
private object[] DataStore;
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
public sealed class WatchedList<T> : System.Collections.Generic.List<T>
{
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public delegate void ChangedEventHandler(object sender, ListEventArgs e);
/// <summary>
///
/// </summary>
public event ChangedEventHandler Changed;
/// <summary>
/// Invoke the Changed event; called whenever list changes
/// </summary>
/// <param name="e"></param>
private void OnChanged(ListEventArgs e)
{
if (Changed != null) Changed(this, e);
}
/// <summary>
/// Override some of the methods that can change the list;
/// invoke event after each
/// </summary>
/// <param name="item"></param>
public new void Add(T item)
{
base.Add(item);
this.OnChanged(new ListEventArgs(ListChangedType.Inserted, (base.Count - 1), item));
return;
}
/// <summary>
///
/// </summary>
public new void Clear()
{
base.Clear();
this.OnChanged(new ListEventArgs(ListChangedType.Cleared,-1, null));
}
/// <summary>
///
/// </summary>
/// <param name="Index"></param>
/// <returns></returns>
public new T this[int Index]
{
get
{
return base[Index];
}
set
{
base[Index] = value;
this.OnChanged(new ListEventArgs(ListChangedType.Updated, Index, value));
}
}
/// <summary>
///
/// </summary>
/// <typeparam name="TOutput"></typeparam>
/// <param name="converter"></param>
/// <returns></returns>
public new Pulse.WatchedList<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter)
{
Pulse.WatchedList<TOutput> Output = new Pulse.WatchedList<TOutput>();
foreach (TOutput Element in base.ConvertAll<TOutput>(converter)) Output.Add(Element);
return Output;
}
/// <summary>
///
/// </summary>
/// <param name="item"></param>
public new void Remove(T item)
{
int Index = base.IndexOf(item);
base.RemoveAt(Index);
this.OnChanged(new ListEventArgs(ListChangedType.Deleted, Index, item));
}
/// <summary>
///
/// </summary>
/// <param name="Index"></param>
public new void RemoveAt(int Index)
{
T Temp = this[Index];
base.RemoveAt(Index);
this.OnChanged(new ListEventArgs(ListChangedType.Deleted, Index, Temp));
}
/// <summary>
///
/// </summary>
/// <param name="Index"></param>
/// <param name="item"></param>
public new void Insert(int Index, T item)
{
base.Insert(Index, item);
this.OnChanged(new ListEventArgs(ListChangedType.Inserted, Index, item));
}
}
}
|