Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/Variable.cs @ 3603

Last change on this file since 3603 was 3555, checked in by swagner, 14 years ago

Implemented reviewers' comments (#893)

File size: 5.0 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 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
22using System;
[3341]23using System.Drawing;
[2818]24using HeuristicLab.Common;
[1823]25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]26
27namespace 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.")]
[3017]32  [StorableClass]
[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
[776]57    /// <summary>
58    /// Initializes a new instance of <see cref="Variable"/> with name <c>Anonymous</c>
59    /// and value <c>null</c>.
60    /// </summary>
[2653]61    public Variable()
62      : base("Anonymous") {
[2830]63      this.value = null;
[2]64    }
[776]65    /// <summary>
[801]66    /// Initializes a new instance of <see cref="Variable"/> with the specified <paramref name="name"/>
[776]67    /// and the specified <paramref name="value"/>.
68    /// </summary>
69    /// <param name="name">The name of the current instance.</param>
70    /// <param name="value">The value of the current instance.</param>
[2830]71    public Variable(string name)
72      : base(name) {
73      this.value = null;
74    }
75    public Variable(string name, string description)
76      : base(name, description) {
77      this.value = null;
78    }
[2653]79    public Variable(string name, IItem value)
80      : base(name) {
[2830]81      this.value = value;
[3317]82      Initialize();
[2]83    }
[2830]84    public Variable(string name, string description, IItem value)
85      : base(name, description) {
86      this.value = value;
[3317]87      Initialize();
[2830]88    }
[3317]89    [StorableConstructor]
90    private Variable(bool deserializing) : base(deserializing) { }
[2]91
[3317]92    [StorableHook(HookType.AfterDeserialization)]
93    private void Initialize() {
[3341]94      RegisterValueEvents();
[3317]95    }
96
[776]97    /// <summary>
98    /// Clones the current instance (deep clone).
99    /// </summary>
100    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
101    /// <returns>The cloned object as <see cref="Variable"/>.</returns>
[2653]102    public override IDeepCloneable Clone(Cloner cloner) {
[3317]103      Variable clone = new Variable(Name, Description);
[2526]104      cloner.RegisterClonedObject(this, clone);
[3317]105      clone.value = (IItem)cloner.Clone(value);
106      clone.Initialize();
[2]107      return clone;
108    }
109
[776]110    /// <summary>
111    /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
112    /// </summary>
113    /// <returns>The current instance as a string.</returns>
[2]114    public override string ToString() {
[2653]115      if (Value == null)
116        return string.Format("{0}: null", Name);
117      else
[3555]118        return string.Format("{0}: {1}", Name, Value.ToString());
[2]119    }
120
[776]121    /// <inheritdoc/>
[2]122    public event EventHandler ValueChanged;
[776]123    /// <summary>
124    /// Fires a new <c>ValueChanged</c> even.
125    /// </summary>
[2653]126    private void OnValueChanged() {
[2]127      if (ValueChanged != null)
[2793]128        ValueChanged(this, EventArgs.Empty);
[3341]129      OnItemImageChanged();
[2989]130      OnToStringChanged();
[2]131    }
[2653]132
[3341]133    private void RegisterValueEvents() {
134      if (value != null) {
135        value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
136        value.ToStringChanged += new EventHandler(Value_ToStringChanged);
137      }
138    }
139    private void DeregisterValueEvents() {
140      if (value != null) {
141        value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
142        value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
143      }
144    }
145    private void Value_ItemImageChanged(object sender, EventArgs e) {
146      OnItemImageChanged();
147    }
[2932]148    private void Value_ToStringChanged(object sender, EventArgs e) {
149      OnToStringChanged();
[2653]150    }
[2]151  }
152}
Note: See TracBrowser for help on using the repository browser.