Added ItemReplaced() to IObservableCollection

git-svn-id: file:///srv/devel/repo-conversion/nusu@255 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2012-03-01 12:52:12 +00:00
parent c082960a9a
commit 5e4de7f027
9 changed files with 250 additions and 142 deletions

View File

@ -85,6 +85,10 @@
<Compile Include="Source\Cloning\SerializationCloner.Test.cs"> <Compile Include="Source\Cloning\SerializationCloner.Test.cs">
<DependentUpon>SerializationCloner.cs</DependentUpon> <DependentUpon>SerializationCloner.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Source\Collections\Constants.cs" />
<Compile Include="Source\Collections\Constants.Test.cs">
<DependentUpon>Constants.cs</DependentUpon>
</Compile>
<Compile Include="Source\Collections\Deque.cs" /> <Compile Include="Source\Collections\Deque.cs" />
<Compile Include="Source\Collections\Deque.Insertion.cs"> <Compile Include="Source\Collections\Deque.Insertion.cs">
<DependentUpon>Deque.cs</DependentUpon> <DependentUpon>Deque.cs</DependentUpon>

View File

@ -116,6 +116,10 @@
<Compile Include="Source\Cloning\SerializationCloner.Test.cs"> <Compile Include="Source\Cloning\SerializationCloner.Test.cs">
<DependentUpon>SerializationCloner.cs</DependentUpon> <DependentUpon>SerializationCloner.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Source\Collections\Constants.cs" />
<Compile Include="Source\Collections\Constants.Test.cs">
<DependentUpon>Constants.cs</DependentUpon>
</Compile>
<Compile Include="Source\Collections\Deque.cs" /> <Compile Include="Source\Collections\Deque.cs" />
<Compile Include="Source\Collections\Deque.Insertion.cs"> <Compile Include="Source\Collections\Deque.Insertion.cs">
<DependentUpon>Deque.cs</DependentUpon> <DependentUpon>Deque.cs</DependentUpon>

View File

@ -127,6 +127,10 @@
<Compile Include="Source\Cloning\SerializationCloner.Test.cs"> <Compile Include="Source\Cloning\SerializationCloner.Test.cs">
<DependentUpon>SerializationCloner.cs</DependentUpon> <DependentUpon>SerializationCloner.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Source\Collections\Constants.cs" />
<Compile Include="Source\Collections\Constants.Test.cs">
<DependentUpon>Constants.cs</DependentUpon>
</Compile>
<Compile Include="Source\Collections\Deque.cs" /> <Compile Include="Source\Collections\Deque.cs" />
<Compile Include="Source\Collections\Deque.Insertion.cs"> <Compile Include="Source\Collections\Deque.Insertion.cs">
<DependentUpon>Deque.cs</DependentUpon> <DependentUpon>Deque.cs</DependentUpon>

View File

@ -0,0 +1,49 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
#if UNITTEST
using System;
using System.Collections.Specialized;
using NUnit.Framework;
namespace Nuclex.Support.Collections {
/// <summary>Unit Test for the collection constants</summary>
[TestFixture]
internal class ConstantsTest {
/// <summary>
/// Verifies that the collection reset event arguments have 'reset' specified as
/// their action
/// </summary>
[Test]
public void CollectionResetEventArgsHaveResetActionSet() {
Assert.AreEqual(
NotifyCollectionChangedAction.Reset, Constants.NotifyCollectionResetEventArgs.Action
);
}
}
} // namespace Nuclex.Support.Collections
#endif // UNITTEST

View File

@ -0,0 +1,35 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Specialized;
namespace Nuclex.Support.Collections {
/// <summary>Contains fixed constants used by some collections</summary>
public static class Constants {
/// <summary>Fixed event args used to notify that the collection has reset</summary>
public static readonly NotifyCollectionChangedEventArgs NotifyCollectionResetEventArgs =
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
}
} // namespace Nuclex.Support.Collections

View File

