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 |
|
---|
22 | using System;
|
---|
23 | using System.Drawing;
|
---|
24 | using HeuristicLab.Collections;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Parameters {
|
---|
30 | /// <summary>
|
---|
31 | /// A parameter whose value has to be chosen from a set of valid values or is null.
|
---|
32 | /// </summary>
|
---|
33 | [Item("OptionalConstrainedValueParameter", "A parameter whose value has to be chosen from a set of valid values or is null.")]
|
---|
34 | [StorableClass]
|
---|
35 | public class OptionalConstrainedValueParameter<T> : Parameter, IValueParameter<T> where T : class, IItem {
|
---|
36 | public override Image ItemImage {
|
---|
37 | get {
|
---|
38 | if (value != null) return value.ItemImage;
|
---|
39 | else return base.ItemImage;
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | [Storable]
|
---|
44 | private ItemSet<T> validValues;
|
---|
45 | public ItemSet<T> ValidValues {
|
---|
46 | get { return validValues; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | [Storable]
|
---|
50 | private T value;
|
---|
51 | public virtual T Value {
|
---|
52 | get { return this.value; }
|
---|
53 | set {
|
---|
54 | if (value != this.value) {
|
---|
55 | if ((value != null) && !validValues.Contains(value)) throw new ArgumentException("Invalid value.");
|
---|
56 | OnValueChanging();
|
---|
57 | DeregisterValueEvents();
|
---|
58 | this.value = value;
|
---|
59 | RegisterValueEvents();
|
---|
60 | OnValueChanged();
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 | IItem IValueParameter.Value {
|
---|
65 | get { return Value; }
|
---|
66 | set {
|
---|
67 | T val = value as T;
|
---|
68 | if ((value != null) && (val == null))
|
---|
69 | throw new InvalidOperationException(
|
---|
70 | string.Format("Type mismatch. Value is not a \"{0}\".",
|
---|
71 | typeof(T).GetPrettyName())
|
---|
72 | );
|
---|
73 | Value = val;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | [Storable(DefaultValue = true)]
|
---|
78 | private bool getsCollected;
|
---|
79 | public bool GetsCollected {
|
---|
80 | get { return getsCollected; }
|
---|
81 | set {
|
---|
82 | if (value != getsCollected) {
|
---|
83 | getsCollected = value;
|
---|
84 | OnGetsCollectedChanged();
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | #region Constructors
|
---|
90 | [StorableConstructor]
|
---|
91 | protected OptionalConstrainedValueParameter(bool deserializing) : base(deserializing) { }
|
---|
92 | protected OptionalConstrainedValueParameter(OptionalConstrainedValueParameter<T> original, Cloner cloner)
|
---|
93 | : base(original, cloner) {
|
---|
94 | validValues = cloner.Clone(original.validValues);
|
---|
95 | value = cloner.Clone(original.value);
|
---|
96 | getsCollected = original.getsCollected;
|
---|
97 | Initialize();
|
---|
98 | }
|
---|
99 | public OptionalConstrainedValueParameter()
|
---|
100 | : base("Anonymous", typeof(T)) {
|
---|
101 | this.validValues = new ItemSet<T>();
|
---|
102 | this.getsCollected = true;
|
---|
103 | Initialize();
|
---|
104 | }
|
---|
105 | public OptionalConstrainedValueParameter(string name)
|
---|
106 | : base(name, typeof(T)) {
|
---|
107 | this.validValues = new ItemSet<T>();
|
---|
108 | this.getsCollected = true;
|
---|
109 | Initialize();
|
---|
110 | }
|
---|
111 | public OptionalConstrainedValueParameter(string name, bool getsCollected)
|
---|
112 | : base(name, typeof(T)) {
|
---|
113 | this.validValues = new ItemSet<T>();
|
---|
114 | this.getsCollected = getsCollected;
|
---|
115 | Initialize();
|
---|
116 | }
|
---|
117 | public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues)
|
---|
118 | : base(name, typeof(T)) {
|
---|
119 | this.validValues = validValues;
|
---|
120 | this.getsCollected = true;
|
---|
121 | Initialize();
|
---|
122 | }
|
---|
123 | public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues, bool getsCollected)
|
---|
124 | : base(name, typeof(T)) {
|
---|
125 | this.validValues = validValues;
|
---|
126 | this.getsCollected = getsCollected;
|
---|
127 | Initialize();
|
---|
128 | }
|
---|
129 | public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues, T value)
|
---|
130 | : base(name, typeof(T)) {
|
---|
131 | this.validValues = validValues;
|
---|
132 | this.value = value;
|
---|
133 | this.getsCollected = true;
|
---|
134 | Initialize();
|
---|
135 | }
|
---|
136 | public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues, T value, bool getsCollected)
|
---|
137 | : base(name, typeof(T)) {
|
---|
138 | this.validValues = validValues;
|
---|
139 | this.value = value;
|
---|
140 | this.getsCollected = getsCollected;
|
---|
141 | Initialize();
|
---|
142 | }
|
---|
143 | public OptionalConstrainedValueParameter(string name, string description)
|
---|
144 | : base(name, description, typeof(T)) {
|
---|
145 | this.validValues = new ItemSet<T>();
|
---|
146 | this.getsCollected = true;
|
---|
147 | Initialize();
|
---|
148 | }
|
---|
149 | public OptionalConstrainedValueParameter(string name, string description, bool getsCollected)
|
---|
150 | : base(name, description, typeof(T)) {
|
---|
151 | this.validValues = new ItemSet<T>();
|
---|
152 | this.getsCollected = getsCollected;
|
---|
153 | Initialize();
|
---|
154 | }
|
---|
155 | public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues)
|
---|
156 | : base(name, description, typeof(T)) {
|
---|
157 | this.validValues = validValues;
|
---|
158 | this.getsCollected = true;
|
---|
159 | Initialize();
|
---|
160 | }
|
---|
161 | public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues, bool getsCollected)
|
---|
162 | : base(name, description, typeof(T)) {
|
---|
163 | this.validValues = validValues;
|
---|
164 | this.getsCollected = getsCollected;
|
---|
165 | Initialize();
|
---|
166 | }
|
---|
167 | public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues, T value)
|
---|
168 | : base(name, description, typeof(T)) {
|
---|
169 | this.validValues = validValues;
|
---|
170 | this.value = value;
|
---|
171 | this.getsCollected = true;
|
---|
172 | Initialize();
|
---|
173 | }
|
---|
174 | public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues, T value, bool getsCollected)
|
---|
175 | : base(name, description, typeof(T)) {
|
---|
176 | this.validValues = validValues;
|
---|
177 | this.value = value;
|
---|
178 | this.getsCollected = getsCollected;
|
---|
179 | Initialize();
|
---|
180 | }
|
---|
181 | #endregion
|
---|
182 |
|
---|
183 | [StorableHook(HookType.AfterDeserialization)]
|
---|
184 | private void AfterDeserialization() {
|
---|
185 | Initialize();
|
---|
186 | }
|
---|
187 |
|
---|
188 | private void Initialize() {
|
---|
189 | RegisterValidValuesEvents();
|
---|
190 | RegisterValueEvents();
|
---|
191 | }
|
---|
192 |
|
---|
193 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
194 | return new OptionalConstrainedValueParameter<T>(this, cloner);
|
---|
195 | }
|
---|
196 |
|
---|
197 | public override string ToString() {
|
---|
198 | return Name + ": " + (Value != null ? Value.ToString() : "null");
|
---|
199 | }
|
---|
200 |
|
---|
201 | protected override IItem GetActualValue() {
|
---|
202 | return Value;
|
---|
203 | }
|
---|
204 | protected override void SetActualValue(IItem value) {
|
---|
205 | ((IValueParameter)this).Value = value;
|
---|
206 | }
|
---|
207 |
|
---|
208 | public event EventHandler ValueChanging;
|
---|
209 | protected virtual void OnValueChanging() {
|
---|
210 | EventHandler handler = ValueChanging;
|
---|
211 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
212 | }
|
---|
213 | public event EventHandler ValueChanged;
|
---|
214 | protected virtual void OnValueChanged() {
|
---|
215 | EventHandler handler = ValueChanged;
|
---|
216 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
217 | OnItemImageChanged();
|
---|
218 | OnToStringChanged();
|
---|
219 | }
|
---|
220 | public event EventHandler GetsCollectedChanged;
|
---|
221 | protected virtual void OnGetsCollectedChanged() {
|
---|
222 | EventHandler handler = GetsCollectedChanged;
|
---|
223 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
224 | }
|
---|
225 |
|
---|
226 | private void RegisterValidValuesEvents() {
|
---|
227 | if (validValues != null) {
|
---|
228 | validValues.ItemsAdded += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
|
---|
229 | validValues.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
|
---|
230 | validValues.CollectionReset += new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
|
---|
231 | }
|
---|
232 | }
|
---|
233 | private void DeregisterValidValuesEvents() {
|
---|
234 | if (validValues != null) {
|
---|
235 | validValues.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
|
---|
236 | validValues.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
|
---|
237 | validValues.CollectionReset -= new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
|
---|
238 | }
|
---|
239 | }
|
---|
240 | protected virtual void ValidValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) { }
|
---|
241 | protected virtual void ValidValues_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
242 | if ((Value != null) && !validValues.Contains(Value)) Value = null;
|
---|
243 | }
|
---|
244 | protected virtual void ValidValues_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
245 | if ((Value != null) && !validValues.Contains(Value)) Value = null;
|
---|
246 | }
|
---|
247 |
|
---|
248 | private void RegisterValueEvents() {
|
---|
249 | if (value != null) {
|
---|
250 | value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
|
---|
251 | value.ToStringChanged += new EventHandler(Value_ToStringChanged);
|
---|
252 | }
|
---|
253 | }
|
---|
254 | private void DeregisterValueEvents() {
|
---|
255 | if (value != null) {
|
---|
256 | value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
|
---|
257 | value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
|
---|
258 | }
|
---|
259 | }
|
---|
260 | private void Value_ItemImageChanged(object sender, EventArgs e) {
|
---|
261 | OnItemImageChanged();
|
---|
262 | }
|
---|
263 | private void Value_ToStringChanged(object sender, EventArgs e) {
|
---|
264 | OnToStringChanged();
|
---|
265 | }
|
---|
266 | }
|
---|
267 | }
|
---|