Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Parameters/3.3/OptionalValueParameter.cs @ 3688

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

Implemented reviewers' comments (#893)

File size: 4.5 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>
[2947]32  [Item("OptionalValueParameter<T>", "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
[2890]68    public OptionalValueParameter()
[2756]69      : base("Anonymous", typeof(T)) {
70    }
[2890]71    public OptionalValueParameter(string name)
[2756]72      : base(name, typeof(T)) {
73    }
[2890]74    public OptionalValueParameter(string name, T value)
[2756]75      : base(name, typeof(T)) {
[3317]76      this.value = value;
77      Initialize();
[2756]78    }
[2890]79    public OptionalValueParameter(string name, string description)
[2756]80      : base(name, description, typeof(T)) {
81    }
[2890]82    public OptionalValueParameter(string name, string description, T value)
[2756]83      : base(name, description, typeof(T)) {
[3317]84      this.value = value;
85      Initialize();
[2756]86    }
[3317]87    [StorableConstructor]
88    protected OptionalValueParameter(bool deserializing) : base(deserializing) { }
[2756]89
[3317]90    [StorableHook(HookType.AfterDeserialization)]
91    private void Initialize() {
[3341]92      RegisterValueEvents();
[3317]93    }
94
[2756]95    public override IDeepCloneable Clone(Cloner cloner) {
[2890]96      OptionalValueParameter<T> clone = (OptionalValueParameter<T>)base.Clone(cloner);
[3317]97      clone.value = (T)cloner.Clone(value);
98      clone.Initialize();
[2756]99      return clone;
100    }
101
102    public override string ToString() {
[3688]103      return Name + ": " + (Value != null ? Value.ToString() : "null");
[2756]104    }
105
[2757]106    protected override IItem GetActualValue() {
107      return Value;
108    }
109    protected override void SetActualValue(IItem value) {
[2796]110      ((IValueParameter)this).Value = value;
[2757]111    }
112
[2756]113    public event EventHandler ValueChanged;
114    private void OnValueChanged() {
115      if (ValueChanged != null)
[2793]116        ValueChanged(this, EventArgs.Empty);
[3341]117      OnItemImageChanged();
[2932]118      OnToStringChanged();
[2756]119    }
[3341]120
121    private void RegisterValueEvents() {
122      if (value != null) {
123        value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
124        value.ToStringChanged += new EventHandler(Value_ToStringChanged);
125      }
126    }
127    private void DeregisterValueEvents() {
128      if (value != null) {
129        value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
130        value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
131      }
132    }
133    private void Value_ItemImageChanged(object sender, EventArgs e) {
134      OnItemImageChanged();
135    }
[2932]136    private void Value_ToStringChanged(object sender, EventArgs e) {
137      OnToStringChanged();
[2756]138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.