Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.1/HeuristicLab.Data/3.3/StringMatrix.cs @ 13398

Last change on this file since 13398 was 3431, checked in by swagner, 14 years ago

Removed property ReadOnlyView (#969)

File size: 10.4 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    public StringMatrix() {
137      matrix = new string[0, 0];
138      columnNames = new List<string>();
139      rowNames = new List<string>();
140      sortableView = false;
141      readOnly = false;
142    }
143    public StringMatrix(int rows, int columns) {
144      matrix = new string[rows, columns];
145      for (int i = 0; i < matrix.GetLength(0); i++) {
146        for (int j = 0; j < matrix.GetLength(1); j++)
147          matrix[i, j] = string.Empty;
148      }
149      columnNames = new List<string>();
150      rowNames = new List<string>();
151      sortableView = false;
152      readOnly = false;
153    }
154    protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames)
155      : this(rows, columns) {
156      ColumnNames = columnNames;
157    }
158    protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
159      : this(rows, columns,columnNames) {
160      RowNames = rowNames;
161    }
162    public StringMatrix(string[,] elements) {
163      if (elements == null) throw new ArgumentNullException();
164      matrix = new string[elements.GetLength(0), elements.GetLength(1)];
165      for (int i = 0; i < matrix.GetLength(0); i++) {
166        for (int j = 0; j < matrix.GetLength(1); j++)
167          matrix[i, j] = elements[i, j] == null ? string.Empty : elements[i, j];
168      }
169      columnNames = new List<string>();
170      rowNames = new List<string>();
171      sortableView = false;
172      readOnly = false;
173    }
174    protected StringMatrix(string[,] elements, IEnumerable<string> columnNames)
175      : this(elements) {
176      ColumnNames = columnNames;
177    }
178    protected StringMatrix(string[,] elements, IEnumerable<string> columnNames,IEnumerable<string> rowNames)
179      : this(elements,columnNames) {
180      RowNames = rowNames;
181    }
182
183    public override IDeepCloneable Clone(Cloner cloner) {
184      StringMatrix clone = new StringMatrix();
185      cloner.RegisterClonedObject(this, clone);
186      clone.matrix = (string[,])matrix.Clone();
187      clone.columnNames = new List<string>(columnNames);
188      clone.rowNames = new List<string>(rowNames);
189      clone.sortableView = sortableView;
190      clone.readOnly = readOnly;
191      return clone;
192    }
193
194    public virtual StringMatrix AsReadOnly() {
195      StringMatrix readOnlyStringMatrix = (StringMatrix)this.Clone();
196      readOnlyStringMatrix.readOnly = true;
197      return readOnlyStringMatrix;
198    }
199
200    public override string ToString() {
201      StringBuilder sb = new StringBuilder();
202      sb.Append("[");
203      if (matrix.Length > 0) {
204        for (int i = 0; i < Rows; i++) {
205          sb.Append("[").Append(matrix[i, 0]);
206          for (int j = 1; j < Columns; j++)
207            sb.Append(";").Append(matrix[i, j]);
208          sb.Append("]");
209        }
210      }
211      sb.Append("]");
212      return sb.ToString();
213    }
214
215    public virtual IEnumerator<string> GetEnumerator() {
216      return matrix.Cast<string>().GetEnumerator();
217    }
218
219    IEnumerator IEnumerable.GetEnumerator() {
220      return GetEnumerator();
221    }
222
223    protected virtual bool Validate(string value, out string errorMessage) {
224      if (value == null) {
225        errorMessage = "Invalid Value (string must not be null)";
226        return false;
227      } else {
228        errorMessage = string.Empty;
229        return true;
230      }
231    }
232    protected virtual string GetValue(int rowIndex, int columIndex) {
233      return this[rowIndex, columIndex];
234    }
235    protected virtual bool SetValue(string value, int rowIndex, int columnIndex) {
236      if (value != null) {
237        this[rowIndex, columnIndex] = value;
238        return true;
239      } else {
240        return false;
241      }
242    }
243
244    public event EventHandler ColumnNamesChanged;
245    protected virtual void OnColumnNamesChanged() {
246      EventHandler handler = ColumnNamesChanged;
247      if (handler != null)
248        handler(this, EventArgs.Empty);
249    }
250    public event EventHandler RowNamesChanged;
251    protected virtual void OnRowNamesChanged() {
252      EventHandler handler = RowNamesChanged;
253      if (handler != null)
254        handler(this, EventArgs.Empty);
255    }
256    public event EventHandler SortableViewChanged;
257    protected virtual void OnSortableViewChanged() {
258      EventHandler handler = SortableViewChanged;
259      if (handler != null)
260        handler(this, EventArgs.Empty);
261    }
262    public event EventHandler<EventArgs<int, int>> ItemChanged;
263    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
264      if (ItemChanged != null)
265        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
266      OnToStringChanged();
267    }
268    public event EventHandler Reset;
269    protected virtual void OnReset() {
270      if (Reset != null)
271        Reset(this, EventArgs.Empty);
272      OnToStringChanged();
273    }
274
275    #region IStringConvertibleMatrix Members
276    int IStringConvertibleMatrix.Rows {
277      get { return Rows; }
278      set { Rows = value; }
279    }
280    int IStringConvertibleMatrix.Columns {
281      get { return Columns; }
282      set { Columns = value; }
283    }
284    bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
285      return Validate(value, out errorMessage);
286    }
287    string IStringConvertibleMatrix.GetValue(int rowIndex, int columIndex) {
288      return GetValue(rowIndex, columIndex);
289    }
290    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
291      return SetValue(value, rowIndex, columnIndex);
292    }
293    #endregion
294  }
295}
Note: See TracBrowser for help on using the repository browser.