Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10893 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 9.6 KB
RevLine 
[2924]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2924]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]
[8121]35  public class OptionalConstrainedValueParameter<T> : Parameter, IConstrainedValueParameter<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    }
[8121]42     
[3317]43    [Storable]
[2924]44    private ItemSet<T> validValues;
[8121]45    public IItemSet<T> ValidValues {
[2924]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.");
[3341]56          DeregisterValueEvents();
[2924]57          this.value = value;
[3341]58          RegisterValueEvents();
[2924]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
[4332]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
[4722]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    }
[2924]98    public OptionalConstrainedValueParameter()
99      : base("Anonymous", typeof(T)) {
[3317]100      this.validValues = new ItemSet<T>();
[4332]101      this.getsCollected = true;
[3317]102      Initialize();
[2924]103    }
104    public OptionalConstrainedValueParameter(string name)
105      : base(name, typeof(T)) {
[3317]106      this.validValues = new ItemSet<T>();
[4332]107      this.getsCollected = true;
[3317]108      Initialize();
[2924]109    }
[4332]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    }
[2924]116    public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues)
117      : base(name, typeof(T)) {
[3317]118      this.validValues = validValues;
[4332]119      this.getsCollected = true;
[3317]120      Initialize();
[2924]121    }
[4332]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    }
[2924]128    public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues, T value)
129      : base(name, typeof(T)) {
[3317]130      this.validValues = validValues;
131      this.value = value;
[4332]132      this.getsCollected = true;
[3317]133      Initialize();
[2924]134    }
[4332]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    }
[2924]142    public OptionalConstrainedValueParameter(string name, string description)
143      : base(name, description, typeof(T)) {
[3317]144      this.validValues = new ItemSet<T>();
[4332]145      this.getsCollected = true;
[3317]146      Initialize();
[2924]147    }
[4332]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    }
[2924]154    public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues)
155      : base(name, description, typeof(T)) {
[3317]156      this.validValues = validValues;
[4332]157      this.getsCollected = true;
[3317]158      Initialize();
[2924]159    }
[4332]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    }
[2924]166    public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues, T value)
167      : base(name, description, typeof(T)) {
[3317]168      this.validValues = validValues;
169      this.value = value;
[4332]170      this.getsCollected = true;
[3317]171      Initialize();
[2924]172    }
[4332]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
[2924]181
[3317]182    [StorableHook(HookType.AfterDeserialization)]
[4722]183    private void AfterDeserialization() {
184      Initialize();
185    }
186
[3317]187    private void Initialize() {
188      RegisterValidValuesEvents();
[3341]189      RegisterValueEvents();
[3317]190    }
191
[2924]192    public override IDeepCloneable Clone(Cloner cloner) {
[4722]193      return new OptionalConstrainedValueParameter<T>(this, cloner);
[2924]194    }
195
196    public override string ToString() {
[3688]197      return Name + ": " + (Value != null ? Value.ToString() : "null");
[2924]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() {
[4332]209      EventHandler handler = ValueChanged;
210      if (handler != null) handler(this, EventArgs.Empty);
[3341]211      OnItemImageChanged();
[2932]212      OnToStringChanged();
[2924]213    }
[4332]214    public event EventHandler GetsCollectedChanged;
215    protected virtual void OnGetsCollectedChanged() {
216      EventHandler handler = GetsCollectedChanged;
217      if (handler != null) handler(this, EventArgs.Empty);
218    }
[2924]219
220    private void RegisterValidValuesEvents() {
221      if (validValues != null) {
[3341]222        validValues.ItemsAdded += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
[2924]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) {
[3341]229        validValues.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
[2924]230        validValues.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
231        validValues.CollectionReset -= new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
232      }
233    }
[3341]234    protected virtual void ValidValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) { }
[2924]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    }
[3341]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) {
[2932]258      OnToStringChanged();
[2924]259    }
260  }
261}
Note: See TracBrowser for help on using the repository browser.