[2694] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) 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;
|
---|
[17097] | 30 | using HEAL.Attic;
|
---|
[2694] | 31 |
|
---|
| 32 | namespace HeuristicLab.Data {
|
---|
[3048] | 33 | [Item("StringArray", "Represents an array of strings.")]
|
---|
[17097] | 34 | [StorableType("E226B187-2D74-4112-A91E-830B0408D1BF")]
|
---|
[3254] | 35 | public class StringArray : Item, IEnumerable<string>, IStringConvertibleArray {
|
---|
[9433] | 36 | private const int maximumToStringLength = 100;
|
---|
| 37 |
|
---|
[7201] | 38 | public static new Image StaticItemImage {
|
---|
[5287] | 39 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
|
---|
[3306] | 40 | }
|
---|
| 41 |
|
---|
[2694] | 42 | [Storable]
|
---|
[3054] | 43 | protected string[] array;
|
---|
[2694] | 44 |
|
---|
[9737] | 45 | [Storable]
|
---|
| 46 | protected List<string> elementNames;
|
---|
| 47 | public virtual IEnumerable<string> ElementNames {
|
---|
| 48 | get { return this.elementNames; }
|
---|
| 49 | set {
|
---|
| 50 | if (ReadOnly) throw new NotSupportedException("ElementNames cannot be set. ValueTypeArray is read-only.");
|
---|
| 51 | if (value == null || !value.Any())
|
---|
| 52 | elementNames = new List<string>();
|
---|
| 53 | else if (value.Count() > Length)
|
---|
| 54 | throw new ArgumentException("The number of element names must not exceed the array length.");
|
---|
| 55 | else
|
---|
| 56 | elementNames = new List<string>(value);
|
---|
| 57 | OnElementNamesChanged();
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[3054] | 61 | public virtual int Length {
|
---|
[2694] | 62 | get { return array.Length; }
|
---|
[13979] | 63 | set {
|
---|
[3430] | 64 | if (ReadOnly) throw new NotSupportedException("Length cannot be set. StringArray is read-only.");
|
---|
[2694] | 65 | if (value != Length) {
|
---|
| 66 | Array.Resize<string>(ref array, value);
|
---|
[9737] | 67 | while (elementNames.Count > value)
|
---|
| 68 | elementNames.RemoveAt(elementNames.Count - 1);
|
---|
| 69 | OnElementNamesChanged();
|
---|
[2694] | 70 | OnReset();
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
[13979] | 74 | [Storable]
|
---|
| 75 | protected bool resizable = true;
|
---|
| 76 | public bool Resizable {
|
---|
| 77 | get { return resizable; }
|
---|
| 78 | set {
|
---|
| 79 | if (resizable != value) {
|
---|
| 80 | resizable = value;
|
---|
| 81 | OnResizableChanged();
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[3054] | 86 | public virtual string this[int index] {
|
---|
[2694] | 87 | get { return array[index]; }
|
---|
| 88 | set {
|
---|
[3430] | 89 | if (ReadOnly) throw new NotSupportedException("Item cannot be set. StringArray is read-only.");
|
---|
[2694] | 90 | if (value != array[index]) {
|
---|
| 91 | if ((value != null) || (array[index] != string.Empty)) {
|
---|
| 92 | array[index] = value != null ? value : string.Empty;
|
---|
| 93 | OnItemChanged(index);
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[3430] | 99 | [Storable]
|
---|
| 100 | protected bool readOnly;
|
---|
| 101 | public virtual bool ReadOnly {
|
---|
| 102 | get { return readOnly; }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[9737] | 105 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 106 | private void AfterDeserialization() {
|
---|
| 107 | if (elementNames == null) { elementNames = new List<string>(); }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[4722] | 110 | [StorableConstructor]
|
---|
[17097] | 111 | protected StringArray(StorableConstructorFlag _) : base(_) { }
|
---|
[4722] | 112 | protected StringArray(StringArray original, Cloner cloner)
|
---|
| 113 | : base(original, cloner) {
|
---|
| 114 | this.array = (string[])original.array.Clone();
|
---|
| 115 | this.readOnly = original.readOnly;
|
---|
[13979] | 116 | this.resizable = original.resizable;
|
---|
[9737] | 117 | this.elementNames = new List<string>(original.elementNames);
|
---|
[4722] | 118 | }
|
---|
[3048] | 119 | public StringArray() {
|
---|
[2694] | 120 | array = new string[0];
|
---|
[3430] | 121 | readOnly = false;
|
---|
[13979] | 122 | resizable = true;
|
---|
[9737] | 123 | elementNames = new List<string>();
|
---|
[2694] | 124 | }
|
---|
[3048] | 125 | public StringArray(int length) {
|
---|
[2694] | 126 | array = new string[length];
|
---|
| 127 | for (int i = 0; i < array.Length; i++)
|
---|
| 128 | array[i] = string.Empty;
|
---|
[3430] | 129 | readOnly = false;
|
---|
[13979] | 130 | resizable = true;
|
---|
[9737] | 131 | elementNames = new List<string>();
|
---|
[2694] | 132 | }
|
---|
[3048] | 133 | public StringArray(string[] elements) {
|
---|
[2694] | 134 | if (elements == null) throw new ArgumentNullException();
|
---|
| 135 | array = new string[elements.Length];
|
---|
| 136 | for (int i = 0; i < array.Length; i++)
|
---|
| 137 | array[i] = elements[i] == null ? string.Empty : elements[i];
|
---|
[3430] | 138 | readOnly = false;
|
---|
[13979] | 139 | resizable = true;
|
---|
[9737] | 140 | elementNames = new List<string>();
|
---|
[2694] | 141 | }
|
---|
| 142 |
|
---|
| 143 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 144 | return new StringArray(this, cloner);
|
---|
[2694] | 145 | }
|
---|
| 146 |
|
---|
[3430] | 147 | public virtual StringArray AsReadOnly() {
|
---|
| 148 | StringArray readOnlyStringArray = (StringArray)this.Clone();
|
---|
| 149 | readOnlyStringArray.readOnly = true;
|
---|
| 150 | return readOnlyStringArray;
|
---|
| 151 | }
|
---|
[13979] | 152 | IValueTypeArray IValueTypeArray.AsReadOnly() {
|
---|
| 153 | return AsReadOnly();
|
---|
| 154 | }
|
---|
[3430] | 155 |
|
---|
[2694] | 156 | public override string ToString() {
|
---|
[9433] | 157 | if (array.Length == 0) return "[]";
|
---|
| 158 |
|
---|
[2694] | 159 | StringBuilder sb = new StringBuilder();
|
---|
| 160 | sb.Append("[");
|
---|
[9433] | 161 | sb.Append(array[0]);
|
---|
| 162 | for (int i = 1; i < array.Length; i++) {
|
---|
| 163 | sb.Append(";").Append(array[i]);
|
---|
| 164 | if (sb.Length > maximumToStringLength) {
|
---|
| 165 | sb.Append("...");
|
---|
| 166 | break;
|
---|
| 167 | }
|
---|
[2694] | 168 | }
|
---|
| 169 | sb.Append("]");
|
---|
| 170 | return sb.ToString();
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[3430] | 173 | public virtual IEnumerator<string> GetEnumerator() {
|
---|
[3263] | 174 | return array.Cast<string>().GetEnumerator();
|
---|
[2694] | 175 | }
|
---|
[3254] | 176 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
[3430] | 177 | return GetEnumerator();
|
---|
[3254] | 178 | }
|
---|
| 179 |
|
---|
[3054] | 180 | protected virtual bool Validate(string value, out string errorMessage) {
|
---|
[2694] | 181 | if (value == null) {
|
---|
| 182 | errorMessage = "Invalid Value (string must not be null)";
|
---|
| 183 | return false;
|
---|
| 184 | } else {
|
---|
| 185 | errorMessage = string.Empty;
|
---|
| 186 | return true;
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
[3054] | 189 | protected virtual string GetValue(int index) {
|
---|
[2973] | 190 | return this[index];
|
---|
[2694] | 191 | }
|
---|
[3054] | 192 | protected virtual bool SetValue(string value, int index) {
|
---|
[2694] | 193 | if (value != null) {
|
---|
[2973] | 194 | this[index] = value;
|
---|
[2694] | 195 | return true;
|
---|
| 196 | } else {
|
---|
| 197 | return false;
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
[3054] | 200 |
|
---|
[13979] | 201 | public event EventHandler ResizableChanged;
|
---|
| 202 | protected virtual void OnResizableChanged() {
|
---|
| 203 | EventHandler handler = ResizableChanged;
|
---|
| 204 | if (handler != null)
|
---|
| 205 | handler(this, EventArgs.Empty);
|
---|
| 206 | }
|
---|
[9737] | 207 | public event EventHandler ElementNamesChanged;
|
---|
| 208 | protected virtual void OnElementNamesChanged() {
|
---|
| 209 | EventHandler handler = ElementNamesChanged;
|
---|
| 210 | if (handler != null)
|
---|
| 211 | handler(this, EventArgs.Empty);
|
---|
| 212 | }
|
---|
[2973] | 213 | public event EventHandler<EventArgs<int>> ItemChanged;
|
---|
[3054] | 214 | protected virtual void OnItemChanged(int index) {
|
---|
[2694] | 215 | if (ItemChanged != null)
|
---|
[2973] | 216 | ItemChanged(this, new EventArgs<int>(index));
|
---|
[9433] | 217 | if (index < maximumToStringLength)
|
---|
| 218 | OnToStringChanged();
|
---|
[2694] | 219 | }
|
---|
[2973] | 220 | public event EventHandler Reset;
|
---|
[3054] | 221 | protected virtual void OnReset() {
|
---|
[2694] | 222 | if (Reset != null)
|
---|
| 223 | Reset(this, EventArgs.Empty);
|
---|
[2932] | 224 | OnToStringChanged();
|
---|
[2694] | 225 | }
|
---|
[3054] | 226 |
|
---|
| 227 | #region IStringConvertibleArray Members
|
---|
| 228 | bool IStringConvertibleArray.Validate(string value, out string errorMessage) {
|
---|
| 229 | return Validate(value, out errorMessage);
|
---|
| 230 | }
|
---|
| 231 | string IStringConvertibleArray.GetValue(int index) {
|
---|
| 232 | return GetValue(index);
|
---|
| 233 | }
|
---|
| 234 | bool IStringConvertibleArray.SetValue(string value, int index) {
|
---|
| 235 | return SetValue(value, index);
|
---|
| 236 | }
|
---|
[2694] | 237 | #endregion
|
---|
| 238 | }
|
---|
| 239 | }
|
---|