[2694] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2694] | 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.Collections;
|
---|
[4068] | 24 | using System.Collections.Generic;
|
---|
[3306] | 25 | using System.Drawing;
|
---|
[4068] | 26 | using System.Linq;
|
---|
[2694] | 27 | using System.Text;
|
---|
| 28 | using HeuristicLab.Common;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Data {
|
---|
[3048] | 33 | [Item("StringArray", "Represents an array of strings.")]
|
---|
[3017] | 34 | [StorableClass]
|
---|
[3254] | 35 | public class StringArray : Item, IEnumerable<string>, IStringConvertibleArray {
|
---|
[3306] | 36 | public override Image ItemImage {
|
---|
| 37 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Class; }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[2694] | 40 | [Storable]
|
---|
[3054] | 41 | protected string[] array;
|
---|
[2694] | 42 |
|
---|
[3054] | 43 | public virtual int Length {
|
---|
[2694] | 44 | get { return array.Length; }
|
---|
[3054] | 45 | protected set {
|
---|
[3430] | 46 | if (ReadOnly) throw new NotSupportedException("Length cannot be set. StringArray is read-only.");
|
---|
[2694] | 47 | if (value != Length) {
|
---|
| 48 | Array.Resize<string>(ref array, value);
|
---|
| 49 | OnReset();
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
[3054] | 53 | public virtual string this[int index] {
|
---|
[2694] | 54 | get { return array[index]; }
|
---|
| 55 | set {
|
---|
[3430] | 56 | if (ReadOnly) throw new NotSupportedException("Item cannot be set. StringArray is read-only.");
|
---|
[2694] | 57 | if (value != array[index]) {
|
---|
| 58 | if ((value != null) || (array[index] != string.Empty)) {
|
---|
| 59 | array[index] = value != null ? value : string.Empty;
|
---|
| 60 | OnItemChanged(index);
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[3430] | 66 | [Storable]
|
---|
| 67 | protected bool readOnly;
|
---|
| 68 | public virtual bool ReadOnly {
|
---|
| 69 | get { return readOnly; }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[3048] | 72 | public StringArray() {
|
---|
[2694] | 73 | array = new string[0];
|
---|
[3430] | 74 | readOnly = false;
|
---|
[2694] | 75 | }
|
---|
[3048] | 76 | public StringArray(int length) {
|
---|
[2694] | 77 | array = new string[length];
|
---|
| 78 | for (int i = 0; i < array.Length; i++)
|
---|
| 79 | array[i] = string.Empty;
|
---|
[3430] | 80 | readOnly = false;
|
---|
[2694] | 81 | }
|
---|
[3048] | 82 | public StringArray(string[] elements) {
|
---|
[2694] | 83 | if (elements == null) throw new ArgumentNullException();
|
---|
| 84 | array = new string[elements.Length];
|
---|
| 85 | for (int i = 0; i < array.Length; i++)
|
---|
| 86 | array[i] = elements[i] == null ? string.Empty : elements[i];
|
---|
[3430] | 87 | readOnly = false;
|
---|
[2694] | 88 | }
|
---|
| 89 |
|
---|
| 90 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[3054] | 91 | StringArray clone = new StringArray();
|
---|
[2694] | 92 | cloner.RegisterClonedObject(this, clone);
|
---|
[3054] | 93 | clone.array = (string[])array.Clone();
|
---|
[3430] | 94 | clone.readOnly = readOnly;
|
---|
[2694] | 95 | return clone;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[3430] | 98 | public virtual StringArray AsReadOnly() {
|
---|
| 99 | StringArray readOnlyStringArray = (StringArray)this.Clone();
|
---|
| 100 | readOnlyStringArray.readOnly = true;
|
---|
| 101 | return readOnlyStringArray;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[2694] | 104 | public override string ToString() {
|
---|
| 105 | StringBuilder sb = new StringBuilder();
|
---|
| 106 | sb.Append("[");
|
---|
| 107 | if (array.Length > 0) {
|
---|
| 108 | sb.Append(array[0]);
|
---|
| 109 | for (int i = 1; i < array.Length; i++)
|
---|
| 110 | sb.Append(";").Append(array[i]);
|
---|
| 111 | }
|
---|
| 112 | sb.Append("]");
|
---|
| 113 | return sb.ToString();
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[3430] | 116 | public virtual IEnumerator<string> GetEnumerator() {
|
---|
[3263] | 117 | return array.Cast<string>().GetEnumerator();
|
---|
[2694] | 118 | }
|
---|
| 119 |
|
---|
[3254] | 120 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
[3430] | 121 | return GetEnumerator();
|
---|
[3254] | 122 | }
|
---|
| 123 |
|
---|
[3054] | 124 | protected virtual bool Validate(string value, out string errorMessage) {
|
---|
[2694] | 125 | if (value == null) {
|
---|
| 126 | errorMessage = "Invalid Value (string must not be null)";
|
---|
| 127 | return false;
|
---|
| 128 | } else {
|
---|
| 129 | errorMessage = string.Empty;
|
---|
| 130 | return true;
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
[3054] | 133 | protected virtual string GetValue(int index) {
|
---|
[2973] | 134 | return this[index];
|
---|
[2694] | 135 | }
|
---|
[3054] | 136 | protected virtual bool SetValue(string value, int index) {
|
---|
[2694] | 137 | if (value != null) {
|
---|
[2973] | 138 | this[index] = value;
|
---|
[2694] | 139 | return true;
|
---|
| 140 | } else {
|
---|
| 141 | return false;
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
[3054] | 144 |
|
---|
[2973] | 145 | public event EventHandler<EventArgs<int>> ItemChanged;
|
---|
[3054] | 146 | protected virtual void OnItemChanged(int index) {
|
---|
[2694] | 147 | if (ItemChanged != null)
|
---|
[2973] | 148 | ItemChanged(this, new EventArgs<int>(index));
|
---|
[2932] | 149 | OnToStringChanged();
|
---|
[2694] | 150 | }
|
---|
[2973] | 151 | public event EventHandler Reset;
|
---|
[3054] | 152 | protected virtual void OnReset() {
|
---|
[2694] | 153 | if (Reset != null)
|
---|
| 154 | Reset(this, EventArgs.Empty);
|
---|
[2932] | 155 | OnToStringChanged();
|
---|
[2694] | 156 | }
|
---|
[3054] | 157 |
|
---|
| 158 | #region IStringConvertibleArray Members
|
---|
| 159 | int IStringConvertibleArray.Length {
|
---|
| 160 | get { return Length; }
|
---|
| 161 | set { Length = value; }
|
---|
| 162 | }
|
---|
| 163 | bool IStringConvertibleArray.Validate(string value, out string errorMessage) {
|
---|
| 164 | return Validate(value, out errorMessage);
|
---|
| 165 | }
|
---|
| 166 | string IStringConvertibleArray.GetValue(int index) {
|
---|
| 167 | return GetValue(index);
|
---|
| 168 | }
|
---|
| 169 | bool IStringConvertibleArray.SetValue(string value, int index) {
|
---|
| 170 | return SetValue(value, index);
|
---|
| 171 | }
|
---|
[2694] | 172 | #endregion
|
---|
| 173 | }
|
---|
| 174 | }
|
---|