Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/NamedItem.cs @ 4058

Last change on this file since 4058 was 3280, checked in by swagner, 14 years ago

Continued work on algorithm batch processing (#947).

File size: 5.5 KB
RevLine 
[2653]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2653]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;
[2931]23using HeuristicLab.Common;
[2653]24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25
26namespace HeuristicLab.Core {
[2664]27  [Item("NamedItem", "Base class for items which have a name and an optional description.")]
[3017]28  [StorableClass]
[2664]29  public abstract class NamedItem : Item, INamedItem {
[2653]30    [Storable]
[2851]31    protected string name;
[2653]32    /// <inheritdoc/>
33    /// <remarks>Calls <see cref="OnNameChanging"/> and also <see cref="OnNameChanged"/>
34    /// eventually in the setter.</remarks>
35    public string Name {
36      get { return name; }
37      set {
[2931]38        if (!CanChangeName) throw new NotSupportedException("Name cannot be changed.");
39        if (!(name.Equals(value) || (value == null) && (name == string.Empty))) {
40          CancelEventArgs<string> e = value == null ? new CancelEventArgs<string>(string.Empty) : new CancelEventArgs<string>(value);
[2653]41          OnNameChanging(e);
42          if (!e.Cancel) {
[2931]43            name = value == null ? string.Empty : value;
[2653]44            OnNameChanged();
45          }
46        }
47      }
48    }
49    public virtual bool CanChangeName {
50      get { return true; }
51    }
52    [Storable]
[2851]53    protected string description;
[2653]54    public string Description {
55      get { return description; }
56      set {
[2931]57        if (!CanChangeDescription) throw new NotSupportedException("Description cannot be changed.");
58        if (!(description.Equals(value) || (value == null) && (description == string.Empty))) {
59          description = value == null ? string.Empty : value;
[2653]60          OnDescriptionChanged();
61        }
62      }
63    }
64    public virtual bool CanChangeDescription {
65      get { return true; }
66    }
67
68    /// <summary>
69    /// Initializes a new instance of <see cref="Variable"/> with name <c>Anonymous</c>
70    /// and value <c>null</c>.
71    /// </summary>
[2664]72    protected NamedItem() {
[2830]73      name = string.Empty;
74      description = string.Empty;
[2653]75    }
76    /// <summary>
77    /// Initializes a new instance of <see cref="Variable"/> with the specified <paramref name="name"/>
78    /// and the specified <paramref name="value"/>.
79    /// </summary>
80    /// <param name="name">The name of the current instance.</param>
81    /// <param name="value">The value of the current instance.</param>
[2830]82    protected NamedItem(string name) {
[2931]83      if (name == null) this.name = string.Empty;
84      else this.name = name;
[2830]85      description = string.Empty;
[2653]86    }
[2830]87    protected NamedItem(string name, string description) {
[2931]88      if (name == null) this.name = string.Empty;
89      else this.name = name;
90      if (description == null) this.description = string.Empty;
91      else this.description = description;
[2653]92    }
[3280]93    [StorableConstructor]
94    protected NamedItem(bool deserializing) : base(deserializing) { }
[2653]95
96    /// <summary>
97    /// Clones the current instance (deep clone).
98    /// </summary>
99    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
100    /// <returns>The cloned object as <see cref="Variable"/>.</returns>
101    public override IDeepCloneable Clone(Cloner cloner) {
[2664]102      NamedItem clone = (NamedItem)base.Clone(cloner);
[2653]103      clone.name = name;
104      clone.description = description;
105      return clone;
106    }
107
108    /// <summary>
109    /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
110    /// </summary>
111    /// <returns>The current instance as a string.</returns>
112    public override string ToString() {
113      return Name;
114    }
115
116    /// <inheritdoc/>
117    public event EventHandler<CancelEventArgs<string>> NameChanging;
118    /// <summary>
119    /// Fires a new <c>NameChanging</c> event.
120    /// </summary>
121    /// <param name="e">The event arguments of the changing.</param>
122    protected virtual void OnNameChanging(CancelEventArgs<string> e) {
123      if (NameChanging != null)
124        NameChanging(this, e);
125    }
126    /// <inheritdoc/>
127    public event EventHandler NameChanged;
128    /// <summary>
129    /// Fires a new <c>NameChanged</c> event.
130    /// </summary>
131    /// <remarks>Calls <see cref="ItemBase.OnChanged"/>.</remarks>
132    protected virtual void OnNameChanged() {
133      if (NameChanged != null)
[2793]134        NameChanged(this, EventArgs.Empty);
[2932]135      OnToStringChanged();
[2653]136    }
137    /// <inheritdoc/>
138    public event EventHandler DescriptionChanged;
139    /// <summary>
140    /// Fires a new <c>DescriptionChanged</c> event.
141    /// </summary>
142    /// <remarks>Calls <see cref="ItemBase.OnChanged"/>.</remarks>
143    protected virtual void OnDescriptionChanged() {
144      if (DescriptionChanged != null)
[2793]145        DescriptionChanged(this, EventArgs.Empty);
[2653]146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.