#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Collections;
using HeuristicLab.Common;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Core {
///
/// Represents a list of checked items.
///
/// The element type (base type is IItem)
[StorableClass]
[Item("CheckedItemList", "Represents a list of items that can be checked or unchecked.")]
public class CheckedItemList : ItemList, ICheckedItemList where T : class, IItem {
[Storable]
private Dictionary checkedState;
///
/// Gets an enumerable of checked items.
///
public IEnumerable> CheckedItems {
get {
return from i in Enumerable.Range(0, list.Count)
where ItemChecked(i)
select new IndexedItem(i, list[i]);
}
}
///
/// Instantiates a new CheckedItemList for deserialization.
///
///
[StorableConstructor]
protected CheckedItemList(bool deserializing) : base(deserializing) { }
protected CheckedItemList(CheckedItemList original, Cloner cloner)
: base(original, cloner) {
list = new List(original.Select(x => (T)cloner.Clone(x)));
checkedState = new Dictionary();
foreach (var pair in original.checkedState)
checkedState.Add(cloner.Clone(pair.Key), pair.Value);
}
///
/// Instantiates an empty CheckedItemList.
///
public CheckedItemList()
: base() {
checkedState = new Dictionary();
}
///
/// Instantiates an empty CheckedItemList with a given initial .
///
/// The initial capacity.
public CheckedItemList(int capacity)
: base(capacity) {
checkedState = new Dictionary();
}
///
/// Instantiates an CheckedItemList initially filled with the elements of .
///
/// Collection of elements.
public CheckedItemList(IEnumerable collection)
: base(collection) {
checkedState = new Dictionary();
foreach (var item in collection) {
if (!checkedState.ContainsKey(item))
checkedState.Add(item, true);
}
}
///
/// Gets the checked state of .
///
/// The element to get the checked state for.
/// The checked state of
public bool ItemChecked(T item) {
return checkedState[item];
}
///
/// Gets the checked state of item with .
///
/// The index of the element to get the checked state for.
/// The checked state of the element at .
public bool ItemChecked(int itemIndex) {
return ItemChecked(this[itemIndex]);
}
///
/// Sets the checked state of to .
///
/// The item to set the checked state for.
/// The new checked state of
public void SetItemCheckedState(T item, bool checkedState) {
if (!this.checkedState.ContainsKey(item)) throw new ArgumentException();
if (this.checkedState[item] != checkedState) {
this.checkedState[item] = checkedState;
OnCheckedItemsChanged(new IndexedItem[] { new IndexedItem(IndexOf(item), item) });
}
}
///
/// Sets the checked state of the element with to .
///
/// The index of the item to set the checked state for.
/// The new checked state of the item.
public void SetItemCheckedState(int itemIndex, bool checkedState) {
SetItemCheckedState(this[itemIndex], checkedState);
}
///
/// Adds a new with to the list.
///
/// The item to add to the list.
/// The checked state of the item added to the list.
public void Add(T item, bool checkedState) {
Add(item);
SetItemCheckedState(item, checkedState);
}
///
/// Inserts a new at with into the list.
///
/// The insertion index of the new element.
/// The element that is inserted into the list.
/// The checked state of the inserted element.
public void Insert(int index, T item, bool checkedState) {
Insert(index, item);
SetItemCheckedState(item, checkedState);
}
///
/// Creates a ReadOnlyCheckedItemList containing the same elements.
///
/// A new ReadOnlyCheckedItemList containing the same elements.
public new ReadOnlyCheckedItemList AsReadOnly() {
return new ReadOnlyCheckedItemList(this);
}
///
/// Raised after the list has been reset.
///
/// Empty
/// The elements of the list before it has been reset.
protected override void OnCollectionReset(IEnumerable> items, IEnumerable> oldItems) {
foreach (var oldIndexedItem in oldItems) {
if (!list.Contains(oldIndexedItem.Value))
checkedState.Remove(oldIndexedItem.Value);
}
foreach (var indexedItem in items) {
if (!checkedState.ContainsKey(indexedItem.Value))
checkedState.Add(indexedItem.Value, true);
}
base.OnCollectionReset(items, oldItems);
}
///
/// Raised when new items are added to the list.
///
/// The items that are added.
protected override void OnItemsAdded(IEnumerable> items) {
foreach (var indexedItem in items)
if (!checkedState.ContainsKey(indexedItem.Value))
checkedState.Add(indexedItem.Value, true);
base.OnItemsAdded(items);
}
///
/// Raised when items are removed from the list.
///
/// Items that are removed.
protected override void OnItemsRemoved(IEnumerable> items) {
foreach (var indexedItem in items)
if (!list.Contains(indexedItem.Value))
checkedState.Remove(indexedItem.Value);
base.OnItemsRemoved(items);
}
///
/// Raised when items are replaced.
///
/// The items which replace
/// The items that are replaced by
protected override void OnItemsReplaced(IEnumerable> items, IEnumerable> oldItems) {
foreach (var oldIndexedItem in oldItems)
if (!list.Contains(oldIndexedItem.Value))
checkedState.Remove(oldIndexedItem.Value);
foreach (var indexedItem in items)
if (!checkedState.ContainsKey(indexedItem.Value))
checkedState.Add(indexedItem.Value, true);
base.OnItemsReplaced(items, oldItems);
}
///
/// Raised after the checked state of items has been changed.
///
/// The items whose check state has been changed.
protected virtual void OnCheckedItemsChanged(IEnumerable> items) {
RaiseCheckedItemsChanged(new CollectionItemsChangedEventArgs>(items));
}
///
/// Raised after the checked state of items has been changed.
///
public event CollectionItemsChangedEventHandler> CheckedItemsChanged;
private void RaiseCheckedItemsChanged(CollectionItemsChangedEventArgs> e) {
var handler = CheckedItemsChanged;
if (handler != null) handler(this, e);
}
///
/// Creates a new deep clone of the CheckedItemList.
///
///
/// A deep clone of the CheckedItemList
public override IDeepCloneable Clone(Cloner cloner) {
return new CheckedItemList(this, cloner);
}
}
}