1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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.Collections;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Text;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Data {
|
---|
33 | [Item("StringArray", "Represents an array of strings.")]
|
---|
34 | [StorableType("3FF6ABD3-8692-4E9F-8925-D48A0EEAEB01")]
|
---|
35 | public class StringArray : Item, IEnumerable<string>, IStringConvertibleArray {
|
---|
36 | private const int maximumToStringLength = 100;
|
---|
37 |
|
---|
38 | public static new Image StaticItemImage {
|
---|
39 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | [Storable]
|
---|
43 | protected string[] array;
|
---|
44 |
|
---|
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 |
|
---|
61 | public virtual int Length {
|
---|
62 | get { return array.Length; }
|
---|
63 | protected set {
|
---|
64 | if (ReadOnly) throw new NotSupportedException("Length cannot be set. StringArray is read-only.");
|
---|
65 | if (value != Length) {
|
---|
66 | Array.Resize<string>(ref array, value);
|
---|
67 | while (elementNames.Count > value)
|
---|
68 | elementNames.RemoveAt(elementNames.Count - 1);
|
---|
69 | OnElementNamesChanged();
|
---|
70 | OnReset();
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|
74 | public virtual string this[int index] {
|
---|
75 | get { return array[index]; }
|
---|
76 | set {
|
---|
77 | if (ReadOnly) throw new NotSupportedException("Item cannot be set. StringArray is read-only.");
|
---|
78 | if (value != array[index]) {
|
---|
79 | if ((value != null) || (array[index] != string.Empty)) {
|
---|
80 | array[index] = value != null ? value : string.Empty;
|
---|
81 | OnItemChanged(index);
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | [Storable]
|
---|
88 | protected bool readOnly;
|
---|
89 | public virtual bool ReadOnly {
|
---|
90 | get { return readOnly; }
|
---|
91 | }
|
---|
92 |
|
---|
93 | [StorableHook(HookType.AfterDeserialization)]
|
---|
94 | private void AfterDeserialization() {
|
---|
95 | if (elementNames == null) { elementNames = new List<string>(); }
|
---|
96 | }
|
---|
97 |
|
---|
98 | [StorableConstructor]
|
---|
99 | protected StringArray(bool deserializing) : base(deserializing) { }
|
---|
100 | protected StringArray(StringArray original, Cloner cloner)
|
---|
101 | : base(original, cloner) {
|
---|
102 | this.array = (string[])original.array.Clone();
|
---|
103 | this.readOnly = original.readOnly;
|
---|
104 | this.elementNames = new List<string>(original.elementNames);
|
---|
105 | }
|
---|
106 | public StringArray() {
|
---|
107 | array = new string[0];
|
---|
108 | readOnly = false;
|
---|
109 | elementNames = new List<string>();
|
---|
110 | }
|
---|
111 | public StringArray(int length) {
|
---|
112 | array = new string[length];
|
---|
113 | for (int i = 0; i < array.Length; i++)
|
---|
114 | array[i] = string.Empty;
|
---|
115 | readOnly = false;
|
---|
116 | elementNames = new List<string>();
|
---|
117 | }
|
---|
118 | public StringArray(string[] elements) {
|
---|
119 | if (elements == null) throw new ArgumentNullException();
|
---|
120 | array = new string[elements.Length];
|
---|
121 | for (int i = 0; i < array.Length; i++)
|
---|
122 | array[i] = elements[i] == null ? string.Empty : elements[i];
|
---|
123 | readOnly = false;
|
---|
124 | elementNames = new List<string>();
|
---|
125 | }
|
---|
126 |
|
---|
127 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
128 | return new StringArray(this, cloner);
|
---|
129 | }
|
---|
130 |
|
---|
131 | public virtual StringArray AsReadOnly() {
|
---|
132 | StringArray readOnlyStringArray = (StringArray)this.Clone();
|
---|
133 | readOnlyStringArray.readOnly = true;
|
---|
134 | return readOnlyStringArray;
|
---|
135 | }
|
---|
136 |
|
---|
137 | public override string ToString() {
|
---|
138 | if (array.Length == 0) return "[]";
|
---|
139 |
|
---|
140 | StringBuilder sb = new StringBuilder();
|
---|
141 | sb.Append("[");
|
---|
142 | sb.Append(array[0]);
|
---|
143 | for (int i = 1; i < array.Length; i++) {
|
---|
144 | sb.Append(";").Append(array[i]);
|
---|
145 | if (sb.Length > maximumToStringLength) {
|
---|
146 | sb.Append("...");
|
---|
147 | break;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | sb.Append("]");
|
---|
151 | return sb.ToString();
|
---|
152 | }
|
---|
153 |
|
---|
154 | public virtual IEnumerator<string> GetEnumerator() {
|
---|
155 | return array.Cast<string>().GetEnumerator();
|
---|
156 | }
|
---|
157 |
|
---|
158 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
159 | return GetEnumerator();
|
---|
160 | }
|
---|
161 |
|
---|
162 | protected virtual bool Validate(string value, out string errorMessage) {
|
---|
163 | if (value == null) {
|
---|
164 | errorMessage = "Invalid Value (string must not be null)";
|
---|
165 | return false;
|
---|
166 | } else {
|
---|
167 | errorMessage = string.Empty;
|
---|
168 | return true;
|
---|
169 | }
|
---|
170 | }
|
---|
171 | protected virtual string GetValue(int index) {
|
---|
172 | return this[index];
|
---|
173 | }
|
---|
174 | protected virtual bool SetValue(string value, int index) {
|
---|
175 | if (value != null) {
|
---|
176 | this[index] = value;
|
---|
177 | return true;
|
---|
178 | } else {
|
---|
179 | return false;
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | public event EventHandler ElementNamesChanged;
|
---|
184 | protected virtual void OnElementNamesChanged() {
|
---|
185 | EventHandler handler = ElementNamesChanged;
|
---|
186 | if (handler != null)
|
---|
187 | handler(this, EventArgs.Empty);
|
---|
188 | }
|
---|
189 |
|
---|
190 | public event EventHandler<EventArgs<int>> ItemChanged;
|
---|
191 | protected virtual void OnItemChanged(int index) {
|
---|
192 | if (ItemChanged != null)
|
---|
193 | ItemChanged(this, new EventArgs<int>(index));
|
---|
194 | if (index < maximumToStringLength)
|
---|
195 | OnToStringChanged();
|
---|
196 | }
|
---|
197 | public event EventHandler Reset;
|
---|
198 | protected virtual void OnReset() {
|
---|
199 | if (Reset != null)
|
---|
200 | Reset(this, EventArgs.Empty);
|
---|
201 | OnToStringChanged();
|
---|
202 | }
|
---|
203 |
|
---|
204 | #region IStringConvertibleArray Members
|
---|
205 | int IStringConvertibleArray.Length {
|
---|
206 | get { return Length; }
|
---|
207 | set { Length = value; }
|
---|
208 | }
|
---|
209 | bool IStringConvertibleArray.Validate(string value, out string errorMessage) {
|
---|
210 | return Validate(value, out errorMessage);
|
---|
211 | }
|
---|
212 | string IStringConvertibleArray.GetValue(int index) {
|
---|
213 | return GetValue(index);
|
---|
214 | }
|
---|
215 | bool IStringConvertibleArray.SetValue(string value, int index) {
|
---|
216 | return SetValue(value, index);
|
---|
217 | }
|
---|
218 | #endregion
|
---|
219 | }
|
---|
220 | }
|
---|