[2653] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2653] | 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;
|
---|
[2756] | 24 | using HeuristicLab.Common;
|
---|
[2714] | 25 | using HeuristicLab.Core;
|
---|
[2653] | 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 27 |
|
---|
[2714] | 28 | namespace HeuristicLab.Parameters {
|
---|
[2653] | 29 | /// <summary>
|
---|
[2947] | 30 | /// A parameter whose value is either defined in the parameter itself or is retrieved from the scope.
|
---|
[2653] | 31 | /// </summary>
|
---|
[3822] | 32 | [Item("ValueLookupParameter", "A parameter whose value is either defined in the parameter itself or is retrieved from or written to a scope.")]
|
---|
[3017] | 33 | [StorableClass]
|
---|
[2773] | 34 | public class ValueLookupParameter<T> : LookupParameter<T>, IValueLookupParameter<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;
|
---|
| 44 | public T Value {
|
---|
| 45 | get { return this.value; }
|
---|
[2653] | 46 | set {
|
---|
[2756] | 47 | if (value != this.value) {
|
---|
[5206] | 48 | OnValueChanging();
|
---|
[3341] | 49 | DeregisterValueEvents();
|
---|
[2756] | 50 | this.value = value;
|
---|
[3341] | 51 | RegisterValueEvents();
|
---|
[2756] | 52 | OnValueChanged();
|
---|
[2653] | 53 | }
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
[2796] | 56 | IItem IValueParameter.Value {
|
---|
| 57 | get { return Value; }
|
---|
| 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 | }
|
---|
| 67 | }
|
---|
[2653] | 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 |
|
---|
| 81 | #region Constructors
|
---|
[4722] | 82 | [StorableConstructor]
|
---|
| 83 | protected ValueLookupParameter(bool deserializing) : base(deserializing) { }
|
---|
| 84 | protected ValueLookupParameter(ValueLookupParameter<T> original, Cloner cloner)
|
---|
| 85 | : base(original, cloner) {
|
---|
| 86 | value = cloner.Clone(original.value);
|
---|
| 87 | getsCollected = original.getsCollected;
|
---|
| 88 | RegisterValueEvents();
|
---|
| 89 | }
|
---|
[2756] | 90 | public ValueLookupParameter()
|
---|
[2773] | 91 | : base() {
|
---|
[4332] | 92 | this.getsCollected = true;
|
---|
[2653] | 93 | }
|
---|
[2756] | 94 | public ValueLookupParameter(string name)
|
---|
[2773] | 95 | : base(name) {
|
---|
[4332] | 96 | this.getsCollected = true;
|
---|
[2756] | 97 | }
|
---|
[4332] | 98 | public ValueLookupParameter(string name, bool getsCollected)
|
---|
| 99 | : base(name) {
|
---|
| 100 | this.getsCollected = getsCollected;
|
---|
| 101 | }
|
---|
[2756] | 102 | public ValueLookupParameter(string name, T value)
|
---|
[2773] | 103 | : base(name) {
|
---|
[3317] | 104 | this.value = value;
|
---|
[4332] | 105 | this.getsCollected = true;
|
---|
[4722] | 106 | RegisterValueEvents();
|
---|
[2756] | 107 | }
|
---|
[4332] | 108 | public ValueLookupParameter(string name, T value, bool getsCollected)
|
---|
| 109 | : base(name) {
|
---|
| 110 | this.value = value;
|
---|
| 111 | this.getsCollected = getsCollected;
|
---|
[4722] | 112 | RegisterValueEvents();
|
---|
[4332] | 113 | }
|
---|
[2756] | 114 | public ValueLookupParameter(string name, string description)
|
---|
[2773] | 115 | : base(name, description) {
|
---|
[4332] | 116 | this.getsCollected = true;
|
---|
[2653] | 117 | }
|
---|
[4332] | 118 | public ValueLookupParameter(string name, string description, bool getsCollected)
|
---|
| 119 | : base(name, description) {
|
---|
| 120 | this.getsCollected = getsCollected;
|
---|
| 121 | }
|
---|
[2756] | 122 | public ValueLookupParameter(string name, string description, T value)
|
---|
[2773] | 123 | : base(name, description) {
|
---|
[3317] | 124 | this.value = value;
|
---|
[4332] | 125 | this.getsCollected = true;
|
---|
[4722] | 126 | RegisterValueEvents();
|
---|
[2653] | 127 | }
|
---|
[4332] | 128 | public ValueLookupParameter(string name, string description, T value, bool getsCollected)
|
---|
| 129 | : base(name, description) {
|
---|
| 130 | this.value = value;
|
---|
| 131 | this.getsCollected = getsCollected;
|
---|
[4722] | 132 | RegisterValueEvents();
|
---|
[4332] | 133 | }
|
---|
[3080] | 134 | public ValueLookupParameter(string name, string description, string actualName)
|
---|
| 135 | : base(name, description, actualName) {
|
---|
[4332] | 136 | this.getsCollected = true;
|
---|
[3080] | 137 | }
|
---|
[4332] | 138 | public ValueLookupParameter(string name, string description, string actualName, bool getsCollected)
|
---|
| 139 | : base(name, description, actualName) {
|
---|
| 140 | this.getsCollected = getsCollected;
|
---|
| 141 | }
|
---|
| 142 | #endregion
|
---|
[2653] | 143 |
|
---|
[3317] | 144 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[4722] | 145 | private void AfterDeserialization() {
|
---|
[3341] | 146 | RegisterValueEvents();
|
---|
[3317] | 147 | }
|
---|
| 148 |
|
---|
[2740] | 149 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 150 | return new ValueLookupParameter<T>(this, cloner);
|
---|
[2740] | 151 | }
|
---|
| 152 |
|
---|
| 153 | public override string ToString() {
|
---|
[3688] | 154 | if (Value != null)
|
---|
| 155 | return Name + ": " + Value.ToString();
|
---|
| 156 | else if (Name.Equals(ActualName))
|
---|
| 157 | return Name;
|
---|
| 158 | else
|
---|
| 159 | return Name + ": " + ActualName;
|
---|
[2740] | 160 | }
|
---|
| 161 |
|
---|
[5206] | 162 | public event EventHandler ValueChanging;
|
---|
| 163 | protected virtual void OnValueChanging() {
|
---|
| 164 | EventHandler handler = ValueChanging;
|
---|
| 165 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 166 | }
|
---|
[2756] | 167 | public event EventHandler ValueChanged;
|
---|
[4332] | 168 | protected virtual void OnValueChanged() {
|
---|
| 169 | EventHandler handler = ValueChanged;
|
---|
| 170 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3341] | 171 | OnItemImageChanged();
|
---|
[2932] | 172 | OnToStringChanged();
|
---|
[2653] | 173 | }
|
---|
[4332] | 174 | public event EventHandler GetsCollectedChanged;
|
---|
| 175 | protected virtual void OnGetsCollectedChanged() {
|
---|
| 176 | EventHandler handler = GetsCollectedChanged;
|
---|
| 177 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 178 | }
|
---|
[3341] | 179 |
|
---|
| 180 | private void RegisterValueEvents() {
|
---|
| 181 | if (value != null) {
|
---|
| 182 | value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
|
---|
| 183 | value.ToStringChanged += new EventHandler(Value_ToStringChanged);
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 | private void DeregisterValueEvents() {
|
---|
| 187 | if (value != null) {
|
---|
| 188 | value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
|
---|
| 189 | value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 | private void Value_ItemImageChanged(object sender, EventArgs e) {
|
---|
| 193 | OnItemImageChanged();
|
---|
| 194 | }
|
---|
[2932] | 195 | private void Value_ToStringChanged(object sender, EventArgs e) {
|
---|
| 196 | OnToStringChanged();
|
---|
[2653] | 197 | }
|
---|
| 198 | }
|
---|
| 199 | }
|
---|