1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Collections;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace 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 | [StorableClass]
|
---|
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(bool deserializing) : base(deserializing) { }
|
---|
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 | /// Adds a new <paramref name="item"/> with the given <paramref name="checkedState"/>.
|
---|
111 | /// </summary>
|
---|
112 | /// <param name="item">The item to add.</param>
|
---|
113 | /// <param name="checkedState">The checked state of the item to add.</param>
|
---|
114 | public void Add(T item, bool checkedState) {
|
---|
115 | Add(item);
|
---|
116 | SetItemCheckedState(item, checkedState);
|
---|
117 | }
|
---|
118 |
|
---|
119 | /// <summary>
|
---|
120 | /// Creates a ReadOnlyCheckedItemCollection containing the same elements.
|
---|
121 | /// </summary>
|
---|
122 | /// <returns>A new ReadOnlyCheckedItemCollection containing the same elements.</returns>
|
---|
123 | public new ReadOnlyCheckedItemCollection<T> AsReadOnly() {
|
---|
124 | return new ReadOnlyCheckedItemCollection<T>(this);
|
---|
125 | }
|
---|
126 |
|
---|
127 | /// <summary>
|
---|
128 | /// Raised when the collection of items is reset.
|
---|
129 | /// </summary>
|
---|
130 | /// <param name="items">Empty</param>
|
---|
131 | /// <param name="oldItems">The elements in the collection before the reset.</param>
|
---|
132 | protected override void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
|
---|
133 | foreach (var oldItem in oldItems)
|
---|
134 | if (!list.Contains(oldItem))
|
---|
135 | checkedState.Remove(oldItem);
|
---|
136 | foreach (var item in items)
|
---|
137 | if (!checkedState.ContainsKey(item))
|
---|
138 | checkedState.Add(item, true);
|
---|
139 | base.OnCollectionReset(items, oldItems);
|
---|
140 | }
|
---|
141 |
|
---|
142 | /// <summary>
|
---|
143 | /// Raised when new items are added to the collection.
|
---|
144 | /// </summary>
|
---|
145 | /// <param name="items">The elements that are added to the collection.</param>
|
---|
146 | protected override void OnItemsAdded(IEnumerable<T> items) {
|
---|
147 | foreach (var item in items)
|
---|
148 | if (!checkedState.ContainsKey(item))
|
---|
149 | checkedState.Add(item, true);
|
---|
150 | base.OnItemsAdded(items);
|
---|
151 | }
|
---|
152 |
|
---|
153 | /// <summary>
|
---|
154 | /// Raised when items are removed from the collection.
|
---|
155 | /// </summary>
|
---|
156 | /// <param name="items">The items that are removed.</param>
|
---|
157 | protected override void OnItemsRemoved(IEnumerable<T> items) {
|
---|
158 | foreach (var item in items) {
|
---|
159 | if (!list.Contains(item))
|
---|
160 | checkedState.Remove(item);
|
---|
161 | }
|
---|
162 | base.OnItemsRemoved(items);
|
---|
163 | }
|
---|
164 |
|
---|
165 | /// <summary>
|
---|
166 | /// Raised when the checked state of items is changed.
|
---|
167 | /// </summary>
|
---|
168 | /// <param name="items">The item whose check state is changed.</param>
|
---|
169 | protected virtual void OnCheckedItemsChanged(IEnumerable<T> items) {
|
---|
170 | RaiseCheckedItemsChanged(new CollectionItemsChangedEventArgs<T>(items));
|
---|
171 | }
|
---|
172 |
|
---|
173 | /// <summary>
|
---|
174 | /// Raised after the checked state of items has been changed.
|
---|
175 | /// </summary>
|
---|
176 | public event CollectionItemsChangedEventHandler<T> CheckedItemsChanged;
|
---|
177 | private void RaiseCheckedItemsChanged(CollectionItemsChangedEventArgs<T> e) {
|
---|
178 | var handler = CheckedItemsChanged;
|
---|
179 | if (handler != null) handler(this, e);
|
---|
180 | }
|
---|
181 |
|
---|
182 | /// <summary>
|
---|
183 | /// Creates a deep clone of the CheckedItemCollection.
|
---|
184 | /// </summary>
|
---|
185 | /// <param name="cloner"></param>
|
---|
186 | /// <returns>A clone of the CheckedItemCollection</returns>
|
---|
187 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
188 | return new CheckedItemCollection<T>(this, cloner);
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|