[2924] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 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;
|
---|
[17149] | 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.")]
|
---|
[17097] | 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 | }
|
---|
[17149] | 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 {
|
---|
[17149] | 54 | if (ReadOnly) throw new InvalidOperationException("Cannot set the value of a readonly parameter.");
|
---|
[2924] | 55 | if (value != this.value) {
|
---|
| 56 | if ((value != null) && !validValues.Contains(value)) throw new ArgumentException("Invalid value.");
|
---|
[3341] | 57 | DeregisterValueEvents();
|
---|
[2924] | 58 | this.value = value;
|
---|
[3341] | 59 | RegisterValueEvents();
|
---|
[2924] | 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 |
|
---|
[17149] | 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 |
|
---|
[4332] | 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
|
---|
[4722] | 101 | [StorableConstructor]
|
---|
[17097] | 102 | protected OptionalConstrainedValueParameter(StorableConstructorFlag _) : base(_) { }
|
---|
[4722] | 103 | protected OptionalConstrainedValueParameter(OptionalConstrainedValueParameter<T> original, Cloner cloner)
|
---|
| 104 | : base(original, cloner) {
|
---|
| 105 | validValues = cloner.Clone(original.validValues);
|
---|
| 106 | value = cloner.Clone(original.value);
|
---|
[17149] | 107 | readOnly = original.readOnly;
|
---|
[4722] | 108 | getsCollected = original.getsCollected;
|
---|
| 109 | Initialize();
|
---|
| 110 | }
|
---|
[2924] | 111 | public OptionalConstrainedValueParameter()
|
---|
| 112 | : base("Anonymous", typeof(T)) {
|
---|
[3317] | 113 | this.validValues = new ItemSet<T>();
|
---|
[17149] | 114 | this.readOnly = false;
|
---|
[4332] | 115 | this.getsCollected = true;
|
---|
[3317] | 116 | Initialize();
|
---|
[2924] | 117 | }
|
---|
| 118 | public OptionalConstrainedValueParameter(string name)
|
---|
| 119 | : base(name, typeof(T)) {
|
---|
[3317] | 120 | this.validValues = new ItemSet<T>();
|
---|
[17149] | 121 | this.readOnly = false;
|
---|
[4332] | 122 | this.getsCollected = true;
|
---|
[3317] | 123 | Initialize();
|
---|
[2924] | 124 | }
|
---|
| 125 | public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues)
|
---|
| 126 | : base(name, typeof(T)) {
|
---|
[3317] | 127 | this.validValues = validValues;
|
---|
[17149] | 128 | this.readOnly = false;
|
---|
[4332] | 129 | this.getsCollected = true;
|
---|
[3317] | 130 | Initialize();
|
---|
[2924] | 131 | }
|
---|
| 132 | public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues, T value)
|
---|
| 133 | : base(name, typeof(T)) {
|
---|
[3317] | 134 | this.validValues = validValues;
|
---|
| 135 | this.value = value;
|
---|
[17149] | 136 | this.readOnly = false;
|
---|
[4332] | 137 | this.getsCollected = true;
|
---|
[3317] | 138 | Initialize();
|
---|
[2924] | 139 | }
|
---|
| 140 | public OptionalConstrainedValueParameter(string name, string description)
|
---|
| 141 | : base(name, description, typeof(T)) {
|
---|
[3317] | 142 | this.validValues = new ItemSet<T>();
|
---|
[17149] | 143 | this.readOnly = false;
|
---|
[4332] | 144 | this.getsCollected = true;
|
---|
[3317] | 145 | Initialize();
|
---|
[2924] | 146 | }
|
---|
| 147 | public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues)
|
---|
| 148 | : base(name, description, typeof(T)) {
|
---|
[3317] | 149 | this.validValues = validValues;
|
---|
[17149] | 150 | this.readOnly = false;
|
---|
[4332] | 151 | this.getsCollected = true;
|
---|
[3317] | 152 | Initialize();
|
---|
[2924] | 153 | }
|
---|
| 154 | public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues, T value)
|
---|
| 155 | : base(name, description, typeof(T)) {
|
---|
[3317] | 156 | this.validValues = validValues;
|
---|
| 157 | this.value = value;
|
---|
[17149] | 158 | this.readOnly = false;
|
---|
[4332] | 159 | this.getsCollected = true;
|
---|
[3317] | 160 | Initialize();
|
---|
[2924] | 161 | }
|
---|
[4332] | 162 | #endregion
|
---|
[2924] | 163 |
|
---|
[3317] | 164 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[4722] | 165 | private void AfterDeserialization() {
|
---|
| 166 | Initialize();
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[3317] | 169 | private void Initialize() {
|
---|
| 170 | RegisterValidValuesEvents();
|
---|
[3341] | 171 | RegisterValueEvents();
|
---|
[3317] | 172 | }
|
---|
| 173 |
|
---|
[2924] | 174 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 175 | return new OptionalConstrainedValueParameter<T>(this, cloner);
|
---|
[2924] | 176 | }
|
---|
| 177 |
|
---|
| 178 | public override string ToString() {
|
---|
[3688] | 179 | return Name + ": " + (Value != null ? Value.ToString() : "null");
|
---|
[2924] | 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() {
|
---|
[4332] | 191 | EventHandler handler = ValueChanged;
|
---|
| 192 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3341] | 193 | OnItemImageChanged();
|
---|
[2932] | 194 | OnToStringChanged();
|
---|
[2924] | 195 | }
|
---|
[17149] | 196 | public event EventHandler ReadOnlyChanged;
|
---|
| 197 | protected virtual void OnReadOnlyChanged() {
|
---|
| 198 | EventHandler handler = ReadOnlyChanged;
|
---|
| 199 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 200 | }
|
---|
[4332] | 201 | public event EventHandler GetsCollectedChanged;
|
---|
| 202 | protected virtual void OnGetsCollectedChanged() {
|
---|
| 203 | EventHandler handler = GetsCollectedChanged;
|
---|
| 204 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 205 | }
|
---|
[2924] | 206 |
|
---|
| 207 | private void RegisterValidValuesEvents() {
|
---|
| 208 | if (validValues != null) {
|
---|
[3341] | 209 | validValues.ItemsAdded += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
|
---|
[2924] | 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) {
|
---|
[3341] | 216 | validValues.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
|
---|
[2924] | 217 | validValues.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
|
---|
| 218 | validValues.CollectionReset -= new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
|
---|
| 219 | }
|
---|
| 220 | }
|
---|
[3341] | 221 | protected virtual void ValidValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) { }
|
---|
[2924] | 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 | }
|
---|
[3341] | 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) {
|
---|
[2932] | 245 | OnToStringChanged();
|
---|
[2924] | 246 | }
|
---|
| 247 | }
|
---|
| 248 | }
|
---|