[2653] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17226] | 3 | * Copyright (C) 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;
|
---|
[16946] | 24 | using HEAL.Attic;
|
---|
[2756] | 25 | using HeuristicLab.Common;
|
---|
[2714] | 26 | using HeuristicLab.Core;
|
---|
[2653] | 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.")]
|
---|
[16723] | 33 | [StorableType("B34BB41A-CF50-4275-9BCD-1861EBA7C58F")]
|
---|
[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 {
|
---|
[16946] | 47 | if (ReadOnly) throw new InvalidOperationException("Cannot set the value of a readonly parameter.");
|
---|
[17317] | 48 | DoSetValue(value);
|
---|
[2653] | 49 | }
|
---|
| 50 | }
|
---|
[17317] | 51 | public virtual void ForceValue(T value) {
|
---|
| 52 | DoSetValue(value);
|
---|
| 53 | }
|
---|
| 54 | private void DoSetValue(T value) {
|
---|
| 55 | if (value != this.value) {
|
---|
| 56 | DeregisterValueEvents();
|
---|
| 57 | this.value = value;
|
---|
| 58 | RegisterValueEvents();
|
---|
| 59 | OnValueChanged();
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[2796] | 63 | IItem IValueParameter.Value {
|
---|
| 64 | get { return Value; }
|
---|
| 65 | set {
|
---|
| 66 | T val = value as T;
|
---|
[2852] | 67 | if ((value != null) && (val == null))
|
---|
[2796] | 68 | throw new InvalidOperationException(
|
---|
| 69 | string.Format("Type mismatch. Value is not a \"{0}\".",
|
---|
| 70 | typeof(T).GetPrettyName())
|
---|
| 71 | );
|
---|
| 72 | Value = val;
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
[2653] | 75 |
|
---|
[16946] | 76 | [Storable(DefaultValue = false)]
|
---|
| 77 | private bool readOnly;
|
---|
| 78 | public bool ReadOnly {
|
---|
| 79 | get { return readOnly; }
|
---|
| 80 | set {
|
---|
| 81 | if (value == readOnly) return;
|
---|
| 82 | readOnly = value;
|
---|
| 83 | OnReadOnlyChanged();
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[4332] | 87 | [Storable(DefaultValue = true)]
|
---|
| 88 | private bool getsCollected;
|
---|
| 89 | public bool GetsCollected {
|
---|
| 90 | get { return getsCollected; }
|
---|
| 91 | set {
|
---|
| 92 | if (value != getsCollected) {
|
---|
| 93 | getsCollected = value;
|
---|
| 94 | OnGetsCollectedChanged();
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | #region Constructors
|
---|
[4722] | 100 | [StorableConstructor]
|
---|
[16723] | 101 | protected ValueLookupParameter(StorableConstructorFlag _) : base(_) { }
|
---|
[4722] | 102 | protected ValueLookupParameter(ValueLookupParameter<T> original, Cloner cloner)
|
---|
| 103 | : base(original, cloner) {
|
---|
| 104 | value = cloner.Clone(original.value);
|
---|
[16946] | 105 | readOnly = original.readOnly;
|
---|
[4722] | 106 | getsCollected = original.getsCollected;
|
---|
| 107 | RegisterValueEvents();
|
---|
| 108 | }
|
---|
[2756] | 109 | public ValueLookupParameter()
|
---|
[2773] | 110 | : base() {
|
---|
[16946] | 111 | this.readOnly = false;
|
---|
[5784] | 112 | this.Hidden = false;
|
---|
[4332] | 113 | this.getsCollected = true;
|
---|
[2653] | 114 | }
|
---|
[2756] | 115 | public ValueLookupParameter(string name)
|
---|
[2773] | 116 | : base(name) {
|
---|
[16946] | 117 | this.readOnly = false;
|
---|
[5784] | 118 | this.Hidden = false;
|
---|
[4332] | 119 | this.getsCollected = true;
|
---|
[2756] | 120 | }
|
---|
| 121 | public ValueLookupParameter(string name, T value)
|
---|
[2773] | 122 | : base(name) {
|
---|
[3317] | 123 | this.value = value;
|
---|
[16946] | 124 | this.readOnly = false;
|
---|
[5784] | 125 | this.Hidden = false;
|
---|
[4332] | 126 | this.getsCollected = true;
|
---|
[4722] | 127 | RegisterValueEvents();
|
---|
[2756] | 128 | }
|
---|
| 129 | public ValueLookupParameter(string name, string description)
|
---|
[2773] | 130 | : base(name, description) {
|
---|
[16946] | 131 | this.readOnly = false;
|
---|
[5784] | 132 | this.Hidden = false;
|
---|
[4332] | 133 | this.getsCollected = true;
|
---|
[2653] | 134 | }
|
---|
[2756] | 135 | public ValueLookupParameter(string name, string description, T value)
|
---|
[2773] | 136 | : base(name, description) {
|
---|
[3317] | 137 | this.value = value;
|
---|
[16946] | 138 | this.readOnly = false;
|
---|
[5784] | 139 | this.Hidden = false;
|
---|
[4332] | 140 | this.getsCollected = true;
|
---|
[4722] | 141 | RegisterValueEvents();
|
---|
[2653] | 142 | }
|
---|
[3080] | 143 | public ValueLookupParameter(string name, string description, string actualName)
|
---|
| 144 | : base(name, description, actualName) {
|
---|
[16946] | 145 | this.readOnly = false;
|
---|
[5784] | 146 | this.Hidden = false;
|
---|
[4332] | 147 | this.getsCollected = true;
|
---|
[3080] | 148 | }
|
---|
[4332] | 149 | #endregion
|
---|
[2653] | 150 |
|
---|
[3317] | 151 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[4722] | 152 | private void AfterDeserialization() {
|
---|
[3341] | 153 | RegisterValueEvents();
|
---|
[3317] | 154 | }
|
---|
| 155 |
|
---|
[2740] | 156 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 157 | return new ValueLookupParameter<T>(this, cloner);
|
---|
[2740] | 158 | }
|
---|
| 159 |
|
---|
| 160 | public override string ToString() {
|
---|
[3688] | 161 | if (Value != null)
|
---|
| 162 | return Name + ": " + Value.ToString();
|
---|
| 163 | else if (Name.Equals(ActualName))
|
---|
| 164 | return Name;
|
---|
| 165 | else
|
---|
| 166 | return Name + ": " + ActualName;
|
---|
[2740] | 167 | }
|
---|
| 168 |
|
---|
[2756] | 169 | public event EventHandler ValueChanged;
|
---|
[4332] | 170 | protected virtual void OnValueChanged() {
|
---|
| 171 | EventHandler handler = ValueChanged;
|
---|
| 172 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3341] | 173 | OnItemImageChanged();
|
---|
[2932] | 174 | OnToStringChanged();
|
---|
[2653] | 175 | }
|
---|
[16946] | 176 | public event EventHandler ReadOnlyChanged;
|
---|
| 177 | protected virtual void OnReadOnlyChanged() {
|
---|
| 178 | EventHandler handler = ReadOnlyChanged;
|
---|
| 179 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 180 | }
|
---|
[4332] | 181 | public event EventHandler GetsCollectedChanged;
|
---|
| 182 | protected virtual void OnGetsCollectedChanged() {
|
---|
| 183 | EventHandler handler = GetsCollectedChanged;
|
---|
| 184 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 185 | }
|
---|
[3341] | 186 |
|
---|
| 187 | private void RegisterValueEvents() {
|
---|
| 188 | if (value != null) {
|
---|
| 189 | value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
|
---|
| 190 | value.ToStringChanged += new EventHandler(Value_ToStringChanged);
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | private void DeregisterValueEvents() {
|
---|
| 194 | if (value != null) {
|
---|
| 195 | value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
|
---|
| 196 | value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 | private void Value_ItemImageChanged(object sender, EventArgs e) {
|
---|
| 200 | OnItemImageChanged();
|
---|
| 201 | }
|
---|
[2932] | 202 | private void Value_ToStringChanged(object sender, EventArgs e) {
|
---|
| 203 | OnToStringChanged();
|
---|
[2653] | 204 | }
|
---|
| 205 | }
|
---|
| 206 | }
|
---|