Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Data/3.3/StringValue.cs @ 17749

Last change on this file since 17749 was 17253, checked in by abeham, 5 years ago

#2521: worked on refactoring PTSP

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Drawing;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27
28namespace HeuristicLab.Data {
29  [Item("StringValue", "Represents a string.")]
30  [StorableType("1091C8E5-4480-447C-8EB3-AA260C59976D")]
31  public class StringValue : Item, IComparable, IStringConvertibleValue {
32    public static new Image StaticItemImage {
33      get { return HeuristicLab.Common.Resources.VSImageLibrary.Field; }
34    }
35
36    [Storable]
37    protected string value;
38    public virtual string Value {
39      get { return value; }
40      set {
41        if (ReadOnly) throw new NotSupportedException("Value cannot be set. StringValue is read-only.");
42        if (value != this.value) {
43          if ((value != null) || (this.value != string.Empty)) {
44            this.value = value != null ? value : string.Empty;
45            OnValueChanged();
46          }
47        }
48      }
49    }
50
51    [Storable]
52    protected bool readOnly;
53    public virtual bool ReadOnly {
54      get { return readOnly; }
55    }
56
57    [StorableConstructor]
58    protected StringValue(StorableConstructorFlag _) : base(_) { }
59    protected StringValue(StringValue original, Cloner cloner)
60      : base(original, cloner) {
61      this.value = original.value != null ? original.value : string.Empty;
62      this.readOnly = original.readOnly;
63    }
64    public StringValue() {
65      this.value = string.Empty;
66      this.readOnly = false;
67    }
68    public StringValue(string value, bool @readonly = false) {
69      this.value = value != null ? value : string.Empty;
70      this.readOnly = @readonly;
71    }
72
73    public override IDeepCloneable Clone(Cloner cloner) {
74      return new StringValue(this, cloner);
75    }
76
77    public virtual StringValue AsReadOnly() {
78      if (ReadOnly) return this;
79      var clone = (StringValue)this.Clone();
80      clone.readOnly = true;
81      return clone;
82    }
83
84    public override string ToString() {
85      return value;
86    }
87
88    public virtual int CompareTo(object obj) {
89      StringValue other = obj as StringValue;
90      if (other != null)
91        return Value.CompareTo(other.Value);
92      else
93        return Value.CompareTo(obj);
94    }
95
96    public event EventHandler ValueChanged;
97    protected virtual void OnValueChanged() {
98      if (ValueChanged != null)
99        ValueChanged(this, EventArgs.Empty);
100      OnToStringChanged();
101    }
102
103    protected virtual bool Validate(string value, out string errorMessage) {
104      if (value == null) {
105        errorMessage = "Invalid Value (string must not be null)";
106        return false;
107      } else {
108        errorMessage = string.Empty;
109        return true;
110      }
111    }
112    protected virtual string GetValue() {
113      return Value;
114    }
115    protected virtual bool SetValue(string value) {
116      if (value != null) {
117        Value = value;
118        return true;
119      } else {
120        return false;
121      }
122    }
123
124    #region IStringConvertibleValue Members
125    bool IStringConvertibleValue.Validate(string value, out string errorMessage) {
126      return Validate(value, out errorMessage);
127    }
128    string IStringConvertibleValue.GetValue() {
129      return GetValue();
130    }
131    bool IStringConvertibleValue.SetValue(string value) {
132      return SetValue(value);
133    }
134    #endregion
135  }
136}
Note: See TracBrowser for help on using the repository browser.