1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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("StringMatrix", "Represents a matrix of strings.")]
|
---|
34 | [StorableClass]
|
---|
35 | public class StringMatrix : Item, IEnumerable<string>, IStringConvertibleMatrix {
|
---|
36 | public static new Image StaticItemImage {
|
---|
37 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | [Storable]
|
---|
41 | protected string[,] matrix;
|
---|
42 |
|
---|
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. StringMatrix is read-only.");
|
---|
49 | if (value == null || value.Count() == 0)
|
---|
50 | columnNames = new List<string>();
|
---|
51 | else if (value.Count() != Columns)
|
---|
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 | [Storable]
|
---|
59 | protected List<string> rowNames;
|
---|
60 | public virtual IEnumerable<string> RowNames {
|
---|
61 | get { return this.rowNames; }
|
---|
62 | set {
|
---|
63 | if (ReadOnly) throw new NotSupportedException("RowNames cannot be set. StringMatrix is read-only.");
|
---|
64 | if (value == null || value.Count() == 0)
|
---|
65 | rowNames = new List<string>();
|
---|
66 | else if (value.Count() != Rows)
|
---|
67 | throw new ArgumentException("A row name must be specified for each row.");
|
---|
68 | else
|
---|
69 | rowNames = new List<string>(value);
|
---|
70 | OnRowNamesChanged();
|
---|
71 | }
|
---|
72 | }
|
---|
73 | [Storable]
|
---|
74 | protected bool sortableView;
|
---|
75 | public virtual bool SortableView {
|
---|
76 | get { return sortableView; }
|
---|
77 | set {
|
---|
78 | if (ReadOnly) throw new NotSupportedException("SortableView cannot be set. StringMatrix is read-only.");
|
---|
79 | if (value != sortableView) {
|
---|
80 | sortableView = value;
|
---|
81 | OnSortableViewChanged();
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | public virtual int Rows {
|
---|
87 | get { return matrix.GetLength(0); }
|
---|
88 | protected set {
|
---|
89 | if (ReadOnly) throw new NotSupportedException("Rows cannot be set. StringMatrix is read-only.");
|
---|
90 | if (value != Rows) {
|
---|
91 | string[,] newMatrix = new string[value, Columns];
|
---|
92 | Array.Copy(matrix, newMatrix, Math.Min(value * Columns, matrix.Length));
|
---|
93 | matrix = newMatrix;
|
---|
94 | while (rowNames.Count > value)
|
---|
95 | rowNames.RemoveAt(rowNames.Count - 1);
|
---|
96 | while (rowNames.Count < value)
|
---|
97 | rowNames.Add("Row " + rowNames.Count);
|
---|
98 | OnRowsChanged();
|
---|
99 | OnRowNamesChanged();
|
---|
100 | OnReset();
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 | public virtual int Columns {
|
---|
105 | get { return matrix.GetLength(1); }
|
---|
106 | protected set {
|
---|
107 | if (ReadOnly) throw new NotSupportedException("Columns cannot be set. StringMatrix is read-only.");
|
---|
108 | if (value != Columns) {
|
---|
109 | string[,] newMatrix = new string[Rows, value];
|
---|
110 | for (int i = 0; i < Rows; i++)
|
---|
111 | Array.Copy(matrix, i * Columns, newMatrix, i * value, Math.Min(value, Columns));
|
---|
112 | matrix = newMatrix;
|
---|
113 | while (columnNames.Count > value)
|
---|
114 | columnNames.RemoveAt(columnNames.Count - 1);
|
---|
115 | while (columnNames.Count < value)
|
---|
116 | columnNames.Add("Column " + columnNames.Count);
|
---|
117 | OnColumnsChanged();
|
---|
118 | OnColumnNamesChanged();
|
---|
119 | OnReset();
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|
123 | public virtual string this[int rowIndex, int columnIndex] {
|
---|
124 | get { return matrix[rowIndex, columnIndex]; }
|
---|
125 | set {
|
---|
126 | if (ReadOnly) throw new NotSupportedException("Item cannot be set. StringMatrix is read-only.");
|
---|
127 | if (value != matrix[rowIndex, columnIndex]) {
|
---|
128 | if ((value != null) || (matrix[rowIndex, columnIndex] != string.Empty)) {
|
---|
129 | matrix[rowIndex, columnIndex] = value != null ? value : string.Empty;
|
---|
130 | OnItemsChanged(new List<Position>(1) { new Position(rowIndex, columnIndex) });
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | [Storable]
|
---|
137 | protected bool readOnly;
|
---|
138 | public virtual bool ReadOnly {
|
---|
139 | get { return readOnly; }
|
---|
140 | }
|
---|
141 |
|
---|
142 | [StorableConstructor]
|
---|
143 | protected StringMatrix(bool deserializing) : base(deserializing) { }
|
---|
144 | protected StringMatrix(StringMatrix original, Cloner cloner)
|
---|
145 | : base(original, cloner) {
|
---|
146 | this.matrix = (string[,])original.matrix.Clone();
|
---|
147 | this.columnNames = new List<string>(original.columnNames);
|
---|
148 | this.rowNames = new List<string>(original.rowNames);
|
---|
149 | this.sortableView = original.sortableView;
|
---|
150 | this.readOnly = original.readOnly;
|
---|
151 | }
|
---|
152 | public StringMatrix() {
|
---|
153 | matrix = new string[0, 0];
|
---|
154 | columnNames = new List<string>();
|
---|
155 | rowNames = new List<string>();
|
---|
156 | sortableView = false;
|
---|
157 | readOnly = false;
|
---|
158 | }
|
---|
159 | public StringMatrix(int rows, int columns) {
|
---|
160 | matrix = new string[rows, columns];
|
---|
161 | for (int i = 0; i < matrix.GetLength(0); i++) {
|
---|
162 | for (int j = 0; j < matrix.GetLength(1); j++)
|
---|
163 | matrix[i, j] = string.Empty;
|
---|
164 | }
|
---|
165 | columnNames = new List<string>();
|
---|
166 | rowNames = new List<string>();
|
---|
167 | sortableView = false;
|
---|
168 | readOnly = false;
|
---|
169 | }
|
---|
170 | protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames)
|
---|
171 | : this(rows, columns) {
|
---|
172 | ColumnNames = columnNames;
|
---|
173 | }
|
---|
174 | protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
|
---|
175 | : this(rows, columns, columnNames) {
|
---|
176 | RowNames = rowNames;
|
---|
177 | }
|
---|
178 | public StringMatrix(string[,] elements) {
|
---|
179 | if (elements == null) throw new ArgumentNullException();
|
---|
180 | matrix = new string[elements.GetLength(0), elements.GetLength(1)];
|
---|
181 | for (int i = 0; i < matrix.GetLength(0); i++) {
|
---|
182 | for (int j = 0; j < matrix.GetLength(1); j++)
|
---|
183 | matrix[i, j] = elements[i, j] == null ? string.Empty : elements[i, j];
|
---|
184 | }
|
---|
185 | columnNames = new List<string>();
|
---|
186 | rowNames = new List<string>();
|
---|
187 | sortableView = false;
|
---|
188 | readOnly = false;
|
---|
189 | }
|
---|
190 | protected StringMatrix(string[,] elements, IEnumerable<string> columnNames)
|
---|
191 | : this(elements) {
|
---|
192 | ColumnNames = columnNames;
|
---|
193 | }
|
---|
194 | protected StringMatrix(string[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
|
---|
195 | : this(elements, columnNames) {
|
---|
196 | RowNames = rowNames;
|
---|
197 | }
|
---|
198 |
|
---|
199 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
200 | return new StringMatrix(this, cloner);
|
---|
201 | }
|
---|
202 |
|
---|
203 | public virtual StringMatrix AsReadOnly() {
|
---|
204 | StringMatrix readOnlyStringMatrix = (StringMatrix)this.Clone();
|
---|
205 | readOnlyStringMatrix.readOnly = true;
|
---|
206 | return readOnlyStringMatrix;
|
---|
207 | }
|
---|
208 |
|
---|
209 | public override string ToString() {
|
---|
210 | StringBuilder sb = new StringBuilder();
|
---|
211 | sb.Append("[");
|
---|
212 | if (matrix.Length > 0) {
|
---|
213 | for (int i = 0; i < Rows; i++) {
|
---|
214 | sb.Append("[").Append(matrix[i, 0]);
|
---|
215 | for (int j = 1; j < Columns; j++)
|
---|
216 | sb.Append(";").Append(matrix[i, j]);
|
---|
217 | sb.Append("]");
|
---|
218 | }
|
---|
219 | }
|
---|
220 | sb.Append("]");
|
---|
221 | return sb.ToString();
|
---|
222 | }
|
---|
223 |
|
---|
224 | public virtual IEnumerator<string> GetEnumerator() {
|
---|
225 | return matrix.Cast<string>().GetEnumerator();
|
---|
226 | }
|
---|
227 |
|
---|
228 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
229 | return GetEnumerator();
|
---|
230 | }
|
---|
231 |
|
---|
232 | protected virtual bool Validate(string value, out string errorMessage) {
|
---|
233 | if (value == null) {
|
---|
234 | errorMessage = "Invalid Value (string must not be null)";
|
---|
235 | return false;
|
---|
236 | } else {
|
---|
237 | errorMessage = string.Empty;
|
---|
238 | return true;
|
---|
239 | }
|
---|
240 | }
|
---|
241 | protected virtual string GetValue(int rowIndex, int columIndex) {
|
---|
242 | return this[rowIndex, columIndex];
|
---|
243 | }
|
---|
244 | protected virtual bool SetValue(string value, int rowIndex, int columnIndex) {
|
---|
245 | if (value != null) {
|
---|
246 | this[rowIndex, columnIndex] = value;
|
---|
247 | return true;
|
---|
248 | } else {
|
---|
249 | return false;
|
---|
250 | }
|
---|
251 | }
|
---|
252 | protected virtual bool SetValue(IEnumerable<RowColumnValue> rowColumnValues) {
|
---|
253 | if (ReadOnly) throw new NotSupportedException("Item cannot be set. StringMatrix is read-only.");
|
---|
254 | if (rowColumnValues.Any(x => x.Value == null)) { return false; }
|
---|
255 | Position pos;
|
---|
256 | foreach (var curValue in rowColumnValues) {
|
---|
257 | pos = curValue.Position;
|
---|
258 | matrix[pos.Row, pos.Column] = curValue.Value;
|
---|
259 | }
|
---|
260 | OnItemsChanged(rowColumnValues.Select(x => x.Position));
|
---|
261 | return true;
|
---|
262 | }
|
---|
263 |
|
---|
264 | #region events
|
---|
265 | public event EventHandler ColumnsChanged;
|
---|
266 | protected virtual void OnColumnsChanged() {
|
---|
267 | EventHandler handler = ColumnsChanged;
|
---|
268 | if (handler != null)
|
---|
269 | handler(this, EventArgs.Empty);
|
---|
270 | }
|
---|
271 | public event EventHandler RowsChanged;
|
---|
272 | protected virtual void OnRowsChanged() {
|
---|
273 | EventHandler handler = RowsChanged;
|
---|
274 | if (handler != null)
|
---|
275 | handler(this, EventArgs.Empty);
|
---|
276 | }
|
---|
277 | public event EventHandler ColumnNamesChanged;
|
---|
278 | protected virtual void OnColumnNamesChanged() {
|
---|
279 | EventHandler handler = ColumnNamesChanged;
|
---|
280 | if (handler != null)
|
---|
281 | handler(this, EventArgs.Empty);
|
---|
282 | }
|
---|
283 | public event EventHandler RowNamesChanged;
|
---|
284 | protected virtual void OnRowNamesChanged() {
|
---|
285 | EventHandler handler = RowNamesChanged;
|
---|
286 | if (handler != null)
|
---|
287 | handler(this, EventArgs.Empty);
|
---|
288 | }
|
---|
289 | public event EventHandler SortableViewChanged;
|
---|
290 | protected virtual void OnSortableViewChanged() {
|
---|
291 | EventHandler handler = SortableViewChanged;
|
---|
292 | if (handler != null)
|
---|
293 | handler(this, EventArgs.Empty);
|
---|
294 | }
|
---|
295 | public event EventHandler<EventArgs<IEnumerable<Position>>> ItemsChanged;
|
---|
296 | protected virtual void OnItemsChanged(IEnumerable<Position> positions) {
|
---|
297 | if (ItemsChanged != null)
|
---|
298 | ItemsChanged(this, new EventArgs<IEnumerable<Position>>(positions));
|
---|
299 | OnToStringChanged();
|
---|
300 | }
|
---|
301 | public event EventHandler Reset;
|
---|
302 | protected virtual void OnReset() {
|
---|
303 | if (Reset != null)
|
---|
304 | Reset(this, EventArgs.Empty);
|
---|
305 | OnToStringChanged();
|
---|
306 | }
|
---|
307 | #endregion
|
---|
308 |
|
---|
309 | #region IStringConvertibleMatrix Members
|
---|
310 | int IStringConvertibleMatrix.Rows {
|
---|
311 | get { return Rows; }
|
---|
312 | set { Rows = value; }
|
---|
313 | }
|
---|
314 | int IStringConvertibleMatrix.Columns {
|
---|
315 | get { return Columns; }
|
---|
316 | set { Columns = value; }
|
---|
317 | }
|
---|
318 | bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
|
---|
319 | return Validate(value, out errorMessage);
|
---|
320 | }
|
---|
321 | string IStringConvertibleMatrix.GetValue(int rowIndex, int columIndex) {
|
---|
322 | return GetValue(rowIndex, columIndex);
|
---|
323 | }
|
---|
324 | bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
325 | return SetValue(value, rowIndex, columnIndex);
|
---|
326 | }
|
---|
327 | public bool SetValue(RowColumnValue rowColumnValue) {
|
---|
328 | return SetValue(rowColumnValue.Value, rowColumnValue.Position.Row, rowColumnValue.Position.Column);
|
---|
329 | }
|
---|
330 | bool IStringConvertibleMatrix.SetValue(IEnumerable<RowColumnValue> rowColumnValues) {
|
---|
331 | return SetValue(rowColumnValues);
|
---|
332 | }
|
---|
333 | #endregion
|
---|
334 | }
|
---|
335 | }
|
---|