Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Core/3.3/Collections/CheckedItemList.cs @ 12449

Last change on this file since 12449 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

File size: 9.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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  [StorableClass]
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(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>
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    /// <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>
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
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>
128    public void SetItemCheckedState(int itemIndex, bool checkedState) {
129      SetItemCheckedState(this[itemIndex], checkedState);
130    }
131
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>
137    public void Add(T item, bool checkedState) {
138      Add(item);
139      SetItemCheckedState(item, checkedState);
140    }
141
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>
148    public void Insert(int index, T item, bool checkedState) {
149      Insert(index, item);
150      SetItemCheckedState(item, checkedState);
151    }
152
153    /// <summary>
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>
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>
166    protected override void OnCollectionReset(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
167      foreach (var oldIndexedItem in oldItems) {
168        if (!list.Contains(oldIndexedItem.Value))
169          checkedState.Remove(oldIndexedItem.Value);
170      }
171      foreach (var indexedItem in items) {
172        if (!checkedState.ContainsKey(indexedItem.Value))
173          checkedState.Add(indexedItem.Value, true);
174      }
175      base.OnCollectionReset(items, oldItems);
176    }
177
178    /// <summary>
179    /// Raised when new items are added to the list.
180    /// </summary>
181    /// <param name="items">The items that are added.</param>
182    protected override void OnItemsAdded(IEnumerable<IndexedItem<T>> items) {
183      foreach (var indexedItem in items)
184        if (!checkedState.ContainsKey(indexedItem.Value))
185          checkedState.Add(indexedItem.Value, true);
186      base.OnItemsAdded(items);
187    }
188
189    /// <summary>
190    /// Raised when items are removed from the list.
191    /// </summary>
192    /// <param name="items">Items that are removed.</param>
193    protected override void OnItemsRemoved(IEnumerable<IndexedItem<T>> items) {
194      foreach (var indexedItem in items)
195        if (!list.Contains(indexedItem.Value))
196          checkedState.Remove(indexedItem.Value);
197      base.OnItemsRemoved(items);
198    }
199
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>
205    protected override void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
206      foreach (var oldIndexedItem in oldItems)
207        if (!list.Contains(oldIndexedItem.Value))
208          checkedState.Remove(oldIndexedItem.Value);
209      foreach (var indexedItem in items)
210        if (!checkedState.ContainsKey(indexedItem.Value))
211          checkedState.Add(indexedItem.Value, true);
212      base.OnItemsReplaced(items, oldItems);
213    }
214
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>
219    protected virtual void OnCheckedItemsChanged(IEnumerable<IndexedItem<T>> items) {
220      RaiseCheckedItemsChanged(new CollectionItemsChangedEventArgs<IndexedItem<T>>(items));
221    }
222
223    /// <summary>
224    /// Raised after the checked state of items has been changed.
225    /// </summary>
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
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) {
238      return new CheckedItemList<T>(this, cloner);
239    }
240  }
241}
Note: See TracBrowser for help on using the repository browser.