#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 HeuristicLab.Common;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Core {
[Item("NamedItem", "Base class for items which have a name and an optional description.")]
[StorableClass]
public abstract class NamedItem : Item, INamedItem {
[Storable]
protected string name;
///
/// Calls and also
/// eventually in the setter.
public string Name {
get { return name; }
set {
if (!CanChangeName) throw new NotSupportedException("Name cannot be changed.");
if (!(name.Equals(value) || (value == null) && (name == string.Empty))) {
CancelEventArgs e = value == null ? new CancelEventArgs(string.Empty) : new CancelEventArgs(value);
OnNameChanging(e);
if (!e.Cancel) {
name = value == null ? string.Empty : value;
OnNameChanged();
}
}
}
}
public virtual bool CanChangeName {
get { return true; }
}
[Storable]
protected string description;
public string Description {
get { return description; }
set {
if (!CanChangeDescription) throw new NotSupportedException("Description cannot be changed.");
if (!(description.Equals(value) || (value == null) && (description == string.Empty))) {
description = value == null ? string.Empty : value;
OnDescriptionChanged();
}
}
}
public virtual bool CanChangeDescription {
get { return true; }
}
[StorableConstructor]
protected NamedItem(bool deserializing) : base(deserializing) { }
protected NamedItem(NamedItem original, Cloner cloner)
: base(original, cloner) {
name = original.name;
description = original.description;
}
///
/// Initializes a new instance of with name Anonymous
/// and value null.
///
protected NamedItem() {
name = string.Empty;
description = string.Empty;
}
///
/// Initializes a new instance of with the specified
/// and the specified .
///
/// The name of the current instance.
/// The value of the current instance.
protected NamedItem(string name) {
if (name == null) this.name = string.Empty;
else this.name = name;
description = string.Empty;
}
protected NamedItem(string name, string description) {
if (name == null) this.name = string.Empty;
else this.name = name;
if (description == null) this.description = string.Empty;
else this.description = description;
}
///
/// Gets the string representation of the current instance in the format: Name: [null|Value].
///
/// The current instance as a string.
public override string ToString() {
return Name;
}
///
public event EventHandler> NameChanging;
///
/// Fires a new NameChanging event.
///
/// The event arguments of the changing.
protected virtual void OnNameChanging(CancelEventArgs e) {
var handler = NameChanging;
if (handler != null) handler(this, e);
}
///
public event EventHandler NameChanged;
///
/// Fires a new NameChanged event.
///
/// Calls .
protected virtual void OnNameChanged() {
var handler = NameChanged;
if (handler != null) handler(this, EventArgs.Empty);
OnToStringChanged();
}
///
public event EventHandler DescriptionChanged;
///
/// Fires a new DescriptionChanged event.
///
/// Calls .
protected virtual void OnDescriptionChanged() {
var handler = DescriptionChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
}
}