1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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, IStringConvertibleMatrix {
|
---|
36 | public override Image ItemImage {
|
---|
37 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Class; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | [Storable]
|
---|
41 | protected string[,] matrix;
|
---|
42 |
|
---|
43 | [Storable]
|
---|
44 | private List<string> columnNames;
|
---|
45 | public IEnumerable<string> ColumnNames {
|
---|
46 | get { return this.columnNames; }
|
---|
47 | set {
|
---|
48 | if (value == null || value.Count() == 0)
|
---|
49 | columnNames = new List<string>();
|
---|
50 | else if (value.Count() != Columns)
|
---|
51 | throw new ArgumentException("A column name must be specified for each column .");
|
---|
52 | else
|
---|
53 | columnNames = new List<string>(value);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | [Storable]
|
---|
57 | private List<string> rowNames;
|
---|
58 | public IEnumerable<string> RowNames {
|
---|
59 | get { return this.rowNames; }
|
---|
60 | set {
|
---|
61 | if (value == null || value.Count() == 0)
|
---|
62 | rowNames = new List<string>();
|
---|
63 | else if (value.Count() != Rows)
|
---|
64 | throw new ArgumentException("A row name must be specified for each row.");
|
---|
65 | else
|
---|
66 | rowNames = new List<string>(value);
|
---|
67 | }
|
---|
68 | }
|
---|
69 | [Storable]
|
---|
70 | private bool sortableView;
|
---|
71 | public bool SortableView {
|
---|
72 | get { return sortableView; }
|
---|
73 | set {
|
---|
74 | if (value != sortableView) {
|
---|
75 | sortableView = value;
|
---|
76 | OnSortableViewChanged();
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public virtual int Rows {
|
---|
82 | get { return matrix.GetLength(0); }
|
---|
83 | protected set {
|
---|
84 | if (value != Rows) {
|
---|
85 | string[,] newMatrix = new string[value, Columns];
|
---|
86 | Array.Copy(matrix, newMatrix, Math.Min(value * Columns, matrix.Length));
|
---|
87 | matrix = newMatrix;
|
---|
88 | while (rowNames.Count > value)
|
---|
89 | rowNames.RemoveAt(rowNames.Count - 1);
|
---|
90 | while (rowNames.Count < value)
|
---|
91 | rowNames.Add("Row " + rowNames.Count);
|
---|
92 | OnReset();
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | public virtual int Columns {
|
---|
97 | get { return matrix.GetLength(1); }
|
---|
98 | protected set {
|
---|
99 | if (value != Columns) {
|
---|
100 | string[,] newMatrix = new string[Rows, value];
|
---|
101 | for (int i = 0; i < Rows; i++)
|
---|
102 | Array.Copy(matrix, i * Columns, newMatrix, i * value, Math.Min(value, Columns));
|
---|
103 | matrix = newMatrix;
|
---|
104 | while (columnNames.Count > value)
|
---|
105 | columnNames.RemoveAt(columnNames.Count - 1);
|
---|
106 | while (columnNames.Count < value)
|
---|
107 | columnNames.Add("Column " + columnNames.Count);
|
---|
108 | OnReset();
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 | public virtual string this[int rowIndex, int columnIndex] {
|
---|
113 | get { return matrix[rowIndex, columnIndex]; }
|
---|
114 | set {
|
---|
115 | if (value != matrix[rowIndex, columnIndex]) {
|
---|
116 | if ((value != null) || (matrix[rowIndex, columnIndex] != string.Empty)) {
|
---|
117 | matrix[rowIndex, columnIndex] = value != null ? value : string.Empty;
|
---|
118 | OnItemChanged(rowIndex, columnIndex);
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | public StringMatrix() {
|
---|
125 | matrix = new string[0, 0];
|
---|
126 | columnNames = new List<string>();
|
---|
127 | rowNames = new List<string>();
|
---|
128 | sortableView = false;
|
---|
129 | }
|
---|
130 | public StringMatrix(int rows, int columns) {
|
---|
131 | matrix = new string[rows, columns];
|
---|
132 | for (int i = 0; i < matrix.GetLength(0); i++) {
|
---|
133 | for (int j = 0; j < matrix.GetLength(1); j++)
|
---|
134 | matrix[i, j] = string.Empty;
|
---|
135 | }
|
---|
136 | columnNames = new List<string>();
|
---|
137 | rowNames = new List<string>();
|
---|
138 | sortableView = false;
|
---|
139 | }
|
---|
140 | protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames)
|
---|
141 | : this(rows, columns) {
|
---|
142 | ColumnNames = columnNames;
|
---|
143 | }
|
---|
144 | protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
|
---|
145 | : this(rows, columns,columnNames) {
|
---|
146 | RowNames = rowNames;
|
---|
147 | }
|
---|
148 | public StringMatrix(string[,] elements) {
|
---|
149 | if (elements == null) throw new ArgumentNullException();
|
---|
150 | matrix = new string[elements.GetLength(0), elements.GetLength(1)];
|
---|
151 | for (int i = 0; i < matrix.GetLength(0); i++) {
|
---|
152 | for (int j = 0; j < matrix.GetLength(1); j++)
|
---|
153 | matrix[i, j] = elements[i, j] == null ? string.Empty : elements[i, j];
|
---|
154 | }
|
---|
155 | columnNames = new List<string>();
|
---|
156 | rowNames = new List<string>();
|
---|
157 | sortableView = false;
|
---|
158 | }
|
---|
159 | protected StringMatrix(string[,] elements, IEnumerable<string> columnNames)
|
---|
160 | : this(elements) {
|
---|
161 | ColumnNames = columnNames;
|
---|
162 | }
|
---|
163 | protected StringMatrix(string[,] elements, IEnumerable<string> columnNames,IEnumerable<string> rowNames)
|
---|
164 | : this(elements,columnNames) {
|
---|
165 | RowNames = rowNames;
|
---|
166 | }
|
---|
167 |
|
---|
168 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
169 | StringMatrix clone = new StringMatrix();
|
---|
170 | cloner.RegisterClonedObject(this, clone);
|
---|
171 | clone.ReadOnlyView = ReadOnlyView;
|
---|
172 | clone.SortableView = SortableView;
|
---|
173 | clone.matrix = (string[,])matrix.Clone();
|
---|
174 | clone.columnNames = new List<string>(columnNames);
|
---|
175 | clone.rowNames = new List<string>(rowNames);
|
---|
176 | return clone;
|
---|
177 | }
|
---|
178 |
|
---|
179 | public override string ToString() {
|
---|
180 | StringBuilder sb = new StringBuilder();
|
---|
181 | sb.Append("[");
|
---|
182 | if (matrix.Length > 0) {
|
---|
183 | for (int i = 0; i < Rows; i++) {
|
---|
184 | sb.Append("[").Append(matrix[i, 0]);
|
---|
185 | for (int j = 1; j < Columns; j++)
|
---|
186 | sb.Append(";").Append(matrix[i, j]);
|
---|
187 | sb.Append("]");
|
---|
188 | }
|
---|
189 | }
|
---|
190 | sb.Append("]");
|
---|
191 | return sb.ToString();
|
---|
192 | }
|
---|
193 |
|
---|
194 | public virtual IEnumerator GetEnumerator() {
|
---|
195 | return matrix.GetEnumerator();
|
---|
196 | }
|
---|
197 |
|
---|
198 | protected virtual bool Validate(string value, out string errorMessage) {
|
---|
199 | if (value == null) {
|
---|
200 | errorMessage = "Invalid Value (string must not be null)";
|
---|
201 | return false;
|
---|
202 | } else {
|
---|
203 | errorMessage = string.Empty;
|
---|
204 | return true;
|
---|
205 | }
|
---|
206 | }
|
---|
207 | protected virtual string GetValue(int rowIndex, int columIndex) {
|
---|
208 | return this[rowIndex, columIndex];
|
---|
209 | }
|
---|
210 | protected virtual bool SetValue(string value, int rowIndex, int columnIndex) {
|
---|
211 | if (value != null) {
|
---|
212 | this[rowIndex, columnIndex] = value;
|
---|
213 | return true;
|
---|
214 | } else {
|
---|
215 | return false;
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | public event EventHandler ColumnNamesChanged;
|
---|
220 | protected virtual void OnColumnNamesChanged() {
|
---|
221 | EventHandler handler = ColumnNamesChanged;
|
---|
222 | if (handler != null)
|
---|
223 | handler(this, EventArgs.Empty);
|
---|
224 | }
|
---|
225 | public event EventHandler RowNamesChanged;
|
---|
226 | protected virtual void OnRowNamesChanged() {
|
---|
227 | EventHandler handler = RowNamesChanged;
|
---|
228 | if (handler != null)
|
---|
229 | handler(this, EventArgs.Empty);
|
---|
230 | }
|
---|
231 | public event EventHandler SortableViewChanged;
|
---|
232 | protected virtual void OnSortableViewChanged() {
|
---|
233 | EventHandler handler = SortableViewChanged;
|
---|
234 | if (handler != null)
|
---|
235 | handler(this, EventArgs.Empty);
|
---|
236 | }
|
---|
237 | public event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
238 | protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
239 | if (ItemChanged != null)
|
---|
240 | ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
241 | OnToStringChanged();
|
---|
242 | }
|
---|
243 | public event EventHandler Reset;
|
---|
244 | protected virtual void OnReset() {
|
---|
245 | if (Reset != null)
|
---|
246 | Reset(this, EventArgs.Empty);
|
---|
247 | OnToStringChanged();
|
---|
248 | }
|
---|
249 |
|
---|
250 | #region IStringConvertibleMatrix Members
|
---|
251 | int IStringConvertibleMatrix.Rows {
|
---|
252 | get { return Rows; }
|
---|
253 | set { Rows = value; }
|
---|
254 | }
|
---|
255 | int IStringConvertibleMatrix.Columns {
|
---|
256 | get { return Columns; }
|
---|
257 | set { Columns = value; }
|
---|
258 | }
|
---|
259 | IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
|
---|
260 | get { return this.ColumnNames; }
|
---|
261 | set { this.ColumnNames = value; }
|
---|
262 | }
|
---|
263 | IEnumerable<string> IStringConvertibleMatrix.RowNames {
|
---|
264 | get { return this.RowNames; }
|
---|
265 | set { this.RowNames = value; }
|
---|
266 | }
|
---|
267 | bool IStringConvertibleMatrix.SortableView {
|
---|
268 | get { return this.SortableView; }
|
---|
269 | set { this.SortableView = value; }
|
---|
270 | }
|
---|
271 | bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
|
---|
272 | return Validate(value, out errorMessage);
|
---|
273 | }
|
---|
274 | string IStringConvertibleMatrix.GetValue(int rowIndex, int columIndex) {
|
---|
275 | return GetValue(rowIndex, columIndex);
|
---|
276 | }
|
---|
277 | bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
278 | return SetValue(value, rowIndex, columnIndex);
|
---|
279 | }
|
---|
280 | #endregion
|
---|
281 | }
|
---|
282 | }
|
---|