Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/StringMatrix.cs @ 4769

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

Merged cloning refactoring branch back into trunk (#922)

File size: 10.6 KB
RevLine 
[2694]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2694]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;
[3308]24using System.Collections.Generic;
[3306]25using System.Drawing;
[3308]26using System.Linq;
[2694]27using System.Text;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Data {
[3048]33  [Item("StringMatrix", "Represents a matrix of strings.")]
[3017]34  [StorableClass]
[3430]35  public class StringMatrix : Item, IEnumerable<string>, IStringConvertibleMatrix {
[3306]36    public override Image ItemImage {
37      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Class; }
38    }
39
[2694]40    [Storable]
[3054]41    protected string[,] matrix;
[2694]42
[3308]43    [Storable]
[3430]44    protected List<string> columnNames;
45    public virtual IEnumerable<string> ColumnNames {
[3308]46      get { return this.columnNames; }
47      set {
[3430]48        if (ReadOnly) throw new NotSupportedException("ColumnNames cannot be set. StringMatrix is read-only.");
[3308]49        if (value == null || value.Count() == 0)
50          columnNames = new List<string>();
51        else if (value.Count() != Columns)
[3310]52          throw new ArgumentException("A column name must be specified for each column .");
[3308]53        else
54          columnNames = new List<string>(value);
55      }
56    }
[3310]57    [Storable]
[3430]58    protected List<string> rowNames;
59    public virtual IEnumerable<string> RowNames {
[3310]60      get { return this.rowNames; }
61      set {
[3430]62        if (ReadOnly) throw new NotSupportedException("RowNames cannot be set. StringMatrix is read-only.");
[3310]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    }
[3320]71    [Storable]
[3430]72    protected bool sortableView;
73    public virtual bool SortableView {
[3320]74      get { return sortableView; }
75      set {
[3430]76        if (ReadOnly) throw new NotSupportedException("SortableView cannot be set. StringMatrix is read-only.");
[3320]77        if (value != sortableView) {
78          sortableView = value;
79          OnSortableViewChanged();
80        }
81      }
82    }
[3308]83
[3054]84    public virtual int Rows {
85      get { return matrix.GetLength(0); }
86      protected set {
[3430]87        if (ReadOnly) throw new NotSupportedException("Rows cannot be set. StringMatrix is read-only.");
[2694]88        if (value != Rows) {
[3054]89          string[,] newMatrix = new string[value, Columns];
90          Array.Copy(matrix, newMatrix, Math.Min(value * Columns, matrix.Length));
91          matrix = newMatrix;
[3320]92          while (rowNames.Count > value)
93            rowNames.RemoveAt(rowNames.Count - 1);
94          while (rowNames.Count < value)
95            rowNames.Add("Row " + rowNames.Count);
[2694]96          OnReset();
97        }
98      }
99    }
[3054]100    public virtual int Columns {
101      get { return matrix.GetLength(1); }
102      protected set {
[3430]103        if (ReadOnly) throw new NotSupportedException("Columns cannot be set. StringMatrix is read-only.");
[2694]104        if (value != Columns) {
[3054]105          string[,] newMatrix = new string[Rows, value];
[2694]106          for (int i = 0; i < Rows; i++)
[3054]107            Array.Copy(matrix, i * Columns, newMatrix, i * value, Math.Min(value, Columns));
108          matrix = newMatrix;
[3320]109          while (columnNames.Count > value)
110            columnNames.RemoveAt(columnNames.Count - 1);
111          while (columnNames.Count < value)
112            columnNames.Add("Column " + columnNames.Count);
[2694]113          OnReset();
114        }
115      }
116    }
[3054]117    public virtual string this[int rowIndex, int columnIndex] {
118      get { return matrix[rowIndex, columnIndex]; }
[2694]119      set {
[3430]120        if (ReadOnly) throw new NotSupportedException("Item cannot be set. StringMatrix is read-only.");
[3054]121        if (value != matrix[rowIndex, columnIndex]) {
122          if ((value != null) || (matrix[rowIndex, columnIndex] != string.Empty)) {
123            matrix[rowIndex, columnIndex] = value != null ? value : string.Empty;
[2694]124            OnItemChanged(rowIndex, columnIndex);
125          }
126        }
127      }
128    }
129
[3430]130    [Storable]
131    protected bool readOnly;
132    public virtual bool ReadOnly {
133      get { return readOnly; }
134    }
135
[4722]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    }
[3048]146    public StringMatrix() {
[3054]147      matrix = new string[0, 0];
[3308]148      columnNames = new List<string>();
[3310]149      rowNames = new List<string>();
[3320]150      sortableView = false;
[3430]151      readOnly = false;
[2694]152    }
[3048]153    public StringMatrix(int rows, int columns) {
[3054]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;
[2694]158      }
[3308]159      columnNames = new List<string>();
[3310]160      rowNames = new List<string>();
[3320]161      sortableView = false;
[3430]162      readOnly = false;
[2694]163    }
[3308]164    protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames)
165      : this(rows, columns) {
166      ColumnNames = columnNames;
167    }
[3310]168    protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
169      : this(rows, columns,columnNames) {
170      RowNames = rowNames;
171    }
[3048]172    public StringMatrix(string[,] elements) {
[2694]173      if (elements == null) throw new ArgumentNullException();
[3054]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];
[2694]178      }
[3308]179      columnNames = new List<string>();
[3310]180      rowNames = new List<string>();
[3320]181      sortableView = false;
[3430]182      readOnly = false;
[2694]183    }
[3308]184    protected StringMatrix(string[,] elements, IEnumerable<string> columnNames)
185      : this(elements) {
186      ColumnNames = columnNames;
187    }
[3310]188    protected StringMatrix(string[,] elements, IEnumerable<string> columnNames,IEnumerable<string> rowNames)
189      : this(elements,columnNames) {
190      RowNames = rowNames;
191    }
[2694]192
193    public override IDeepCloneable Clone(Cloner cloner) {
[4722]194      return new StringMatrix(this, cloner);
[2694]195    }
196
[3430]197    public virtual StringMatrix AsReadOnly() {
198      StringMatrix readOnlyStringMatrix = (StringMatrix)this.Clone();
199      readOnlyStringMatrix.readOnly = true;
200      return readOnlyStringMatrix;
201    }
202
[2694]203    public override string ToString() {
204      StringBuilder sb = new StringBuilder();
205      sb.Append("[");
[3054]206      if (matrix.Length > 0) {
[2694]207        for (int i = 0; i < Rows; i++) {
[3054]208          sb.Append("[").Append(matrix[i, 0]);
[2694]209          for (int j = 1; j < Columns; j++)
[3054]210            sb.Append(";").Append(matrix[i, j]);
[2694]211          sb.Append("]");
212        }
213      }
214      sb.Append("]");
215      return sb.ToString();
216    }
217
[3430]218    public virtual IEnumerator<string> GetEnumerator() {
219      return matrix.Cast<string>().GetEnumerator();
[2694]220    }
221
[3430]222    IEnumerator IEnumerable.GetEnumerator() {
223      return GetEnumerator();
224    }
225
[3054]226    protected virtual bool Validate(string value, out string errorMessage) {
[2694]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    }
[3054]235    protected virtual string GetValue(int rowIndex, int columIndex) {
[2694]236      return this[rowIndex, columIndex];
237    }
[3054]238    protected virtual bool SetValue(string value, int rowIndex, int columnIndex) {
[2694]239      if (value != null) {
240        this[rowIndex, columnIndex] = value;
241        return true;
242      } else {
243        return false;
244      }
245    }
[3054]246
[3320]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    }
[2973]265    public event EventHandler<EventArgs<int, int>> ItemChanged;
[3054]266    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
[2694]267      if (ItemChanged != null)
268        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
[2932]269      OnToStringChanged();
[2694]270    }
[2973]271    public event EventHandler Reset;
[3054]272    protected virtual void OnReset() {
[2694]273      if (Reset != null)
274        Reset(this, EventArgs.Empty);
[2932]275      OnToStringChanged();
[2694]276    }
[3054]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    }
[2694]296    #endregion
297  }
298}
Note: See TracBrowser for help on using the repository browser.