Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2972_PDPRowSelect/HeuristicLab.Parameters/3.3/OptionalValueParameter.cs @ 16724

Last change on this file since 16724 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 8.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Parameters {
29  /// <summary>
30  /// A parameter whose value is defined in the parameter itself or is null.
31  /// </summary>
32  [Item("OptionalValueParameter", "A parameter whose value is defined in the parameter itself or is null.")]
33  [StorableClass]
34  public class OptionalValueParameter<T> : Parameter, IValueParameter<T> where T : class, IItem {
35    public override Image ItemImage {
36      get {
37        if (value != null) return value.ItemImage;
38        else return base.ItemImage;
39      }
40    }
41
42    [Storable]
43    private T value;
44    public virtual T Value {
45      get { return this.value; }
46      set {
47        if (value != this.value) {
48          DeregisterValueEvents();
49          this.value = value;
50          RegisterValueEvents();
51          OnValueChanged();
52        }
53      }
54    }
55    IItem IValueParameter.Value {
56      get { return Value; }
57      set {
58        T val = value as T;
59        if ((value != null) && (val == null))
60          throw new InvalidOperationException(
61            string.Format("Type mismatch. Value is not a \"{0}\".",
62                          typeof(T).GetPrettyName())
63          );
64        Value = val;
65      }
66    }
67
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
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;
94          if (reactOnValueToStringChangedAndValueItemImageChanged) {
95            RegisterValueEvents();
96            OnToStringChanged();
97            OnItemImageChanged();
98          } else
99            DeregisterValueEvents();
100        }
101      }
102    }
103
104    #region Constructors
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;
111      reactOnValueToStringChangedAndValueItemImageChanged = original.reactOnValueToStringChangedAndValueItemImageChanged;
112      RegisterValueEvents();
113    }
114    public OptionalValueParameter()
115      : base("Anonymous", typeof(T)) {
116      this.getsCollected = true;
117      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
118    }
119    public OptionalValueParameter(string name)
120      : base(name, typeof(T)) {
121      this.getsCollected = true;
122      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
123    }
124    public OptionalValueParameter(string name, bool getsCollected)
125      : base(name, typeof(T)) {
126      this.getsCollected = getsCollected;
127      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
128    }
129    public OptionalValueParameter(string name, T value)
130      : base(name, typeof(T)) {
131      this.value = value;
132      this.getsCollected = true;
133      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
134      RegisterValueEvents();
135    }
136    public OptionalValueParameter(string name, T value, bool getsCollected)
137      : base(name, typeof(T)) {
138      this.value = value;
139      this.getsCollected = getsCollected;
140      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
141      RegisterValueEvents();
142    }
143    public OptionalValueParameter(string name, string description)
144      : base(name, description, typeof(T)) {
145      this.getsCollected = true;
146      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
147    }
148    public OptionalValueParameter(string name, string description, bool getsCollected)
149      : base(name, description, typeof(T)) {
150      this.getsCollected = getsCollected;
151      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
152    }
153    public OptionalValueParameter(string name, string description, T value)
154      : base(name, description, typeof(T)) {
155      this.value = value;
156      this.getsCollected = true;
157      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
158      RegisterValueEvents();
159    }
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;
164      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
165      RegisterValueEvents();
166    }
167    #endregion
168
169    [StorableHook(HookType.AfterDeserialization)]
170    private void AfterDeserialization() {
171      RegisterValueEvents();
172    }
173
174    public override IDeepCloneable Clone(Cloner cloner) {
175      return new OptionalValueParameter<T>(this, cloner);
176    }
177
178    public override string ToString() {
179      if (reactOnValueToStringChangedAndValueItemImageChanged)
180        return Name + ": " + (Value != null ? Value.ToString() : "null");
181      else
182        return Name;
183    }
184
185    protected override IItem GetActualValue() {
186      return Value;
187    }
188    protected override void SetActualValue(IItem value) {
189      ((IValueParameter)this).Value = value;
190    }
191
192    public event EventHandler ValueChanged;
193    protected virtual void OnValueChanged() {
194      EventHandler handler = ValueChanged;
195      if (handler != null) handler(this, EventArgs.Empty);
196      OnItemImageChanged();
197      OnToStringChanged();
198    }
199    public event EventHandler GetsCollectedChanged;
200    protected virtual void OnGetsCollectedChanged() {
201      EventHandler handler = GetsCollectedChanged;
202      if (handler != null) handler(this, EventArgs.Empty);
203    }
204
205    private void RegisterValueEvents() {
206      if ((value != null) && reactOnValueToStringChangedAndValueItemImageChanged) {
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    }
220    private void Value_ToStringChanged(object sender, EventArgs e) {
221      OnToStringChanged();
222    }
223  }
224}
Note: See TracBrowser for help on using the repository browser.