Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5304 was 5206, checked in by cneumuel, 14 years ago

#1361 fixed deregistration of events on parameter values

File size: 9.8 KB
RevLine 
[2924]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;
[3341]23using System.Drawing;
[2924]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>
[3822]33  [Item("OptionalConstrainedValueParameter", "A parameter whose value has to be chosen from a set of valid values or is null.")]
[3017]34  [StorableClass]
[2924]35  public class OptionalConstrainedValueParameter<T> : Parameter, IValueParameter<T> where T : class, IItem {
[3341]36    public override Image ItemImage {
37      get {
38        if (value != null) return value.ItemImage;
39        else return base.ItemImage;
40      }
41    }
42
[3317]43    [Storable]
[2924]44    private ItemSet<T> validValues;
45    public ItemSet<T> ValidValues {
46      get { return validValues; }
47    }
48
[3317]49    [Storable]
[2924]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.");
[5206]56          OnValueChanging();
[3341]57          DeregisterValueEvents();
[2924]58          this.value = value;
[3341]59          RegisterValueEvents();
[2924]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
[4332]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
[4722]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    }
[2924]99    public OptionalConstrainedValueParameter()
100      : base("Anonymous", typeof(T)) {
[3317]101      this.validValues = new ItemSet<T>();
[4332]102      this.getsCollected = true;
[3317]103      Initialize();
[2924]104    }
105    public OptionalConstrainedValueParameter(string name)
106      : base(name, typeof(T)) {
[3317]107      this.validValues = new ItemSet<T>();
[4332]108      this.getsCollected = true;
[3317]109      Initialize();
[2924]110    }
[4332]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    }
[2924]117    public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues)
118      : base(name, typeof(T)) {
[3317]119      this.validValues = validValues;
[4332]120      this.getsCollected = true;
[3317]121      Initialize();
[2924]122    }
[4332]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    }
[2924]129    public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues, T value)
130      : base(name, typeof(T)) {
[3317]131      this.validValues = validValues;
132      this.value = value;
[4332]133      this.getsCollected = true;
[3317]134      Initialize();
[2924]135    }
[4332]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    }
[2924]143    public OptionalConstrainedValueParameter(string name, string description)
144      : base(name, description, typeof(T)) {
[3317]145      this.validValues = new ItemSet<T>();
[4332]146      this.getsCollected = true;
[3317]147      Initialize();
[2924]148    }
[4332]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    }
[2924]155    public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues)
156      : base(name, description, typeof(T)) {
[3317]157      this.validValues = validValues;
[4332]158      this.getsCollected = true;
[3317]159      Initialize();
[2924]160    }
[4332]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    }
[2924]167    public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues, T value)
168      : base(name, description, typeof(T)) {
[3317]169      this.validValues = validValues;
170      this.value = value;
[4332]171      this.getsCollected = true;
[3317]172      Initialize();
[2924]173    }
[4332]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
[2924]182
[3317]183    [StorableHook(HookType.AfterDeserialization)]
[4722]184    private void AfterDeserialization() {
185      Initialize();
186    }
187
[3317]188    private void Initialize() {
189      RegisterValidValuesEvents();
[3341]190      RegisterValueEvents();
[3317]191    }
192
[2924]193    public override IDeepCloneable Clone(Cloner cloner) {
[4722]194      return new OptionalConstrainedValueParameter<T>(this, cloner);
[2924]195    }
196
197    public override string ToString() {
[3688]198      return Name + ": " + (Value != null ? Value.ToString() : "null");
[2924]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
[5206]208    public event EventHandler ValueChanging;
209    protected virtual void OnValueChanging() {
210      EventHandler handler = ValueChanging;
211      if (handler != null) handler(this, EventArgs.Empty);
212    }
[2924]213    public event EventHandler ValueChanged;
214    protected virtual void OnValueChanged() {
[4332]215      EventHandler handler = ValueChanged;
216      if (handler != null) handler(this, EventArgs.Empty);
[3341]217      OnItemImageChanged();
[2932]218      OnToStringChanged();
[2924]219    }
[4332]220    public event EventHandler GetsCollectedChanged;
221    protected virtual void OnGetsCollectedChanged() {
222      EventHandler handler = GetsCollectedChanged;
223      if (handler != null) handler(this, EventArgs.Empty);
224    }
[2924]225
226    private void RegisterValidValuesEvents() {
227      if (validValues != null) {
[3341]228        validValues.ItemsAdded += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
[2924]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) {
[3341]235        validValues.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
[2924]236        validValues.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
237        validValues.CollectionReset -= new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
238      }
239    }
[3341]240    protected virtual void ValidValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) { }
[2924]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    }
[3341]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) {
[2932]264      OnToStringChanged();
[2924]265    }
266  }
267}
Note: See TracBrowser for help on using the repository browser.