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