Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/StringValue.cs @ 3306

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

Implemented reviewers' comments (#893).

File size: 3.4 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 System.Drawing;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Data {
28  [Item("StringValue", "Represents a string.")]
29  [StorableClass]
30  public class StringValue : Item, IComparable, IStringConvertibleValue {
31    public override Image ItemImage {
32      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Field; }
33    }
34
35    [Storable]
36    protected string value;
37    public virtual string Value {
38      get { return value; }
39      set {
40        if (value != this.value) {
41          if ((value != null) || (this.value != string.Empty)) {
42            this.value = value != null ? value : string.Empty;
43            OnValueChanged();
44          }
45        }
46      }
47    }
48
49    public StringValue() {
50      this.value = string.Empty;
51    }
52    public StringValue(string value) {
53      this.value = value != null ? value : string.Empty;
54    }
55
56    public override IDeepCloneable Clone(Cloner cloner) {
57      StringValue clone = new StringValue(value);
58      cloner.RegisterClonedObject(this, clone);
59      return clone;
60    }
61
62    public override string ToString() {
63      return value;
64    }
65
66    public virtual int CompareTo(object obj) {
67      StringValue other = obj as StringValue;
68      if (other != null)
69        return Value.CompareTo(other.Value);
70      else
71        return Value.CompareTo(obj);
72    }
73
74    public event EventHandler ValueChanged;
75    protected virtual void OnValueChanged() {
76      if (ValueChanged != null)
77        ValueChanged(this, EventArgs.Empty);
78      OnToStringChanged();
79    }
80
81    protected virtual bool Validate(string value, out string errorMessage) {
82      if (value == null) {
83        errorMessage = "Invalid Value (string must not be null)";
84        return false;
85      } else {
86        errorMessage = string.Empty;
87        return true;
88      }
89    }
90    protected virtual string GetValue() {
91      return Value;
92    }
93    protected virtual bool SetValue(string value) {
94      if (value != null) {
95        Value = value;
96        return true;
97      } else {
98        return false;
99      }
100    }
101
102    #region IStringConvertibleValue Members
103    bool IStringConvertibleValue.Validate(string value, out string errorMessage) {
104      return Validate(value, out errorMessage);
105    }
106    string IStringConvertibleValue.GetValue() {
107      return GetValue();
108    }
109    bool IStringConvertibleValue.SetValue(string value) {
110      return SetValue(value);
111    }
112    #endregion
113  }
114}
Note: See TracBrowser for help on using the repository browser.