Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Core/3.3/Collections/CheckedItemList.cs @ 17609

Last change on this file since 17609 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 11.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HEAL.Attic;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28
29namespace HeuristicLab.Core {
30  /// <summary>
31  /// Represents a list of checked items.
32  /// </summary>
33  /// <typeparam name="T">The element type (base type is IItem)</typeparam>
34  [StorableType("83222907-689C-484B-8431-BA85DF516761")]
35  [Item("CheckedItemList", "Represents a list of items that can be checked or unchecked.")]
36  public class CheckedItemList<T> : ItemList<T>, ICheckedItemList<T> where T : class, IItem {
37    [Storable]
38    private Dictionary<T, bool> checkedState;
39
40    /// <summary>
41    /// Gets an enumerable of checked items.
42    /// </summary>
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    }
50
51    /// <summary>
52    /// Instantiates a new CheckedItemList for deserialization.
53    /// </summary>
54    /// <param name="deserializing"></param>
55    [StorableConstructor]
56    protected CheckedItemList(StorableConstructorFlag _) : base(_) { }
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>
65    /// Instantiates an empty CheckedItemList.
66    /// </summary>
67    public CheckedItemList()
68      : base() {
69      checkedState = new Dictionary<T, bool>();
70    }
71    /// <summary>
72    /// Instantiates an empty CheckedItemList with a given initial <paramref name="capacity"/>.
73    /// </summary>
74    /// <param name="capacity">The initial capacity.</param>
75    public CheckedItemList(int capacity)
76      : base(capacity) {
77      checkedState = new Dictionary<T, bool>();
78    }
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>
83    public CheckedItemList(IEnumerable<T> collection)
84      : base(collection) {
85      checkedState = new Dictionary<T, bool>();
86      foreach (var item in list) {
87        if (!checkedState.ContainsKey(item))
88          checkedState.Add(item, true);
89      }
90    }
91
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>
97    public bool ItemChecked(T item) {
98      return checkedState[item];
99    }
100
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>
106    public bool ItemChecked(int itemIndex) {
107      return ItemChecked(this[itemIndex]);
108    }
109
110    /// <summary>
111    /// Sets the checked state of <paramref name="item"/> to <paramref name="checkedState"/>.
112    /// </summary>
113    /// <remarks>
114    /// This method is slower than <see cref="SetItemCheckedState(int, bool)"/>.
115    /// </remarks>
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>
118    public void SetItemCheckedState(T item, bool checkedState) {
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        }
138      }
139      if (changed.Count > 0) OnCheckedItemsChanged(changed);
140    }
141
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>
147    public void SetItemCheckedState(int itemIndex, bool checkedState) {
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      }
154    }
155
156    /// <summary>
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>
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>
179    public void Add(T item, bool checkedState) {
180      Add(item);
181      SetItemCheckedState(item, checkedState);
182    }
183
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>
190    public void Insert(int index, T item, bool checkedState) {
191      Insert(index, item);
192      SetItemCheckedState(item, checkedState);
193    }
194
195    /// <summary>
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>
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>
208    protected override void OnCollectionReset(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
209      foreach (var oldIndexedItem in oldItems) {
210        if (!list.Contains(oldIndexedItem.Value))
211          checkedState.Remove(oldIndexedItem.Value);
212      }
213      foreach (var indexedItem in items) {
214        if (!checkedState.ContainsKey(indexedItem.Value))
215          checkedState.Add(indexedItem.Value, true);
216      }
217      base.OnCollectionReset(items, oldItems);
218    }
219
220    /// <summary>
221    /// Raised when new items are added to the list.
222    /// </summary>
223    /// <param name="items">The items that are added.</param>
224    protected override void OnItemsAdded(IEnumerable<IndexedItem<T>> items) {
225      foreach (var indexedItem in items)
226        if (!checkedState.ContainsKey(indexedItem.Value))
227          checkedState.Add(indexedItem.Value, true);
228      base.OnItemsAdded(items);
229    }
230
231    /// <summary>
232    /// Raised when items are removed from the list.
233    /// </summary>
234    /// <param name="items">Items that are removed.</param>
235    protected override void OnItemsRemoved(IEnumerable<IndexedItem<T>> items) {
236      foreach (var indexedItem in items)
237        if (!list.Contains(indexedItem.Value))
238          checkedState.Remove(indexedItem.Value);
239      base.OnItemsRemoved(items);
240    }
241
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>
247    protected override void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
248      foreach (var oldIndexedItem in oldItems)
249        if (!list.Contains(oldIndexedItem.Value))
250          checkedState.Remove(oldIndexedItem.Value);
251      foreach (var indexedItem in items)
252        if (!checkedState.ContainsKey(indexedItem.Value))
253          checkedState.Add(indexedItem.Value, true);
254      base.OnItemsReplaced(items, oldItems);
255    }
256
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>
261    protected virtual void OnCheckedItemsChanged(IEnumerable<IndexedItem<T>> items) {
262      RaiseCheckedItemsChanged(new CollectionItemsChangedEventArgs<IndexedItem<T>>(items));
263    }
264
265    /// <summary>
266    /// Raised after the checked state of items has been changed.
267    /// </summary>
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
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) {
280      return new CheckedItemList<T>(this, cloner);
281    }
282  }
283}
Note: See TracBrowser for help on using the repository browser.