#region License Information
/* HeuristicLab
* Copyright (C) 2002-2019 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 HEAL.Attic;
namespace HeuristicLab.Core {
///
/// Represents a collection of checked items.
///
/// The element type (base type IItem)
[StorableType("CAD59659-15B5-4CB0-A199-272E28F40832")]
[Item("CheckedItemCollection", "Represents a collection of items that can be checked or unchecked.")]
public class CheckedItemCollection : ItemCollection, ICheckedItemCollection where T : class, IItem {
[Storable]
private Dictionary checkedState;
///
/// Gets an enumerable of checked items
///
public IEnumerable CheckedItems {
get { return from pair in checkedState where pair.Value select pair.Key; }
}
///
/// Instantiates an empty CheckedItemCollection for deserialization.
///
///
[StorableConstructor]
protected CheckedItemCollection(StorableConstructorFlag _) : base(_) { }
protected CheckedItemCollection(CheckedItemCollection original, Cloner cloner)
: base(original, cloner) {
list = new List(original.Select(x => cloner.Clone(x)));
checkedState = new Dictionary();
foreach (var pair in original.checkedState)
checkedState.Add(cloner.Clone(pair.Key), pair.Value);
}
///
/// Instantiates a new CheckedItemCollection.
///
public CheckedItemCollection()
: base() {
checkedState = new Dictionary();
}
///
/// Instantiates a new CheckedItemCollection with a predefined capacity.
///
/// Initial capacity.
public CheckedItemCollection(int capacity)
: base(capacity) {
checkedState = new Dictionary(capacity);
}
///
/// Instantiates a new CheckedItemCollection containing the elements of .
///
/// Initial element collection.
public CheckedItemCollection(IEnumerable collection)
: base(collection) {
checkedState = new Dictionary();
foreach (var item in list)
if (!checkedState.ContainsKey(item))
checkedState.Add(item, true);
}
///
/// Gets the checked state of .
///
/// The element to get the checked state for.
/// Checked state of
public bool ItemChecked(T item) {
return checkedState[item];
}
///
/// Sets the checked state of .
///
/// The element to set the checked state for.
/// The new checked state of the item
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 T[] { item });
}
}
///
/// Adds a new with the given .
///
/// The item to add.
/// The checked state of the item to add.
public void Add(T item, bool checkedState) {
Add(item);
SetItemCheckedState(item, checkedState);
}
///
/// Creates a ReadOnlyCheckedItemCollection containing the same elements.
///
/// A new ReadOnlyCheckedItemCollection containing the same elements.
public new ReadOnlyCheckedItemCollection AsReadOnly() {
return new ReadOnlyCheckedItemCollection(this);
}
///
/// Raised when the collection of items is reset.
///
/// Empty
/// The elements in the collection before the reset.
protected override void OnCollectionReset(IEnumerable items, IEnumerable oldItems) {
foreach (var oldItem in oldItems)
if (!list.Contains(oldItem))
checkedState.Remove(oldItem);
foreach (var item in items)
if (!checkedState.ContainsKey(item))
checkedState.Add(item, true);
base.OnCollectionReset(items, oldItems);
}
///
/// Raised when new items are added to the collection.
///
/// The elements that are added to the collection.
protected override void OnItemsAdded(IEnumerable items) {
foreach (var item in items)
if (!checkedState.ContainsKey(item))
checkedState.Add(item, true);
base.OnItemsAdded(items);
}
///
/// Raised when items are removed from the collection.
///
/// The items that are removed.
protected override void OnItemsRemoved(IEnumerable items) {
foreach (var item in items) {
if (!list.Contains(item))
checkedState.Remove(item);
}
base.OnItemsRemoved(items);
}
///
/// Raised when the checked state of items is changed.
///
/// The item whose check state is 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 deep clone of the CheckedItemCollection.
///
///
/// A clone of the CheckedItemCollection
public override IDeepCloneable Clone(Cloner cloner) {
return new CheckedItemCollection(this, cloner);
}
}
}