@ -34,10 +34,8 @@ namespace Nuclex.Support.Collections {
/// <summary>Raised when an item is removed from the collection</summary> /// <summary>Raised when an item is removed from the collection</summary>
event EventHandler<ItemEventArgs<TItem>> ItemRemoved; event EventHandler<ItemEventArgs<TItem>> ItemRemoved;
#if false
/// <summary>Raised when an item is replaced in the collection</summary> /// <summary>Raised when an item is replaced in the collection</summary>
event EventHandler<ItemReplaceEventArgs<ItemType>> ItemReplaced; event EventHandler<ItemReplaceEventArgs<TItem>> ItemReplaced;
#endif
/// <summary>Raised when the collection is about to be cleared</summary> /// <summary>Raised when the collection is about to be cleared</summary>
/// <remarks> /// <remarks>

View File

@ -34,14 +34,19 @@ namespace Nuclex.Support.Collections {
ICollection<TItem>, ICollection<TItem>,
ICollection, ICollection,
#if !NO_SPECIALIZED_COLLECTIONS #if !NO_SPECIALIZED_COLLECTIONS
INotifyCollectionChanged, INotifyCollectionChanged,
#endif #endif
IObservableCollection<TItem> { IObservableCollection<TItem> {
/// <summary>Raised when an item has been added to the collection</summary> /// <summary>Raised when an item has been added to the collection</summary>
public event EventHandler<ItemEventArgs<TItem>> ItemAdded; public event EventHandler<ItemEventArgs<TItem>> ItemAdded;
/// <summary>Raised when an item is removed from the collection</summary> /// <summary>Raised when an item is removed from the collection</summary>
public event EventHandler<ItemEventArgs<TItem>> ItemRemoved; public event EventHandler<ItemEventArgs<TItem>> ItemRemoved;
/// <summary>Raised when an item is replaced in the collection</summary>
public event EventHandler<ItemReplaceEventArgs<TItem>> ItemReplaced {
add { }
remove { }
}
/// <summary>Raised when the collection is about to be cleared</summary> /// <summary>Raised when the collection is about to be cleared</summary>
/// <remarks> /// <remarks>
/// This could be covered by calling ItemRemoved for each item currently /// This could be covered by calling ItemRemoved for each item currently
@ -58,7 +63,7 @@ namespace Nuclex.Support.Collections {
#endif #endif
/// <summary>Initializes a new ObservableCollection with no items</summary> /// <summary>Initializes a new ObservableCollection with no items</summary>
public ObservableCollection() : this(new Collection<TItem>()) {} public ObservableCollection() : this(new Collection<TItem>()) { }
/// <summary> /// <summary>
/// Initializes a new ObservableCollection as a wrapper for an existing collection /// Initializes a new ObservableCollection as a wrapper for an existing collection
@ -75,11 +80,6 @@ namespace Nuclex.Support.Collections {
OnClearing(); OnClearing();
this.typedCollection.Clear(); this.typedCollection.Clear();
OnCleared(); OnCleared();
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged(this, CollectionResetEventArgs);
}
#endif
} }
/// <summary>Adds an item to the collection</summary> /// <summary>Adds an item to the collection</summary>
@ -87,14 +87,6 @@ namespace Nuclex.Support.Collections {
public void Add(TItem item) { public void Add(TItem item) {
this.typedCollection.Add(item); this.typedCollection.Add(item);
OnAdded(item); OnAdded(item);
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged(
this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item)
);
}
#endif
} }
/// <summary>Determines whether the collection contains the specified item</summary> /// <summary>Determines whether the collection contains the specified item</summary>
@ -132,14 +124,6 @@ namespace Nuclex.Support.Collections {
bool wasRemoved = this.typedCollection.Remove(item); bool wasRemoved = this.typedCollection.Remove(item);
if(wasRemoved) { if(wasRemoved) {
OnRemoved(item); OnRemoved(item);
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged(
this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item)
);
}
#endif
} }
return wasRemoved; return wasRemoved;
@ -154,27 +138,52 @@ namespace Nuclex.Support.Collections {
/// <summary>Fires the 'ItemAdded' event</summary> /// <summary>Fires the 'ItemAdded' event</summary>
/// <param name="item">Item that has been added to the collection</param> /// <param name="item">Item that has been added to the collection</param>
protected virtual void OnAdded(TItem item) { protected virtual void OnAdded(TItem item) {
if(ItemAdded != null) if(ItemAdded != null) {
ItemAdded(this, new ItemEventArgs<TItem>(item)); ItemAdded(this, new ItemEventArgs<TItem>(item));
}
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged(
this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item)
);
}
#endif
} }
/// <summary>Fires the 'ItemRemoved' event</summary> /// <summary>Fires the 'ItemRemoved' event</summary>
/// <param name="item">Item that has been removed from the collection</param> /// <param name="item">Item that has been removed from the collection</param>
protected virtual void OnRemoved(TItem item) { protected virtual void OnRemoved(TItem item) {
if(ItemRemoved != null) if(ItemRemoved != null) {
ItemRemoved(this, new ItemEventArgs<TItem>(item)); ItemRemoved(this, new ItemEventArgs<TItem>(item));
}
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged(
this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item)
);
}
#endif
} }
/// <summary>Fires the 'Clearing' event</summary> /// <summary>Fires the 'Clearing' event</summary>
protected virtual void OnClearing() { protected virtual void OnClearing() {
if(Clearing != null) if(Clearing != null) {
Clearing(this, EventArgs.Empty); Clearing(this, EventArgs.Empty);
}
} }
/// <summary>Fires the 'Cleared' event</summary> /// <summary>Fires the 'Cleared' event</summary>
protected virtual void OnCleared() { protected virtual void OnCleared() {
if(Cleared != null) if(Cleared != null) {
Cleared(this, EventArgs.Empty); Cleared(this, EventArgs.Empty);
}
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged(this, Constants.NotifyCollectionResetEventArgs);
}
#endif
} }
#region IEnumerable implementation #region IEnumerable implementation
@ -212,12 +221,6 @@ namespace Nuclex.Support.Collections {
#endregion // IEnumerable implementation #endregion // IEnumerable implementation
#if !NO_SPECIALIZED_COLLECTIONS
/// <summary>Fixed event args used to notify that the collection has reset</summary>
private static readonly NotifyCollectionChangedEventArgs CollectionResetEventArgs =
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
#endif
/// <summary>The wrapped collection under its type-safe interface</summary> /// <summary>The wrapped collection under its type-safe interface</summary>
private ICollection<TItem> typedCollection; private ICollection<TItem> typedCollection;
/// <summary>The wrapped collection under its object interface</summary> /// <summary>The wrapped collection under its object interface</summary>

View File

@ -36,15 +36,15 @@ namespace Nuclex.Support.Collections {
#endif #endif
public class ObservableDictionary<TKey, TValue> : public class ObservableDictionary<TKey, TValue> :
#if !NO_SERIALIZATION #if !NO_SERIALIZATION
ISerializable, ISerializable,
IDeserializationCallback, IDeserializationCallback,
#endif #endif
IDictionary<TKey, TValue>, IDictionary<TKey, TValue>,
IDictionary, IDictionary,
#if !NO_SPECIALIZED_COLLECTIONS #if !NO_SPECIALIZED_COLLECTIONS
INotifyCollectionChanged, INotifyCollectionChanged,
#endif #endif
IObservableCollection<KeyValuePair<TKey, TValue>> { IObservableCollection<KeyValuePair<TKey, TValue>> {
#if !NO_SERIALIZATION #if !NO_SERIALIZATION
#region class SerializedDictionary #region class SerializedDictionary
@ -81,6 +81,8 @@ namespace Nuclex.Support.Collections {
public event EventHandler<ItemEventArgs<KeyValuePair<TKey, TValue>>> ItemAdded; public event EventHandler<ItemEventArgs<KeyValuePair<TKey, TValue>>> ItemAdded;
/// <summary>Raised when an item is removed from the dictionary</summary> /// <summary>Raised when an item is removed from the dictionary</summary>
public event EventHandler<ItemEventArgs<KeyValuePair<TKey, TValue>>> ItemRemoved; public event EventHandler<ItemEventArgs<KeyValuePair<TKey, TValue>>> ItemRemoved;
/// <summary>Raised when an item is replaced in the collection</summary>
public event EventHandler<ItemReplaceEventArgs<KeyValuePair<TKey, TValue>>> ItemReplaced;
/// <summary>Raised when the dictionary is about to be cleared</summary> /// <summary>Raised when the dictionary is about to be cleared</summary>
public event EventHandler Clearing; public event EventHandler Clearing;
/// <summary>Raised when the dictionary has been cleared</summary> /// <summary>Raised when the dictionary has been cleared</summary>
@ -202,9 +204,13 @@ namespace Nuclex.Support.Collections {
this.typedDictionary[key] = value; this.typedDictionary[key] = value;
if(removed) { if(removed) {
OnRemoved(new KeyValuePair<TKey, TValue>(key, oldValue)); OnReplaced(
new KeyValuePair<TKey, TValue>(key, oldValue),
new KeyValuePair<TKey, TValue>(key, value)
);
} else {
OnAdded(new KeyValuePair<TKey, TValue>(key, value));
} }
OnAdded(new KeyValuePair<TKey, TValue>(key, value));
} }
} }
@ -244,28 +250,51 @@ namespace Nuclex.Support.Collections {
ItemAdded(this, new ItemEventArgs<KeyValuePair<TKey, TValue>>(item)); ItemAdded(this, new ItemEventArgs<KeyValuePair<TKey, TValue>>(item));
#if !NO_SPECIALIZED_COLLECTIONS #if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) if(CollectionChanged != null) {
CollectionChanged( CollectionChanged(
this, new NotifyCollectionChangedEventArgs( this,
NotifyCollectionChangedAction.Add, item new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item)
)
); );
}
#endif #endif
} }
/// <summary>Fires the 'ItemRemoved' event</summary> /// <summary>Fires the 'ItemRemoved' event</summary>
/// <param name="item">Item that has been removed from the collection</param> /// <param name="item">Item that has been removed from the collection</param>
protected virtual void OnRemoved(KeyValuePair<TKey, TValue> item) { protected virtual void OnRemoved(KeyValuePair<TKey, TValue> item) {
if(ItemRemoved != null) if(ItemRemoved != null) {
ItemRemoved(this, new ItemEventArgs<KeyValuePair<TKey, TValue>>(item)); ItemRemoved(this, new ItemEventArgs<KeyValuePair<TKey, TValue>>(item));
}
#if !NO_SPECIALIZED_COLLECTIONS #if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) if(CollectionChanged != null) {
CollectionChanged(
this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item)
);
}
#endif
}
/// <summary>Fires the 'ItemReplaced' event</summary>
/// <param name="oldItem">Item that has been replaced in the collection</param>
/// <param name="newItem">Item with which the original item was replaced</param>
protected virtual void OnReplaced(
KeyValuePair<TKey, TValue> oldItem, KeyValuePair<TKey, TValue> newItem
) {
if(ItemReplaced != null) {
ItemReplaced(
this,
new ItemReplaceEventArgs<KeyValuePair<TKey, TValue>>(oldItem, newItem)
);
}
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged( CollectionChanged(
this, new NotifyCollectionChangedEventArgs( this, new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Remove, item NotifyCollectionChangedAction.Replace, newItem, oldItem
) )
); );
}
#endif #endif
} }
@ -281,8 +310,9 @@ namespace Nuclex.Support.Collections {
Cleared(this, EventArgs.Empty); Cleared(this, EventArgs.Empty);
#if !NO_SPECIALIZED_COLLECTIONS #if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) if(CollectionChanged != null) {
CollectionChanged(this, CollectionResetEventArgs); CollectionChanged(this, Constants.NotifyCollectionResetEventArgs);
}
#endif #endif
} }
@ -449,12 +479,6 @@ namespace Nuclex.Support.Collections {
/// <summary>The wrapped Dictionary under its object interface</summary> /// <summary>The wrapped Dictionary under its object interface</summary>
private IDictionary objectDictionary; private IDictionary objectDictionary;
#if !NO_SPECIALIZED_COLLECTIONS
/// <summary>Fixed event args used to notify that the collection has reset</summary>
private static readonly NotifyCollectionChangedEventArgs CollectionResetEventArgs =
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
#endif
} }
} // namespace Nuclex.Support.Collections } // namespace Nuclex.Support.Collections

