Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16872 was 16872, checked in by mkommend, 5 years ago

#3005: Added readonly flag to all value parameters.

File size: 9.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
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  [StorableType("1A825EE0-3A72-458C-B621-7CE989EE2F0D")]
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 (ReadOnly) throw new InvalidOperationException("Cannot set the value of a readonly parameter.");
48        if (value != this.value) {
49          DeregisterValueEvents();
50          this.value = value;
51          RegisterValueEvents();
52          OnValueChanged();
53        }
54      }
55    }
56    IItem IValueParameter.Value {
57      get { return Value; }
58      set {
59        T val = value as T;
60        if ((value != null) && (val == null))
61          throw new InvalidOperationException(
62            string.Format("Type mismatch. Value is not a \"{0}\".",
63                          typeof(T).GetPrettyName())
64          );
65        Value = val;
66      }
67    }
68
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
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
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;
106          if (reactOnValueToStringChangedAndValueItemImageChanged) {
107            RegisterValueEvents();
108            OnToStringChanged();
109            OnItemImageChanged();
110          } else
111            DeregisterValueEvents();
112        }
113      }
114    }
115
116    #region Constructors
117    [StorableConstructor]
118    protected OptionalValueParameter(StorableConstructorFlag _) : base(_) { }
119    protected OptionalValueParameter(OptionalValueParameter<T> original, Cloner cloner)
120      : base(original, cloner) {
121      value = cloner.Clone(original.value);
122      readOnly = original.readOnly;
123      getsCollected = original.getsCollected;
124      reactOnValueToStringChangedAndValueItemImageChanged = original.reactOnValueToStringChangedAndValueItemImageChanged;
125      RegisterValueEvents();
126    }
127    public OptionalValueParameter()
128      : base("Anonymous", typeof(T)) {
129      this.readOnly = false;
130      this.getsCollected = true;
131      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
132    }
133    public OptionalValueParameter(string name)
134      : base(name, typeof(T)) {
135      this.readOnly = false;
136      this.getsCollected = true;
137      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
138    }
139    public OptionalValueParameter(string name, bool getsCollected)
140      : base(name, typeof(T)) {
141      this.readOnly = false;
142      this.getsCollected = getsCollected;
143      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
144    }
145    public OptionalValueParameter(string name, T value)
146      : base(name, typeof(T)) {
147      this.value = value;
148      this.readOnly = false;
149      this.getsCollected = true;
150      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
151      RegisterValueEvents();
152    }
153    public OptionalValueParameter(string name, T value, bool getsCollected)
154      : base(name, typeof(T)) {
155      this.value = value;
156      this.readOnly = false;
157      this.getsCollected = getsCollected;
158      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
159      RegisterValueEvents();
160    }
161    public OptionalValueParameter(string name, string description)
162      : base(name, description, typeof(T)) {
163      this.readOnly = false;
164      this.getsCollected = true;
165      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
166    }
167    public OptionalValueParameter(string name, string description, bool getsCollected)
168      : base(name, description, typeof(T)) {
169      this.readOnly = false;
170      this.getsCollected = getsCollected;
171      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
172    }
173    public OptionalValueParameter(string name, string description, T value)
174      : base(name, description, typeof(T)) {
175      this.value = value;
176      this.readOnly = false;
177      this.getsCollected = true;
178      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
179      RegisterValueEvents();
180    }
181    public OptionalValueParameter(string name, string description, T value, bool getsCollected)
182      : base(name, description, typeof(T)) {
183      this.value = value;
184      this.readOnly = false;
185      this.getsCollected = getsCollected;
186      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
187      RegisterValueEvents();
188    }
189    #endregion
190
191    [StorableHook(HookType.AfterDeserialization)]
192    private void AfterDeserialization() {
193      RegisterValueEvents();
194    }
195
196    public override IDeepCloneable Clone(Cloner cloner) {
197      return new OptionalValueParameter<T>(this, cloner);
198    }
199
200    public override string ToString() {
201      if (reactOnValueToStringChangedAndValueItemImageChanged)
202        return Name + ": " + (Value != null ? Value.ToString() : "null");
203      else
204        return Name;
205    }
206
207    protected override IItem GetActualValue() {
208      return Value;
209    }
210    protected override void SetActualValue(IItem value) {
211      ((IValueParameter)this).Value = value;
212    }
213
214    public event EventHandler ValueChanged;
215    protected virtual void OnValueChanged() {
216      EventHandler handler = ValueChanged;
217      if (handler != null) handler(this, EventArgs.Empty);
218      OnItemImageChanged();
219      OnToStringChanged();
220    }
221
222    public event EventHandler ReadOnlyChanged;
223    protected virtual void OnReadOnlyChanged() {
224      EventHandler handler = ReadOnlyChanged;
225      if (handler != null) handler(this, EventArgs.Empty);
226    }
227    public event EventHandler GetsCollectedChanged;
228    protected virtual void OnGetsCollectedChanged() {
229      EventHandler handler = GetsCollectedChanged;
230      if (handler != null) handler(this, EventArgs.Empty);
231    }
232
233    private void RegisterValueEvents() {
234      if ((value != null) && reactOnValueToStringChangedAndValueItemImageChanged) {
235        value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
236        value.ToStringChanged += new EventHandler(Value_ToStringChanged);
237      }
238    }
239    private void DeregisterValueEvents() {
240      if (value != null) {
241        value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
242        value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
243      }
244    }
245    private void Value_ItemImageChanged(object sender, EventArgs e) {
246      OnItemImageChanged();
247    }
248    private void Value_ToStringChanged(object sender, EventArgs e) {
249      OnToStringChanged();
250    }
251  }
252}
Note: See TracBrowser for help on using the repository browser.