Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Parameters/3.3/ValueLookupParameter.cs @ 7018

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

Merged cloning refactoring branch back into trunk (#922)

File size: 6.4 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 either defined in the parameter itself or is retrieved from the scope.
31  /// </summary>
32  [Item("ValueLookupParameter", "A parameter whose value is either defined in the parameter itself or is retrieved from or written to a scope.")]
33  [StorableClass]
34  public class ValueLookupParameter<T> : LookupParameter<T>, IValueLookupParameter<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 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    [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
81    [StorableConstructor]
82    protected ValueLookupParameter(bool deserializing) : base(deserializing) { }
83    protected ValueLookupParameter(ValueLookupParameter<T> original, Cloner cloner)
84      : base(original, cloner) {
85      value = cloner.Clone(original.value);
86      getsCollected = original.getsCollected;
87      RegisterValueEvents();
88    }
89    public ValueLookupParameter()
90      : base() {
91      this.getsCollected = true;
92    }
93    public ValueLookupParameter(string name)
94      : base(name) {
95      this.getsCollected = true;
96    }
97    public ValueLookupParameter(string name, bool getsCollected)
98      : base(name) {
99      this.getsCollected = getsCollected;
100    }
101    public ValueLookupParameter(string name, T value)
102      : base(name) {
103      this.value = value;
104      this.getsCollected = true;
105      RegisterValueEvents();
106    }
107    public ValueLookupParameter(string name, T value, bool getsCollected)
108      : base(name) {
109      this.value = value;
110      this.getsCollected = getsCollected;
111      RegisterValueEvents();
112    }
113    public ValueLookupParameter(string name, string description)
114      : base(name, description) {
115      this.getsCollected = true;
116    }
117    public ValueLookupParameter(string name, string description, bool getsCollected)
118      : base(name, description) {
119      this.getsCollected = getsCollected;
120    }
121    public ValueLookupParameter(string name, string description, T value)
122      : base(name, description) {
123      this.value = value;
124      this.getsCollected = true;
125      RegisterValueEvents();
126    }
127    public ValueLookupParameter(string name, string description, T value, bool getsCollected)
128      : base(name, description) {
129      this.value = value;
130      this.getsCollected = getsCollected;
131      RegisterValueEvents();
132    }
133    public ValueLookupParameter(string name, string description, string actualName)
134      : base(name, description, actualName) {
135      this.getsCollected = true;
136    }
137    public ValueLookupParameter(string name, string description, string actualName, bool getsCollected)
138      : base(name, description, actualName) {
139      this.getsCollected = getsCollected;
140    }
141    #endregion
142
143    [StorableHook(HookType.AfterDeserialization)]
144    private void AfterDeserialization() {
145      RegisterValueEvents();
146    }
147
148    public override IDeepCloneable Clone(Cloner cloner) {
149      return new ValueLookupParameter<T>(this, cloner);
150    }
151
152    public override string ToString() {
153      if (Value != null)
154        return Name + ": " + Value.ToString();
155      else if (Name.Equals(ActualName))
156        return Name;
157      else
158        return Name + ": " + ActualName;
159    }
160
161    public event EventHandler ValueChanged;
162    protected virtual void OnValueChanged() {
163      EventHandler handler = ValueChanged;
164      if (handler != null) handler(this, EventArgs.Empty);
165      OnItemImageChanged();
166      OnToStringChanged();
167    }
168    public event EventHandler GetsCollectedChanged;
169    protected virtual void OnGetsCollectedChanged() {
170      EventHandler handler = GetsCollectedChanged;
171      if (handler != null) handler(this, EventArgs.Empty);
172    }
173
174    private void RegisterValueEvents() {
175      if (value != null) {
176        value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
177        value.ToStringChanged += new EventHandler(Value_ToStringChanged);
178      }
179    }
180    private void DeregisterValueEvents() {
181      if (value != null) {
182        value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
183        value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
184      }
185    }
186    private void Value_ItemImageChanged(object sender, EventArgs e) {
187      OnItemImageChanged();
188    }
189    private void Value_ToStringChanged(object sender, EventArgs e) {
190      OnToStringChanged();
191    }
192  }
193}
Note: See TracBrowser for help on using the repository browser.