Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented reviewers' comments (#893)

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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<T>", "A parameter whose value is defined in the parameter itself or is null.")]
33  [StorableClass]
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 (value != this.value) {
48          DeregisterValueEvents();
49          this.value = value;
50          RegisterValueEvents();
51          OnValueChanged();
52        }
53      }
54    }
55    IItem IValueParameter.Value {
56      get { return Value; }
57      set {
58        T val = value as T;
59        if ((value != null) && (val == null))
60          throw new InvalidOperationException(
61            string.Format("Type mismatch. Value is not a \"{0}\".",
62                          typeof(T).GetPrettyName())
63          );
64        Value = val;
65      }
66    }
67
68    public OptionalValueParameter()
69      : base("Anonymous", typeof(T)) {
70    }
71    public OptionalValueParameter(string name)
72      : base(name, typeof(T)) {
73    }
74    public OptionalValueParameter(string name, T value)
75      : base(name, typeof(T)) {
76      this.value = value;
77      Initialize();
78    }
79    public OptionalValueParameter(string name, string description)
80      : base(name, description, typeof(T)) {
81    }
82    public OptionalValueParameter(string name, string description, T value)
83      : base(name, description, typeof(T)) {
84      this.value = value;
85      Initialize();
86    }
87    [StorableConstructor]
88    protected OptionalValueParameter(bool deserializing) : base(deserializing) { }
89
90    [StorableHook(HookType.AfterDeserialization)]
91    private void Initialize() {
92      RegisterValueEvents();
93    }
94
95    public override IDeepCloneable Clone(Cloner cloner) {
96      OptionalValueParameter<T> clone = (OptionalValueParameter<T>)base.Clone(cloner);
97      clone.value = (T)cloner.Clone(value);
98      clone.Initialize();
99      return clone;
100    }
101
102    public override string ToString() {
103      return Name + ": " + (Value != null ? Value.ToString() : "null");
104    }
105
106    protected override IItem GetActualValue() {
107      return Value;
108    }
109    protected override void SetActualValue(IItem value) {
110      ((IValueParameter)this).Value = value;
111    }
112
113    public event EventHandler ValueChanged;
114    private void OnValueChanged() {
115      if (ValueChanged != null)
116        ValueChanged(this, EventArgs.Empty);
117      OnItemImageChanged();
118      OnToStringChanged();
119    }
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    }
136    private void Value_ToStringChanged(object sender, EventArgs e) {
137      OnToStringChanged();
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.