Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

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