Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17989 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 8.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
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  [StorableType("CAD59659-15B5-4CB0-A199-272E28F40832")]
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 an empty CheckedItemCollection for deserialization.
49    /// </summary>
50    /// <param name="deserializing"></param>
51    [StorableConstructor]
52    protected CheckedItemCollection(StorableConstructorFlag _) : base(_) { }
53    protected CheckedItemCollection(CheckedItemCollection<T> original, Cloner cloner)
54      : base(original, cloner) {
55      list = new List<T>(original.Select(x => cloner.Clone(x)));
56      checkedState = new Dictionary<T, bool>();
57      foreach (var pair in original.checkedState)
58        checkedState.Add(cloner.Clone(pair.Key), pair.Value);
59    }
60    /// <summary>
61    /// Instantiates a new CheckedItemCollection.
62    /// </summary>
63    public CheckedItemCollection()
64      : base() {
65      checkedState = new Dictionary<T, bool>();
66    }
67    /// <summary>
68    /// Instantiates a new CheckedItemCollection with a predefined capacity.
69    /// </summary>
70    /// <param name="capacity">Initial capacity.</param>
71    public CheckedItemCollection(int capacity)
72      : base(capacity) {
73      checkedState = new Dictionary<T, bool>(capacity);
74    }
75    /// <summary>
76    /// Instantiates a new CheckedItemCollection containing the elements of <paramref name="collection"/>.
77    /// </summary>
78    /// <param name="collection">Initial element collection.</param>
79    public CheckedItemCollection(IEnumerable<T> collection)
80      : base(collection) {
81      checkedState = new Dictionary<T, bool>();
82      foreach (var item in list)
83        if (!checkedState.ContainsKey(item))
84          checkedState.Add(item, true);
85    }
86
87    /// <summary>
88    /// Gets the checked state of <paramref name="item"/>.
89    /// </summary>
90    /// <param name="item">The element to get the checked state for.</param>
91    /// <returns>Checked state of <paramref name="item"/></returns>
92    public bool ItemChecked(T item) {
93      return checkedState[item];
94    }
95
96    /// <summary>
97    /// Sets the checked state <paramref name="checkedState"/> of <paramref name="item"/>.
98    /// </summary>
99    /// <param name="item">The element to set the checked state for.</param>
100    /// <param name="checkedState">The new checked state of the item</param>
101    public void SetItemCheckedState(T item, bool checkedState) {
102      if (!this.checkedState.ContainsKey(item)) throw new ArgumentException();
103      if (this.checkedState[item] != checkedState) {
104        this.checkedState[item] = checkedState;
105        OnCheckedItemsChanged(new T[] { item });
106      }
107    }
108
109    /// <summary>
110    /// Sets the checked state of <paramref name="items"/> to <paramref name="checkedState"/>.
111    /// </summary>
112    /// <param name="items">The items to set the checked state for.</param>
113    /// <param name="checkedState">The new checked state of <paramref name="item"/></param>
114    public void SetItemCheckedState(IEnumerable<T> items, bool checkedState) {
115      var changed = new List<T>();
116      foreach (var item in items) {
117        if (!this.checkedState.TryGetValue(item, out bool currentState)) throw new ArgumentException();
118        if (currentState != checkedState) {
119          this.checkedState[item] = checkedState;
120          changed.Add(item);
121        }
122      }
123      if (changed.Count > 0) OnCheckedItemsChanged(changed);
124    }
125
126    /// <summary>
127    /// Adds a new <paramref name="item"/> with the given <paramref name="checkedState"/>.
128    /// </summary>
129    /// <param name="item">The item to add.</param>
130    /// <param name="checkedState">The checked state of the item to add.</param>
131    public void Add(T item, bool checkedState) {
132      Add(item);
133      SetItemCheckedState(item, checkedState);
134    }
135
136    /// <summary>
137    /// Creates a ReadOnlyCheckedItemCollection containing the same elements.
138    /// </summary>
139    /// <returns>A new ReadOnlyCheckedItemCollection containing the same elements.</returns>
140    public new ReadOnlyCheckedItemCollection<T> AsReadOnly() {
141      return new ReadOnlyCheckedItemCollection<T>(this);
142    }
143
144    /// <summary>
145    /// Raised when the collection of items is reset.
146    /// </summary>
147    /// <param name="items">Empty</param>
148    /// <param name="oldItems">The elements in the collection before the reset.</param>
149    protected override void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
150      foreach (var oldItem in oldItems)
151        if (!list.Contains(oldItem))
152          checkedState.Remove(oldItem);
153      foreach (var item in items)
154        if (!checkedState.ContainsKey(item))
155          checkedState.Add(item, true);
156      base.OnCollectionReset(items, oldItems);
157    }
158
159    /// <summary>
160    /// Raised when new items are added to the collection.
161    /// </summary>
162    /// <param name="items">The elements that are added to the collection.</param>
163    protected override void OnItemsAdded(IEnumerable<T> items) {
164      foreach (var item in items)
165        if (!checkedState.ContainsKey(item))
166          checkedState.Add(item, true);
167      base.OnItemsAdded(items);
168    }
169
170    /// <summary>
171    /// Raised when items are removed from the collection.
172    /// </summary>
173    /// <param name="items">The items that are removed.</param>
174    protected override void OnItemsRemoved(IEnumerable<T> items) {
175      foreach (var item in items) {
176        if (!list.Contains(item))
177          checkedState.Remove(item);
178      }
179      base.OnItemsRemoved(items);
180    }
181
182    /// <summary>
183    /// Raised when the checked state of items is changed.
184    /// </summary>
185    /// <param name="items">The item whose check state is changed.</param>
186    protected virtual void OnCheckedItemsChanged(IEnumerable<T> items) {
187      RaiseCheckedItemsChanged(new CollectionItemsChangedEventArgs<T>(items));
188    }
189
190    /// <summary>
191    /// Raised after the checked state of items has been changed.
192    /// </summary>
193    public event CollectionItemsChangedEventHandler<T> CheckedItemsChanged;
194    private void RaiseCheckedItemsChanged(CollectionItemsChangedEventArgs<T> e) {
195      var handler = CheckedItemsChanged;
196      if (handler != null) handler(this, e);
197    }
198
199    /// <summary>
200    /// Creates a deep clone of the CheckedItemCollection.
201    /// </summary>
202    /// <param name="cloner"></param>
203    /// <returns>A clone of the CheckedItemCollection</returns>
204    public override IDeepCloneable Clone(Cloner cloner) {
205      return new CheckedItemCollection<T>(this, cloner);
206    }
207  }
208}
Note: See TracBrowser for help on using the repository browser.