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