1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
22 | using System;
|
---|
23 | using System.Drawing;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Data {
|
---|
29 | [Item("StringValue", "Represents a string.")]
|
---|
30 | [StorableClass]
|
---|
31 | public class StringValue : Item, IComparable, IStringConvertibleValue {
|
---|
32 | public static new Image StaticItemImage {
|
---|
33 | get { return HeuristicLab.Common.Resources.VSImageLibrary.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 | [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 | }
|
---|
64 | public StringValue() {
|
---|
65 | this.value = string.Empty;
|
---|
66 | this.readOnly = false;
|
---|
67 | }
|
---|
68 | public StringValue(string value) {
|
---|
69 | this.value = value != null ? value : string.Empty;
|
---|
70 | this.readOnly = false;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
74 | return new StringValue(this, cloner);
|
---|
75 | }
|
---|
76 |
|
---|
77 | public virtual StringValue AsReadOnly() {
|
---|
78 | StringValue readOnlyStringValue = (StringValue)this.Clone();
|
---|
79 | readOnlyStringValue.readOnly = true;
|
---|
80 | return readOnlyStringValue;
|
---|
81 | }
|
---|
82 |
|
---|
83 | public override string ToString() {
|
---|
84 | return value;
|
---|
85 | }
|
---|
86 |
|
---|
87 | public virtual int CompareTo(object obj) {
|
---|
88 | StringValue other = obj as StringValue;
|
---|
89 | if (other != null)
|
---|
90 | return Value.CompareTo(other.Value);
|
---|
91 | else
|
---|
92 | return Value.CompareTo(obj);
|
---|
93 | }
|
---|
94 |
|
---|
95 | public event EventHandler ValueChanged;
|
---|
96 | protected virtual void OnValueChanged() {
|
---|
97 | if (ValueChanged != null)
|
---|
98 | ValueChanged(this, EventArgs.Empty);
|
---|
99 | OnToStringChanged();
|
---|
100 | }
|
---|
101 |
|
---|
102 | protected virtual bool Validate(string value, out string errorMessage) {
|
---|
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 | }
|
---|
110 | }
|
---|
111 | protected virtual string GetValue() {
|
---|
112 | return Value;
|
---|
113 | }
|
---|
114 | protected virtual bool SetValue(string value) {
|
---|
115 | if (value != null) {
|
---|
116 | Value = value;
|
---|
117 | return true;
|
---|
118 | } else {
|
---|
119 | return false;
|
---|
120 | }
|
---|
121 | }
|
---|
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 | }
|
---|
133 | #endregion
|
---|
134 | }
|
---|
135 | }
|
---|