Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/Collections/CheckedItemCollection.cs @ 4058

Last change on this file since 4058 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: 7.4 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.Drawing;
25using System.Linq;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Common.Resources;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Core {
32  /// <summary>
33  /// Represents a collection of checked items.
34  /// </summary>
35  /// <typeparam name="T">The element type (base type IItem)</typeparam>
36  [StorableClass]
37  [Item("CheckedItemCollection", "Represents a collection of items that can be checked or unchecked.")]
38  public class CheckedItemCollection<T> : ItemCollection<T>, ICheckedItemCollection<T> where T : class, IItem {
39    [Storable]
40    private Dictionary<T, bool> checkedState;
41
42    /// <summary>
43    /// Gets an enumerable of checked items
44    /// </summary>
45    public IEnumerable<T> CheckedItems {
46      get { return from pair in checkedState where pair.Value select pair.Key; }
47    }
48
49    /// <summary>
50    /// Instantiates a new CheckedItemCollection.
51    /// </summary>
52    public CheckedItemCollection()
53      : base() {
54      checkedState = new Dictionary<T, bool>();
55    }
56    /// <summary>
57    /// Instantiates a new CheckedItemCollection with a predefined capacity.
58    /// </summary>
59    /// <param name="capacity">Initial capacity.</param>
60    public CheckedItemCollection(int capacity)
61      : base(capacity) {
62      checkedState = new Dictionary<T, bool>(capacity);
63    }
64    /// <summary>
65    /// Instantiates a new CheckedItemCollection containing the elements of <paramref name="collection"/>.
66    /// </summary>
67    /// <param name="collection">Initial element collection.</param>
68    public CheckedItemCollection(IEnumerable<T> collection)
69      : base(collection) {
70      checkedState = new Dictionary<T, bool>();
71      foreach (var item in collection)
72        if (!checkedState.ContainsKey(item))
73          checkedState.Add(item, true);
74    }
75    /// <summary>
76    /// Instantiates an empty CheckedItemCollection for deserialization.
77    /// </summary>
78    /// <param name="deserializing"></param>
79    [StorableConstructor]
80    protected CheckedItemCollection(bool deserializing) : base(deserializing) { }
81
82    /// <summary>
83    /// Gets the checked state of <paramref name="item"/>.
84    /// </summary>
85    /// <param name="item">The element to get the checked state for.</param>
86    /// <returns>Checked state of <paramref name="item"/></returns>
87    public bool ItemChecked(T item) {
88      return checkedState[item];
89    }
90
91    /// <summary>
92    /// Sets the checked state <paramref name="checkedState"/> of <paramref name="item"/>.
93    /// </summary>
94    /// <param name="item">The element to set the checked state for.</param>
95    /// <param name="checkedState">The new checked state of the item</param>
96    public void SetItemCheckedState(T item, bool checkedState) {
97      if (!this.checkedState.ContainsKey(item)) throw new ArgumentException();
98      if (this.checkedState[item] != checkedState) {
99        this.checkedState[item] = checkedState;
100        OnCheckedItemsChanged(new T[] { item });
101      }
102    }
103
104    /// <summary>
105    /// Adds a new <paramref name="item"/> with the given <paramref name="checkedState"/>.
106    /// </summary>
107    /// <param name="item">The item to add.</param>
108    /// <param name="checkedState">The checked state of the item to add.</param>
109    public void Add(T item, bool checkedState) {
110      Add(item);
111      SetItemCheckedState(item, checkedState);
112    }
113
114    /// <summary>
115    /// Creates a ReadOnlyCheckedItemCollection containing the same elements.
116    /// </summary>
117    /// <returns>A new ReadOnlyCheckedItemCollection containing the same elements.</returns>
118    public new ReadOnlyCheckedItemCollection<T> AsReadOnly() {
119      return new ReadOnlyCheckedItemCollection<T>(this);
120    }
121
122    /// <summary>
123    /// Raised when the collection of items is reset.
124    /// </summary>
125    /// <param name="items">Empty</param>
126    /// <param name="oldItems">The elements in the collection before the reset.</param>
127    protected override void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
128      foreach (var oldItem in oldItems)
129        if (!list.Contains(oldItem))
130          checkedState.Remove(oldItem);
131      foreach (var item in items)
132        if (!checkedState.ContainsKey(item))
133          checkedState.Add(item, true);
134      base.OnCollectionReset(items, oldItems);
135    }
136
137    /// <summary>
138    /// Raised when new items are added to the collection.
139    /// </summary>
140    /// <param name="items">The elements that are added to the collection.</param>
141    protected override void OnItemsAdded(IEnumerable<T> items) {
142      foreach (var item in items)
143        if (!checkedState.ContainsKey(item))
144          checkedState.Add(item, true);
145      base.OnItemsAdded(items);
146    }
147
148    /// <summary>
149    /// Raised when items are removed from the collection.
150    /// </summary>
151    /// <param name="items">The items that are removed.</param>
152    protected override void OnItemsRemoved(IEnumerable<T> items) {
153      foreach (var item in items) {
154        if (!list.Contains(item))
155          checkedState.Remove(item);
156      }
157      base.OnItemsRemoved(items);
158    }
159
160    /// <summary>
161    /// Raised when the checked state of items is changed.
162    /// </summary>
163    /// <param name="items">The item whose check state is changed.</param>
164    protected virtual void OnCheckedItemsChanged(IEnumerable<T> items) {
165      RaiseCheckedItemsChanged(new CollectionItemsChangedEventArgs<T>(items));
166    }
167
168    /// <summary>
169    /// Raised after the checked state of items has been changed.
170    /// </summary>
171    public event CollectionItemsChangedEventHandler<T> CheckedItemsChanged;
172    private void RaiseCheckedItemsChanged(CollectionItemsChangedEventArgs<T> e) {
173      var handler = CheckedItemsChanged;
174      if (handler != null) handler(this, e);
175    }
176
177    /// <summary>
178    /// Creates a deep clone of the CheckedItemCollection.
179    /// </summary>
180    /// <param name="cloner"></param>
181    /// <returns>A clone of the CheckedItemCollection</returns>
182    public override IDeepCloneable Clone(Cloner cloner) {
183      CheckedItemCollection<T> clone = (CheckedItemCollection<T>)Activator.CreateInstance(this.GetType());
184      cloner.RegisterClonedObject(this, clone);
185      clone.list = new List<T>(this.Select(x => (T)cloner.Clone(x)));
186      clone.checkedState = new Dictionary<T, bool>();
187      foreach (var pair in checkedState)
188        clone.checkedState.Add((T)cloner.Clone(pair.Key), pair.Value);
189      return clone;
190    }
191  }
192}
Note: See TracBrowser for help on using the repository browser.