[2653] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16565] | 3 | * Copyright (C) 2002-2019 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 |
|
---|
| 22 | using System;
|
---|
[2931] | 23 | using HeuristicLab.Common;
|
---|
[16565] | 24 | using HEAL.Attic;
|
---|
[2653] | 25 |
|
---|
| 26 | namespace HeuristicLab.Core {
|
---|
[2664] | 27 | [Item("NamedItem", "Base class for items which have a name and an optional description.")]
|
---|
[16565] | 28 | [StorableType("7492EDE5-CA7C-45D5-B5CE-8709BFAC9239")]
|
---|
[2664] | 29 | public abstract class NamedItem : Item, INamedItem {
|
---|
[2653] | 30 | [Storable]
|
---|
[2851] | 31 | protected string name;
|
---|
[16942] | 32 | /// Gets and sets the name of the item.
|
---|
[2653] | 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 |
|
---|
[4722] | 68 | [StorableConstructor]
|
---|
[16565] | 69 | protected NamedItem(StorableConstructorFlag _) : base(_) { }
|
---|
[4722] | 70 | protected NamedItem(NamedItem original, Cloner cloner)
|
---|
| 71 | : base(original, cloner) {
|
---|
| 72 | name = original.name;
|
---|
| 73 | description = original.description;
|
---|
| 74 | }
|
---|
[2653] | 75 | /// <summary>
|
---|
[16942] | 76 | /// Initializes a new instance of <see cref="NamedItem"/> with name and description <c>string.Empty</c>
|
---|
[2653] | 77 | /// and value <c>null</c>.
|
---|
| 78 | /// </summary>
|
---|
[2664] | 79 | protected NamedItem() {
|
---|
[2830] | 80 | name = string.Empty;
|
---|
| 81 | description = string.Empty;
|
---|
[2653] | 82 | }
|
---|
| 83 | /// <summary>
|
---|
[16942] | 84 | /// Initializes a new instance of <see cref="NamedItem"/> with the specified <paramref name="name"/>
|
---|
[2653] | 85 | /// </summary>
|
---|
| 86 | /// <param name="name">The name of the current instance.</param>
|
---|
[2830] | 87 | protected NamedItem(string name) {
|
---|
[2931] | 88 | if (name == null) this.name = string.Empty;
|
---|
| 89 | else this.name = name;
|
---|
[2830] | 90 | description = string.Empty;
|
---|
[2653] | 91 | }
|
---|
[16942] | 92 | /// <summary>
|
---|
| 93 | /// Initializes a new instance of <see cref="NamedItem"/> with the specified <paramref name="name"/> and <paramref name="description"/>.
|
---|
| 94 | /// </summary>
|
---|
[2830] | 95 | protected NamedItem(string name, string description) {
|
---|
[2931] | 96 | if (name == null) this.name = string.Empty;
|
---|
| 97 | else this.name = name;
|
---|
| 98 | if (description == null) this.description = string.Empty;
|
---|
| 99 | else this.description = description;
|
---|
[2653] | 100 | }
|
---|
| 101 |
|
---|
| 102 | /// <summary>
|
---|
[16942] | 103 | /// Gets the string representation of the current instance.
|
---|
[2653] | 104 | /// </summary>
|
---|
| 105 | /// <returns>The current instance as a string.</returns>
|
---|
| 106 | public override string ToString() {
|
---|
| 107 | return Name;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /// <inheritdoc/>
|
---|
| 111 | public event EventHandler<CancelEventArgs<string>> NameChanging;
|
---|
| 112 | /// <summary>
|
---|
| 113 | /// Fires a new <c>NameChanging</c> event.
|
---|
| 114 | /// </summary>
|
---|
| 115 | /// <param name="e">The event arguments of the changing.</param>
|
---|
| 116 | protected virtual void OnNameChanging(CancelEventArgs<string> e) {
|
---|
[4722] | 117 | var handler = NameChanging;
|
---|
| 118 | if (handler != null) handler(this, e);
|
---|
[2653] | 119 | }
|
---|
| 120 | /// <inheritdoc/>
|
---|
| 121 | public event EventHandler NameChanged;
|
---|
| 122 | /// <summary>
|
---|
| 123 | /// Fires a new <c>NameChanged</c> event.
|
---|
| 124 | /// </summary>
|
---|
[16942] | 125 | /// <remarks>Calls <see cref="Item.OnToStringChanged"/>.</remarks>
|
---|
[2653] | 126 | protected virtual void OnNameChanged() {
|
---|
[4722] | 127 | var handler = NameChanged;
|
---|
| 128 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[2932] | 129 | OnToStringChanged();
|
---|
[2653] | 130 | }
|
---|
| 131 | /// <inheritdoc/>
|
---|
| 132 | public event EventHandler DescriptionChanged;
|
---|
| 133 | /// <summary>
|
---|
| 134 | /// Fires a new <c>DescriptionChanged</c> event.
|
---|
| 135 | /// </summary>
|
---|
| 136 | protected virtual void OnDescriptionChanged() {
|
---|
[4722] | 137 | var handler = DescriptionChanged;
|
---|
| 138 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[2653] | 139 | }
|
---|
| 140 | }
|
---|
| 141 | }
|
---|