Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented ReadOnlyView property for items (#969).

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      clone.ReadOnlyView = ReadOnlyView;
60      return clone;
61    }
62
63    public override string ToString() {
64      return value;
65    }
66
67    public virtual int CompareTo(object obj) {
68      StringValue other = obj as StringValue;
69      if (other != null)
70        return Value.CompareTo(other.Value);
71      else
72        return Value.CompareTo(obj);
73    }
74
75    public event EventHandler ValueChanged;
76    protected virtual void OnValueChanged() {
77      if (ValueChanged != null)
78        ValueChanged(this, EventArgs.Empty);
79      OnToStringChanged();
80    }
81
82    protected virtual bool Validate(string value, out string errorMessage) {
83      if (value == null) {
84        errorMessage = "Invalid Value (string must not be null)";
85        return false;
86      } else {
87        errorMessage = string.Empty;
88        return true;
89      }
90    }
91    protected virtual string GetValue() {
92      return Value;
93    }
94    protected virtual bool SetValue(string value) {
95      if (value != null) {
96        Value = value;
97        return true;
98      } else {
99        return false;
100      }
101    }
102
103    #region IStringConvertibleValue Members
104    bool IStringConvertibleValue.Validate(string value, out string errorMessage) {
105      return Validate(value, out errorMessage);
106    }
107    string IStringConvertibleValue.GetValue() {
108      return GetValue();
109    }
110    bool IStringConvertibleValue.SetValue(string value) {
111      return SetValue(value);
112    }
113    #endregion
114  }
115}
Note: See TracBrowser for help on using the repository browser.