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">
<DependentUpon>SerializationCloner.cs</DependentUpon>
</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.Insertion.cs">
<DependentUpon>Deque.cs</DependentUpon>

View File

@ -116,6 +116,10 @@
<Compile Include="Source\Cloning\SerializationCloner.Test.cs">
<DependentUpon>SerializationCloner.cs</DependentUpon>
</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.Insertion.cs">
<DependentUpon>Deque.cs</DependentUpon>

View File

@ -127,6 +127,10 @@
<Compile Include="Source\Cloning\SerializationCloner.Test.cs">
<DependentUpon>SerializationCloner.cs</DependentUpon>
</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.Insertion.cs">
<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>
event EventHandler<ItemEventArgs<TItem>> ItemRemoved;
#if false
/// <summary>Raised when an item is replaced in the collection</summary>
event EventHandler<ItemReplaceEventArgs<ItemType>> ItemReplaced;
#endif
event EventHandler<ItemReplaceEventArgs<TItem>> ItemReplaced;
/// <summary>Raised when the collection is about to be cleared</summary>
/// <remarks>

View File

@ -42,6 +42,11 @@ namespace Nuclex.Support.Collections {
public event EventHandler<ItemEventArgs<TItem>> ItemAdded;
/// <summary>Raised when an item is removed from the collection</summary>
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>
/// <remarks>
/// This could be covered by calling ItemRemoved for each item currently
@ -58,7 +63,7 @@ namespace Nuclex.Support.Collections {
#endif
/// <summary>Initializes a new ObservableCollection with no items</summary>
public ObservableCollection() : this(new Collection<TItem>()) {}
public ObservableCollection() : this(new Collection<TItem>()) { }
/// <summary>
/// Initializes a new ObservableCollection as a wrapper for an existing collection
@ -75,11 +80,6 @@ namespace Nuclex.Support.Collections {
OnClearing();
this.typedCollection.Clear();
OnCleared();
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged(this, CollectionResetEventArgs);
}
#endif
}
/// <summary>Adds an item to the collection</summary>
@ -87,14 +87,6 @@ namespace Nuclex.Support.Collections {
public void Add(TItem item) {
this.typedCollection.Add(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>
@ -132,14 +124,6 @@ namespace Nuclex.Support.Collections {
bool wasRemoved = this.typedCollection.Remove(item);
if(wasRemoved) {
OnRemoved(item);
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged(
this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item)
);
}
#endif
}
return wasRemoved;
@ -154,28 +138,53 @@ namespace Nuclex.Support.Collections {
/// <summary>Fires the 'ItemAdded' event</summary>
/// <param name="item">Item that has been added to the collection</param>
protected virtual void OnAdded(TItem item) {
if(ItemAdded != null)
if(ItemAdded != null) {
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>
/// <param name="item">Item that has been removed from the collection</param>
protected virtual void OnRemoved(TItem item) {
if(ItemRemoved != null)
if(ItemRemoved != null) {
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>
protected virtual void OnClearing() {
if(Clearing != null)
if(Clearing != null) {
Clearing(this, EventArgs.Empty);
}
}
/// <summary>Fires the 'Cleared' event</summary>
protected virtual void OnCleared() {
if(Cleared != null)
if(Cleared != null) {
Cleared(this, EventArgs.Empty);
}
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged(this, Constants.NotifyCollectionResetEventArgs);
}
#endif
}
#region IEnumerable implementation
@ -212,12 +221,6 @@ namespace Nuclex.Support.Collections {
#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>
private ICollection<TItem> typedCollection;
/// <summary>The wrapped collection under its object interface</summary>

View File

@ -81,6 +81,8 @@ namespace Nuclex.Support.Collections {
public event EventHandler<ItemEventArgs<KeyValuePair<TKey, TValue>>> ItemAdded;
/// <summary>Raised when an item is removed from the dictionary</summary>
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>
public event EventHandler Clearing;
/// <summary>Raised when the dictionary has been cleared</summary>
@ -202,11 +204,15 @@ namespace Nuclex.Support.Collections {
this.typedDictionary[key] = value;
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));
}
}
}
/// <summary>Inserts an item into the Dictionary</summary>
/// <param name="key">Key under which to add the new item</param>
@ -244,28 +250,51 @@ namespace Nuclex.Support.Collections {
ItemAdded(this, new ItemEventArgs<KeyValuePair<TKey, TValue>>(item));
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null)
if(CollectionChanged != null) {
CollectionChanged(
this, new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Add, item
)
this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item)
);
}
#endif
}
/// <summary>Fires the 'ItemRemoved' event</summary>
/// <param name="item">Item that has been removed from the collection</param>
protected virtual void OnRemoved(KeyValuePair<TKey, TValue> item) {
if(ItemRemoved != null)
if(ItemRemoved != null) {
ItemRemoved(this, new ItemEventArgs<KeyValuePair<TKey, TValue>>(item));
}
#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(
this, new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Remove, item
NotifyCollectionChangedAction.Replace, newItem, oldItem
)
);
}
#endif
}
@ -281,8 +310,9 @@ namespace Nuclex.Support.Collections {
Cleared(this, EventArgs.Empty);
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null)
CollectionChanged(this, CollectionResetEventArgs);
if(CollectionChanged != null) {
CollectionChanged(this, Constants.NotifyCollectionResetEventArgs);
}
#endif
}
@ -449,12 +479,6 @@ namespace Nuclex.Support.Collections {
/// <summary>The wrapped Dictionary under its object interface</summary>
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

View File

@ -30,7 +30,10 @@ namespace Nuclex.Support.Collections {
/// <summary>List which fires events when items are added or removed</summary>
/// <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
INotifyCollectionChanged,
#endif
@ -40,6 +43,8 @@ namespace Nuclex.Support.Collections {
public event EventHandler<ItemEventArgs<TItem>> ItemAdded;
/// <summary>Raised when an item is removed from the collection</summary>
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>
/// <remarks>
/// 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>
public void Insert(int index, TItem item) {
this.typedList.Insert(index, item);
OnAdded(item);
#if !NO_SPECIALIZED_COLLECTIONS
OnCollectionChanged(NotifyCollectionChangedAction.Add, item, index);
#endif
OnAdded(item, index);
}
/// <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) {
TItem item = this.typedList[index];
this.typedList.RemoveAt(index);
OnRemoved(item);
#if !NO_SPECIALIZED_COLLECTIONS
OnCollectionChanged(NotifyCollectionChangedAction.Remove, item, index);
#endif
OnRemoved(item, index);
}
/// <summary>Accesses the item at the specified index in the list</summary>
@ -108,18 +107,7 @@ namespace Nuclex.Support.Collections {
set {
TItem oldItem = this.typedList[index];
this.typedList[index] = value;
OnRemoved(oldItem);
OnAdded(value);
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged(
this, new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Replace, value, oldItem, index
)
);
}
#endif
OnReplaced(oldItem, value, index);
}
}
@ -127,7 +115,7 @@ namespace Nuclex.Support.Collections {
/// <param name="item">Item that will be added to the list</param>
public void Add(TItem item) {
this.typedList.Add(item);
OnAdded(item);
OnAdded(item, this.typedList.Count - 1);
}
/// <summary>Removes all items from the list</summary>
@ -135,11 +123,6 @@ namespace Nuclex.Support.Collections {
OnClearing();
this.typedList.Clear();
OnCleared();
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged(this, CollectionResetEventArgs);
}
#endif
}
/// <summary>Checks whether the list contains the specified item</summary>
@ -181,10 +164,8 @@ namespace Nuclex.Support.Collections {
TItem removedItem = this.typedList[index];
this.typedList.RemoveAt(index);
OnRemoved(removedItem);
#if !NO_SPECIALIZED_COLLECTIONS
OnCollectionChanged(NotifyCollectionChangedAction.Remove, item, index);
#endif
OnRemoved(removedItem, index);
return true;
}
@ -237,10 +218,7 @@ namespace Nuclex.Support.Collections {
int IList.Add(object value) {
int index = this.objectList.Add(value);
TItem addedItem = this.typedList[index];
OnAdded(addedItem);
#if !NO_SPECIALIZED_COLLECTIONS
OnCollectionChanged(NotifyCollectionChangedAction.Add, addedItem, index);
#endif
OnAdded(addedItem, index);
return index;
}
@ -264,10 +242,7 @@ namespace Nuclex.Support.Collections {
void IList.Insert(int index, object item) {
this.objectList.Insert(index, item);
TItem addedItem = this.typedList[index];
OnAdded(addedItem);
#if !NO_SPECIALIZED_COLLECTIONS
OnCollectionChanged(NotifyCollectionChangedAction.Add, addedItem, index);
#endif
OnAdded(addedItem, index);
}
/// <summary>Whether the list is of a fixed size</summary>
@ -285,10 +260,7 @@ namespace Nuclex.Support.Collections {
TItem removedItem = this.typedList[index];
this.objectList.RemoveAt(index);
OnRemoved(removedItem);
#if !NO_SPECIALIZED_COLLECTIONS
OnCollectionChanged(NotifyCollectionChangedAction.Remove, removedItem, index);
#endif
OnRemoved(removedItem, index);
}
/// <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];
this.objectList[index] = value;
TItem newItem = this.typedList[index];
OnRemoved(oldItem);
OnAdded(newItem);
OnReplaced(oldItem, newItem, index);
}
}
#endregion // IList implementation
/// <summary>Fires the 'ItemAdded' event</summary>
/// <param name="item">Item that has been added to the collection</param>
/// <param name="index">Index of the added item</param>
protected virtual void OnAdded(TItem item, int index) {
if(ItemAdded != null) {
ItemAdded(this, new ItemEventArgs<TItem>(item));
}
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged(
this, new NotifyCollectionChangedEventArgs(
this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index)
);
}
#endif
}
/// <summary>Fires the 'ItemRemoved' event</summary>
/// <param name="item">Item that has been removed from the collection</param>
/// <param name="index">Index the item has been removed from</param>
protected virtual void OnRemoved(TItem item, int index) {
if(ItemRemoved != null) {
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
}
}
#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>
/// <param name="item">Item that has been added to the collection</param>
protected virtual void OnAdded(TItem item) {
if(ItemAdded != null)
ItemAdded(this, new ItemEventArgs<TItem>(item));
}
/// <summary>Fires the 'ItemRemoved' event</summary>
/// <param name="item">Item that has been removed from the collection</param>
protected virtual void OnRemoved(TItem item) {
if(ItemRemoved != null)
ItemRemoved(this, new ItemEventArgs<TItem>(item));
}
/// <summary>Fires the 'Clearing' event</summary>
protected virtual void OnClearing() {
if(Clearing != null)
if(Clearing != null) {
Clearing(this, EventArgs.Empty);
}
}
/// <summary>Fires the 'Cleared' event</summary>
protected virtual void OnCleared() {
if(Cleared != null)
if(Cleared != null) {
Cleared(this, EventArgs.Empty);
}
#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);
if(CollectionChanged != null) {
CollectionChanged(this, Constants.NotifyCollectionResetEventArgs);
}
#endif
}
/// <summary>The wrapped list under its type-safe interface</summary>
private IList<TItem> typedList;