Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Parameters/3.3/ValueLookupParameter.cs @ 4695

Last change on this file since 4695 was 4477, checked in by gkronber, 14 years ago

Merged r4458, r4459,r4462,r4464 from data analysis exploration branch into trunk. #1142

File size: 6.3 KB
RevLine 
[2653]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2653]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;
[2756]24using HeuristicLab.Common;
[2714]25using HeuristicLab.Core;
[2653]26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
[2714]28namespace HeuristicLab.Parameters {
[2653]29  /// <summary>
[2947]30  /// A parameter whose value is either defined in the parameter itself or is retrieved from the scope.
[2653]31  /// </summary>
[3822]32  [Item("ValueLookupParameter", "A parameter whose value is either defined in the parameter itself or is retrieved from or written to a scope.")]
[3017]33  [StorableClass]
[2773]34  public class ValueLookupParameter<T> : LookupParameter<T>, IValueLookupParameter<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;
44    public T Value {
45      get { return this.value; }
[2653]46      set {
[2756]47        if (value != this.value) {
[3341]48          DeregisterValueEvents();
[2756]49          this.value = value;
[3341]50          RegisterValueEvents();
[2756]51          OnValueChanged();
[2653]52        }
53      }
54    }
[2796]55    IItem IValueParameter.Value {
56      get { return Value; }
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      }
66    }
[2653]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
[2756]81    public ValueLookupParameter()
[2773]82      : base() {
[4332]83      this.getsCollected = true;
[2653]84    }
[2756]85    public ValueLookupParameter(string name)
[2773]86      : base(name) {
[4332]87      this.getsCollected = true;
[2756]88    }
[4332]89    public ValueLookupParameter(string name, bool getsCollected)
90      : base(name) {
91      this.getsCollected = getsCollected;
92    }
[2756]93    public ValueLookupParameter(string name, T value)
[2773]94      : base(name) {
[3317]95      this.value = value;
[4332]96      this.getsCollected = true;
[3317]97      Initialize();
[2756]98    }
[4332]99    public ValueLookupParameter(string name, T value, bool getsCollected)
100      : base(name) {
101      this.value = value;
102      this.getsCollected = getsCollected;
103      Initialize();
104    }
[2756]105    public ValueLookupParameter(string name, string description)
[2773]106      : base(name, description) {
[4332]107      this.getsCollected = true;
[2653]108    }
[4332]109    public ValueLookupParameter(string name, string description, bool getsCollected)
110      : base(name, description) {
111      this.getsCollected = getsCollected;
112    }
[2756]113    public ValueLookupParameter(string name, string description, T value)
[2773]114      : base(name, description) {
[3317]115      this.value = value;
[4332]116      this.getsCollected = true;
[3317]117      Initialize();
[2653]118    }
[4332]119    public ValueLookupParameter(string name, string description, T value, bool getsCollected)
120      : base(name, description) {
121      this.value = value;
122      this.getsCollected = getsCollected;
123      Initialize();
124    }
[3080]125    public ValueLookupParameter(string name, string description, string actualName)
126      : base(name, description, actualName) {
[4332]127      this.getsCollected = true;
[3080]128    }
[4332]129    public ValueLookupParameter(string name, string description, string actualName, bool getsCollected)
130      : base(name, description, actualName) {
131      this.getsCollected = getsCollected;
132    }
[3317]133    [StorableConstructor]
134    protected ValueLookupParameter(bool deserializing) : base(deserializing) { }
[4332]135    #endregion
[2653]136
[3317]137    [StorableHook(HookType.AfterDeserialization)]
138    private void Initialize() {
[3341]139      RegisterValueEvents();
[3317]140    }
141
[2740]142    public override IDeepCloneable Clone(Cloner cloner) {
[2756]143      ValueLookupParameter<T> clone = (ValueLookupParameter<T>)base.Clone(cloner);
[3317]144      clone.value = (T)cloner.Clone(value);
[4332]145      clone.getsCollected = getsCollected;
[3317]146      clone.Initialize();
[2740]147      return clone;
148    }
149
150    public override string ToString() {
[3688]151      if (Value != null)
152        return Name + ": " + Value.ToString();
153      else if (Name.Equals(ActualName))
154        return Name;
155      else
156        return Name + ": " + ActualName;
[2740]157    }
158
[2756]159    public event EventHandler ValueChanged;
[4332]160    protected virtual void OnValueChanged() {
161      EventHandler handler = ValueChanged;
162      if (handler != null) handler(this, EventArgs.Empty);
[3341]163      OnItemImageChanged();
[2932]164      OnToStringChanged();
[2653]165    }
[4332]166    public event EventHandler GetsCollectedChanged;
167    protected virtual void OnGetsCollectedChanged() {
168      EventHandler handler = GetsCollectedChanged;
169      if (handler != null) handler(this, EventArgs.Empty);
170    }
[3341]171
172    private void RegisterValueEvents() {
173      if (value != null) {
174        value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
175        value.ToStringChanged += new EventHandler(Value_ToStringChanged);
176      }
177    }
178    private void DeregisterValueEvents() {
179      if (value != null) {
180        value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
181        value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
182      }
183    }
184    private void Value_ItemImageChanged(object sender, EventArgs e) {
185      OnItemImageChanged();
186    }
[2932]187    private void Value_ToStringChanged(object sender, EventArgs e) {
188      OnToStringChanged();
[2653]189    }
190  }
191}
Note: See TracBrowser for help on using the repository browser.