Free cookie consent management tool by TermsFeed Policy Generator

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

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

Changed default checked state of items in CheckedItemCollection and CheckedItemList from false to true (#992)

File size: 4.5 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, true);
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    public void Add(T item, bool checkedState) {
76      Add(item);
77      SetItemCheckedState(item, checkedState);
78    }
79
80    protected override void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
81      foreach (var oldItem in oldItems)
82        checkedState.Remove(oldItem);
83      foreach (var item in items)
84        if (!checkedState.ContainsKey(item))
85          checkedState.Add(item, true);
86      base.OnCollectionReset(items, oldItems);
87    }
88
89    protected override void OnItemsAdded(IEnumerable<T> items) {
90      foreach (var item in items)
91        if (!checkedState.ContainsKey(item))
92          checkedState.Add(item, true);
93      base.OnItemsAdded(items);
94    }
95
96    protected override void OnItemsRemoved(IEnumerable<T> items) {
97      foreach (var item in items) {
98        checkedState.Remove(item);
99      }
100      base.OnItemsRemoved(items);
101    }
102
103    protected virtual void OnCheckedItemsChanged(IEnumerable<T> items) {
104      RaiseCheckedItemsChanged(new CollectionItemsChangedEventArgs<T>(items));
105    }
106
107    public event CollectionItemsChangedEventHandler<T> CheckedItemsChanged;
108    private void RaiseCheckedItemsChanged(CollectionItemsChangedEventArgs<T> e) {
109      var handler = CheckedItemsChanged;
110      if (handler != null) handler(this, e);
111    }
112
113    public object Clone() {
114      return Clone(new Cloner());
115    }
116    public virtual IDeepCloneable Clone(Cloner cloner) {
117      CheckedItemCollection<T> clone = (CheckedItemCollection<T>)Activator.CreateInstance(this.GetType());
118      cloner.RegisterClonedObject(this, clone);
119      clone.list = new List<T>(this.Select(x => (T)cloner.Clone(x)));
120      clone.checkedState = new Dictionary<T, bool>();
121      foreach (var pair in checkedState)
122        clone.checkedState.Add((T)cloner.Clone(pair.Key), pair.Value);
123      return clone;
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.