Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Parameters/3.3/OptionalValueParameter.cs @ 4682

Last change on this file since 4682 was 4671, checked in by swagner, 14 years ago

Finished cloning refactoring of HeuristicLab.Parameters and simplified cloning code of HeuristicLab.Core (#922)

File size: 6.2 KB
RevLine 
[2756]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 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;
[2757]24using HeuristicLab.Common;
[2756]25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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.")]
[3017]33  [StorableClass]
[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 {
47        if (value != this.value) {
[3341]48          DeregisterValueEvents();
[2756]49          this.value = value;
[3341]50          RegisterValueEvents();
[2756]51          OnValueChanged();
52        }
53      }
54    }
[2796]55    IItem IValueParameter.Value {
[2757]56      get { return Value; }
[2796]57      set {
58        T val = value as T;
[2852]59        if ((value != null) && (val == null))
[2796]60          throw new InvalidOperationException(
61            string.Format("Type mismatch. Value is not a \"{0}\".",
62                          typeof(T).GetPrettyName())
63          );
64        Value = val;
65      }
[2757]66    }
[2756]67
[4332]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    #region Constructors
[4671]81    [StorableConstructor]
82    protected OptionalValueParameter(bool deserializing) : base(deserializing) { }
83    protected OptionalValueParameter(OptionalValueParameter<T> original, Cloner cloner)
84      : base(original, cloner) {
85      value = cloner.Clone(original.value);
86      getsCollected = original.getsCollected;
87      RegisterValueEvents();
88    }
[2890]89    public OptionalValueParameter()
[2756]90      : base("Anonymous", typeof(T)) {
[4332]91      this.getsCollected = true;
[2756]92    }
[2890]93    public OptionalValueParameter(string name)
[2756]94      : base(name, typeof(T)) {
[4332]95      this.getsCollected = true;
[2756]96    }
[4332]97    public OptionalValueParameter(string name, bool getsCollected)
98      : base(name, typeof(T)) {
99      this.getsCollected = getsCollected;
100    }
[2890]101    public OptionalValueParameter(string name, T value)
[2756]102      : base(name, typeof(T)) {
[3317]103      this.value = value;
[4332]104      this.getsCollected = true;
[4671]105      RegisterValueEvents();
[2756]106    }
[4332]107    public OptionalValueParameter(string name, T value, bool getsCollected)
108      : base(name, typeof(T)) {
109      this.value = value;
110      this.getsCollected = getsCollected;
[4671]111      RegisterValueEvents();
[4332]112    }
[2890]113    public OptionalValueParameter(string name, string description)
[2756]114      : base(name, description, typeof(T)) {
[4332]115      this.getsCollected = true;
[2756]116    }
[4332]117    public OptionalValueParameter(string name, string description, bool getsCollected)
118      : base(name, description, typeof(T)) {
119      this.getsCollected = getsCollected;
120    }
[2890]121    public OptionalValueParameter(string name, string description, T value)
[2756]122      : base(name, description, typeof(T)) {
[3317]123      this.value = value;
[4332]124      this.getsCollected = true;
[4671]125      RegisterValueEvents();
[2756]126    }
[4332]127    public OptionalValueParameter(string name, string description, T value, bool getsCollected)
128      : base(name, description, typeof(T)) {
129      this.value = value;
130      this.getsCollected = getsCollected;
[4671]131      RegisterValueEvents();
[4332]132    }
133    #endregion
[2756]134
[3317]135    [StorableHook(HookType.AfterDeserialization)]
[4671]136    private void AfterDeserialization() {
[3341]137      RegisterValueEvents();
[3317]138    }
139
[2756]140    public override IDeepCloneable Clone(Cloner cloner) {
[4671]141      return new OptionalValueParameter<T>(this, cloner);
[2756]142    }
143
144    public override string ToString() {
[3688]145      return Name + ": " + (Value != null ? Value.ToString() : "null");
[2756]146    }
147
[2757]148    protected override IItem GetActualValue() {
149      return Value;
150    }
151    protected override void SetActualValue(IItem value) {
[2796]152      ((IValueParameter)this).Value = value;
[2757]153    }
154
[2756]155    public event EventHandler ValueChanged;
[4332]156    protected virtual void OnValueChanged() {
157      EventHandler handler = ValueChanged;
158      if (handler != null) handler(this, EventArgs.Empty);
[3341]159      OnItemImageChanged();
[2932]160      OnToStringChanged();
[2756]161    }
[4332]162    public event EventHandler GetsCollectedChanged;
163    protected virtual void OnGetsCollectedChanged() {
164      EventHandler handler = GetsCollectedChanged;
165      if (handler != null) handler(this, EventArgs.Empty);
166    }
[3341]167
168    private void RegisterValueEvents() {
169      if (value != null) {
170        value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
171        value.ToStringChanged += new EventHandler(Value_ToStringChanged);
172      }
173    }
174    private void DeregisterValueEvents() {
175      if (value != null) {
176        value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
177        value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
178      }
179    }
180    private void Value_ItemImageChanged(object sender, EventArgs e) {
181      OnItemImageChanged();
182    }
[2932]183    private void Value_ToStringChanged(object sender, EventArgs e) {
184      OnToStringChanged();
[2756]185    }
186  }
187}
Note: See TracBrowser for help on using the repository browser.