Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3629 was 3628, checked in by gkronber, 15 years ago

Added ReadOnlyCheckedItemCollection and ReadOnlyCheckedItemList #992 (CheckedItemList and CheckedItemCollection is necessary)

File size: 7.3 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<T>", "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        checkedState.Remove(oldItem);
130      foreach (var item in items)
131        if (!checkedState.ContainsKey(item))
132          checkedState.Add(item, true);
133      base.OnCollectionReset(items, oldItems);
134    }
135
136    /// <summary>
137    /// Raised when new items are added to the collection.
138    /// </summary>
139    /// <param name="items">The elements that are added to the collection.</param>
140    protected override void OnItemsAdded(IEnumerable<T> items) {
141      foreach (var item in items)
142        if (!checkedState.ContainsKey(item))
143          checkedState.Add(item, true);
144      base.OnItemsAdded(items);
145    }
146
147    /// <summary>
148    /// Raised when items are removed from the collection.
149    /// </summary>
150    /// <param name="items">The items that are removed.</param>
151    protected override void OnItemsRemoved(IEnumerable<T> items) {
152      foreach (var item in items) {
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.