Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/NamedItemBase.cs @ 2660

Last change on this file since 2660 was 2653, checked in by swagner, 15 years ago

Committing first results of the refactoring of HeuristicLab.Core and related plugins (#95)

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