[2653] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 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;
|
---|
[14927] | 26 | using HeuristicLab.Persistence;
|
---|
[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.")]
|
---|
[14927] | 33 | [StorableType("56f24741-39ad-4ad1-b966-55771b328524")]
|
---|
[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) {
|
---|
[3341] | 48 | DeregisterValueEvents();
|
---|
[2756] | 49 | this.value = value;
|
---|
[3341] | 50 | RegisterValueEvents();
|
---|
[2756] | 51 | OnValueChanged();
|
---|
[2653] | 52 | }
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
[2796] | 55 | IItem IValueParameter.Value {
|
---|
| 56 | get { return Value; }
|
---|
| 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 | }
|
---|
| 66 | }
|
---|
[2653] | 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 |
|
---|
| 80 | #region Constructors
|
---|
[4722] | 81 | [StorableConstructor]
|
---|
[15018] | 82 | protected ValueLookupParameter(StorableConstructorFlag deserializing) : base(deserializing) { }
|
---|
[4722] | 83 | protected ValueLookupParameter(ValueLookupParameter<T> original, Cloner cloner)
|
---|
| 84 | : base(original, cloner) {
|
---|
| 85 | value = cloner.Clone(original.value);
|
---|
| 86 | getsCollected = original.getsCollected;
|
---|
| 87 | RegisterValueEvents();
|
---|
| 88 | }
|
---|
[2756] | 89 | public ValueLookupParameter()
|
---|
[2773] | 90 | : base() {
|
---|
[5784] | 91 | this.Hidden = false;
|
---|
[4332] | 92 | this.getsCollected = true;
|
---|
[2653] | 93 | }
|
---|
[2756] | 94 | public ValueLookupParameter(string name)
|
---|
[2773] | 95 | : base(name) {
|
---|
[5784] | 96 | this.Hidden = false;
|
---|
[4332] | 97 | this.getsCollected = true;
|
---|
[2756] | 98 | }
|
---|
[4332] | 99 | public ValueLookupParameter(string name, bool getsCollected)
|
---|
| 100 | : base(name) {
|
---|
[5784] | 101 | this.Hidden = false;
|
---|
[4332] | 102 | this.getsCollected = getsCollected;
|
---|
| 103 | }
|
---|
[2756] | 104 | public ValueLookupParameter(string name, T value)
|
---|
[2773] | 105 | : base(name) {
|
---|
[3317] | 106 | this.value = value;
|
---|
[5784] | 107 | this.Hidden = false;
|
---|
[4332] | 108 | this.getsCollected = true;
|
---|
[4722] | 109 | RegisterValueEvents();
|
---|
[2756] | 110 | }
|
---|
[4332] | 111 | public ValueLookupParameter(string name, T value, bool getsCollected)
|
---|
| 112 | : base(name) {
|
---|
| 113 | this.value = value;
|
---|
[5784] | 114 | this.Hidden = false;
|
---|
[4332] | 115 | this.getsCollected = getsCollected;
|
---|
[4722] | 116 | RegisterValueEvents();
|
---|
[4332] | 117 | }
|
---|
[2756] | 118 | public ValueLookupParameter(string name, string description)
|
---|
[2773] | 119 | : base(name, description) {
|
---|
[5784] | 120 | this.Hidden = false;
|
---|
[4332] | 121 | this.getsCollected = true;
|
---|
[2653] | 122 | }
|
---|
[4332] | 123 | public ValueLookupParameter(string name, string description, bool getsCollected)
|
---|
| 124 | : base(name, description) {
|
---|
[5784] | 125 | this.Hidden = false;
|
---|
[4332] | 126 | this.getsCollected = getsCollected;
|
---|
| 127 | }
|
---|
[2756] | 128 | public ValueLookupParameter(string name, string description, T value)
|
---|
[2773] | 129 | : base(name, description) {
|
---|
[3317] | 130 | this.value = value;
|
---|
[5784] | 131 | this.Hidden = false;
|
---|
[4332] | 132 | this.getsCollected = true;
|
---|
[4722] | 133 | RegisterValueEvents();
|
---|
[2653] | 134 | }
|
---|
[4332] | 135 | public ValueLookupParameter(string name, string description, T value, bool getsCollected)
|
---|
| 136 | : base(name, description) {
|
---|
| 137 | this.value = value;
|
---|
[5784] | 138 | this.Hidden = false;
|
---|
[4332] | 139 | this.getsCollected = getsCollected;
|
---|
[4722] | 140 | RegisterValueEvents();
|
---|
[4332] | 141 | }
|
---|
[3080] | 142 | public ValueLookupParameter(string name, string description, string actualName)
|
---|
| 143 | : base(name, description, actualName) {
|
---|
[5784] | 144 | this.Hidden = false;
|
---|
[4332] | 145 | this.getsCollected = true;
|
---|
[3080] | 146 | }
|
---|
[4332] | 147 | public ValueLookupParameter(string name, string description, string actualName, bool getsCollected)
|
---|
| 148 | : base(name, description, actualName) {
|
---|
[5784] | 149 | this.Hidden = false;
|
---|
[4332] | 150 | this.getsCollected = getsCollected;
|
---|
| 151 | }
|
---|
| 152 | #endregion
|
---|
[2653] | 153 |
|
---|
[3317] | 154 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[4722] | 155 | private void AfterDeserialization() {
|
---|
[3341] | 156 | RegisterValueEvents();
|
---|
[3317] | 157 | }
|
---|
| 158 |
|
---|
[2740] | 159 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 160 | return new ValueLookupParameter<T>(this, cloner);
|
---|
[2740] | 161 | }
|
---|
| 162 |
|
---|
| 163 | public override string ToString() {
|
---|
[3688] | 164 | if (Value != null)
|
---|
| 165 | return Name + ": " + Value.ToString();
|
---|
| 166 | else if (Name.Equals(ActualName))
|
---|
| 167 | return Name;
|
---|
| 168 | else
|
---|
| 169 | return Name + ": " + ActualName;
|
---|
[2740] | 170 | }
|
---|
| 171 |
|
---|
[2756] | 172 | public event EventHandler ValueChanged;
|
---|
[4332] | 173 | protected virtual void OnValueChanged() {
|
---|
| 174 | EventHandler handler = ValueChanged;
|
---|
| 175 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3341] | 176 | OnItemImageChanged();
|
---|
[2932] | 177 | OnToStringChanged();
|
---|
[2653] | 178 | }
|
---|
[4332] | 179 | public event EventHandler GetsCollectedChanged;
|
---|
| 180 | protected virtual void OnGetsCollectedChanged() {
|
---|
| 181 | EventHandler handler = GetsCollectedChanged;
|
---|
| 182 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 183 | }
|
---|
[3341] | 184 |
|
---|
| 185 | private void RegisterValueEvents() {
|
---|
| 186 | if (value != null) {
|
---|
| 187 | value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
|
---|
| 188 | value.ToStringChanged += new EventHandler(Value_ToStringChanged);
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | private void DeregisterValueEvents() {
|
---|
| 192 | if (value != null) {
|
---|
| 193 | value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
|
---|
| 194 | value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 | private void Value_ItemImageChanged(object sender, EventArgs e) {
|
---|
| 198 | OnItemImageChanged();
|
---|
| 199 | }
|
---|
[2932] | 200 | private void Value_ToStringChanged(object sender, EventArgs e) {
|
---|
| 201 | OnToStringChanged();
|
---|
[2653] | 202 | }
|
---|
| 203 | }
|
---|
| 204 | }
|
---|