Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Parameters/3.3/OptionalConstrainedValueParameter.cs @ 17231

Last change on this file since 17231 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 8.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
25using HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28
29namespace HeuristicLab.Parameters {
30  /// <summary>
31  /// A parameter whose value has to be chosen from a set of valid values or is null.
32  /// </summary>
33  [Item("OptionalConstrainedValueParameter", "A parameter whose value has to be chosen from a set of valid values or is null.")]
34  [StorableType("9B2BFAE8-CD6E-499C-83A0-401B6CEE3A08")]
35  public class OptionalConstrainedValueParameter<T> : Parameter, IConstrainedValueParameter<T> where T : class, IItem {
36    public override Image ItemImage {
37      get {
38        if (value != null) return value.ItemImage;
39        else return base.ItemImage;
40      }
41    }
42
43    [Storable]
44    private ItemSet<T> validValues;
45    public IItemSet<T> ValidValues {
46      get { return validValues; }
47    }
48
49    [Storable]
50    private T value;
51    public virtual T Value {
52      get { return this.value; }
53      set {
54        if (ReadOnly) throw new InvalidOperationException("Cannot set the value of a readonly parameter.");
55        if (value != this.value) {
56          if ((value != null) && !validValues.Contains(value)) throw new ArgumentException("Invalid value.");
57          DeregisterValueEvents();
58          this.value = value;
59          RegisterValueEvents();
60          OnValueChanged();
61        }
62      }
63    }
64    IItem IValueParameter.Value {
65      get { return Value; }
66      set {
67        T val = value as T;
68        if ((value != null) && (val == null))
69          throw new InvalidOperationException(
70            string.Format("Type mismatch. Value is not a \"{0}\".",
71                          typeof(T).GetPrettyName())
72          );
73        Value = val;
74      }
75    }
76
77    [Storable(DefaultValue = false)]
78    private bool readOnly;
79    public bool ReadOnly {
80      get { return readOnly; }
81      set {
82        if (value == readOnly) return;
83        readOnly = value;
84        OnReadOnlyChanged();
85      }
86    }
87
88    [Storable(DefaultValue = true)]
89    private bool getsCollected;
90    public bool GetsCollected {
91      get { return getsCollected; }
92      set {
93        if (value != getsCollected) {
94          getsCollected = value;
95          OnGetsCollectedChanged();
96        }
97      }
98    }
99
100    #region Constructors
101    [StorableConstructor]
102    protected OptionalConstrainedValueParameter(StorableConstructorFlag _) : base(_) { }
103    protected OptionalConstrainedValueParameter(OptionalConstrainedValueParameter<T> original, Cloner cloner)
104      : base(original, cloner) {
105      validValues = cloner.Clone(original.validValues);
106      value = cloner.Clone(original.value);
107      readOnly = original.readOnly;
108      getsCollected = original.getsCollected;
109      Initialize();
110    }
111    public OptionalConstrainedValueParameter()
112      : base("Anonymous", typeof(T)) {
113      this.validValues = new ItemSet<T>();
114      this.readOnly = false;
115      this.getsCollected = true;
116      Initialize();
117    }
118    public OptionalConstrainedValueParameter(string name)
119      : base(name, typeof(T)) {
120      this.validValues = new ItemSet<T>();
121      this.readOnly = false;
122      this.getsCollected = true;
123      Initialize();
124    }
125    public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues)
126      : base(name, typeof(T)) {
127      this.validValues = validValues;
128      this.readOnly = false;
129      this.getsCollected = true;
130      Initialize();
131    }
132    public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues, T value)
133      : base(name, typeof(T)) {
134      this.validValues = validValues;
135      this.value = value;
136      this.readOnly = false;
137      this.getsCollected = true;
138      Initialize();
139    }
140    public OptionalConstrainedValueParameter(string name, string description)
141      : base(name, description, typeof(T)) {
142      this.validValues = new ItemSet<T>();
143      this.readOnly = false;
144      this.getsCollected = true;
145      Initialize();
146    }
147    public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues)
148      : base(name, description, typeof(T)) {
149      this.validValues = validValues;
150      this.readOnly = false;
151      this.getsCollected = true;
152      Initialize();
153    }
154    public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues, T value)
155      : base(name, description, typeof(T)) {
156      this.validValues = validValues;
157      this.value = value;
158      this.readOnly = false;
159      this.getsCollected = true;
160      Initialize();
161    }
162    #endregion
163
164    [StorableHook(HookType.AfterDeserialization)]
165    private void AfterDeserialization() {
166      Initialize();
167    }
168
169    private void Initialize() {
170      RegisterValidValuesEvents();
171      RegisterValueEvents();
172    }
173
174    public override IDeepCloneable Clone(Cloner cloner) {
175      return new OptionalConstrainedValueParameter<T>(this, cloner);
176    }
177
178    public override string ToString() {
179      return Name + ": " + (Value != null ? Value.ToString() : "null");
180    }
181
182    protected override IItem GetActualValue() {
183      return Value;
184    }
185    protected override void SetActualValue(IItem value) {
186      ((IValueParameter)this).Value = value;
187    }
188
189    public event EventHandler ValueChanged;
190    protected virtual void OnValueChanged() {
191      EventHandler handler = ValueChanged;
192      if (handler != null) handler(this, EventArgs.Empty);
193      OnItemImageChanged();
194      OnToStringChanged();
195    }
196    public event EventHandler ReadOnlyChanged;
197    protected virtual void OnReadOnlyChanged() {
198      EventHandler handler = ReadOnlyChanged;
199      if (handler != null) handler(this, EventArgs.Empty);
200    }
201    public event EventHandler GetsCollectedChanged;
202    protected virtual void OnGetsCollectedChanged() {
203      EventHandler handler = GetsCollectedChanged;
204      if (handler != null) handler(this, EventArgs.Empty);
205    }
206
207    private void RegisterValidValuesEvents() {
208      if (validValues != null) {
209        validValues.ItemsAdded += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
210        validValues.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
211        validValues.CollectionReset += new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
212      }
213    }
214    private void DeregisterValidValuesEvents() {
215      if (validValues != null) {
216        validValues.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
217        validValues.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
218        validValues.CollectionReset -= new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
219      }
220    }
221    protected virtual void ValidValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) { }
222    protected virtual void ValidValues_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
223      if ((Value != null) && !validValues.Contains(Value)) Value = null;
224    }
225    protected virtual void ValidValues_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
226      if ((Value != null) && !validValues.Contains(Value)) Value = null;
227    }
228
229    private void RegisterValueEvents() {
230      if (value != null) {
231        value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
232        value.ToStringChanged += new EventHandler(Value_ToStringChanged);
233      }
234    }
235    private void DeregisterValueEvents() {
236      if (value != null) {
237        value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
238        value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
239      }
240    }
241    private void Value_ItemImageChanged(object sender, EventArgs e) {
242      OnItemImageChanged();
243    }
244    private void Value_ToStringChanged(object sender, EventArgs e) {
245      OnToStringChanged();
246    }
247  }
248}
Note: See TracBrowser for help on using the repository browser.