Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3562 was 3562, checked in by gkronber, 14 years ago

Implemented changes as requested by swagner. #992 (CheckedItemList and CheckedItemCollection is necessary)

File size: 4.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  [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        checkedState.Add(item, false);
58    }
59    [StorableConstructor]
60    protected CheckedItemCollection(bool deserializing) : base(deserializing) { }
61
62    public bool ItemChecked(T item) {
63      return checkedState[item];
64    }
65
66    public void SetItemCheckedState(T item, bool checkedState) {
67      if (!this.checkedState.ContainsKey(item)) throw new ArgumentException();
68      if (this.checkedState[item] != checkedState) {
69        this.checkedState[item] = checkedState;
70        OnItemsChecked(new T[] { item });
71      }
72    }
73
74    protected override void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
75      foreach (var oldItem in oldItems)
76        checkedState.Remove(oldItem);
77      foreach (var item in items)
78        if (!checkedState.ContainsKey(item))
79          checkedState.Add(item, false);
80      base.OnCollectionReset(items, oldItems);
81    }
82
83    protected override void OnItemsAdded(IEnumerable<T> items) {
84      foreach (var item in items)
85        if (!checkedState.ContainsKey(item))
86          checkedState.Add(item, false);
87      base.OnItemsAdded(items);
88    }
89
90    protected override void OnItemsRemoved(IEnumerable<T> items) {
91      foreach (var item in items) {
92        checkedState.Remove(item);
93      }
94      base.OnItemsRemoved(items);
95    }
96
97    protected virtual void OnItemsChecked(IEnumerable<T> items) {
98      RaiseCheckedItemsChanged(new CollectionItemsChangedEventArgs<T>(items));
99    }
100
101    public event CollectionItemsChangedEventHandler<T> CheckedItemsChanged;
102    private void RaiseCheckedItemsChanged(CollectionItemsChangedEventArgs<T> e) {
103      var handler = CheckedItemsChanged;
104      if (handler != null) handler(this, e);
105    }
106
107    public object Clone() {
108      return Clone(new Cloner());
109    }
110    public virtual IDeepCloneable Clone(Cloner cloner) {
111      CheckedItemCollection<T> clone = (CheckedItemCollection<T>)Activator.CreateInstance(this.GetType());
112      cloner.RegisterClonedObject(this, clone);
113      clone.list = new List<T>(this.Select(x => (T)cloner.Clone(x)));
114      clone.checkedState = new Dictionary<T, bool>();
115      foreach (var pair in checkedState)
116        clone.checkedState.Add((T)cloner.Clone(pair.Key), pair.Value);
117      return clone;
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.