[3565] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12009] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3565] | 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;
|
---|
[4068] | 23 | using System.Collections.Generic;
|
---|
[3565] | 24 | using System.Linq;
|
---|
[4068] | 25 | using HeuristicLab.Collections;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
[3565] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Core {
|
---|
[3594] | 30 | /// <summary>
|
---|
| 31 | /// Represents a list of checked items.
|
---|
| 32 | /// </summary>
|
---|
| 33 | /// <typeparam name="T">The element type (base type is IItem)</typeparam>
|
---|
[3565] | 34 | [StorableClass]
|
---|
[3822] | 35 | [Item("CheckedItemList", "Represents a list of items that can be checked or unchecked.")]
|
---|
[3565] | 36 | public class CheckedItemList<T> : ItemList<T>, ICheckedItemList<T> where T : class, IItem {
|
---|
| 37 | [Storable]
|
---|
| 38 | private Dictionary<T, bool> checkedState;
|
---|
| 39 |
|
---|
[3594] | 40 | /// <summary>
|
---|
| 41 | /// Gets an enumerable of checked items.
|
---|
| 42 | /// </summary>
|
---|
[3565] | 43 | public IEnumerable<IndexedItem<T>> CheckedItems {
|
---|
| 44 | get {
|
---|
| 45 | return from i in Enumerable.Range(0, list.Count)
|
---|
| 46 | where ItemChecked(i)
|
---|
| 47 | select new IndexedItem<T>(i, list[i]);
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
[4722] | 50 |
|
---|
[3594] | 51 | /// <summary>
|
---|
[4722] | 52 | /// Instantiates a new CheckedItemList for deserialization.
|
---|
| 53 | /// </summary>
|
---|
| 54 | /// <param name="deserializing"></param>
|
---|
| 55 | [StorableConstructor]
|
---|
| 56 | protected CheckedItemList(bool deserializing) : base(deserializing) { }
|
---|
| 57 | protected CheckedItemList(CheckedItemList<T> original, Cloner cloner)
|
---|
| 58 | : base(original, cloner) {
|
---|
| 59 | list = new List<T>(original.Select(x => (T)cloner.Clone(x)));
|
---|
| 60 | checkedState = new Dictionary<T, bool>();
|
---|
| 61 | foreach (var pair in original.checkedState)
|
---|
| 62 | checkedState.Add(cloner.Clone(pair.Key), pair.Value);
|
---|
| 63 | }
|
---|
| 64 | /// <summary>
|
---|
[3594] | 65 | /// Instantiates an empty CheckedItemList.
|
---|
| 66 | /// </summary>
|
---|
[3565] | 67 | public CheckedItemList()
|
---|
| 68 | : base() {
|
---|
| 69 | checkedState = new Dictionary<T, bool>();
|
---|
| 70 | }
|
---|
[3594] | 71 | /// <summary>
|
---|
| 72 | /// Instantiates an empty CheckedItemList with a given initial <paramref name="capacity"/>.
|
---|
| 73 | /// </summary>
|
---|
| 74 | /// <param name="capacity">The initial capacity.</param>
|
---|
[3565] | 75 | public CheckedItemList(int capacity)
|
---|
| 76 | : base(capacity) {
|
---|
| 77 | checkedState = new Dictionary<T, bool>();
|
---|
| 78 | }
|
---|
[3594] | 79 | /// <summary>
|
---|
| 80 | /// Instantiates an CheckedItemList initially filled with the elements of <paramref name="collection"/>.
|
---|
| 81 | /// </summary>
|
---|
| 82 | /// <param name="collection">Collection of elements.</param>
|
---|
[3565] | 83 | public CheckedItemList(IEnumerable<T> collection)
|
---|
| 84 | : base(collection) {
|
---|
| 85 | checkedState = new Dictionary<T, bool>();
|
---|
[5809] | 86 | foreach (var item in list) {
|
---|
[3565] | 87 | if (!checkedState.ContainsKey(item))
|
---|
[3575] | 88 | checkedState.Add(item, true);
|
---|
[3565] | 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[3594] | 92 | /// <summary>
|
---|
| 93 | /// Gets the checked state of <paramref name="item"/>.
|
---|
| 94 | /// </summary>
|
---|
| 95 | /// <param name="item">The element to get the checked state for.</param>
|
---|
| 96 | /// <returns>The checked state of <paramref name="item"/></returns>
|
---|
[3565] | 97 | public bool ItemChecked(T item) {
|
---|
| 98 | return checkedState[item];
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[3594] | 101 | /// <summary>
|
---|
| 102 | /// Gets the checked state of item with <paramref name="index"/>.
|
---|
| 103 | /// </summary>
|
---|
| 104 | /// <param name="itemIndex">The index of the element to get the checked state for.</param>
|
---|
| 105 | /// <returns>The checked state of the element at <paramref name="itemIndex"/>.</returns>
|
---|
[3565] | 106 | public bool ItemChecked(int itemIndex) {
|
---|
| 107 | return ItemChecked(this[itemIndex]);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[3594] | 110 | /// <summary>
|
---|
| 111 | /// Sets the checked state of <paramref name="item"/> to <paramref name="checkedState"/>.
|
---|
| 112 | /// </summary>
|
---|
| 113 | /// <param name="item">The item to set the checked state for.</param>
|
---|
| 114 | /// <param name="checkedState">The new checked state of <paramref name="item"/></param>
|
---|
[3565] | 115 | public void SetItemCheckedState(T item, bool checkedState) {
|
---|
| 116 | if (!this.checkedState.ContainsKey(item)) throw new ArgumentException();
|
---|
| 117 | if (this.checkedState[item] != checkedState) {
|
---|
| 118 | this.checkedState[item] = checkedState;
|
---|
| 119 | OnCheckedItemsChanged(new IndexedItem<T>[] { new IndexedItem<T>(IndexOf(item), item) });
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[3594] | 123 | /// <summary>
|
---|
| 124 | /// Sets the checked state of the element with <paramref name="itemIndex"/> to <paramref name="checkedState"/>.
|
---|
| 125 | /// </summary>
|
---|
| 126 | /// <param name="itemIndex">The index of the item to set the checked state for.</param>
|
---|
| 127 | /// <param name="checkedState">The new checked state of the item.</param>
|
---|
[3565] | 128 | public void SetItemCheckedState(int itemIndex, bool checkedState) {
|
---|
| 129 | SetItemCheckedState(this[itemIndex], checkedState);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[3594] | 132 | /// <summary>
|
---|
| 133 | /// Adds a new <paramref name="item"/> with <paramref name="checkedState"/> to the list.
|
---|
| 134 | /// </summary>
|
---|
| 135 | /// <param name="item">The item to add to the list.</param>
|
---|
| 136 | /// <param name="checkedState">The checked state of the item added to the list.</param>
|
---|
[3567] | 137 | public void Add(T item, bool checkedState) {
|
---|
| 138 | Add(item);
|
---|
| 139 | SetItemCheckedState(item, checkedState);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[3594] | 142 | /// <summary>
|
---|
| 143 | /// Inserts a new <paramref name="item"/> at <paramref name="index"/> with <paramref name="checkedState"/> into the list.
|
---|
| 144 | /// </summary>
|
---|
| 145 | /// <param name="index">The insertion index of the new element.</param>
|
---|
| 146 | /// <param name="item">The element that is inserted into the list.</param>
|
---|
| 147 | /// <param name="checkedState">The checked state of the inserted element.</param>
|
---|
[3567] | 148 | public void Insert(int index, T item, bool checkedState) {
|
---|
| 149 | Insert(index, item);
|
---|
| 150 | SetItemCheckedState(item, checkedState);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[3594] | 153 | /// <summary>
|
---|
[3628] | 154 | /// Creates a ReadOnlyCheckedItemList containing the same elements.
|
---|
| 155 | /// </summary>
|
---|
| 156 | /// <returns>A new ReadOnlyCheckedItemList containing the same elements.</returns>
|
---|
| 157 | public new ReadOnlyCheckedItemList<T> AsReadOnly() {
|
---|
| 158 | return new ReadOnlyCheckedItemList<T>(this);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | /// <summary>
|
---|
[3594] | 162 | /// Raised after the list has been reset.
|
---|
| 163 | /// </summary>
|
---|
| 164 | /// <param name="items">Empty</param>
|
---|
| 165 | /// <param name="oldItems">The elements of the list before it has been reset.</param>
|
---|
[3565] | 166 | protected override void OnCollectionReset(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
|
---|
| 167 | foreach (var oldIndexedItem in oldItems) {
|
---|
[3798] | 168 | if (!list.Contains(oldIndexedItem.Value))
|
---|
| 169 | checkedState.Remove(oldIndexedItem.Value);
|
---|
[3565] | 170 | }
|
---|
| 171 | foreach (var indexedItem in items) {
|
---|
| 172 | if (!checkedState.ContainsKey(indexedItem.Value))
|
---|
[3575] | 173 | checkedState.Add(indexedItem.Value, true);
|
---|
[3565] | 174 | }
|
---|
| 175 | base.OnCollectionReset(items, oldItems);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[3594] | 178 | /// <summary>
|
---|
| 179 | /// Raised when new items are added to the list.
|
---|
| 180 | /// </summary>
|
---|
| 181 | /// <param name="items">The items that are added.</param>
|
---|
[3565] | 182 | protected override void OnItemsAdded(IEnumerable<IndexedItem<T>> items) {
|
---|
| 183 | foreach (var indexedItem in items)
|
---|
| 184 | if (!checkedState.ContainsKey(indexedItem.Value))
|
---|
[3575] | 185 | checkedState.Add(indexedItem.Value, true);
|
---|
[3565] | 186 | base.OnItemsAdded(items);
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[3594] | 189 | /// <summary>
|
---|
| 190 | /// Raised when items are removed from the list.
|
---|
| 191 | /// </summary>
|
---|
| 192 | /// <param name="items">Items that are removed.</param>
|
---|
[3565] | 193 | protected override void OnItemsRemoved(IEnumerable<IndexedItem<T>> items) {
|
---|
| 194 | foreach (var indexedItem in items)
|
---|
[3798] | 195 | if (!list.Contains(indexedItem.Value))
|
---|
| 196 | checkedState.Remove(indexedItem.Value);
|
---|
[3565] | 197 | base.OnItemsRemoved(items);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[3594] | 200 | /// <summary>
|
---|
| 201 | /// Raised when items are replaced.
|
---|
| 202 | /// </summary>
|
---|
| 203 | /// <param name="items">The items which replace <paramref name="oldItems"/></param>
|
---|
| 204 | /// <param name="oldItems">The items that are replaced by <paramref name="items"/></param>
|
---|
[3565] | 205 | protected override void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
|
---|
| 206 | foreach (var oldIndexedItem in oldItems)
|
---|
[3798] | 207 | if (!list.Contains(oldIndexedItem.Value))
|
---|
| 208 | checkedState.Remove(oldIndexedItem.Value);
|
---|
[3565] | 209 | foreach (var indexedItem in items)
|
---|
| 210 | if (!checkedState.ContainsKey(indexedItem.Value))
|
---|
[3575] | 211 | checkedState.Add(indexedItem.Value, true);
|
---|
[3565] | 212 | base.OnItemsReplaced(items, oldItems);
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[3594] | 215 | /// <summary>
|
---|
| 216 | /// Raised after the checked state of items has been changed.
|
---|
| 217 | /// </summary>
|
---|
| 218 | /// <param name="items">The items whose check state has been changed.</param>
|
---|
[3565] | 219 | protected virtual void OnCheckedItemsChanged(IEnumerable<IndexedItem<T>> items) {
|
---|
| 220 | RaiseCheckedItemsChanged(new CollectionItemsChangedEventArgs<IndexedItem<T>>(items));
|
---|
| 221 | }
|
---|
| 222 |
|
---|
[3594] | 223 | /// <summary>
|
---|
| 224 | /// Raised after the checked state of items has been changed.
|
---|
| 225 | /// </summary>
|
---|
[3565] | 226 | public event CollectionItemsChangedEventHandler<IndexedItem<T>> CheckedItemsChanged;
|
---|
| 227 | private void RaiseCheckedItemsChanged(CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
| 228 | var handler = CheckedItemsChanged;
|
---|
| 229 | if (handler != null) handler(this, e);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[3594] | 232 | /// <summary>
|
---|
| 233 | /// Creates a new deep clone of the CheckedItemList.
|
---|
| 234 | /// </summary>
|
---|
| 235 | /// <param name="cloner"></param>
|
---|
| 236 | /// <returns>A deep clone of the CheckedItemList</returns>
|
---|
| 237 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 238 | return new CheckedItemList<T>(this, cloner);
|
---|
[3565] | 239 | }
|
---|
| 240 | }
|
---|
| 241 | }
|
---|