Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3565 was 3564, checked in by gkronber, 15 years ago

Fixed a few minor issues in CheckedItemCollection. #992 (CheckedItemList and CheckedItemCollection is necessary)

File size: 4.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  [StorableClass]
33  [Item("CheckedItemCollection<T>", "Represents a collection of items that can be checked or unchecked.")]
34  public class CheckedItemCollection<T> : ItemCollection<T>, ICheckedItemCollection<T> where T : class, IItem {
35    [Storable]
36    private Dictionary<T, bool> checkedState;
37
38    /// <summary>
39    /// gets an enumerable of checked items
40    /// </summary>
41    public IEnumerable<T> CheckedItems {
42      get { return from pair in checkedState where pair.Value select pair.Key; }
43    }
44
45    public CheckedItemCollection()
46      : base() {
47      checkedState = new Dictionary<T, bool>();
48    }
49    public CheckedItemCollection(int capacity)
50      : base(capacity) {
51      checkedState = new Dictionary<T, bool>(capacity);
52    }
53    public CheckedItemCollection(IEnumerable<T> collection)
54      : base(collection) {
55      checkedState = new Dictionary<T, bool>();
56      foreach (var item in collection)
57        if (!checkedState.ContainsKey(item))
58          checkedState.Add(item, false);
59    }
60    [StorableConstructor]
61    protected CheckedItemCollection(bool deserializing) : base(deserializing) { }
62
63    public bool ItemChecked(T item) {
64      return checkedState[item];
65    }
66
67    public void SetItemCheckedState(T item, bool checkedState) {
68      if (!this.checkedState.ContainsKey(item)) throw new ArgumentException();
69      if (this.checkedState[item] != checkedState) {
70        this.checkedState[item] = checkedState;
71        OnCheckedItemsChanged(new T[] { item });
72      }
73    }
74
75    protected override void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
76      foreach (var oldItem in oldItems)
77        checkedState.Remove(oldItem);
78      foreach (var item in items)
79        if (!checkedState.ContainsKey(item))
80          checkedState.Add(item, false);
81      base.OnCollectionReset(items, oldItems);
82    }
83
84    protected override void OnItemsAdded(IEnumerable<T> items) {
85      foreach (var item in items)
86        if (!checkedState.ContainsKey(item))
87          checkedState.Add(item, false);
88      base.OnItemsAdded(items);
89    }
90
91    protected override void OnItemsRemoved(IEnumerable<T> items) {
92      foreach (var item in items) {
93        checkedState.Remove(item);
94      }
95      base.OnItemsRemoved(items);
96    }
97
98    protected virtual void OnCheckedItemsChanged(IEnumerable<T> items) {
99      RaiseCheckedItemsChanged(new CollectionItemsChangedEventArgs<T>(items));
100    }
101
102    public event CollectionItemsChangedEventHandler<T> CheckedItemsChanged;
103    private void RaiseCheckedItemsChanged(CollectionItemsChangedEventArgs<T> e) {
104      var handler = CheckedItemsChanged;
105      if (handler != null) handler(this, e);
106    }
107
108    public object Clone() {
109      return Clone(new Cloner());
110    }
111    public virtual IDeepCloneable Clone(Cloner cloner) {
112      CheckedItemCollection<T> clone = (CheckedItemCollection<T>)Activator.CreateInstance(this.GetType());
113      cloner.RegisterClonedObject(this, clone);
114      clone.list = new List<T>(this.Select(x => (T)cloner.Clone(x)));
115      clone.checkedState = new Dictionary<T, bool>();
116      foreach (var pair in checkedState)
117        clone.checkedState.Add((T)cloner.Clone(pair.Key), pair.Value);
118      return clone;
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.