View File

@ -30,16 +30,21 @@ namespace Nuclex.Support.Collections {
/// <summary>List which fires events when items are added or removed</summary> /// <summary>List which fires events when items are added or removed</summary>
/// <typeparam name="TItem">Type of items the collection manages</typeparam> /// <typeparam name="TItem">Type of items the collection manages</typeparam>
public class ObservableList<TItem> : IList<TItem>, IList, ICollection, public class ObservableList<TItem> :
IList<TItem>,
IList,
ICollection,
#if !NO_SPECIALIZED_COLLECTIONS #if !NO_SPECIALIZED_COLLECTIONS
INotifyCollectionChanged, INotifyCollectionChanged,
#endif #endif
IObservableCollection<TItem> { IObservableCollection<TItem> {
/// <summary>Raised when an item has been added to the collection</summary> /// <summary>Raised when an item has been added to the collection</summary>
public event EventHandler<ItemEventArgs<TItem>> ItemAdded; public event EventHandler<ItemEventArgs<TItem>> ItemAdded;
/// <summary>Raised when an item is removed from the collection</summary> /// <summary>Raised when an item is removed from the collection</summary>
public event EventHandler<ItemEventArgs<TItem>> ItemRemoved; public event EventHandler<ItemEventArgs<TItem>> ItemRemoved;
/// <summary>Raised when an item is replaced in the collection</summary>
public event EventHandler<ItemReplaceEventArgs<TItem>> ItemReplaced;
/// <summary>Raised when the collection is about to be cleared</summary> /// <summary>Raised when the collection is about to be cleared</summary>
/// <remarks> /// <remarks>
/// This could be covered by calling ItemRemoved for each item currently /// This could be covered by calling ItemRemoved for each item currently
@ -83,10 +88,7 @@ namespace Nuclex.Support.Collections {
/// <param name="item">Item that will be inserted into the list</param> /// <param name="item">Item that will be inserted into the list</param>
public void Insert(int index, TItem item) { public void Insert(int index, TItem item) {
this.typedList.Insert(index, item); this.typedList.Insert(index, item);
OnAdded(item); OnAdded(item, index);
#if !NO_SPECIALIZED_COLLECTIONS
OnCollectionChanged(NotifyCollectionChangedAction.Add, item, index);
#endif
} }
/// <summary>Removes the item at the specified index from the list</summary> /// <summary>Removes the item at the specified index from the list</summary>
@ -94,10 +96,7 @@ namespace Nuclex.Support.Collections {
public void RemoveAt(int index) { public void RemoveAt(int index) {
TItem item = this.typedList[index]; TItem item = this.typedList[index];
this.typedList.RemoveAt(index); this.typedList.RemoveAt(index);
OnRemoved(item); OnRemoved(item, index);
#if !NO_SPECIALIZED_COLLECTIONS
OnCollectionChanged(NotifyCollectionChangedAction.Remove, item, index);
#endif
} }
/// <summary>Accesses the item at the specified index in the list</summary> /// <summary>Accesses the item at the specified index in the list</summary>
@ -108,18 +107,7 @@ namespace Nuclex.Support.Collections {
set { set {
TItem oldItem = this.typedList[index]; TItem oldItem = this.typedList[index];
this.typedList[index] = value; this.typedList[index] = value;
OnRemoved(oldItem); OnReplaced(oldItem, value, index);
OnAdded(value);
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged(
this, new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Replace, value, oldItem, index
)
);
}
#endif
} }
} }
@ -127,7 +115,7 @@ namespace Nuclex.Support.Collections {
/// <param name="item">Item that will be added to the list</param> /// <param name="item">Item that will be added to the list</param>
public void Add(TItem item) { public void Add(TItem item) {
this.typedList.Add(item); this.typedList.Add(item);
OnAdded(item); OnAdded(item, this.typedList.Count - 1);
} }
/// <summary>Removes all items from the list</summary> /// <summary>Removes all items from the list</summary>
@ -135,11 +123,6 @@ namespace Nuclex.Support.Collections {
OnClearing(); OnClearing();
this.typedList.Clear(); this.typedList.Clear();
OnCleared(); OnCleared();
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged(this, CollectionResetEventArgs);
}
#endif
} }
/// <summary>Checks whether the list contains the specified item</summary> /// <summary>Checks whether the list contains the specified item</summary>
@ -181,10 +164,8 @@ namespace Nuclex.Support.Collections {
TItem removedItem = this.typedList[index]; TItem removedItem = this.typedList[index];
this.typedList.RemoveAt(index); this.typedList.RemoveAt(index);
OnRemoved(removedItem); OnRemoved(removedItem, index);
#if !NO_SPECIALIZED_COLLECTIONS
OnCollectionChanged(NotifyCollectionChangedAction.Remove, item, index);
#endif
return true; return true;
} }
@ -237,10 +218,7 @@ namespace Nuclex.Support.Collections {
int IList.Add(object value) { int IList.Add(object value) {
int index = this.objectList.Add(value); int index = this.objectList.Add(value);
TItem addedItem = this.typedList[index]; TItem addedItem = this.typedList[index];
OnAdded(addedItem); OnAdded(addedItem, index);
#if !NO_SPECIALIZED_COLLECTIONS
OnCollectionChanged(NotifyCollectionChangedAction.Add, addedItem, index);
#endif
return index; return index;
} }
@ -264,10 +242,7 @@ namespace Nuclex.Support.Collections {
void IList.Insert(int index, object item) { void IList.Insert(int index, object item) {
this.objectList.Insert(index, item); this.objectList.Insert(index, item);
TItem addedItem = this.typedList[index]; TItem addedItem = this.typedList[index];
OnAdded(addedItem); OnAdded(addedItem, index);
#if !NO_SPECIALIZED_COLLECTIONS
OnCollectionChanged(NotifyCollectionChangedAction.Add, addedItem, index);
#endif
} }
/// <summary>Whether the list is of a fixed size</summary> /// <summary>Whether the list is of a fixed size</summary>
@ -285,10 +260,7 @@ namespace Nuclex.Support.Collections {
TItem removedItem = this.typedList[index]; TItem removedItem = this.typedList[index];
this.objectList.RemoveAt(index); this.objectList.RemoveAt(index);
OnRemoved(removedItem); OnRemoved(removedItem, index);
#if !NO_SPECIALIZED_COLLECTIONS
OnCollectionChanged(NotifyCollectionChangedAction.Remove, removedItem, index);
#endif
} }
/// <summary>Accesses the item at the specified index in the list</summary> /// <summary>Accesses the item at the specified index in the list</summary>
@ -300,69 +272,84 @@ namespace Nuclex.Support.Collections {
TItem oldItem = this.typedList[index]; TItem oldItem = this.typedList[index];
this.objectList[index] = value; this.objectList[index] = value;
TItem newItem = this.typedList[index]; TItem newItem = this.typedList[index];
OnRemoved(oldItem); OnReplaced(oldItem, newItem, index);
OnAdded(newItem);
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged(
this, new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Replace, newItem, oldItem, index
)
);
}
#endif
} }
} }
#endregion // IList implementation #endregion // IList implementation
#if !NO_SPECIALIZED_COLLECTIONS
/// <summary>Fires the CollectionChanged event</summary>
/// <param name="action">Type of change that has occured</param>
/// <param name="item">The item that has been added, removed or replaced</param>
/// <param name="index">Index of the changed item</param>
protected virtual void OnCollectionChanged(
NotifyCollectionChangedAction action, TItem item, int index
) {
if(CollectionChanged != null) {
CollectionChanged(
this, new NotifyCollectionChangedEventArgs(action, item, index)
);
}
}
#endif
/// <summary>Fires the 'ItemAdded' event</summary> /// <summary>Fires the 'ItemAdded' event</summary>
/// <param name="item">Item that has been added to the collection</param> /// <param name="item">Item that has been added to the collection</param>
protected virtual void OnAdded(TItem item) { /// <param name="index">Index of the added item</param>
if(ItemAdded != null) protected virtual void OnAdded(TItem item, int index) {
if(ItemAdded != null) {
ItemAdded(this, new ItemEventArgs<TItem>(item)); ItemAdded(this, new ItemEventArgs<TItem>(item));
}
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged(
this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index)
);
}
#endif
} }
/// <summary>Fires the 'ItemRemoved' event</summary> /// <summary>Fires the 'ItemRemoved' event</summary>
/// <param name="item">Item that has been removed from the collection</param> /// <param name="item">Item that has been removed from the collection</param>
protected virtual void OnRemoved(TItem item) { /// <param name="index">Index the item has been removed from</param>
if(ItemRemoved != null) protected virtual void OnRemoved(TItem item, int index) {
if(ItemRemoved != null) {
ItemRemoved(this, new ItemEventArgs<TItem>(item)); ItemRemoved(this, new ItemEventArgs<TItem>(item));
}
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged(
this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index)
);
}
#endif
}
/// <summary>Fires the 'ItemReplaced' event</summary>
/// <param name="oldItem">Item that has been replaced</param>
/// <param name="newItem">New item the original item was replaced with</param>
/// <param name="index">Index of the replaced item</param>
protected virtual void OnReplaced(TItem oldItem, TItem newItem, int index) {
if(ItemReplaced != null) {
ItemReplaced(this, new ItemReplaceEventArgs<TItem>(oldItem, newItem));
}
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged(
this,
new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Replace, newItem, oldItem, index
)
);
}
#endif
} }
/// <summary>Fires the 'Clearing' event</summary> /// <summary>Fires the 'Clearing' event</summary>
protected virtual void OnClearing() { protected virtual void OnClearing() {
if(Clearing != null) if(Clearing != null) {
Clearing(this, EventArgs.Empty); Clearing(this, EventArgs.Empty);
}
} }
/// <summary>Fires the 'Cleared' event</summary> /// <summary>Fires the 'Cleared' event</summary>
protected virtual void OnCleared() { protected virtual void OnCleared() {
if(Cleared != null) if(Cleared != null) {
Cleared(this, EventArgs.Empty); Cleared(this, EventArgs.Empty);
} }
#if !NO_SPECIALIZED_COLLECTIONS #if !NO_SPECIALIZED_COLLECTIONS
/// <summary>Fixed event args used to notify that the collection has reset</summary> if(CollectionChanged != null) {
private static readonly NotifyCollectionChangedEventArgs CollectionResetEventArgs = CollectionChanged(this, Constants.NotifyCollectionResetEventArgs);
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset); }
#endif #endif
}
/// <summary>The wrapped list under its type-safe interface</summary> /// <summary>The wrapped list under its type-safe interface</summary>
private IList<TItem> typedList; private IList<TItem> typedList;