Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2582 created branch for Hive Web Job Manager

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 static new Image StaticItemImage
33    {
34      get { return new Bitmap(25, 25); }
35    }
36
37    [Storable]
38    protected string value;
39    public virtual string Value
40    {
41      get { return value; }
42      set
43      {
44        if (ReadOnly) throw new NotSupportedException("Value cannot be set. StringValue is read-only.");
45        if (value != this.value) {
46          if ((value != null) || (this.value != string.Empty)) {
47            this.value = value != null ? value : string.Empty;
48            OnValueChanged();
49          }
50        }
51      }
52    }
53
54    [Storable]
55    protected bool readOnly;
56    public virtual bool ReadOnly
57    {
58      get { return readOnly; }
59    }
60
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    }
68    public StringValue() {
69      this.value = string.Empty;
70      this.readOnly = false;
71    }
72    public StringValue(string value) {
73      this.value = value != null ? value : string.Empty;
74      this.readOnly = false;
75    }
76
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return new StringValue(this, cloner);
79    }
80
81    public virtual StringValue AsReadOnly() {
82      StringValue readOnlyStringValue = (StringValue)this.Clone();
83      readOnlyStringValue.readOnly = true;
84      return readOnlyStringValue;
85    }
86
87    public override string ToString() {
88      return value;
89    }
90
91    public virtual int CompareTo(object obj) {
92      StringValue other = obj as StringValue;
93      if (other != null)
94        return Value.CompareTo(other.Value);
95      else
96        return Value.CompareTo(obj);
97    }
98
99    public event EventHandler ValueChanged;
100    protected virtual void OnValueChanged() {
101      if (ValueChanged != null)
102        ValueChanged(this, EventArgs.Empty);
103      OnToStringChanged();
104    }
105
106    protected virtual bool Validate(string value, out string errorMessage) {
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      }
114    }
115    protected virtual string GetValue() {
116      return Value;
117    }
118    protected virtual bool SetValue(string value) {
119      if (value != null) {
120        Value = value;
121        return true;
122      } else {
123        return false;
124      }
125    }
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    }
137    #endregion
138  }
139}
Note: See TracBrowser for help on using the repository browser.