Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented CheckedItemList and CheckedItemListView. #992

File size: 5.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.Linq;
24using System.Collections;
25using System.Collections.Generic;
26using System.Collections.ObjectModel;
27using System.Text;
28using System.Drawing;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Common;
31using HeuristicLab.Common.Resources;
32using HeuristicLab.Collections;
33
34namespace HeuristicLab.Core {
35  [StorableClass]
36  [Item("CheckedItemList<T>", "Represents a list of items that can be checked or unchecked.")]
37  public class CheckedItemList<T> : ItemList<T>, ICheckedItemList<T> where T : class, IItem {
38    [Storable]
39    private Dictionary<T, bool> checkedState;
40
41    public IEnumerable<IndexedItem<T>> CheckedItems {
42      get {
43        return from i in Enumerable.Range(0, list.Count)
44               where ItemChecked(i)
45               select new IndexedItem<T>(i, list[i]);
46      }
47    }
48    public CheckedItemList()
49      : base() {
50      checkedState = new Dictionary<T, bool>();
51    }
52    public CheckedItemList(int capacity)
53      : base(capacity) {
54      checkedState = new Dictionary<T, bool>();
55    }
56    public CheckedItemList(IEnumerable<T> collection)
57      : base(collection) {
58      checkedState = new Dictionary<T, bool>();
59      foreach (var item in collection) {
60        if (!checkedState.ContainsKey(item))
61          checkedState.Add(item, false);
62      }
63    }
64    [StorableConstructor]
65    protected CheckedItemList(bool deserializing) : base(deserializing) { }
66
67    public bool ItemChecked(T item) {
68      return checkedState[item];
69    }
70
71    public bool ItemChecked(int itemIndex) {
72      return ItemChecked(this[itemIndex]);
73    }
74
75    public void SetItemCheckedState(T item, bool checkedState) {
76      if (!this.checkedState.ContainsKey(item)) throw new ArgumentException();
77      if (this.checkedState[item] != checkedState) {
78        this.checkedState[item] = checkedState;
79        OnCheckedItemsChanged(new IndexedItem<T>[] { new IndexedItem<T>(IndexOf(item), item) });
80      }
81    }
82
83    public void SetItemCheckedState(int itemIndex, bool checkedState) {
84      SetItemCheckedState(this[itemIndex], checkedState);
85    }
86
87    protected override void OnCollectionReset(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
88      foreach (var oldIndexedItem in oldItems) {
89        checkedState.Remove(oldIndexedItem.Value);
90      }
91      foreach (var indexedItem in items) {
92        if (!checkedState.ContainsKey(indexedItem.Value))
93          checkedState.Add(indexedItem.Value, false);
94      }
95      base.OnCollectionReset(items, oldItems);
96    }
97
98    protected override void OnItemsAdded(IEnumerable<IndexedItem<T>> items) {
99      foreach (var indexedItem in items)
100        if (!checkedState.ContainsKey(indexedItem.Value))
101          checkedState.Add(indexedItem.Value, false);
102      base.OnItemsAdded(items);
103    }
104
105    protected override void OnItemsRemoved(IEnumerable<IndexedItem<T>> items) {
106      foreach (var indexedItem in items)
107        checkedState.Remove(indexedItem.Value);
108      base.OnItemsRemoved(items);
109    }
110
111    protected override void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
112      foreach (var oldIndexedItem in oldItems)
113        checkedState.Remove(oldIndexedItem.Value);
114      foreach (var indexedItem in items)
115        if (!checkedState.ContainsKey(indexedItem.Value))
116          checkedState.Add(indexedItem.Value, false);
117      base.OnItemsReplaced(items, oldItems);
118    }
119
120    protected virtual void OnCheckedItemsChanged(IEnumerable<IndexedItem<T>> items) {
121      RaiseCheckedItemsChanged(new CollectionItemsChangedEventArgs<IndexedItem<T>>(items));
122    }
123
124    public event CollectionItemsChangedEventHandler<IndexedItem<T>> CheckedItemsChanged;
125    private void RaiseCheckedItemsChanged(CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
126      var handler = CheckedItemsChanged;
127      if (handler != null) handler(this, e);
128    }
129
130    public object Clone() {
131      return Clone(new Cloner());
132    }
133    public virtual IDeepCloneable Clone(Cloner cloner) {
134      CheckedItemList<T> clone = (CheckedItemList<T>)Activator.CreateInstance(this.GetType());
135      cloner.RegisterClonedObject(this, clone);
136      clone.list = new List<T>(this.Select(x => (T)cloner.Clone(x)));
137      clone.checkedState = new Dictionary<T, bool>();
138      foreach (var pair in checkedState)
139        clone.checkedState.Add((T)cloner.Clone(pair.Key), pair.Value);
140      return clone;
141    }
142  }
143}
Note: See TracBrowser for help on using the repository browser.