[3558] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12009] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3558] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Collections;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Core {
|
---|
[3594] | 30 | /// <summary>
|
---|
| 31 | /// Represents a collection of checked items.
|
---|
| 32 | /// </summary>
|
---|
| 33 | /// <typeparam name="T">The element type (base type IItem)</typeparam>
|
---|
[3558] | 34 | [StorableClass]
|
---|
[3822] | 35 | [Item("CheckedItemCollection", "Represents a collection of items that can be checked or unchecked.")]
|
---|
[3558] | 36 | public class CheckedItemCollection<T> : ItemCollection<T>, ICheckedItemCollection<T> where T : class, IItem {
|
---|
| 37 | [Storable]
|
---|
| 38 | private Dictionary<T, bool> checkedState;
|
---|
| 39 |
|
---|
| 40 | /// <summary>
|
---|
[3594] | 41 | /// Gets an enumerable of checked items
|
---|
[3558] | 42 | /// </summary>
|
---|
| 43 | public IEnumerable<T> CheckedItems {
|
---|
| 44 | get { return from pair in checkedState where pair.Value select pair.Key; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[3594] | 47 | /// <summary>
|
---|
[4722] | 48 | /// Instantiates an empty CheckedItemCollection for deserialization.
|
---|
| 49 | /// </summary>
|
---|
| 50 | /// <param name="deserializing"></param>
|
---|
| 51 | [StorableConstructor]
|
---|
| 52 | protected CheckedItemCollection(bool deserializing) : base(deserializing) { }
|
---|
| 53 | protected CheckedItemCollection(CheckedItemCollection<T> original, Cloner cloner)
|
---|
| 54 | : base(original, cloner) {
|
---|
| 55 | list = new List<T>(original.Select(x => cloner.Clone(x)));
|
---|
| 56 | checkedState = new Dictionary<T, bool>();
|
---|
| 57 | foreach (var pair in original.checkedState)
|
---|
| 58 | checkedState.Add(cloner.Clone(pair.Key), pair.Value);
|
---|
| 59 | }
|
---|
| 60 | /// <summary>
|
---|
[3594] | 61 | /// Instantiates a new CheckedItemCollection.
|
---|
| 62 | /// </summary>
|
---|
[3558] | 63 | public CheckedItemCollection()
|
---|
| 64 | : base() {
|
---|
| 65 | checkedState = new Dictionary<T, bool>();
|
---|
| 66 | }
|
---|
[3594] | 67 | /// <summary>
|
---|
| 68 | /// Instantiates a new CheckedItemCollection with a predefined capacity.
|
---|
| 69 | /// </summary>
|
---|
| 70 | /// <param name="capacity">Initial capacity.</param>
|
---|
[3558] | 71 | public CheckedItemCollection(int capacity)
|
---|
| 72 | : base(capacity) {
|
---|
| 73 | checkedState = new Dictionary<T, bool>(capacity);
|
---|
| 74 | }
|
---|
[3594] | 75 | /// <summary>
|
---|
| 76 | /// Instantiates a new CheckedItemCollection containing the elements of <paramref name="collection"/>.
|
---|
| 77 | /// </summary>
|
---|
| 78 | /// <param name="collection">Initial element collection.</param>
|
---|
[3558] | 79 | public CheckedItemCollection(IEnumerable<T> collection)
|
---|
| 80 | : base(collection) {
|
---|
| 81 | checkedState = new Dictionary<T, bool>();
|
---|
[5809] | 82 | foreach (var item in list)
|
---|
[3564] | 83 | if (!checkedState.ContainsKey(item))
|
---|
[3575] | 84 | checkedState.Add(item, true);
|
---|
[3558] | 85 | }
|
---|
| 86 |
|
---|
[3594] | 87 | /// <summary>
|
---|
| 88 | /// Gets the checked state of <paramref name="item"/>.
|
---|
| 89 | /// </summary>
|
---|
| 90 | /// <param name="item">The element to get the checked state for.</param>
|
---|
| 91 | /// <returns>Checked state of <paramref name="item"/></returns>
|
---|
[3562] | 92 | public bool ItemChecked(T item) {
|
---|
[3558] | 93 | return checkedState[item];
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[3594] | 96 | /// <summary>
|
---|
| 97 | /// Sets the checked state <paramref name="checkedState"/> of <paramref name="item"/>.
|
---|
| 98 | /// </summary>
|
---|
| 99 | /// <param name="item">The element to set the checked state for.</param>
|
---|
| 100 | /// <param name="checkedState">The new checked state of the item</param>
|
---|
[3558] | 101 | public void SetItemCheckedState(T item, bool checkedState) {
|
---|
| 102 | if (!this.checkedState.ContainsKey(item)) throw new ArgumentException();
|
---|
| 103 | if (this.checkedState[item] != checkedState) {
|
---|
| 104 | this.checkedState[item] = checkedState;
|
---|
[3564] | 105 | OnCheckedItemsChanged(new T[] { item });
|
---|
[3558] | 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[3594] | 109 | /// <summary>
|
---|
| 110 | /// Adds a new <paramref name="item"/> with the given <paramref name="checkedState"/>.
|
---|
| 111 | /// </summary>
|
---|
| 112 | /// <param name="item">The item to add.</param>
|
---|
| 113 | /// <param name="checkedState">The checked state of the item to add.</param>
|
---|
[3567] | 114 | public void Add(T item, bool checkedState) {
|
---|
| 115 | Add(item);
|
---|
| 116 | SetItemCheckedState(item, checkedState);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[3594] | 119 | /// <summary>
|
---|
[3628] | 120 | /// Creates a ReadOnlyCheckedItemCollection containing the same elements.
|
---|
| 121 | /// </summary>
|
---|
| 122 | /// <returns>A new ReadOnlyCheckedItemCollection containing the same elements.</returns>
|
---|
| 123 | public new ReadOnlyCheckedItemCollection<T> AsReadOnly() {
|
---|
| 124 | return new ReadOnlyCheckedItemCollection<T>(this);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /// <summary>
|
---|
[3594] | 128 | /// Raised when the collection of items is reset.
|
---|
| 129 | /// </summary>
|
---|
| 130 | /// <param name="items">Empty</param>
|
---|
| 131 | /// <param name="oldItems">The elements in the collection before the reset.</param>
|
---|
[3558] | 132 | protected override void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
|
---|
| 133 | foreach (var oldItem in oldItems)
|
---|
[3798] | 134 | if (!list.Contains(oldItem))
|
---|
| 135 | checkedState.Remove(oldItem);
|
---|
[3558] | 136 | foreach (var item in items)
|
---|
[3562] | 137 | if (!checkedState.ContainsKey(item))
|
---|
[3575] | 138 | checkedState.Add(item, true);
|
---|
[3558] | 139 | base.OnCollectionReset(items, oldItems);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[3594] | 142 | /// <summary>
|
---|
| 143 | /// Raised when new items are added to the collection.
|
---|
| 144 | /// </summary>
|
---|
| 145 | /// <param name="items">The elements that are added to the collection.</param>
|
---|
[3558] | 146 | protected override void OnItemsAdded(IEnumerable<T> items) {
|
---|
| 147 | foreach (var item in items)
|
---|
[3562] | 148 | if (!checkedState.ContainsKey(item))
|
---|
[3575] | 149 | checkedState.Add(item, true);
|
---|
[3558] | 150 | base.OnItemsAdded(items);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[3594] | 153 | /// <summary>
|
---|
| 154 | /// Raised when items are removed from the collection.
|
---|
| 155 | /// </summary>
|
---|
| 156 | /// <param name="items">The items that are removed.</param>
|
---|
[3558] | 157 | protected override void OnItemsRemoved(IEnumerable<T> items) {
|
---|
| 158 | foreach (var item in items) {
|
---|
[3798] | 159 | if (!list.Contains(item))
|
---|
| 160 | checkedState.Remove(item);
|
---|
[3558] | 161 | }
|
---|
| 162 | base.OnItemsRemoved(items);
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[3594] | 165 | /// <summary>
|
---|
| 166 | /// Raised when the checked state of items is changed.
|
---|
| 167 | /// </summary>
|
---|
| 168 | /// <param name="items">The item whose check state is changed.</param>
|
---|
[3564] | 169 | protected virtual void OnCheckedItemsChanged(IEnumerable<T> items) {
|
---|
[3562] | 170 | RaiseCheckedItemsChanged(new CollectionItemsChangedEventArgs<T>(items));
|
---|
[3558] | 171 | }
|
---|
| 172 |
|
---|
[3594] | 173 | /// <summary>
|
---|
| 174 | /// Raised after the checked state of items has been changed.
|
---|
| 175 | /// </summary>
|
---|
[3562] | 176 | public event CollectionItemsChangedEventHandler<T> CheckedItemsChanged;
|
---|
| 177 | private void RaiseCheckedItemsChanged(CollectionItemsChangedEventArgs<T> e) {
|
---|
| 178 | var handler = CheckedItemsChanged;
|
---|
[3558] | 179 | if (handler != null) handler(this, e);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[3594] | 182 | /// <summary>
|
---|
| 183 | /// Creates a deep clone of the CheckedItemCollection.
|
---|
| 184 | /// </summary>
|
---|
| 185 | /// <param name="cloner"></param>
|
---|
| 186 | /// <returns>A clone of the CheckedItemCollection</returns>
|
---|
| 187 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 188 | return new CheckedItemCollection<T>(this, cloner);
|
---|
[3558] | 189 | }
|
---|
| 190 | }
|
---|
| 191 | }
|
---|