Provided a common class for observable collections similar to what the .NET Framework 3.0 offers; ParentingCollection no longer requires the parent type to be a reference type; Laid the foundation for a new progress tracking framework that can be used to drive progress bars, loading screens and such

git-svn-id: file:///srv/devel/repo-conversion/nusu@9 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2007-04-16 17:18:16 +00:00
parent 355e5766d4
commit 2d145ca867
11 changed files with 379 additions and 6 deletions

View file

@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Nuclex.Support.Collections {
/// <summary>Generic collection of progressions</summary>
public class ObservableCollection<ItemType> : Collection<ItemType> {
#region class ItemEventArgs
/// <summary>Arguments class for events that need to pass a progression</summary>
public class ItemEventArgs : EventArgs {
/// <summary>Initializes a new event arguments supplier</summary>
/// <param name="item">Item to be supplied to the event handler</param>
public ItemEventArgs(ItemType item) {
this.item = item;
}
/// <summary>Obtains the collection item the event arguments are carrying</summary>
public ItemType Item {
get { return this.item; }
}
/// <summary>Item that's passed to the event handler</summary>
private ItemType item;
}
#endregion // class ItemEventArgs
/// <summary>Raised when an item has been added to the collection</summary>
public event EventHandler<ItemEventArgs> ItemAdded;
/// <summary>Raised when an item is removed from the collection</summary>
public event EventHandler<ItemEventArgs> ItemRemoved;
/// <summary>Raised the collection is about to be cleared</summary>
public event EventHandler Clearing;
/// <summary>Removes all elements from the Collection</summary>
protected override void ClearItems() {
OnClearing();
base.ClearItems();
}
/// <summary>
/// Inserts an element into the ProgressionCollection at the specified index
/// </summary>
/// <param name="index">
/// The object to insert. The value can be null for reference types.
/// </param>
/// <param name="item">The zero-based index at which item should be inserted</param>
protected override void InsertItem(int index, ItemType item) {
base.InsertItem(index, item);
OnAdded(item);
}
/// <summary>
/// Removes the element at the specified index of the ProgressionCollection
/// </summary>
/// <param name="index">The zero-based index of the element to remove</param>
protected override void RemoveItem(int index) {
ItemType item = base[index];
base.RemoveItem(index);
OnRemoved(item);
}
/// <summary>Replaces the element at the specified index</summary>
/// <param name="index">
/// The new value for the element at the specified index. The value can be null
/// for reference types
/// </param>
/// <param name="item">The zero-based index of the element to replace</param>
protected override void SetItem(int index, ItemType item) {
ItemType oldItem = base[index];
base.SetItem(index, item);
OnRemoved(oldItem);
OnAdded(item);
}
/// <summary>Fires the 'ItemAdded' event</summary>
/// <param name="item">Item that has been added to the collection</param>
protected virtual void OnAdded(ItemType item) {
if(ItemAdded != null)
ItemAdded(this, new ItemEventArgs(item));
}
/// <summary>Fires the 'ItemRemoved' event</summary>
/// <param name="item">Item that has been removed from the collection</param>
protected virtual void OnRemoved(ItemType item) {
if(ItemRemoved != null)
ItemRemoved(this, new ItemEventArgs(item));
}
/// <summary>Fires the 'Clearing' event</summary>
protected virtual void OnClearing() {
if(Clearing != null)
Clearing(this, EventArgs.Empty);
}
}
} // namespace Nuclex.Support.Collections

View file

@ -5,7 +5,7 @@ namespace Nuclex.Support.Collections {
/// <summary>Base class for objects that can be parented to an owner</summary>
/// <typeparam name="ParentType">Type of the parent object</typeparam>
public class Parentable<ParentType> where ParentType : class {
public class Parentable<ParentType> {
/// <summary>Assigns a new parent to this instance</summary>
internal void SetParent(ParentType parent) {

View file

@ -15,8 +15,7 @@ namespace Nuclex.Support.Collections {
/// <typeparam name="ParentType">Type of the parent object to assign to items</typeparam>
/// <typeparam name="ItemType">Type of the items being managed in the collection</typeparam>
public class ParentingCollection<ParentType, ItemType> : Collection<ItemType>
where ItemType : Parentable<ParentType>
where ParentType : class {
where ItemType : Parentable<ParentType> {
/// <summary>Reparents all elements in the collection</summary>
/// <param name="parent">New parent to take ownership of the items</param>
@ -30,7 +29,7 @@ namespace Nuclex.Support.Collections {
/// <summary>Clears all elements from the collection</summary>
protected override void ClearItems() {
for(int index = 0; index < Count; ++index)
base[index].SetParent(null);
base[index].SetParent(default(ParentType));
base.ClearItems();
}
@ -46,7 +45,7 @@ namespace Nuclex.Support.Collections {
/// <summary>Removes an element from the collection</summary>
/// <param name="index">Index of the element to remove</param>
protected override void RemoveItem(int index) {
base[index].SetParent(null);
base[index].SetParent(default(ParentType));
base.RemoveItem(index);
}
@ -70,7 +69,7 @@ namespace Nuclex.Support.Collections {
if(disposeItems) {
// Have the items do their cleanup work
Reparent(null);
Reparent(default(ParentType));
// Dispose of all the items in the collection that implement IDisposable
foreach(ItemType item in this) {