Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Parameters/3.3/OptionalValueParameter.cs @ 17577

Last change on this file since 17577 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 7.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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, T value)
140      : base(name, typeof(T)) {
141      this.value = value;
142      this.readOnly = false;
143      this.getsCollected = true;
144      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
145      RegisterValueEvents();
146    }
147    public OptionalValueParameter(string name, string description)
148      : base(name, description, typeof(T)) {
149      this.readOnly = false;
150      this.getsCollected = true;
151      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
152    }
153
154    public OptionalValueParameter(string name, string description, T value)
155      : base(name, description, typeof(T)) {
156      this.value = value;
157      this.readOnly = false;
158      this.getsCollected = true;
159      this.reactOnValueToStringChangedAndValueItemImageChanged = true;
160      RegisterValueEvents();
161    }
162    #endregion
163
164    [StorableHook(HookType.AfterDeserialization)]
165    private void AfterDeserialization() {
166      RegisterValueEvents();
167    }
168
169    public override IDeepCloneable Clone(Cloner cloner) {
170      return new OptionalValueParameter<T>(this, cloner);
171    }
172
173    public override string ToString() {
174      if (reactOnValueToStringChangedAndValueItemImageChanged)
175        return Name + ": " + (Value != null ? Value.ToString() : "null");
176      else
177        return Name;
178    }
179
180    protected override IItem GetActualValue() {
181      return Value;
182    }
183    protected override void SetActualValue(IItem value) {
184      ((IValueParameter)this).Value = value;
185    }
186
187    public event EventHandler ValueChanged;
188    protected virtual void OnValueChanged() {
189      EventHandler handler = ValueChanged;
190      if (handler != null) handler(this, EventArgs.Empty);
191      OnItemImageChanged();
192      OnToStringChanged();
193    }
194
195    public event EventHandler ReadOnlyChanged;
196    protected virtual void OnReadOnlyChanged() {
197      EventHandler handler = ReadOnlyChanged;
198      if (handler != null) handler(this, EventArgs.Empty);
199    }
200    public event EventHandler GetsCollectedChanged;
201    protected virtual void OnGetsCollectedChanged() {
202      EventHandler handler = GetsCollectedChanged;
203      if (handler != null) handler(this, EventArgs.Empty);
204    }
205
206    private void RegisterValueEvents() {
207      if ((value != null) && reactOnValueToStringChangedAndValueItemImageChanged) {
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    }
221    private void Value_ToStringChanged(object sender, EventArgs e) {
222      OnToStringChanged();
223    }
224  }
225}
Note: See TracBrowser for help on using the repository browser.