Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4068 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 9.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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    /// <summary>
51    /// Instantiates an empty CheckedItemList.
52    /// </summary>
53    public CheckedItemList()
54      : base() {
55      checkedState = new Dictionary<T, bool>();
56    }
57    /// <summary>
58    /// Instantiates an empty CheckedItemList with a given initial <paramref name="capacity"/>.
59    /// </summary>
60    /// <param name="capacity">The initial capacity.</param>
61    public CheckedItemList(int capacity)
62      : base(capacity) {
63      checkedState = new Dictionary<T, bool>();
64    }
65    /// <summary>
66    /// Instantiates an CheckedItemList initially filled with the elements of <paramref name="collection"/>.
67    /// </summary>
68    /// <param name="collection">Collection of elements.</param>
69    public CheckedItemList(IEnumerable<T> collection)
70      : base(collection) {
71      checkedState = new Dictionary<T, bool>();
72      foreach (var item in collection) {
73        if (!checkedState.ContainsKey(item))
74          checkedState.Add(item, true);
75      }
76    }
77    /// <summary>
78    /// Instantiates a new CheckedItemList for deserialization.
79    /// </summary>
80    /// <param name="deserializing"></param>
81    [StorableConstructor]
82    protected CheckedItemList(bool deserializing) : base(deserializing) { }
83
84    /// <summary>
85    /// Gets the checked state of <paramref name="item"/>.
86    /// </summary>
87    /// <param name="item">The element to get the checked state for.</param>
88    /// <returns>The checked state of <paramref name="item"/></returns>
89    public bool ItemChecked(T item) {
90      return checkedState[item];
91    }
92
93    /// <summary>
94    /// Gets the checked state of item with <paramref name="index"/>.
95    /// </summary>
96    /// <param name="itemIndex">The index of the element to get the checked state for.</param>
97    /// <returns>The checked state of the element at <paramref name="itemIndex"/>.</returns>
98    public bool ItemChecked(int itemIndex) {
99      return ItemChecked(this[itemIndex]);
100    }
101
102    /// <summary>
103    /// Sets the checked state of <paramref name="item"/> to <paramref name="checkedState"/>.
104    /// </summary>
105    /// <param name="item">The item to set the checked state for.</param>
106    /// <param name="checkedState">The new checked state of <paramref name="item"/></param>
107    public void SetItemCheckedState(T item, bool checkedState) {
108      if (!this.checkedState.ContainsKey(item)) throw new ArgumentException();
109      if (this.checkedState[item] != checkedState) {
110        this.checkedState[item] = checkedState;
111        OnCheckedItemsChanged(new IndexedItem<T>[] { new IndexedItem<T>(IndexOf(item), item) });
112      }
113    }
114
115    /// <summary>
116    /// Sets the checked state of the element with <paramref name="itemIndex"/> to <paramref name="checkedState"/>.
117    /// </summary>
118    /// <param name="itemIndex">The index of the item to set the checked state for.</param>
119    /// <param name="checkedState">The new checked state of the item.</param>
120    public void SetItemCheckedState(int itemIndex, bool checkedState) {
121      SetItemCheckedState(this[itemIndex], checkedState);
122    }
123
124    /// <summary>
125    /// Adds a new <paramref name="item"/> with <paramref name="checkedState"/> to the list.
126    /// </summary>
127    /// <param name="item">The item to add to the list.</param>
128    /// <param name="checkedState">The checked state of the item added to the list.</param>
129    public void Add(T item, bool checkedState) {
130      Add(item);
131      SetItemCheckedState(item, checkedState);
132    }
133
134    /// <summary>
135    /// Inserts a new <paramref name="item"/> at <paramref name="index"/> with <paramref name="checkedState"/> into the list.
136    /// </summary>
137    /// <param name="index">The insertion index of the new element.</param>
138    /// <param name="item">The element that is inserted into the list.</param>
139    /// <param name="checkedState">The checked state of the inserted element.</param>
140    public void Insert(int index, T item, bool checkedState) {
141      Insert(index, item);
142      SetItemCheckedState(item, checkedState);
143    }
144
145    /// <summary>
146    /// Creates a ReadOnlyCheckedItemList containing the same elements.
147    /// </summary>
148    /// <returns>A new ReadOnlyCheckedItemList containing the same elements.</returns>
149    public new ReadOnlyCheckedItemList<T> AsReadOnly() {
150      return new ReadOnlyCheckedItemList<T>(this);
151    }
152
153    /// <summary>
154    /// Raised after the list has been reset.
155    /// </summary>
156    /// <param name="items">Empty</param>
157    /// <param name="oldItems">The elements of the list before it has been reset.</param>
158    protected override void OnCollectionReset(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
159      foreach (var oldIndexedItem in oldItems) {
160        if (!list.Contains(oldIndexedItem.Value))
161          checkedState.Remove(oldIndexedItem.Value);
162      }
163      foreach (var indexedItem in items) {
164        if (!checkedState.ContainsKey(indexedItem.Value))
165          checkedState.Add(indexedItem.Value, true);
166      }
167      base.OnCollectionReset(items, oldItems);
168    }
169
170    /// <summary>
171    /// Raised when new items are added to the list.
172    /// </summary>
173    /// <param name="items">The items that are added.</param>
174    protected override void OnItemsAdded(IEnumerable<IndexedItem<T>> items) {
175      foreach (var indexedItem in items)
176        if (!checkedState.ContainsKey(indexedItem.Value))
177          checkedState.Add(indexedItem.Value, true);
178      base.OnItemsAdded(items);
179    }
180
181    /// <summary>
182    /// Raised when items are removed from the list.
183    /// </summary>
184    /// <param name="items">Items that are removed.</param>
185    protected override void OnItemsRemoved(IEnumerable<IndexedItem<T>> items) {
186      foreach (var indexedItem in items)
187        if (!list.Contains(indexedItem.Value))
188          checkedState.Remove(indexedItem.Value);
189      base.OnItemsRemoved(items);
190    }
191
192    /// <summary>
193    /// Raised when items are replaced.
194    /// </summary>
195    /// <param name="items">The items which replace <paramref name="oldItems"/></param>
196    /// <param name="oldItems">The items that are replaced by <paramref name="items"/></param>
197    protected override void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
198      foreach (var oldIndexedItem in oldItems)
199        if (!list.Contains(oldIndexedItem.Value))
200          checkedState.Remove(oldIndexedItem.Value);
201      foreach (var indexedItem in items)
202        if (!checkedState.ContainsKey(indexedItem.Value))
203          checkedState.Add(indexedItem.Value, true);
204      base.OnItemsReplaced(items, oldItems);
205    }
206
207    /// <summary>
208    /// Raised after the checked state of items has been changed.
209    /// </summary>
210    /// <param name="items">The items whose check state has been changed.</param>
211    protected virtual void OnCheckedItemsChanged(IEnumerable<IndexedItem<T>> items) {
212      RaiseCheckedItemsChanged(new CollectionItemsChangedEventArgs<IndexedItem<T>>(items));
213    }
214
215    /// <summary>
216    /// Raised after the checked state of items has been changed.
217    /// </summary>
218    public event CollectionItemsChangedEventHandler<IndexedItem<T>> CheckedItemsChanged;
219    private void RaiseCheckedItemsChanged(CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
220      var handler = CheckedItemsChanged;
221      if (handler != null) handler(this, e);
222    }
223
224    /// <summary>
225    /// Creates a new deep clone of the CheckedItemList.
226    /// </summary>
227    /// <param name="cloner"></param>
228    /// <returns>A deep clone of the CheckedItemList</returns>
229    public override IDeepCloneable Clone(Cloner cloner) {
230      CheckedItemList<T> clone = (CheckedItemList<T>)Activator.CreateInstance(this.GetType());
231      cloner.RegisterClonedObject(this, clone);
232      clone.list = new List<T>(this.Select(x => (T)cloner.Clone(x)));
233      clone.checkedState = new Dictionary<T, bool>();
234      foreach (var pair in checkedState)
235        clone.checkedState.Add((T)cloner.Clone(pair.Key), pair.Value);
236      return clone;
237    }
238  }
239}
Note: See TracBrowser for help on using the repository browser.