Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Parameters/3.3/OptionalValueParameter.cs @ 17578

Last change on this file since 17578 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

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