[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17246] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 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;
|
---|
[2818] | 24 | using HeuristicLab.Common;
|
---|
[16662] | 25 | using HEAL.Attic;
|
---|
[2] | 26 |
|
---|
| 27 | namespace HeuristicLab.Core {
|
---|
[776] | 28 | /// <summary>
|
---|
[2653] | 29 | /// Represents a variable which has a name and holds an IItem.
|
---|
[776] | 30 | /// </summary>
|
---|
[2653] | 31 | [Item("Variable", "A variable which has a name and holds an IItem.")]
|
---|
[16662] | 32 | [StorableType("FF4ED8C3-C0A0-42F9-8EFF-3DC6AAD90EE7")]
|
---|
[2687] | 33 | public sealed class Variable : NamedItem, IVariable {
|
---|
[3341] | 34 | public override Image ItemImage {
|
---|
| 35 | get {
|
---|
| 36 | if (value != null) return value.ItemImage;
|
---|
| 37 | else return base.ItemImage;
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[3317] | 41 | [Storable]
|
---|
[2653] | 42 | private IItem value;
|
---|
[776] | 43 | /// <inheritdoc/>
|
---|
[2653] | 44 | /// <remarks>Calls <see cref="OnValueChanged"/> in the setter.</remarks>
|
---|
[2] | 45 | public IItem Value {
|
---|
[2653] | 46 | get { return value; }
|
---|
[2] | 47 | set {
|
---|
[2653] | 48 | if (this.value != value) {
|
---|
[3341] | 49 | DeregisterValueEvents();
|
---|
[2653] | 50 | this.value = value;
|
---|
[3341] | 51 | RegisterValueEvents();
|
---|
[2] | 52 | OnValueChanged();
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
[1667] | 56 |
|
---|
[4722] | 57 | [StorableConstructor]
|
---|
[16662] | 58 | private Variable(StorableConstructorFlag _) : base(_) { }
|
---|
[4722] | 59 | private Variable(Variable original, Cloner cloner)
|
---|
| 60 | : base(original, cloner) {
|
---|
| 61 | value = cloner.Clone(original.value);
|
---|
| 62 | RegisterValueEvents();
|
---|
| 63 | }
|
---|
[776] | 64 | /// <summary>
|
---|
| 65 | /// Initializes a new instance of <see cref="Variable"/> with name <c>Anonymous</c>
|
---|
| 66 | /// and value <c>null</c>.
|
---|
| 67 | /// </summary>
|
---|
[2653] | 68 | public Variable()
|
---|
| 69 | : base("Anonymous") {
|
---|
[2830] | 70 | this.value = null;
|
---|
[2] | 71 | }
|
---|
[776] | 72 | /// <summary>
|
---|
[801] | 73 | /// Initializes a new instance of <see cref="Variable"/> with the specified <paramref name="name"/>
|
---|
[776] | 74 | /// and the specified <paramref name="value"/>.
|
---|
| 75 | /// </summary>
|
---|
| 76 | /// <param name="name">The name of the current instance.</param>
|
---|
| 77 | /// <param name="value">The value of the current instance.</param>
|
---|
[2830] | 78 | public Variable(string name)
|
---|
| 79 | : base(name) {
|
---|
| 80 | this.value = null;
|
---|
| 81 | }
|
---|
| 82 | public Variable(string name, string description)
|
---|
| 83 | : base(name, description) {
|
---|
| 84 | this.value = null;
|
---|
| 85 | }
|
---|
[2653] | 86 | public Variable(string name, IItem value)
|
---|
| 87 | : base(name) {
|
---|
[2830] | 88 | this.value = value;
|
---|
[4722] | 89 | RegisterValueEvents();
|
---|
[2] | 90 | }
|
---|
[2830] | 91 | public Variable(string name, string description, IItem value)
|
---|
| 92 | : base(name, description) {
|
---|
| 93 | this.value = value;
|
---|
[4722] | 94 | RegisterValueEvents();
|
---|
[2830] | 95 | }
|
---|
[2] | 96 |
|
---|
[3317] | 97 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[4722] | 98 | private void AfterDeserialization() {
|
---|
[3341] | 99 | RegisterValueEvents();
|
---|
[3317] | 100 | }
|
---|
| 101 |
|
---|
[776] | 102 | /// <summary>
|
---|
| 103 | /// Clones the current instance (deep clone).
|
---|
| 104 | /// </summary>
|
---|
| 105 | /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
|
---|
| 106 | /// <returns>The cloned object as <see cref="Variable"/>.</returns>
|
---|
[2653] | 107 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 108 | return new Variable(this, cloner);
|
---|
[2] | 109 | }
|
---|
| 110 |
|
---|
[776] | 111 | /// <summary>
|
---|
| 112 | /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
|
---|
| 113 | /// </summary>
|
---|
| 114 | /// <returns>The current instance as a string.</returns>
|
---|
[2] | 115 | public override string ToString() {
|
---|
[2653] | 116 | if (Value == null)
|
---|
| 117 | return string.Format("{0}: null", Name);
|
---|
| 118 | else
|
---|
[3555] | 119 | return string.Format("{0}: {1}", Name, Value.ToString());
|
---|
[2] | 120 | }
|
---|
| 121 |
|
---|
[776] | 122 | /// <inheritdoc/>
|
---|
[2] | 123 | public event EventHandler ValueChanged;
|
---|
[776] | 124 | /// <summary>
|
---|
| 125 | /// Fires a new <c>ValueChanged</c> even.
|
---|
| 126 | /// </summary>
|
---|
[2653] | 127 | private void OnValueChanged() {
|
---|
[4722] | 128 | var handler = ValueChanged;
|
---|
| 129 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3341] | 130 | OnItemImageChanged();
|
---|
[2989] | 131 | OnToStringChanged();
|
---|
[2] | 132 | }
|
---|
[2653] | 133 |
|
---|
[3341] | 134 | private void RegisterValueEvents() {
|
---|
| 135 | if (value != null) {
|
---|
| 136 | value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
|
---|
| 137 | value.ToStringChanged += new EventHandler(Value_ToStringChanged);
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | private void DeregisterValueEvents() {
|
---|
| 141 | if (value != null) {
|
---|
| 142 | value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
|
---|
| 143 | value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 | private void Value_ItemImageChanged(object sender, EventArgs e) {
|
---|
| 147 | OnItemImageChanged();
|
---|
| 148 | }
|
---|
[2932] | 149 | private void Value_ToStringChanged(object sender, EventArgs e) {
|
---|
| 150 | OnToStringChanged();
|
---|
[2653] | 151 | }
|
---|
[2] | 152 | }
|
---|
| 153 | }
|
---|