[2924] | 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 |
|
---|
| 22 | using System;
|
---|
[3341] | 23 | using System.Drawing;
|
---|
[2924] | 24 | using HeuristicLab.Collections;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 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.")]
|
---|
[3017] | 34 | [StorableClass]
|
---|
[2924] | 35 | public class OptionalConstrainedValueParameter<T> : Parameter, IValueParameter<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 | }
|
---|
| 42 |
|
---|
[3317] | 43 | [Storable]
|
---|
[2924] | 44 | private ItemSet<T> validValues;
|
---|
| 45 | public ItemSet<T> ValidValues {
|
---|
| 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 {
|
---|
| 54 | if (value != this.value) {
|
---|
| 55 | if ((value != null) && !validValues.Contains(value)) throw new ArgumentException("Invalid value.");
|
---|
[3341] | 56 | DeregisterValueEvents();
|
---|
[2924] | 57 | this.value = value;
|
---|
[3341] | 58 | RegisterValueEvents();
|
---|
[2924] | 59 | OnValueChanged();
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | IItem IValueParameter.Value {
|
---|
| 64 | get { return Value; }
|
---|
| 65 | set {
|
---|
| 66 | T val = value as T;
|
---|
| 67 | if ((value != null) && (val == null))
|
---|
| 68 | throw new InvalidOperationException(
|
---|
| 69 | string.Format("Type mismatch. Value is not a \"{0}\".",
|
---|
| 70 | typeof(T).GetPrettyName())
|
---|
| 71 | );
|
---|
| 72 | Value = val;
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[4332] | 76 | [Storable(DefaultValue = true)]
|
---|
| 77 | private bool getsCollected;
|
---|
| 78 | public bool GetsCollected {
|
---|
| 79 | get { return getsCollected; }
|
---|
| 80 | set {
|
---|
| 81 | if (value != getsCollected) {
|
---|
| 82 | getsCollected = value;
|
---|
| 83 | OnGetsCollectedChanged();
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | #region Constructors
|
---|
[2924] | 89 | public OptionalConstrainedValueParameter()
|
---|
| 90 | : base("Anonymous", typeof(T)) {
|
---|
[3317] | 91 | this.validValues = new ItemSet<T>();
|
---|
[4332] | 92 | this.getsCollected = true;
|
---|
[3317] | 93 | Initialize();
|
---|
[2924] | 94 | }
|
---|
| 95 | public OptionalConstrainedValueParameter(string name)
|
---|
| 96 | : base(name, typeof(T)) {
|
---|
[3317] | 97 | this.validValues = new ItemSet<T>();
|
---|
[4332] | 98 | this.getsCollected = true;
|
---|
[3317] | 99 | Initialize();
|
---|
[2924] | 100 | }
|
---|
[4332] | 101 | public OptionalConstrainedValueParameter(string name, bool getsCollected)
|
---|
| 102 | : base(name, typeof(T)) {
|
---|
| 103 | this.validValues = new ItemSet<T>();
|
---|
| 104 | this.getsCollected = getsCollected;
|
---|
| 105 | Initialize();
|
---|
| 106 | }
|
---|
[2924] | 107 | public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues)
|
---|
| 108 | : base(name, typeof(T)) {
|
---|
[3317] | 109 | this.validValues = validValues;
|
---|
[4332] | 110 | this.getsCollected = true;
|
---|
[3317] | 111 | Initialize();
|
---|
[2924] | 112 | }
|
---|
[4332] | 113 | public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues, bool getsCollected)
|
---|
| 114 | : base(name, typeof(T)) {
|
---|
| 115 | this.validValues = validValues;
|
---|
| 116 | this.getsCollected = getsCollected;
|
---|
| 117 | Initialize();
|
---|
| 118 | }
|
---|
[2924] | 119 | public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues, T value)
|
---|
| 120 | : base(name, typeof(T)) {
|
---|
[3317] | 121 | this.validValues = validValues;
|
---|
| 122 | this.value = value;
|
---|
[4332] | 123 | this.getsCollected = true;
|
---|
[3317] | 124 | Initialize();
|
---|
[2924] | 125 | }
|
---|
[4332] | 126 | public OptionalConstrainedValueParameter(string name, ItemSet<T> validValues, T value, bool getsCollected)
|
---|
| 127 | : base(name, typeof(T)) {
|
---|
| 128 | this.validValues = validValues;
|
---|
| 129 | this.value = value;
|
---|
| 130 | this.getsCollected = getsCollected;
|
---|
| 131 | Initialize();
|
---|
| 132 | }
|
---|
[2924] | 133 | public OptionalConstrainedValueParameter(string name, string description)
|
---|
| 134 | : base(name, description, typeof(T)) {
|
---|
[3317] | 135 | this.validValues = new ItemSet<T>();
|
---|
[4332] | 136 | this.getsCollected = true;
|
---|
[3317] | 137 | Initialize();
|
---|
[2924] | 138 | }
|
---|
[4332] | 139 | public OptionalConstrainedValueParameter(string name, string description, bool getsCollected)
|
---|
| 140 | : base(name, description, typeof(T)) {
|
---|
| 141 | this.validValues = new ItemSet<T>();
|
---|
| 142 | this.getsCollected = getsCollected;
|
---|
| 143 | Initialize();
|
---|
| 144 | }
|
---|
[2924] | 145 | public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues)
|
---|
| 146 | : base(name, description, typeof(T)) {
|
---|
[3317] | 147 | this.validValues = validValues;
|
---|
[4332] | 148 | this.getsCollected = true;
|
---|
[3317] | 149 | Initialize();
|
---|
[2924] | 150 | }
|
---|
[4332] | 151 | public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues, bool getsCollected)
|
---|
| 152 | : base(name, description, typeof(T)) {
|
---|
| 153 | this.validValues = validValues;
|
---|
| 154 | this.getsCollected = getsCollected;
|
---|
| 155 | Initialize();
|
---|
| 156 | }
|
---|
[2924] | 157 | public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues, T value)
|
---|
| 158 | : base(name, description, typeof(T)) {
|
---|
[3317] | 159 | this.validValues = validValues;
|
---|
| 160 | this.value = value;
|
---|
[4332] | 161 | this.getsCollected = true;
|
---|
[3317] | 162 | Initialize();
|
---|
[2924] | 163 | }
|
---|
[4332] | 164 | public OptionalConstrainedValueParameter(string name, string description, ItemSet<T> validValues, T value, bool getsCollected)
|
---|
| 165 | : base(name, description, typeof(T)) {
|
---|
| 166 | this.validValues = validValues;
|
---|
| 167 | this.value = value;
|
---|
| 168 | this.getsCollected = getsCollected;
|
---|
| 169 | Initialize();
|
---|
| 170 | }
|
---|
[3317] | 171 | [StorableConstructor]
|
---|
| 172 | protected OptionalConstrainedValueParameter(bool deserializing) : base(deserializing) { }
|
---|
[4332] | 173 | #endregion
|
---|
[2924] | 174 |
|
---|
[3317] | 175 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 176 | private void Initialize() {
|
---|
| 177 | RegisterValidValuesEvents();
|
---|
[3341] | 178 | RegisterValueEvents();
|
---|
[3317] | 179 | }
|
---|
| 180 |
|
---|
[2924] | 181 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 182 | OptionalConstrainedValueParameter<T> clone = (OptionalConstrainedValueParameter<T>)base.Clone(cloner);
|
---|
[3317] | 183 | clone.validValues = (ItemSet<T>)cloner.Clone(validValues);
|
---|
| 184 | clone.value = (T)cloner.Clone(value);
|
---|
[4332] | 185 | clone.getsCollected = getsCollected;
|
---|
[3317] | 186 | clone.Initialize();
|
---|
[2924] | 187 | return clone;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | public override string ToString() {
|
---|
[3688] | 191 | return Name + ": " + (Value != null ? Value.ToString() : "null");
|
---|
[2924] | 192 | }
|
---|
| 193 |
|
---|
| 194 | protected override IItem GetActualValue() {
|
---|
| 195 | return Value;
|
---|
| 196 | }
|
---|
| 197 | protected override void SetActualValue(IItem value) {
|
---|
| 198 | ((IValueParameter)this).Value = value;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | public event EventHandler ValueChanged;
|
---|
| 202 | protected virtual void OnValueChanged() {
|
---|
[4332] | 203 | EventHandler handler = ValueChanged;
|
---|
| 204 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3341] | 205 | OnItemImageChanged();
|
---|
[2932] | 206 | OnToStringChanged();
|
---|
[2924] | 207 | }
|
---|
[4332] | 208 | public event EventHandler GetsCollectedChanged;
|
---|
| 209 | protected virtual void OnGetsCollectedChanged() {
|
---|
| 210 | EventHandler handler = GetsCollectedChanged;
|
---|
| 211 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 212 | }
|
---|
[2924] | 213 |
|
---|
| 214 | private void RegisterValidValuesEvents() {
|
---|
| 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 | }
|
---|
| 221 | private void DeregisterValidValuesEvents() {
|
---|
| 222 | if (validValues != null) {
|
---|
[3341] | 223 | validValues.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
|
---|
[2924] | 224 | validValues.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
|
---|
| 225 | validValues.CollectionReset -= new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
|
---|
| 226 | }
|
---|
| 227 | }
|
---|
[3341] | 228 | protected virtual void ValidValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) { }
|
---|
[2924] | 229 | protected virtual void ValidValues_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
| 230 | if ((Value != null) && !validValues.Contains(Value)) Value = null;
|
---|
| 231 | }
|
---|
| 232 | protected virtual void ValidValues_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
| 233 | if ((Value != null) && !validValues.Contains(Value)) Value = null;
|
---|
| 234 | }
|
---|
[3341] | 235 |
|
---|
| 236 | private void RegisterValueEvents() {
|
---|
| 237 | if (value != null) {
|
---|
| 238 | value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
|
---|
| 239 | value.ToStringChanged += new EventHandler(Value_ToStringChanged);
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 | private void DeregisterValueEvents() {
|
---|
| 243 | if (value != null) {
|
---|
| 244 | value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
|
---|
| 245 | value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 | private void Value_ItemImageChanged(object sender, EventArgs e) {
|
---|
| 249 | OnItemImageChanged();
|
---|
| 250 | }
|
---|
| 251 | private void Value_ToStringChanged(object sender, EventArgs e) {
|
---|
[2932] | 252 | OnToStringChanged();
|
---|
[2924] | 253 | }
|
---|
| 254 | }
|
---|
| 255 | }
|
---|