[3558] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) 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;
|
---|
[17009] | 25 | using HEAL.Attic;
|
---|
[3558] | 26 | using HeuristicLab.Collections;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 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>
|
---|
[16565] | 34 | [StorableType("CAD59659-15B5-4CB0-A199-272E28F40832")]
|
---|
[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]
|
---|
[16565] | 52 | protected CheckedItemCollection(StorableConstructorFlag _) : base(_) { }
|
---|
[4722] | 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>
|
---|
[17009] | 110 | /// Sets the checked state of <paramref name="items"/> to <paramref name="checkedState"/>.
|
---|
| 111 | /// </summary>
|
---|
| 112 | /// <param name="items">The items to set the checked state for.</param>
|
---|
| 113 | /// <param name="checkedState">The new checked state of <paramref name="item"/></param>
|
---|
| 114 | public void SetItemCheckedState(IEnumerable<T> items, bool checkedState) {
|
---|
| 115 | var changed = new List<T>();
|
---|
| 116 | foreach (var item in items) {
|
---|
| 117 | if (!this.checkedState.TryGetValue(item, out bool currentState)) throw new ArgumentException();
|
---|
| 118 | if (currentState != checkedState) {
|
---|
| 119 | this.checkedState[item] = checkedState;
|
---|
| 120 | changed.Add(item);
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | if (changed.Count > 0) OnCheckedItemsChanged(changed);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /// <summary>
|
---|
[3594] | 127 | /// Adds a new <paramref name="item"/> with the given <paramref name="checkedState"/>.
|
---|
| 128 | /// </summary>
|
---|
| 129 | /// <param name="item">The item to add.</param>
|
---|
| 130 | /// <param name="checkedState">The checked state of the item to add.</param>
|
---|
[3567] | 131 | public void Add(T item, bool checkedState) {
|
---|
| 132 | Add(item);
|
---|
| 133 | SetItemCheckedState(item, checkedState);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[3594] | 136 | /// <summary>
|
---|
[3628] | 137 | /// Creates a ReadOnlyCheckedItemCollection containing the same elements.
|
---|
| 138 | /// </summary>
|
---|
| 139 | /// <returns>A new ReadOnlyCheckedItemCollection containing the same elements.</returns>
|
---|
| 140 | public new ReadOnlyCheckedItemCollection<T> AsReadOnly() {
|
---|
| 141 | return new ReadOnlyCheckedItemCollection<T>(this);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | /// <summary>
|
---|
[3594] | 145 | /// Raised when the collection of items is reset.
|
---|
| 146 | /// </summary>
|
---|
| 147 | /// <param name="items">Empty</param>
|
---|
| 148 | /// <param name="oldItems">The elements in the collection before the reset.</param>
|
---|
[3558] | 149 | protected override void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
|
---|
| 150 | foreach (var oldItem in oldItems)
|
---|
[3798] | 151 | if (!list.Contains(oldItem))
|
---|
| 152 | checkedState.Remove(oldItem);
|
---|
[3558] | 153 | foreach (var item in items)
|
---|
[3562] | 154 | if (!checkedState.ContainsKey(item))
|
---|
[3575] | 155 | checkedState.Add(item, true);
|
---|
[3558] | 156 | base.OnCollectionReset(items, oldItems);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[3594] | 159 | /// <summary>
|
---|
| 160 | /// Raised when new items are added to the collection.
|
---|
| 161 | /// </summary>
|
---|
| 162 | /// <param name="items">The elements that are added to the collection.</param>
|
---|
[3558] | 163 | protected override void OnItemsAdded(IEnumerable<T> items) {
|
---|
| 164 | foreach (var item in items)
|
---|
[3562] | 165 | if (!checkedState.ContainsKey(item))
|
---|
[3575] | 166 | checkedState.Add(item, true);
|
---|
[3558] | 167 | base.OnItemsAdded(items);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[3594] | 170 | /// <summary>
|
---|
| 171 | /// Raised when items are removed from the collection.
|
---|
| 172 | /// </summary>
|
---|
| 173 | /// <param name="items">The items that are removed.</param>
|
---|
[3558] | 174 | protected override void OnItemsRemoved(IEnumerable<T> items) {
|
---|
| 175 | foreach (var item in items) {
|
---|
[3798] | 176 | if (!list.Contains(item))
|
---|
| 177 | checkedState.Remove(item);
|
---|
[3558] | 178 | }
|
---|
| 179 | base.OnItemsRemoved(items);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[3594] | 182 | /// <summary>
|
---|
| 183 | /// Raised when the checked state of items is changed.
|
---|
| 184 | /// </summary>
|
---|
| 185 | /// <param name="items">The item whose check state is changed.</param>
|
---|
[3564] | 186 | protected virtual void OnCheckedItemsChanged(IEnumerable<T> items) {
|
---|
[3562] | 187 | RaiseCheckedItemsChanged(new CollectionItemsChangedEventArgs<T>(items));
|
---|
[3558] | 188 | }
|
---|
| 189 |
|
---|
[3594] | 190 | /// <summary>
|
---|
| 191 | /// Raised after the checked state of items has been changed.
|
---|
| 192 | /// </summary>
|
---|
[3562] | 193 | public event CollectionItemsChangedEventHandler<T> CheckedItemsChanged;
|
---|
| 194 | private void RaiseCheckedItemsChanged(CollectionItemsChangedEventArgs<T> e) {
|
---|
| 195 | var handler = CheckedItemsChanged;
|
---|
[3558] | 196 | if (handler != null) handler(this, e);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[3594] | 199 | /// <summary>
|
---|
| 200 | /// Creates a deep clone of the CheckedItemCollection.
|
---|
| 201 | /// </summary>
|
---|
| 202 | /// <param name="cloner"></param>
|
---|
| 203 | /// <returns>A clone of the CheckedItemCollection</returns>
|
---|
| 204 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 205 | return new CheckedItemCollection<T>(this, cloner);
|
---|
[3558] | 206 | }
|
---|
| 207 | }
|
---|
| 208 | }
|
---|