Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10543 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

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