Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2989 was 2932, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

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