Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Data/3.3/StringMatrix.cs @ 5264

Last change on this file since 5264 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 10.6 KB
Line 
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
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.Drawing;
26using System.Linq;
27using System.Text;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Data {
33  [Item("StringMatrix", "Represents a matrix of strings.")]
34  [StorableClass]
35  public class StringMatrix : Item, IEnumerable<string>, 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    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      }
56    }
57    [Storable]
58    protected List<string> rowNames;
59    public virtual IEnumerable<string> RowNames {
60      get { return this.rowNames; }
61      set {
62        if (ReadOnly) throw new NotSupportedException("RowNames cannot be set. StringMatrix is read-only.");
63        if (value == null || value.Count() == 0)
64          rowNames = new List<string>();
65        else if (value.Count() != Rows)
66          throw new ArgumentException("A row name must be specified for each row.");
67        else
68          rowNames = new List<string>(value);
69      }
70    }
71    [Storable]
72    protected bool sortableView;
73    public virtual bool SortableView {
74      get { return sortableView; }
75      set {
76        if (ReadOnly) throw new NotSupportedException("SortableView cannot be set. StringMatrix is read-only.");
77        if (value != sortableView) {
78          sortableView = value;
79          OnSortableViewChanged();
80        }
81      }
82    }
83
84    public virtual int Rows {
85      get { return matrix.GetLength(0); }
86      protected set {
87        if (ReadOnly) throw new NotSupportedException("Rows cannot be set. StringMatrix is read-only.");
88        if (value != Rows) {
89          string[,] newMatrix = new string[value, Columns];
90          Array.Copy(matrix, newMatrix, Math.Min(value * Columns, matrix.Length));
91          matrix = newMatrix;
92          while (rowNames.Count > value)
93            rowNames.RemoveAt(rowNames.Count - 1);
94          while (rowNames.Count < value)
95            rowNames.Add("Row " + rowNames.Count);
96          OnReset();
97        }
98      }
99    }
100    public virtual int Columns {
101      get { return matrix.GetLength(1); }
102      protected set {
103        if (ReadOnly) throw new NotSupportedException("Columns cannot be set. StringMatrix is read-only.");
104        if (value != Columns) {
105          string[,] newMatrix = new string[Rows, value];
106          for (int i = 0; i < Rows; i++)
107            Array.Copy(matrix, i * Columns, newMatrix, i * value, Math.Min(value, Columns));
108          matrix = newMatrix;
109          while (columnNames.Count > value)
110            columnNames.RemoveAt(columnNames.Count - 1);
111          while (columnNames.Count < value)
112            columnNames.Add("Column " + columnNames.Count);
113          OnReset();
114        }
115      }
116    }
117    public virtual string this[int rowIndex, int columnIndex] {
118      get { return matrix[rowIndex, columnIndex]; }
119      set {
120        if (ReadOnly) throw new NotSupportedException("Item cannot be set. StringMatrix is read-only.");
121        if (value != matrix[rowIndex, columnIndex]) {
122          if ((value != null) || (matrix[rowIndex, columnIndex] != string.Empty)) {
123            matrix[rowIndex, columnIndex] = value != null ? value : string.Empty;
124            OnItemChanged(rowIndex, columnIndex);
125          }
126        }
127      }
128    }
129
130    [Storable]
131    protected bool readOnly;
132    public virtual bool ReadOnly {
133      get { return readOnly; }
134    }
135
136    [StorableConstructor]
137    protected StringMatrix(bool deserializing) : base(deserializing) { }
138    protected StringMatrix(StringMatrix original, Cloner cloner)
139      : base(original, cloner) {
140      this.matrix = (string[,])original.matrix.Clone();
141      this.columnNames = new List<string>(original.columnNames);
142      this.rowNames = new List<string>(original.rowNames);
143      this.sortableView = original.sortableView;
144      this.readOnly = original.readOnly;
145    }
146    public StringMatrix() {
147      matrix = new string[0, 0];
148      columnNames = new List<string>();
149      rowNames = new List<string>();
150      sortableView = false;
151      readOnly = false;
152    }
153    public StringMatrix(int rows, int columns) {
154      matrix = new string[rows, columns];
155      for (int i = 0; i < matrix.GetLength(0); i++) {
156        for (int j = 0; j < matrix.GetLength(1); j++)
157          matrix[i, j] = string.Empty;
158      }
159      columnNames = new List<string>();
160      rowNames = new List<string>();
161      sortableView = false;
162      readOnly = false;
163    }
164    protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames)
165      : this(rows, columns) {
166      ColumnNames = columnNames;
167    }
168    protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
169      : this(rows, columns,columnNames) {
170      RowNames = rowNames;
171    }
172    public StringMatrix(string[,] elements) {
173      if (elements == null) throw new ArgumentNullException();
174      matrix = new string[elements.GetLength(0), elements.GetLength(1)];
175      for (int i = 0; i < matrix.GetLength(0); i++) {
176        for (int j = 0; j < matrix.GetLength(1); j++)
177          matrix[i, j] = elements[i, j] == null ? string.Empty : elements[i, j];
178      }
179      columnNames = new List<string>();
180      rowNames = new List<string>();
181      sortableView = false;
182      readOnly = false;
183    }
184    protected StringMatrix(string[,] elements, IEnumerable<string> columnNames)
185      : this(elements) {
186      ColumnNames = columnNames;
187    }
188    protected StringMatrix(string[,] elements, IEnumerable<string> columnNames,IEnumerable<string> rowNames)
189      : this(elements,columnNames) {
190      RowNames = rowNames;
191    }
192
193    public override IDeepCloneable Clone(Cloner cloner) {
194      return new StringMatrix(this, cloner);
195    }
196
197    public virtual StringMatrix AsReadOnly() {
198      StringMatrix readOnlyStringMatrix = (StringMatrix)this.Clone();
199      readOnlyStringMatrix.readOnly = true;
200      return readOnlyStringMatrix;
201    }
202
203    public override string ToString() {
204      StringBuilder sb = new StringBuilder();
205      sb.Append("[");
206      if (matrix.Length > 0) {
207        for (int i = 0; i < Rows; i++) {
208          sb.Append("[").Append(matrix[i, 0]);
209          for (int j = 1; j < Columns; j++)
210            sb.Append(";").Append(matrix[i, j]);
211          sb.Append("]");
212        }
213      }
214      sb.Append("]");
215      return sb.ToString();
216    }
217
218    public virtual IEnumerator<string> GetEnumerator() {
219      return matrix.Cast<string>().GetEnumerator();
220    }
221
222    IEnumerator IEnumerable.GetEnumerator() {
223      return GetEnumerator();
224    }
225
226    protected virtual bool Validate(string value, out string errorMessage) {
227      if (value == null) {
228        errorMessage = "Invalid Value (string must not be null)";
229        return false;
230      } else {
231        errorMessage = string.Empty;
232        return true;
233      }
234    }
235    protected virtual string GetValue(int rowIndex, int columIndex) {
236      return this[rowIndex, columIndex];
237    }
238    protected virtual bool SetValue(string value, int rowIndex, int columnIndex) {
239      if (value != null) {
240        this[rowIndex, columnIndex] = value;
241        return true;
242      } else {
243        return false;
244      }
245    }
246
247    public event EventHandler ColumnNamesChanged;
248    protected virtual void OnColumnNamesChanged() {
249      EventHandler handler = ColumnNamesChanged;
250      if (handler != null)
251        handler(this, EventArgs.Empty);
252    }
253    public event EventHandler RowNamesChanged;
254    protected virtual void OnRowNamesChanged() {
255      EventHandler handler = RowNamesChanged;
256      if (handler != null)
257        handler(this, EventArgs.Empty);
258    }
259    public event EventHandler SortableViewChanged;
260    protected virtual void OnSortableViewChanged() {
261      EventHandler handler = SortableViewChanged;
262      if (handler != null)
263        handler(this, EventArgs.Empty);
264    }
265    public event EventHandler<EventArgs<int, int>> ItemChanged;
266    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
267      if (ItemChanged != null)
268        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
269      OnToStringChanged();
270    }
271    public event EventHandler Reset;
272    protected virtual void OnReset() {
273      if (Reset != null)
274        Reset(this, EventArgs.Empty);
275      OnToStringChanged();
276    }
277
278    #region IStringConvertibleMatrix Members
279    int IStringConvertibleMatrix.Rows {
280      get { return Rows; }
281      set { Rows = value; }
282    }
283    int IStringConvertibleMatrix.Columns {
284      get { return Columns; }
285      set { Columns = value; }
286    }
287    bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
288      return Validate(value, out errorMessage);
289    }
290    string IStringConvertibleMatrix.GetValue(int rowIndex, int columIndex) {
291      return GetValue(rowIndex, columIndex);
292    }
293    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
294      return SetValue(value, rowIndex, columnIndex);
295    }
296    #endregion
297  }
298}
Note: See TracBrowser for help on using the repository browser.