Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Core/3.3/NamedItem.cs @ 9882

Last change on this file since 9882 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 5.2 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  [StorableClass]
29  public abstract class NamedItem : Item, INamedItem {
30    [Storable]
31    protected string name;
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 {
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);
41          OnNameChanging(e);
42          if (!e.Cancel) {
43            name = value == null ? string.Empty : value;
44            OnNameChanged();
45          }
46        }
47      }
48    }
49    public virtual bool CanChangeName {
50      get { return true; }
51    }
52    [Storable]
53    protected string description;
54    public string Description {
55      get { return description; }
56      set {
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;
60          OnDescriptionChanged();
61        }
62      }
63    }
64    public virtual bool CanChangeDescription {
65      get { return true; }
66    }
67
68    [StorableConstructor]
69    protected NamedItem(bool deserializing) : base(deserializing) { }
70    protected NamedItem(NamedItem original, Cloner cloner)
71      : base(original, cloner) {
72      name = original.name;
73      description = original.description;
74    }
75    /// <summary>
76    /// Initializes a new instance of <see cref="Variable"/> with name <c>Anonymous</c>
77    /// and value <c>null</c>.
78    /// </summary>
79    protected NamedItem() {
80      name = string.Empty;
81      description = string.Empty;
82    }
83    /// <summary>
84    /// Initializes a new instance of <see cref="Variable"/> with the specified <paramref name="name"/>
85    /// and the specified <paramref name="value"/>.
86    /// </summary>
87    /// <param name="name">The name of the current instance.</param>
88    /// <param name="value">The value of the current instance.</param>
89    protected NamedItem(string name) {
90      if (name == null) this.name = string.Empty;
91      else this.name = name;
92      description = string.Empty;
93    }
94    protected NamedItem(string name, string description) {
95      if (name == null) this.name = string.Empty;
96      else this.name = name;
97      if (description == null) this.description = string.Empty;
98      else this.description = description;
99    }
100
101    /// <summary>
102    /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
103    /// </summary>
104    /// <returns>The current instance as a string.</returns>
105    public override string ToString() {
106      return Name;
107    }
108
109    /// <inheritdoc/>
110    public event EventHandler<CancelEventArgs<string>> NameChanging;
111    /// <summary>
112    /// Fires a new <c>NameChanging</c> event.
113    /// </summary>
114    /// <param name="e">The event arguments of the changing.</param>
115    protected virtual void OnNameChanging(CancelEventArgs<string> e) {
116      var handler = NameChanging;
117      if (handler != null) handler(this, e);
118    }
119    /// <inheritdoc/>
120    public event EventHandler NameChanged;
121    /// <summary>
122    /// Fires a new <c>NameChanged</c> event.
123    /// </summary>
124    /// <remarks>Calls <see cref="ItemBase.OnChanged"/>.</remarks>
125    protected virtual void OnNameChanged() {
126      var handler = NameChanged;
127      if (handler != null) handler(this, EventArgs.Empty);
128      OnToStringChanged();
129    }
130    /// <inheritdoc/>
131    public event EventHandler DescriptionChanged;
132    /// <summary>
133    /// Fires a new <c>DescriptionChanged</c> event.
134    /// </summary>
135    /// <remarks>Calls <see cref="ItemBase.OnChanged"/>.</remarks>
136    protected virtual void OnDescriptionChanged() {
137      var handler = DescriptionChanged;
138      if (handler != null) handler(this, EventArgs.Empty);
139    }
140  }
141}
Note: See TracBrowser for help on using the repository browser.