Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Core/3.3/Variable.cs @ 10247

Last change on this file since 10247 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Drawing;
24using HeuristicLab.Common;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Core {
28  /// <summary>
29  /// Represents a variable which has a name and holds an IItem.
30  /// </summary>
31  [Item("Variable", "A variable which has a name and holds an IItem.")]
32  [StorableClass]
33  public sealed class Variable : NamedItem, IVariable {
34    public override Image ItemImage {
35      get {
36        if (value != null) return value.ItemImage;
37        else return base.ItemImage;
38      }
39    }
40
41    [Storable]
42    private IItem value;
43    /// <inheritdoc/>
44    /// <remarks>Calls <see cref="OnValueChanged"/> in the setter.</remarks>
45    public IItem Value {
46      get { return value; }
47      set {
48        if (this.value != value) {
49          DeregisterValueEvents();
50          this.value = value;
51          RegisterValueEvents();
52          OnValueChanged();
53        }
54      }
55    }
56
57    [StorableConstructor]
58    private Variable(bool deserializing) : base(deserializing) { }
59    private Variable(Variable original, Cloner cloner)
60      : base(original, cloner) {
61      value = cloner.Clone(original.value);
62      RegisterValueEvents();
63    }
64    /// <summary>
65    /// Initializes a new instance of <see cref="Variable"/> with name <c>Anonymous</c>
66    /// and value <c>null</c>.
67    /// </summary>
68    public Variable()
69      : base("Anonymous") {
70      this.value = null;
71    }
72    /// <summary>
73    /// Initializes a new instance of <see cref="Variable"/> with the specified <paramref name="name"/>
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>
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    }
86    public Variable(string name, IItem value)
87      : base(name) {
88      this.value = value;
89      RegisterValueEvents();
90    }
91    public Variable(string name, string description, IItem value)
92      : base(name, description) {
93      this.value = value;
94      RegisterValueEvents();
95    }
96
97    [StorableHook(HookType.AfterDeserialization)]
98    private void AfterDeserialization() {
99      RegisterValueEvents();
100    }
101
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>
107    public override IDeepCloneable Clone(Cloner cloner) {
108      return new Variable(this, cloner);
109    }
110
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>
115    public override string ToString() {
116      if (Value == null)
117        return string.Format("{0}: null", Name);
118      else
119        return string.Format("{0}: {1}", Name, Value.ToString());
120    }
121
122    /// <inheritdoc/>
123    public event EventHandler ValueChanged;
124    /// <summary>
125    /// Fires a new <c>ValueChanged</c> even.
126    /// </summary>
127    private void OnValueChanged() {
128      var handler = ValueChanged;
129      if (handler != null) handler(this, EventArgs.Empty);
130      OnItemImageChanged();
131      OnToStringChanged();
132    }
133
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    }
149    private void Value_ToStringChanged(object sender, EventArgs e) {
150      OnToStringChanged();
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.