Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.0/HeuristicLab.Core/3.3/Collections/CheckedItemList.cs @ 8512

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

Implemented reviewers' comments (#863)

  • adapted item names of generic items to reflect the type of their generic parameters
File size: 9.9 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.Linq;
24using System.Collections;
25using System.Collections.Generic;
26using System.Collections.ObjectModel;
27using System.Text;
28using System.Drawing;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Common;
31using HeuristicLab.Common.Resources;
32using HeuristicLab.Collections;
33
34namespace HeuristicLab.Core {
35  /// <summary>
36  /// Represents a list of checked items.
37  /// </summary>
38  /// <typeparam name="T">The element type (base type is IItem)</typeparam>
39  [StorableClass]
40  [Item("CheckedItemList", "Represents a list of items that can be checked or unchecked.")]
41  public class CheckedItemList<T> : ItemList<T>, ICheckedItemList<T> where T : class, IItem {
42    [Storable]
43    private Dictionary<T, bool> checkedState;
44
45    /// <summary>
46    /// Gets an enumerable of checked items.
47    /// </summary>
48    public IEnumerable<IndexedItem<T>> CheckedItems {
49      get {
50        return from i in Enumerable.Range(0, list.Count)
51               where ItemChecked(i)
52               select new IndexedItem<T>(i, list[i]);
53      }
54    }
55    /// <summary>
56    /// Instantiates an empty CheckedItemList.
57    /// </summary>
58    public CheckedItemList()
59      : base() {
60      checkedState = new Dictionary<T, bool>();
61    }
62    /// <summary>
63    /// Instantiates an empty CheckedItemList with a given initial <paramref name="capacity"/>.
64    /// </summary>
65    /// <param name="capacity">The initial capacity.</param>
66    public CheckedItemList(int capacity)
67      : base(capacity) {
68      checkedState = new Dictionary<T, bool>();
69    }
70    /// <summary>
71    /// Instantiates an CheckedItemList initially filled with the elements of <paramref name="collection"/>.
72    /// </summary>
73    /// <param name="collection">Collection of elements.</param>
74    public CheckedItemList(IEnumerable<T> collection)
75      : base(collection) {
76      checkedState = new Dictionary<T, bool>();
77      foreach (var item in collection) {
78        if (!checkedState.ContainsKey(item))
79          checkedState.Add(item, true);
80      }
81    }
82    /// <summary>
83    /// Instantiates a new CheckedItemList for deserialization.
84    /// </summary>
85    /// <param name="deserializing"></param>
86    [StorableConstructor]
87    protected CheckedItemList(bool deserializing) : base(deserializing) { }
88
89    /// <summary>
90    /// Gets the checked state of <paramref name="item"/>.
91    /// </summary>
92    /// <param name="item">The element to get the checked state for.</param>
93    /// <returns>The checked state of <paramref name="item"/></returns>
94    public bool ItemChecked(T item) {
95      return checkedState[item];
96    }
97
98    /// <summary>
99    /// Gets the checked state of item with <paramref name="index"/>.
100    /// </summary>
101    /// <param name="itemIndex">The index of the element to get the checked state for.</param>
102    /// <returns>The checked state of the element at <paramref name="itemIndex"/>.</returns>
103    public bool ItemChecked(int itemIndex) {
104      return ItemChecked(this[itemIndex]);
105    }
106
107    /// <summary>
108    /// Sets the checked state of <paramref name="item"/> to <paramref name="checkedState"/>.
109    /// </summary>
110    /// <param name="item">The item to set the checked state for.</param>
111    /// <param name="checkedState">The new checked state of <paramref name="item"/></param>
112    public void SetItemCheckedState(T item, bool checkedState) {
113      if (!this.checkedState.ContainsKey(item)) throw new ArgumentException();
114      if (this.checkedState[item] != checkedState) {
115        this.checkedState[item] = checkedState;
116        OnCheckedItemsChanged(new IndexedItem<T>[] { new IndexedItem<T>(IndexOf(item), item) });
117      }
118    }
119
120    /// <summary>
121    /// Sets the checked state of the element with <paramref name="itemIndex"/> to <paramref name="checkedState"/>.
122    /// </summary>
123    /// <param name="itemIndex">The index of the item to set the checked state for.</param>
124    /// <param name="checkedState">The new checked state of the item.</param>
125    public void SetItemCheckedState(int itemIndex, bool checkedState) {
126      SetItemCheckedState(this[itemIndex], checkedState);
127    }
128
129    /// <summary>
130    /// Adds a new <paramref name="item"/> with <paramref name="checkedState"/> to the list.
131    /// </summary>
132    /// <param name="item">The item to add to the list.</param>
133    /// <param name="checkedState">The checked state of the item added to the list.</param>
134    public void Add(T item, bool checkedState) {
135      Add(item);
136      SetItemCheckedState(item, checkedState);
137    }
138
139    /// <summary>
140    /// Inserts a new <paramref name="item"/> at <paramref name="index"/> with <paramref name="checkedState"/> into the list.
141    /// </summary>
142    /// <param name="index">The insertion index of the new element.</param>
143    /// <param name="item">The element that is inserted into the list.</param>
144    /// <param name="checkedState">The checked state of the inserted element.</param>
145    public void Insert(int index, T item, bool checkedState) {
146      Insert(index, item);
147      SetItemCheckedState(item, checkedState);
148    }
149
150    /// <summary>
151    /// Creates a ReadOnlyCheckedItemList containing the same elements.
152    /// </summary>
153    /// <returns>A new ReadOnlyCheckedItemList containing the same elements.</returns>
154    public new ReadOnlyCheckedItemList<T> AsReadOnly() {
155      return new ReadOnlyCheckedItemList<T>(this);
156    }
157
158    /// <summary>
159    /// Raised after the list has been reset.
160    /// </summary>
161    /// <param name="items">Empty</param>
162    /// <param name="oldItems">The elements of the list before it has been reset.</param>
163    protected override void OnCollectionReset(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
164      foreach (var oldIndexedItem in oldItems) {
165        if (!list.Contains(oldIndexedItem.Value))
166          checkedState.Remove(oldIndexedItem.Value);
167      }
168      foreach (var indexedItem in items) {
169        if (!checkedState.ContainsKey(indexedItem.Value))
170          checkedState.Add(indexedItem.Value, true);
171      }
172      base.OnCollectionReset(items, oldItems);
173    }
174
175    /// <summary>
176    /// Raised when new items are added to the list.
177    /// </summary>
178    /// <param name="items">The items that are added.</param>
179    protected override void OnItemsAdded(IEnumerable<IndexedItem<T>> items) {
180      foreach (var indexedItem in items)
181        if (!checkedState.ContainsKey(indexedItem.Value))
182          checkedState.Add(indexedItem.Value, true);
183      base.OnItemsAdded(items);
184    }
185
186    /// <summary>
187    /// Raised when items are removed from the list.
188    /// </summary>
189    /// <param name="items">Items that are removed.</param>
190    protected override void OnItemsRemoved(IEnumerable<IndexedItem<T>> items) {
191      foreach (var indexedItem in items)
192        if (!list.Contains(indexedItem.Value))
193          checkedState.Remove(indexedItem.Value);
194      base.OnItemsRemoved(items);
195    }
196
197    /// <summary>
198    /// Raised when items are replaced.
199    /// </summary>
200    /// <param name="items">The items which replace <paramref name="oldItems"/></param>
201    /// <param name="oldItems">The items that are replaced by <paramref name="items"/></param>
202    protected override void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
203      foreach (var oldIndexedItem in oldItems)
204        if (!list.Contains(oldIndexedItem.Value))
205          checkedState.Remove(oldIndexedItem.Value);
206      foreach (var indexedItem in items)
207        if (!checkedState.ContainsKey(indexedItem.Value))
208          checkedState.Add(indexedItem.Value, true);
209      base.OnItemsReplaced(items, oldItems);
210    }
211
212    /// <summary>
213    /// Raised after the checked state of items has been changed.
214    /// </summary>
215    /// <param name="items">The items whose check state has been changed.</param>
216    protected virtual void OnCheckedItemsChanged(IEnumerable<IndexedItem<T>> items) {
217      RaiseCheckedItemsChanged(new CollectionItemsChangedEventArgs<IndexedItem<T>>(items));
218    }
219
220    /// <summary>
221    /// Raised after the checked state of items has been changed.
222    /// </summary>
223    public event CollectionItemsChangedEventHandler<IndexedItem<T>> CheckedItemsChanged;
224    private void RaiseCheckedItemsChanged(CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
225      var handler = CheckedItemsChanged;
226      if (handler != null) handler(this, e);
227    }
228
229    /// <summary>
230    /// Creates a new deep clone of the CheckedItemList.
231    /// </summary>
232    /// <param name="cloner"></param>
233    /// <returns>A deep clone of the CheckedItemList</returns>
234    public override IDeepCloneable Clone(Cloner cloner) {
235      CheckedItemList<T> clone = (CheckedItemList<T>)Activator.CreateInstance(this.GetType());
236      cloner.RegisterClonedObject(this, clone);
237      clone.list = new List<T>(this.Select(x => (T)cloner.Clone(x)));
238      clone.checkedState = new Dictionary<T, bool>();
239      foreach (var pair in checkedState)
240        clone.checkedState.Add((T)cloner.Clone(pair.Key), pair.Value);
241      return clone;
242    }
243  }
244}
Note: See TracBrowser for help on using the repository browser.