Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/OptionalValueParameter.cs @ 17567

Last change on this file since 17567 was 17567, checked in by abeham, 4 years ago

#2521: work in progress

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