Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3561 was 3431, checked in by swagner, 15 years ago

Removed property ReadOnlyView (#969)

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