[3565] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) 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;
|
---|
[17147] | 25 | using HEAL.Attic;
|
---|
[4068] | 26 | using HeuristicLab.Collections;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
[3565] | 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>
|
---|
[17097] | 34 | [StorableType("83222907-689C-484B-8431-BA85DF516761")]
|
---|
[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]
|
---|
[17097] | 56 | protected CheckedItemList(StorableConstructorFlag _) : base(_) { }
|
---|
[4722] | 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>
|
---|
[17147] | 113 | /// <remarks>
|
---|
| 114 | /// This method is slower than <see cref="SetItemCheckedState(int, bool)"/>.
|
---|
| 115 | /// </remarks>
|
---|
[3594] | 116 | /// <param name="item">The item to set the checked state for.</param>
|
---|
| 117 | /// <param name="checkedState">The new checked state of <paramref name="item"/></param>
|
---|
[3565] | 118 | public void SetItemCheckedState(T item, bool checkedState) {
|
---|
[17147] | 119 | SetItemCheckedState(IndexOf(item), checkedState);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | /// <summary>
|
---|
| 123 | /// Sets the checked state of <paramref name="items"/> to <paramref name="checkedState"/>.
|
---|
| 124 | /// </summary>
|
---|
| 125 | /// <remarks>
|
---|
| 126 | /// This method is slower than <see cref="SetItemCheckedState(IEnumerable{int}, bool)"/>.
|
---|
| 127 | /// </remarks>
|
---|
| 128 | /// <param name="items">The items to set the checked state for.</param>
|
---|
| 129 | /// <param name="checkedState">The new checked state of <paramref name="item"/></param>
|
---|
| 130 | public void SetItemCheckedState(IEnumerable<T> items, bool checkedState) {
|
---|
| 131 | var changed = new List<IndexedItem<T>>();
|
---|
| 132 | foreach (var item in items) {
|
---|
| 133 | if (!this.checkedState.TryGetValue(item, out bool currentState)) throw new ArgumentException();
|
---|
| 134 | if (currentState != checkedState) {
|
---|
| 135 | this.checkedState[item] = checkedState;
|
---|
| 136 | changed.Add(new IndexedItem<T>(IndexOf(item), item));
|
---|
| 137 | }
|
---|
[3565] | 138 | }
|
---|
[17147] | 139 | if (changed.Count > 0) OnCheckedItemsChanged(changed);
|
---|
[3565] | 140 | }
|
---|
| 141 |
|
---|
[3594] | 142 | /// <summary>
|
---|
| 143 | /// Sets the checked state of the element with <paramref name="itemIndex"/> to <paramref name="checkedState"/>.
|
---|
| 144 | /// </summary>
|
---|
| 145 | /// <param name="itemIndex">The index of the item to set the checked state for.</param>
|
---|
| 146 | /// <param name="checkedState">The new checked state of the item.</param>
|
---|
[3565] | 147 | public void SetItemCheckedState(int itemIndex, bool checkedState) {
|
---|
[17147] | 148 | var item = list[itemIndex];
|
---|
| 149 | if (!this.checkedState.TryGetValue(item, out bool currentState)) throw new ArgumentException();
|
---|
| 150 | if (currentState != checkedState) {
|
---|
| 151 | this.checkedState[item] = checkedState;
|
---|
| 152 | OnCheckedItemsChanged(new IndexedItem<T>[] { new IndexedItem<T>(itemIndex, item) });
|
---|
| 153 | }
|
---|
[3565] | 154 | }
|
---|
| 155 |
|
---|
[3594] | 156 | /// <summary>
|
---|
[17147] | 157 | /// Sets the checked state of all <paramref name="itemIndices"/> to <paramref name="checkedState"/>.
|
---|
| 158 | /// </summary>
|
---|
| 159 | /// <param name="itemIndices">The indices of all items to set the checked state for.</param>
|
---|
| 160 | /// <param name="checkedState">The new checked state of the item.</param>
|
---|
| 161 | public void SetItemCheckedState(IEnumerable<int> itemIndices, bool checkedState) {
|
---|
| 162 | var changed = new List<IndexedItem<T>>();
|
---|
| 163 | foreach (var index in itemIndices) {
|
---|
| 164 | var item = list[index];
|
---|
| 165 | if (!this.checkedState.TryGetValue(item, out bool currentState)) throw new ArgumentException();
|
---|
| 166 | if (currentState != checkedState) {
|
---|
| 167 | this.checkedState[item] = checkedState;
|
---|
| 168 | changed.Add(new IndexedItem<T>(index, item));
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 | if (changed.Count > 0) OnCheckedItemsChanged(changed);
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | /// <summary>
|
---|
[3594] | 175 | /// Adds a new <paramref name="item"/> with <paramref name="checkedState"/> to the list.
|
---|
| 176 | /// </summary>
|
---|
| 177 | /// <param name="item">The item to add to the list.</param>
|
---|
| 178 | /// <param name="checkedState">The checked state of the item added to the list.</param>
|
---|
[3567] | 179 | public void Add(T item, bool checkedState) {
|
---|
| 180 | Add(item);
|
---|
| 181 | SetItemCheckedState(item, checkedState);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[3594] | 184 | /// <summary>
|
---|
| 185 | /// Inserts a new <paramref name="item"/> at <paramref name="index"/> with <paramref name="checkedState"/> into the list.
|
---|
| 186 | /// </summary>
|
---|
| 187 | /// <param name="index">The insertion index of the new element.</param>
|
---|
| 188 | /// <param name="item">The element that is inserted into the list.</param>
|
---|
| 189 | /// <param name="checkedState">The checked state of the inserted element.</param>
|
---|
[3567] | 190 | public void Insert(int index, T item, bool checkedState) {
|
---|
| 191 | Insert(index, item);
|
---|
| 192 | SetItemCheckedState(item, checkedState);
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[3594] | 195 | /// <summary>
|
---|
[3628] | 196 | /// Creates a ReadOnlyCheckedItemList containing the same elements.
|
---|
| 197 | /// </summary>
|
---|
| 198 | /// <returns>A new ReadOnlyCheckedItemList containing the same elements.</returns>
|
---|
| 199 | public new ReadOnlyCheckedItemList<T> AsReadOnly() {
|
---|
| 200 | return new ReadOnlyCheckedItemList<T>(this);
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | /// <summary>
|
---|
[3594] | 204 | /// Raised after the list has been reset.
|
---|
| 205 | /// </summary>
|
---|
| 206 | /// <param name="items">Empty</param>
|
---|
| 207 | /// <param name="oldItems">The elements of the list before it has been reset.</param>
|
---|
[3565] | 208 | protected override void OnCollectionReset(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
|
---|
| 209 | foreach (var oldIndexedItem in oldItems) {
|
---|
[3798] | 210 | if (!list.Contains(oldIndexedItem.Value))
|
---|
| 211 | checkedState.Remove(oldIndexedItem.Value);
|
---|
[3565] | 212 | }
|
---|
| 213 | foreach (var indexedItem in items) {
|
---|
| 214 | if (!checkedState.ContainsKey(indexedItem.Value))
|
---|
[3575] | 215 | checkedState.Add(indexedItem.Value, true);
|
---|
[3565] | 216 | }
|
---|
| 217 | base.OnCollectionReset(items, oldItems);
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[3594] | 220 | /// <summary>
|
---|
| 221 | /// Raised when new items are added to the list.
|
---|
| 222 | /// </summary>
|
---|
| 223 | /// <param name="items">The items that are added.</param>
|
---|
[3565] | 224 | protected override void OnItemsAdded(IEnumerable<IndexedItem<T>> items) {
|
---|
| 225 | foreach (var indexedItem in items)
|
---|
| 226 | if (!checkedState.ContainsKey(indexedItem.Value))
|
---|
[3575] | 227 | checkedState.Add(indexedItem.Value, true);
|
---|
[3565] | 228 | base.OnItemsAdded(items);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[3594] | 231 | /// <summary>
|
---|
| 232 | /// Raised when items are removed from the list.
|
---|
| 233 | /// </summary>
|
---|
| 234 | /// <param name="items">Items that are removed.</param>
|
---|
[3565] | 235 | protected override void OnItemsRemoved(IEnumerable<IndexedItem<T>> items) {
|
---|
| 236 | foreach (var indexedItem in items)
|
---|
[3798] | 237 | if (!list.Contains(indexedItem.Value))
|
---|
| 238 | checkedState.Remove(indexedItem.Value);
|
---|
[3565] | 239 | base.OnItemsRemoved(items);
|
---|
| 240 | }
|
---|
| 241 |
|
---|
[3594] | 242 | /// <summary>
|
---|
| 243 | /// Raised when items are replaced.
|
---|
| 244 | /// </summary>
|
---|
| 245 | /// <param name="items">The items which replace <paramref name="oldItems"/></param>
|
---|
| 246 | /// <param name="oldItems">The items that are replaced by <paramref name="items"/></param>
|
---|
[3565] | 247 | protected override void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
|
---|
| 248 | foreach (var oldIndexedItem in oldItems)
|
---|
[3798] | 249 | if (!list.Contains(oldIndexedItem.Value))
|
---|
| 250 | checkedState.Remove(oldIndexedItem.Value);
|
---|
[3565] | 251 | foreach (var indexedItem in items)
|
---|
| 252 | if (!checkedState.ContainsKey(indexedItem.Value))
|
---|
[3575] | 253 | checkedState.Add(indexedItem.Value, true);
|
---|
[3565] | 254 | base.OnItemsReplaced(items, oldItems);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
[3594] | 257 | /// <summary>
|
---|
| 258 | /// Raised after the checked state of items has been changed.
|
---|
| 259 | /// </summary>
|
---|
| 260 | /// <param name="items">The items whose check state has been changed.</param>
|
---|
[3565] | 261 | protected virtual void OnCheckedItemsChanged(IEnumerable<IndexedItem<T>> items) {
|
---|
| 262 | RaiseCheckedItemsChanged(new CollectionItemsChangedEventArgs<IndexedItem<T>>(items));
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[3594] | 265 | /// <summary>
|
---|
| 266 | /// Raised after the checked state of items has been changed.
|
---|
| 267 | /// </summary>
|
---|
[3565] | 268 | public event CollectionItemsChangedEventHandler<IndexedItem<T>> CheckedItemsChanged;
|
---|
| 269 | private void RaiseCheckedItemsChanged(CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
| 270 | var handler = CheckedItemsChanged;
|
---|
| 271 | if (handler != null) handler(this, e);
|
---|
| 272 | }
|
---|
| 273 |
|
---|
[3594] | 274 | /// <summary>
|
---|
| 275 | /// Creates a new deep clone of the CheckedItemList.
|
---|
| 276 | /// </summary>
|
---|
| 277 | /// <param name="cloner"></param>
|
---|
| 278 | /// <returns>A deep clone of the CheckedItemList</returns>
|
---|
| 279 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 280 | return new CheckedItemList<T>(this, cloner);
|
---|
[3565] | 281 | }
|
---|
| 282 | }
|
---|
| 283 | }
|
---|