#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Drawing;
using HeuristicLab.Common;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Core {
///
/// Represents a variable which has a name and holds an IItem.
///
[Item("Variable", "A variable which has a name and holds an IItem.")]
[StorableClass]
public sealed class Variable : NamedItem, IVariable {
public override Image ItemImage {
get {
if (value != null) return value.ItemImage;
else return base.ItemImage;
}
}
[Storable]
private IItem value;
///
/// Calls in the setter.
public IItem Value {
get { return value; }
set {
if (this.value != value) {
DeregisterValueEvents();
this.value = value;
RegisterValueEvents();
OnValueChanged();
}
}
}
[StorableConstructor]
private Variable(bool deserializing) : base(deserializing) { }
private Variable(Variable original, Cloner cloner)
: base(original, cloner) {
value = cloner.Clone(original.value);
RegisterValueEvents();
}
///
/// Initializes a new instance of with name Anonymous
/// and value null.
///
public Variable()
: base("Anonymous") {
this.value = null;
}
///
/// Initializes a new instance of with the specified
/// and the specified .
///
/// The name of the current instance.
/// The value of the current instance.
public Variable(string name)
: base(name) {
this.value = null;
}
public Variable(string name, string description)
: base(name, description) {
this.value = null;
}
public Variable(string name, IItem value)
: base(name) {
this.value = value;
RegisterValueEvents();
}
public Variable(string name, string description, IItem value)
: base(name, description) {
this.value = value;
RegisterValueEvents();
}
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
RegisterValueEvents();
}
///
/// Clones the current instance (deep clone).
///
/// Dictionary of all already cloned objects. (Needed to avoid cycles.)
/// The cloned object as .
public override IDeepCloneable Clone(Cloner cloner) {
return new Variable(this, cloner);
}
///
/// Gets the string representation of the current instance in the format: Name: [null|Value].
///
/// The current instance as a string.
public override string ToString() {
if (Value == null)
return string.Format("{0}: null", Name);
else
return string.Format("{0}: {1}", Name, Value.ToString());
}
///
public event EventHandler ValueChanged;
///
/// Fires a new ValueChanged even.
///
private void OnValueChanged() {
var handler = ValueChanged;
if (handler != null) handler(this, EventArgs.Empty);
OnItemImageChanged();
OnToStringChanged();
}
private void RegisterValueEvents() {
if (value != null) {
value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
value.ToStringChanged += new EventHandler(Value_ToStringChanged);
}
}
private void DeregisterValueEvents() {
if (value != null) {
value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
}
}
private void Value_ItemImageChanged(object sender, EventArgs e) {
OnItemImageChanged();
}
private void Value_ToStringChanged(object sender, EventArgs e) {
OnToStringChanged();
}
}
}