Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Parameters/3.3/OptionalValueParameter.cs @ 13782

Last change on this file since 13782 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 8.2 KB
RevLine 
[2756]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2756]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;
[2757]24using HeuristicLab.Common;
[2756]25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Parameters {
29  /// <summary>
[2947]30  /// A parameter whose value is defined in the parameter itself or is null.
[2756]31  /// </summary>
[3822]32  [Item("OptionalValueParameter", "A parameter whose value is defined in the parameter itself or is null.")]
[3017]33  [StorableClass]
[2890]34  public class OptionalValueParameter<T> : Parameter, IValueParameter<T> where T : class, IItem {
[3341]35    public override Image ItemImage {
36      get {
37        if (value != null) return value.ItemImage;
38        else return base.ItemImage;
39      }
40    }
41
[3317]42    [Storable]
[2756]43    private T value;
[2924]44    public virtual T Value {
[2756]45      get { return this.value; }
46      set {
47        if (value != this.value) {
[3341]48          DeregisterValueEvents();
[2756]49          this.value = value;
[3341]50          RegisterValueEvents();
[2756]51          OnValueChanged();
52        }
53      }
54    }
[2796]55    IItem IValueParameter.Value {
[2757]56      get { return Value; }
[2796]57      set {
58        T val = value as T;
[2852]59        if ((value != null) && (val == null))
[2796]60          throw new InvalidOperationException(
61            string.Format("Type mismatch. Value is not a \"{0}\".",
62                          typeof(T).GetPrettyName())
63          );
64        Value = val;
65      }
[2757]66    }
[2756]67
[4332]68    [Storable(DefaultValue = true)]
69    private bool getsCollected;
70    public bool GetsCollected {
71      get { return getsCollected; }
72      set {
73        if (value != getsCollected) {
74          getsCollected = value;
75          OnGetsCollectedChanged();
76        }
77      }
78    }
79
[4825]80    [Storable(DefaultValue = true)]
81    private bool reactOnValueToStringChangedAndValueItemImageChanged;
82    /// <summary>
83    ///   True if this parameter should react on the ToStringChanged and ItemImageChanged events of its value, otherwise false.
84    /// </summary>
85    /// <remarks>
86    ///   In some rare cases when the value of the parameter is not deeply cloned, this property has to be set to false
87    ///   to avoid a memory leak (cf. ticket #1268). In all other cases this property should always be true.
88    /// </remarks>
89    public bool ReactOnValueToStringChangedAndValueItemImageChanged {
90      get { return reactOnValueToStringChangedAndValueItemImageChanged; }
91      set {
92        if (value != reactOnValueToStringChangedAndValueItemImageChanged) {
93          reactOnValueToStringChangedAndValueItemImageChanged = value;
[5809]94          if (reactOnValueToStringChangedAndValueItemImageChanged) {
[4825]95            RegisterValueEvents();
[5809]96            OnToStringChanged();
97            OnItemImageChanged();
98          } else
[4825]99            DeregisterValueEvents();
100        }
101      }
102    }
103
[4332]104    #region Constructors
[4722]105    [StorableConstructor]
106    protected OptionalValueParameter(bool deserializing) : base(deserializing) { }
107    protected OptionalValueParameter(OptionalValueParameter<T> original, Cloner cloner)
108      : base(original, cloner) {
109      value = cloner.Clone(original.value);
110      getsCollected = original.getsCollected;
[4825]111      reactOnValueToStringChangedAndValueItemImageChanged = original.reactOnValueToStringChangedAndValueItemImageChanged;
[4722]112      RegisterValueEvents();
113    }
[2890]114    public OptionalValueParameter()
[2756]115      : base("Anonymous", typeof(T)) {
[4332]116      this.getsCollected = true;
[4825]117      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
[2756]118    }
[2890]119    public OptionalValueParameter(string name)
[2756]120      : base(name, typeof(T)) {
[4332]121      this.getsCollected = true;
[4825]122      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
[2756]123    }
[4332]124    public OptionalValueParameter(string name, bool getsCollected)
125      : base(name, typeof(T)) {
126      this.getsCollected = getsCollected;
[4825]127      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
[4332]128    }
[2890]129    public OptionalValueParameter(string name, T value)
[2756]130      : base(name, typeof(T)) {
[3317]131      this.value = value;
[4332]132      this.getsCollected = true;
[4825]133      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
[4722]134      RegisterValueEvents();
[2756]135    }
[4332]136    public OptionalValueParameter(string name, T value, bool getsCollected)
137      : base(name, typeof(T)) {
138      this.value = value;
139      this.getsCollected = getsCollected;
[4825]140      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
[4722]141      RegisterValueEvents();
[4332]142    }
[2890]143    public OptionalValueParameter(string name, string description)
[2756]144      : base(name, description, typeof(T)) {
[4332]145      this.getsCollected = true;
[4825]146      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
[2756]147    }
[4332]148    public OptionalValueParameter(string name, string description, bool getsCollected)
149      : base(name, description, typeof(T)) {
150      this.getsCollected = getsCollected;
[4825]151      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
[4332]152    }
[2890]153    public OptionalValueParameter(string name, string description, T value)
[2756]154      : base(name, description, typeof(T)) {
[3317]155      this.value = value;
[4332]156      this.getsCollected = true;
[4825]157      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
[4722]158      RegisterValueEvents();
[2756]159    }
[4332]160    public OptionalValueParameter(string name, string description, T value, bool getsCollected)
161      : base(name, description, typeof(T)) {
162      this.value = value;
163      this.getsCollected = getsCollected;
[4825]164      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
[4722]165      RegisterValueEvents();
[4332]166    }
167    #endregion
[2756]168
[3317]169    [StorableHook(HookType.AfterDeserialization)]
[4722]170    private void AfterDeserialization() {
[3341]171      RegisterValueEvents();
[3317]172    }
173
[2756]174    public override IDeepCloneable Clone(Cloner cloner) {
[4722]175      return new OptionalValueParameter<T>(this, cloner);
[2756]176    }
177
178    public override string ToString() {
[4825]179      if (reactOnValueToStringChangedAndValueItemImageChanged)
180        return Name + ": " + (Value != null ? Value.ToString() : "null");
181      else
182        return Name;
[2756]183    }
184
[2757]185    protected override IItem GetActualValue() {
186      return Value;
187    }
188    protected override void SetActualValue(IItem value) {
[2796]189      ((IValueParameter)this).Value = value;
[2757]190    }
[5809]191
[2756]192    public event EventHandler ValueChanged;
[4332]193    protected virtual void OnValueChanged() {
194      EventHandler handler = ValueChanged;
195      if (handler != null) handler(this, EventArgs.Empty);
[3341]196      OnItemImageChanged();
[2932]197      OnToStringChanged();
[2756]198    }
[4332]199    public event EventHandler GetsCollectedChanged;
200    protected virtual void OnGetsCollectedChanged() {
201      EventHandler handler = GetsCollectedChanged;
202      if (handler != null) handler(this, EventArgs.Empty);
203    }
[3341]204
205    private void RegisterValueEvents() {
[4825]206      if ((value != null) && reactOnValueToStringChangedAndValueItemImageChanged) {
[3341]207        value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
208        value.ToStringChanged += new EventHandler(Value_ToStringChanged);
209      }
210    }
211    private void DeregisterValueEvents() {
212      if (value != null) {
213        value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
214        value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
215      }
216    }
217    private void Value_ItemImageChanged(object sender, EventArgs e) {
218      OnItemImageChanged();
219    }
[2932]220    private void Value_ToStringChanged(object sender, EventArgs e) {
221      OnToStringChanged();
[2756]222    }
223  }
224}
Note: See TracBrowser for help on using the repository browser.