Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Data/3.3/StringValue.cs @ 13783

Last change on this file since 13783 was 13656, checked in by ascheibe, 9 years ago

#2582 created branch for Hive Web Job Manager

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