18 using System.Collections.Generic;
20 namespace sage.ew.formul.ButtonTittle.Utils
22 internal class ListModificationEventArgs : ListRangeEventArgs
24 private readonly ListModification modification;
26 public ListModificationEventArgs(ListModification modification,
int startIndex,
int count)
27 : base(startIndex, count)
29 this.modification = modification;
32 public ListModification Modification
34 get {
return modification; }
38 internal class ListItemEventArgs : EventArgs
40 private readonly
int itemIndex;
42 public ListItemEventArgs(
int itemIndex)
44 this.itemIndex = itemIndex;
49 get {
return itemIndex; }
53 internal class ListRangeEventArgs : EventArgs
55 private readonly
int count;
56 private readonly
int startIndex;
58 public ListRangeEventArgs(
int startIndex,
int count)
60 this.startIndex = startIndex;
66 get {
return startIndex; }
75 internal enum ListModification
104 internal class ListWithEvents<T> : List<T>, IList<T>, IList
106 private readonly
object syncRoot =
new object();
107 private bool suppressEvents;
109 public ListWithEvents()
113 public ListWithEvents(IEnumerable<T> collection)
118 public ListWithEvents(
int capacity)
123 protected bool EventsSuppressed
125 get {
return suppressEvents; }
128 #region IList Members 130 public object SyncRoot
132 get {
return syncRoot; }
135 int IList.Add(
object value)
147 #region IList<T> Members 149 public new virtual T
this[
int index]
151 get {
return base[index]; }
157 if (base[index] != null)
159 equal = base[index].Equals(value);
161 else if (base[index] == null && value == null)
169 OnItemModified(
new ListItemEventArgs(index));
175 public new virtual void Add(T item)
181 count = base.Count - 1;
183 OnItemAdded(
new ListItemEventArgs(count));
186 public new virtual void Clear()
192 OnCleared(EventArgs.Empty);
195 public new virtual void Insert(
int index, T item)
199 base.Insert(index, item);
201 OnItemAdded(
new ListItemEventArgs(index));
204 public new virtual bool Remove(T item)
210 result = base.Remove(item);
216 OnItemRemoved(EventArgs.Empty);
222 public new virtual void RemoveAt(
int index)
226 base.RemoveAt(index);
228 OnItemRemoved(EventArgs.Empty);
233 public event EventHandler<ListModificationEventArgs> CollectionModified;
235 public event EventHandler Cleared;
237 public event EventHandler<EventArgs> ItemAdded;
239 public event EventHandler<EventArgs> ItemModified;
241 public event EventHandler ItemRemoved;
243 public event EventHandler<ListRangeEventArgs> RangeAdded;
245 public event EventHandler RangeRemoved;
247 public new virtual void AddRange(IEnumerable<T> collection)
251 InsertRange(base.Count, collection);
255 public new virtual void InsertRange(
int index, IEnumerable<T> collection)
260 base.InsertRange(index, collection);
261 count = base.Count - index;
263 OnRangeAdded(
new ListRangeEventArgs(index, count));
266 public new virtual int RemoveAll(Predicate<T> match)
272 count = base.RemoveAll(match);
278 OnRangeRemoved(EventArgs.Empty);
290 public new virtual void RemoveRange(
int index,
int count)
292 int listCountOld, listCountNew;
295 listCountOld = base.Count;
296 base.RemoveRange(index, count);
297 listCountNew = base.Count;
301 if (listCountOld != listCountNew)
303 OnRangeRemoved(EventArgs.Empty);
307 public virtual void RemoveRange(List<T> collection)
309 for (
int i = 0; i < collection.Count; i++)
311 Remove(collection[i]);
315 public void SuppressEvents()
317 suppressEvents =
true;
320 public void ResumeEvents()
322 suppressEvents =
false;
325 protected virtual void OnCleared(EventArgs e)
337 OnCollectionModified(
new ListModificationEventArgs(ListModification.Cleared, -1, -1));
340 protected virtual void OnCollectionModified(ListModificationEventArgs e)
347 if (CollectionModified != null)
349 CollectionModified(
this, e);
353 protected virtual void OnItemAdded(ListItemEventArgs e)
360 if (ItemAdded != null)
365 OnCollectionModified(
new ListModificationEventArgs(ListModification.ItemAdded, e.ItemIndex, 1));
368 protected virtual void OnItemModified(ListItemEventArgs e)
375 if (ItemModified != null)
377 ItemModified(
this, e);
380 OnCollectionModified(
new ListModificationEventArgs(ListModification.ItemModified, e.ItemIndex, 1));
383 protected virtual void OnItemRemoved(EventArgs e)
390 if (ItemRemoved != null)
392 ItemRemoved(
this, e);
395 OnCollectionModified(
new ListModificationEventArgs(ListModification.ItemRemoved, -1, 1));
398 protected virtual void OnRangeAdded(ListRangeEventArgs e)
405 if (RangeAdded != null)
410 OnCollectionModified(
new ListModificationEventArgs(ListModification.RangeAdded, e.StartIndex, e.Count));
413 protected virtual void OnRangeRemoved(EventArgs e)
420 if (RangeRemoved != null)
422 RangeRemoved(
this, e);
425 OnCollectionModified(
new ListModificationEventArgs(ListModification.RangeRemoved, -1, -1));