Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3392 was 3376, checked in by swagner, 15 years ago

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

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