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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using System.Xml;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Core {
|
---|
30 | [Item("NamedItem", "Base class for items which have a name and an optional description.")]
|
---|
31 | public abstract class NamedItem : Item, 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 NamedItem() {
|
---|
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 NamedItem(string name)
|
---|
86 | : this() {
|
---|
87 | if (name == null) throw new ArgumentNullException();
|
---|
88 | this.name = name;
|
---|
89 | }
|
---|
90 | protected NamedItem(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 | NamedItem clone = (NamedItem)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 | }
|
---|