Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Parameters/3.3/OptionalConstrainedValueParameter.cs @ 3199

Last change on this file since 3199 was 3136, checked in by swagner, 15 years ago

Restructured code of changes made in r3106 (#919).

File size: 6.1 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 HeuristicLab.Collections;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Parameters {
29  /// <summary>
30  /// A parameter whose value has to be chosen from a set of valid values or is null.
31  /// </summary>
32  [Item("OptionalConstrainedValueParameter<T>", "A parameter whose value has to be chosen from a set of valid values or is null.")]
33  [StorableClass]
34  public class OptionalConstrainedValueParameter<T> : Parameter, IValueParameter<T> where T : class, IItem {
35    private ItemSet<T> validValues;
36    [Storable]
37    public ItemSet<T> ValidValues {
38      get { return validValues; }
39      private set {
40        DeregisterValidValuesEvents();
41        validValues = value;
42        RegisterValidValuesEvents();
43      }
44    }
45
46    private T value;
47    [Storable]
48    public virtual T Value {
49      get { return this.value; }
50      set {
51        if (value != this.value) {
52          if ((value != null) && !validValues.Contains(value)) throw new ArgumentException("Invalid value.");
53          if (this.value != null) this.value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
54          this.value = value;
55          if (this.value != null) this.value.ToStringChanged += new EventHandler(Value_ToStringChanged);
56          OnValueChanged();
57        }
58      }
59    }
60
61    IItem IValueParameter.Value {
62      get { return Value; }
63      set {
64        T val = value as T;
65        if ((value != null) && (val == null))
66          throw new InvalidOperationException(
67            string.Format("Type mismatch. Value is not a \"{0}\".",
68                          typeof(T).GetPrettyName())
69          );
70        Value = val;
71      }
72    }
73
74    public OptionalConstrainedValueParameter()
75      : base("Anonymous", typeof(T)) {
76      ValidValues = new ItemSet<T>();
77    }
78    public OptionalConstrainedValueParameter(string name)
79      : base(name, typeof(T)) {
80      ValidValues = new ItemSet<T>();
81    }
82    public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues)
83      : base(name, typeof(T)) {
84      ValidValues = validValues;
85    }
86    public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues, T value)
87      : base(name, typeof(T)) {
88      ValidValues = validValues;
89      Value = value;
90    }
91    public OptionalConstrainedValueParameter(string name, string description)
92      : base(name, description, typeof(T)) {
93      ValidValues = new ItemSet<T>();
94    }
95    public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues)
96      : base(name, description, typeof(T)) {
97      ValidValues = validValues;
98    }
99    public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues, T value)
100      : base(name, description, typeof(T)) {
101      ValidValues = validValues;
102      Value = value;
103    }
104
105    public override IDeepCloneable Clone(Cloner cloner) {
106      OptionalConstrainedValueParameter<T> clone = (OptionalConstrainedValueParameter<T>)base.Clone(cloner);
107      clone.ValidValues = (ItemSet<T>)cloner.Clone(validValues);
108      clone.Value = (T)cloner.Clone(value);
109      return clone;
110    }
111
112    public override string ToString() {
113      return string.Format("{0}: {1} ({2})", Name, Value != null ? Value.ToString() : "null", DataType.GetPrettyName());
114    }
115
116    protected override IItem GetActualValue() {
117      return Value;
118    }
119    protected override void SetActualValue(IItem value) {
120      ((IValueParameter)this).Value = value;
121    }
122
123    public event EventHandler ValueChanged;
124    protected virtual void OnValueChanged() {
125      if (ValueChanged != null)
126        ValueChanged(this, EventArgs.Empty);
127      OnToStringChanged();
128    }
129
130    private void RegisterValidValuesEvents() {
131      if (validValues != null) {
132        validValues.ItemsAdded += new CollectionItemsChangedEventHandler<T>(validValues_ItemsAdded);
133        validValues.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
134        validValues.CollectionReset += new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
135      }
136    }
137
138    private void DeregisterValidValuesEvents() {
139      if (validValues != null) {
140        validValues.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(validValues_ItemsAdded);
141        validValues.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
142        validValues.CollectionReset -= new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
143      }
144    }
145
146    protected virtual void validValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) { }
147    protected virtual void ValidValues_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
148      if ((Value != null) && !validValues.Contains(Value)) Value = null;
149    }
150    protected virtual void ValidValues_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
151      if ((Value != null) && !validValues.Contains(Value)) Value = null;
152    }
153    protected virtual void Value_ToStringChanged(object sender, EventArgs e) {
154      OnToStringChanged();
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.