[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 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 |
|
---|
| 22 | using System;
|
---|
[3306] | 23 | using System.Drawing;
|
---|
[3376] | 24 | using HeuristicLab.Common;
|
---|
[2] | 25 | using HeuristicLab.Core;
|
---|
[1853] | 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 27 |
|
---|
| 28 | namespace HeuristicLab.Data {
|
---|
[3048] | 29 | [Item("StringValue", "Represents a string.")]
|
---|
[3017] | 30 | [StorableClass]
|
---|
[3054] | 31 | public class StringValue : Item, IComparable, IStringConvertibleValue {
|
---|
[3306] | 32 | public override Image ItemImage {
|
---|
| 33 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Field; }
|
---|
| 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 |
|
---|
[3048] | 57 | public StringValue() {
|
---|
[2669] | 58 | this.value = string.Empty;
|
---|
[3430] | 59 | this.readOnly = false;
|
---|
[2] | 60 | }
|
---|
[3048] | 61 | public StringValue(string value) {
|
---|
[2669] | 62 | this.value = value != null ? value : string.Empty;
|
---|
[3430] | 63 | this.readOnly = false;
|
---|
[2669] | 64 | }
|
---|
[2] | 65 |
|
---|
[2665] | 66 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[3054] | 67 | StringValue clone = new StringValue(value);
|
---|
[2526] | 68 | cloner.RegisterClonedObject(this, clone);
|
---|
[3430] | 69 | clone.readOnly = readOnly;
|
---|
[2] | 70 | return clone;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[3430] | 73 | public virtual StringValue AsReadOnly() {
|
---|
| 74 | StringValue readOnlyStringValue = (StringValue)this.Clone();
|
---|
| 75 | readOnlyStringValue.readOnly = true;
|
---|
| 76 | return readOnlyStringValue;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[2669] | 79 | public override string ToString() {
|
---|
| 80 | return value;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[3054] | 83 | public virtual int CompareTo(object obj) {
|
---|
[3048] | 84 | StringValue other = obj as StringValue;
|
---|
[2757] | 85 | if (other != null)
|
---|
| 86 | return Value.CompareTo(other.Value);
|
---|
| 87 | else
|
---|
| 88 | return Value.CompareTo(obj);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[2932] | 91 | public event EventHandler ValueChanged;
|
---|
[3054] | 92 | protected virtual void OnValueChanged() {
|
---|
[2932] | 93 | if (ValueChanged != null)
|
---|
| 94 | ValueChanged(this, EventArgs.Empty);
|
---|
| 95 | OnToStringChanged();
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[3054] | 98 | protected virtual bool Validate(string value, out string errorMessage) {
|
---|
[2694] | 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 | }
|
---|
[2676] | 106 | }
|
---|
[3054] | 107 | protected virtual string GetValue() {
|
---|
[2665] | 108 | return Value;
|
---|
[2] | 109 | }
|
---|
[3054] | 110 | protected virtual bool SetValue(string value) {
|
---|
[2694] | 111 | if (value != null) {
|
---|
| 112 | Value = value;
|
---|
| 113 | return true;
|
---|
| 114 | } else {
|
---|
| 115 | return false;
|
---|
| 116 | }
|
---|
[2665] | 117 | }
|
---|
[3054] | 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 | }
|
---|
[2676] | 129 | #endregion
|
---|
[2] | 130 | }
|
---|
| 131 | }
|
---|