Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Parameters/3.3/OptionalConstrainedValueParameter.cs @ 7542

Last change on this file since 7542 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 9.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Drawing;
24using HeuristicLab.Collections;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace 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          DeregisterValueEvents();
57          this.value = value;
58          RegisterValueEvents();
59          OnValueChanged();
60        }
61      }
62    }
63    IItem IValueParameter.Value {
64      get { return Value; }
65      set {
66        T val = value as T;
67        if ((value != null) && (val == null))
68          throw new InvalidOperationException(
69            string.Format("Type mismatch. Value is not a \"{0}\".",
70                          typeof(T).GetPrettyName())
71          );
72        Value = val;
73      }
74    }
75
76    [Storable(DefaultValue = true)]
77    private bool getsCollected;
78    public bool GetsCollected {
79      get { return getsCollected; }
80      set {
81        if (value != getsCollected) {
82          getsCollected = value;
83          OnGetsCollectedChanged();
84        }
85      }
86    }
87
88    #region Constructors
89    [StorableConstructor]
90    protected OptionalConstrainedValueParameter(bool deserializing) : base(deserializing) { }
91    protected OptionalConstrainedValueParameter(OptionalConstrainedValueParameter<T> original, Cloner cloner)
92      : base(original, cloner) {
93      validValues = cloner.Clone(original.validValues);
94      value = cloner.Clone(original.value);
95      getsCollected = original.getsCollected;
96      Initialize();
97    }
98    public OptionalConstrainedValueParameter()
99      : base("Anonymous", typeof(T)) {
100      this.validValues = new ItemSet<T>();
101      this.getsCollected = true;
102      Initialize();
103    }
104    public OptionalConstrainedValueParameter(string name)
105      : base(name, typeof(T)) {
106      this.validValues = new ItemSet<T>();
107      this.getsCollected = true;
108      Initialize();
109    }
110    public OptionalConstrainedValueParameter(string name, bool getsCollected)
111      : base(name, typeof(T)) {
112      this.validValues = new ItemSet<T>();
113      this.getsCollected = getsCollected;
114      Initialize();
115    }
116    public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues)
117      : base(name, typeof(T)) {
118      this.validValues = validValues;
119      this.getsCollected = true;
120      Initialize();
121    }
122    public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues, bool getsCollected)
123      : base(name, typeof(T)) {
124      this.validValues = validValues;
125      this.getsCollected = getsCollected;
126      Initialize();
127    }
128    public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues, T value)
129      : base(name, typeof(T)) {
130      this.validValues = validValues;
131      this.value = value;
132      this.getsCollected = true;
133      Initialize();
134    }
135    public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues, T value, bool getsCollected)
136      : base(name, typeof(T)) {
137      this.validValues = validValues;
138      this.value = value;
139      this.getsCollected = getsCollected;
140      Initialize();
141    }
142    public OptionalConstrainedValueParameter(string name, string description)
143      : base(name, description, typeof(T)) {
144      this.validValues = new ItemSet<T>();
145      this.getsCollected = true;
146      Initialize();
147    }
148    public OptionalConstrainedValueParameter(string name, string description, bool getsCollected)
149      : base(name, description, typeof(T)) {
150      this.validValues = new ItemSet<T>();
151      this.getsCollected = getsCollected;
152      Initialize();
153    }
154    public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues)
155      : base(name, description, typeof(T)) {
156      this.validValues = validValues;
157      this.getsCollected = true;
158      Initialize();
159    }
160    public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues, bool getsCollected)
161      : base(name, description, typeof(T)) {
162      this.validValues = validValues;
163      this.getsCollected = getsCollected;
164      Initialize();
165    }
166    public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues, T value)
167      : base(name, description, typeof(T)) {
168      this.validValues = validValues;
169      this.value = value;
170      this.getsCollected = true;
171      Initialize();
172    }
173    public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues, T value, bool getsCollected)
174      : base(name, description, typeof(T)) {
175      this.validValues = validValues;
176      this.value = value;
177      this.getsCollected = getsCollected;
178      Initialize();
179    }
180    #endregion
181
182    [StorableHook(HookType.AfterDeserialization)]
183    private void AfterDeserialization() {
184      Initialize();
185    }
186
187    private void Initialize() {
188      RegisterValidValuesEvents();
189      RegisterValueEvents();
190    }
191
192    public override IDeepCloneable Clone(Cloner cloner) {
193      return new OptionalConstrainedValueParameter<T>(this, cloner);
194    }
195
196    public override string ToString() {
197      return Name + ": " + (Value != null ? Value.ToString() : "null");
198    }
199
200    protected override IItem GetActualValue() {
201      return Value;
202    }
203    protected override void SetActualValue(IItem value) {
204      ((IValueParameter)this).Value = value;
205    }
206
207    public event EventHandler ValueChanged;
208    protected virtual void OnValueChanged() {
209      EventHandler handler = ValueChanged;
210      if (handler != null) handler(this, EventArgs.Empty);
211      OnItemImageChanged();
212      OnToStringChanged();
213    }
214    public event EventHandler GetsCollectedChanged;
215    protected virtual void OnGetsCollectedChanged() {
216      EventHandler handler = GetsCollectedChanged;
217      if (handler != null) handler(this, EventArgs.Empty);
218    }
219
220    private void RegisterValidValuesEvents() {
221      if (validValues != null) {
222        validValues.ItemsAdded += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
223        validValues.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
224        validValues.CollectionReset += new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
225      }
226    }
227    private void DeregisterValidValuesEvents() {
228      if (validValues != null) {
229        validValues.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
230        validValues.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
231        validValues.CollectionReset -= new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
232      }
233    }
234    protected virtual void ValidValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) { }
235    protected virtual void ValidValues_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
236      if ((Value != null) && !validValues.Contains(Value)) Value = null;
237    }
238    protected virtual void ValidValues_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
239      if ((Value != null) && !validValues.Contains(Value)) Value = null;
240    }
241
242    private void RegisterValueEvents() {
243      if (value != null) {
244        value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
245        value.ToStringChanged += new EventHandler(Value_ToStringChanged);
246      }
247    }
248    private void DeregisterValueEvents() {
249      if (value != null) {
250        value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
251        value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
252      }
253    }
254    private void Value_ItemImageChanged(object sender, EventArgs e) {
255      OnItemImageChanged();
256    }
257    private void Value_ToStringChanged(object sender, EventArgs e) {
258      OnToStringChanged();
259    }
260  }
261}
Note: See TracBrowser for help on using the repository browser.