[2756] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2756] | 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;
|
---|
[2757] | 24 | using HeuristicLab.Common;
|
---|
[2756] | 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.Parameters {
|
---|
| 29 | /// <summary>
|
---|
[2947] | 30 | /// A parameter whose value is defined in the parameter itself or is null.
|
---|
[2756] | 31 | /// </summary>
|
---|
[3822] | 32 | [Item("OptionalValueParameter", "A parameter whose value is defined in the parameter itself or is null.")]
|
---|
[3017] | 33 | [StorableClass]
|
---|
[2890] | 34 | public class OptionalValueParameter<T> : Parameter, IValueParameter<T> where T : class, IItem {
|
---|
[3341] | 35 | public override Image ItemImage {
|
---|
| 36 | get {
|
---|
| 37 | if (value != null) return value.ItemImage;
|
---|
| 38 | else return base.ItemImage;
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[3317] | 42 | [Storable]
|
---|
[2756] | 43 | private T value;
|
---|
[2924] | 44 | public virtual T Value {
|
---|
[2756] | 45 | get { return this.value; }
|
---|
| 46 | set {
|
---|
| 47 | if (value != this.value) {
|
---|
[5206] | 48 | OnValueChanging();
|
---|
[3341] | 49 | DeregisterValueEvents();
|
---|
[2756] | 50 | this.value = value;
|
---|
[3341] | 51 | RegisterValueEvents();
|
---|
[2756] | 52 | OnValueChanged();
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
[2796] | 56 | IItem IValueParameter.Value {
|
---|
[2757] | 57 | get { return Value; }
|
---|
[2796] | 58 | set {
|
---|
| 59 | T val = value as T;
|
---|
[2852] | 60 | if ((value != null) && (val == null))
|
---|
[2796] | 61 | throw new InvalidOperationException(
|
---|
| 62 | string.Format("Type mismatch. Value is not a \"{0}\".",
|
---|
| 63 | typeof(T).GetPrettyName())
|
---|
| 64 | );
|
---|
| 65 | Value = val;
|
---|
| 66 | }
|
---|
[2757] | 67 | }
|
---|
[2756] | 68 |
|
---|
[4332] | 69 | [Storable(DefaultValue = true)]
|
---|
| 70 | private bool getsCollected;
|
---|
| 71 | public bool GetsCollected {
|
---|
| 72 | get { return getsCollected; }
|
---|
| 73 | set {
|
---|
| 74 | if (value != getsCollected) {
|
---|
| 75 | getsCollected = value;
|
---|
| 76 | OnGetsCollectedChanged();
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[4825] | 81 | [Storable(DefaultValue = true)]
|
---|
| 82 | private bool reactOnValueToStringChangedAndValueItemImageChanged;
|
---|
| 83 | /// <summary>
|
---|
| 84 | /// True if this parameter should react on the ToStringChanged and ItemImageChanged events of its value, otherwise false.
|
---|
| 85 | /// </summary>
|
---|
| 86 | /// <remarks>
|
---|
| 87 | /// In some rare cases when the value of the parameter is not deeply cloned, this property has to be set to false
|
---|
| 88 | /// to avoid a memory leak (cf. ticket #1268). In all other cases this property should always be true.
|
---|
| 89 | /// </remarks>
|
---|
| 90 | public bool ReactOnValueToStringChangedAndValueItemImageChanged {
|
---|
| 91 | get { return reactOnValueToStringChangedAndValueItemImageChanged; }
|
---|
| 92 | set {
|
---|
| 93 | if (value != reactOnValueToStringChangedAndValueItemImageChanged) {
|
---|
| 94 | reactOnValueToStringChangedAndValueItemImageChanged = value;
|
---|
| 95 | if (reactOnValueToStringChangedAndValueItemImageChanged)
|
---|
| 96 | RegisterValueEvents();
|
---|
| 97 | else
|
---|
| 98 | DeregisterValueEvents();
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[4332] | 103 | #region Constructors
|
---|
[4722] | 104 | [StorableConstructor]
|
---|
| 105 | protected OptionalValueParameter(bool deserializing) : base(deserializing) { }
|
---|
| 106 | protected OptionalValueParameter(OptionalValueParameter<T> original, Cloner cloner)
|
---|
| 107 | : base(original, cloner) {
|
---|
| 108 | value = cloner.Clone(original.value);
|
---|
| 109 | getsCollected = original.getsCollected;
|
---|
[4825] | 110 | reactOnValueToStringChangedAndValueItemImageChanged = original.reactOnValueToStringChangedAndValueItemImageChanged;
|
---|
[4722] | 111 | RegisterValueEvents();
|
---|
| 112 | }
|
---|
[2890] | 113 | public OptionalValueParameter()
|
---|
[2756] | 114 | : base("Anonymous", typeof(T)) {
|
---|
[4332] | 115 | this.getsCollected = true;
|
---|
[4825] | 116 | this.reactOnValueToStringChangedAndValueItemImageChanged = true;
|
---|
[2756] | 117 | }
|
---|
[2890] | 118 | public OptionalValueParameter(string name)
|
---|
[2756] | 119 | : base(name, typeof(T)) {
|
---|
[4332] | 120 | this.getsCollected = true;
|
---|
[4825] | 121 | this.reactOnValueToStringChangedAndValueItemImageChanged = true;
|
---|
[2756] | 122 | }
|
---|
[4332] | 123 | public OptionalValueParameter(string name, bool getsCollected)
|
---|
| 124 | : base(name, typeof(T)) {
|
---|
| 125 | this.getsCollected = getsCollected;
|
---|
[4825] | 126 | this.reactOnValueToStringChangedAndValueItemImageChanged = true;
|
---|
[4332] | 127 | }
|
---|
[2890] | 128 | public OptionalValueParameter(string name, T value)
|
---|
[2756] | 129 | : base(name, typeof(T)) {
|
---|
[3317] | 130 | this.value = value;
|
---|
[4332] | 131 | this.getsCollected = true;
|
---|
[4825] | 132 | this.reactOnValueToStringChangedAndValueItemImageChanged = true;
|
---|
[4722] | 133 | RegisterValueEvents();
|
---|
[2756] | 134 | }
|
---|
[4332] | 135 | public OptionalValueParameter(string name, T value, bool getsCollected)
|
---|
| 136 | : base(name, typeof(T)) {
|
---|
| 137 | this.value = value;
|
---|
| 138 | this.getsCollected = getsCollected;
|
---|
[4825] | 139 | this.reactOnValueToStringChangedAndValueItemImageChanged = true;
|
---|
[4722] | 140 | RegisterValueEvents();
|
---|
[4332] | 141 | }
|
---|
[2890] | 142 | public OptionalValueParameter(string name, string description)
|
---|
[2756] | 143 | : base(name, description, typeof(T)) {
|
---|
[4332] | 144 | this.getsCollected = true;
|
---|
[4825] | 145 | this.reactOnValueToStringChangedAndValueItemImageChanged = true;
|
---|
[2756] | 146 | }
|
---|
[4332] | 147 | public OptionalValueParameter(string name, string description, bool getsCollected)
|
---|
| 148 | : base(name, description, typeof(T)) {
|
---|
| 149 | this.getsCollected = getsCollected;
|
---|
[4825] | 150 | this.reactOnValueToStringChangedAndValueItemImageChanged = true;
|
---|
[4332] | 151 | }
|
---|
[2890] | 152 | public OptionalValueParameter(string name, string description, T value)
|
---|
[2756] | 153 | : base(name, description, typeof(T)) {
|
---|
[3317] | 154 | this.value = value;
|
---|
[4332] | 155 | this.getsCollected = true;
|
---|
[4825] | 156 | this.reactOnValueToStringChangedAndValueItemImageChanged = true;
|
---|
[4722] | 157 | RegisterValueEvents();
|
---|
[2756] | 158 | }
|
---|
[4332] | 159 | public OptionalValueParameter(string name, string description, T value, bool getsCollected)
|
---|
| 160 | : base(name, description, typeof(T)) {
|
---|
| 161 | this.value = value;
|
---|
| 162 | this.getsCollected = getsCollected;
|
---|
[4825] | 163 | this.reactOnValueToStringChangedAndValueItemImageChanged = true;
|
---|
[4722] | 164 | RegisterValueEvents();
|
---|
[4332] | 165 | }
|
---|
| 166 | #endregion
|
---|
[2756] | 167 |
|
---|
[3317] | 168 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[4722] | 169 | private void AfterDeserialization() {
|
---|
[3341] | 170 | RegisterValueEvents();
|
---|
[3317] | 171 | }
|
---|
| 172 |
|
---|
[2756] | 173 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 174 | return new OptionalValueParameter<T>(this, cloner);
|
---|
[2756] | 175 | }
|
---|
| 176 |
|
---|
| 177 | public override string ToString() {
|
---|
[4825] | 178 | if (reactOnValueToStringChangedAndValueItemImageChanged)
|
---|
| 179 | return Name + ": " + (Value != null ? Value.ToString() : "null");
|
---|
| 180 | else
|
---|
| 181 | return Name;
|
---|
[2756] | 182 | }
|
---|
| 183 |
|
---|
[2757] | 184 | protected override IItem GetActualValue() {
|
---|
| 185 | return Value;
|
---|
| 186 | }
|
---|
| 187 | protected override void SetActualValue(IItem value) {
|
---|
[2796] | 188 | ((IValueParameter)this).Value = value;
|
---|
[2757] | 189 | }
|
---|
[5206] | 190 |
|
---|
| 191 | public event EventHandler ValueChanging;
|
---|
| 192 | protected virtual void OnValueChanging() {
|
---|
| 193 | EventHandler handler = ValueChanging;
|
---|
| 194 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 195 | }
|
---|
[2756] | 196 | public event EventHandler ValueChanged;
|
---|
[4332] | 197 | protected virtual void OnValueChanged() {
|
---|
| 198 | EventHandler handler = ValueChanged;
|
---|
| 199 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3341] | 200 | OnItemImageChanged();
|
---|
[2932] | 201 | OnToStringChanged();
|
---|
[2756] | 202 | }
|
---|
[4332] | 203 | public event EventHandler GetsCollectedChanged;
|
---|
| 204 | protected virtual void OnGetsCollectedChanged() {
|
---|
| 205 | EventHandler handler = GetsCollectedChanged;
|
---|
| 206 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 207 | }
|
---|
[3341] | 208 |
|
---|
| 209 | private void RegisterValueEvents() {
|
---|
[4825] | 210 | if ((value != null) && reactOnValueToStringChangedAndValueItemImageChanged) {
|
---|
[3341] | 211 | value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
|
---|
| 212 | value.ToStringChanged += new EventHandler(Value_ToStringChanged);
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
| 215 | private void DeregisterValueEvents() {
|
---|
| 216 | if (value != null) {
|
---|
| 217 | value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
|
---|
| 218 | value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
|
---|
| 219 | }
|
---|
| 220 | }
|
---|
| 221 | private void Value_ItemImageChanged(object sender, EventArgs e) {
|
---|
| 222 | OnItemImageChanged();
|
---|
| 223 | }
|
---|
[2932] | 224 | private void Value_ToStringChanged(object sender, EventArgs e) {
|
---|
| 225 | OnToStringChanged();
|
---|
[2756] | 226 | }
|
---|
| 227 | }
|
---|
| 228 | }
|
---|