2007-05-11 21:15:35 +00:00
|
|
|
#region CPL License
|
|
|
|
/*
|
|
|
|
Nuclex Framework
|
2009-01-07 19:05:29 +00:00
|
|
|
Copyright (C) 2002-2009 Nuclex Development Labs
|
2007-05-11 21:15:35 +00:00
|
|
|
|
|
|
|
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
|
2007-07-24 20:15:19 +00:00
|
|
|
|
2007-04-16 17:18:16 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
|
|
|
namespace Nuclex.Support.Collections {
|
|
|
|
|
2007-07-24 20:15:19 +00:00
|
|
|
/// <summary>Collection which fires events when items are added or removed</summary>
|
2007-07-24 19:51:51 +00:00
|
|
|
public class ObservableCollection<ItemType> : Collection<ItemType> {
|
|
|
|
|
2007-05-09 19:17:53 +00:00
|
|
|
/// <summary>Raised when an item has been added to the collection</summary>
|
2007-07-24 20:15:19 +00:00
|
|
|
public event EventHandler<ItemEventArgs<ItemType>> ItemAdded;
|
2007-05-09 19:17:53 +00:00
|
|
|
/// <summary>Raised when an item is removed from the collection</summary>
|
2007-07-24 20:15:19 +00:00
|
|
|
public event EventHandler<ItemEventArgs<ItemType>> ItemRemoved;
|
2008-07-17 20:51:35 +00:00
|
|
|
/// <summary>Raised when the collection is about to be cleared</summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// This could be covered by calling ItemRemoved for each item currently
|
|
|
|
/// contained in the collection, but it is often simpler and more efficient
|
|
|
|
/// to process the clearing of the entire collection as a special operation.
|
|
|
|
/// </remarks>
|
2007-05-09 19:17:53 +00:00
|
|
|
public event EventHandler Clearing;
|
|
|
|
|
2007-04-18 20:20:03 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the ObservableCollection class that is empty.
|
|
|
|
/// </summary>
|
|
|
|
public ObservableCollection() : base() { }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the ObservableCollection class as a wrapper
|
|
|
|
/// for the specified list.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="list">The list that is wrapped by the new collection.</param>
|
2007-07-24 19:51:51 +00:00
|
|
|
/// <exception cref="System.ArgumentNullException">
|
|
|
|
/// List is null.
|
|
|
|
/// </exception>
|
2007-04-18 20:20:03 +00:00
|
|
|
public ObservableCollection(IList<ItemType> list) : base(list) { }
|
|
|
|
|
2007-07-24 19:51:51 +00:00
|
|
|
/// <summary>Removes all elements from the Collection</summary>
|
|
|
|
protected override void ClearItems() {
|
|
|
|
OnClearing();
|
|
|
|
|
|
|
|
base.ClearItems();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2008-06-11 20:28:08 +00:00
|
|
|
/// Inserts an element into the ObservableCollection at the specified index
|
2007-07-24 19:51:51 +00:00
|
|
|
/// </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>
|
2008-06-11 20:28:08 +00:00
|
|
|
/// Removes the element at the specified index of the ObservableCollection
|
2007-07-24 19:51:51 +00:00
|
|
|
/// </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);
|
|
|
|
}
|
|
|
|
|
2007-04-16 17:18:16 +00:00
|
|
|
/// <summary>Fires the 'ItemAdded' event</summary>
|
|
|
|
/// <param name="item">Item that has been added to the collection</param>
|
2007-07-24 19:51:51 +00:00
|
|
|
protected virtual void OnAdded(ItemType item) {
|
2007-04-16 17:18:16 +00:00
|
|
|
if(ItemAdded != null)
|
2007-07-24 20:15:19 +00:00
|
|
|
ItemAdded(this, new ItemEventArgs<ItemType>(item));
|
2007-04-16 17:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Fires the 'ItemRemoved' event</summary>
|
|
|
|
/// <param name="item">Item that has been removed from the collection</param>
|
2007-07-24 19:51:51 +00:00
|
|
|
protected virtual void OnRemoved(ItemType item) {
|
2007-04-16 17:18:16 +00:00
|
|
|
if(ItemRemoved != null)
|
2007-07-24 20:15:19 +00:00
|
|
|
ItemRemoved(this, new ItemEventArgs<ItemType>(item));
|
2007-04-16 17:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Fires the 'Clearing' event</summary>
|
2007-07-24 19:51:51 +00:00
|
|
|
protected virtual void OnClearing() {
|
2007-04-16 17:18:16 +00:00
|
|
|
if(Clearing != null)
|
|
|
|
Clearing(this, EventArgs.Empty);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Nuclex.Support.Collections
|