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