Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4708 was 3431, checked in by swagner, 15 years ago

Removed property ReadOnlyView (#969)

File size: 10.4 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
[3048]136    public StringMatrix() {
[3054]137      matrix = new string[0, 0];
[3308]138      columnNames = new List<string>();
[3310]139      rowNames = new List<string>();
[3320]140      sortableView = false;
[3430]141      readOnly = false;
[2694]142    }
[3048]143    public StringMatrix(int rows, int columns) {
[3054]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;
[2694]148      }
[3308]149      columnNames = new List<string>();
[3310]150      rowNames = new List<string>();
[3320]151      sortableView = false;
[3430]152      readOnly = false;
[2694]153    }
[3308]154    protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames)
155      : this(rows, columns) {
156      ColumnNames = columnNames;
157    }
[3310]158    protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
159      : this(rows, columns,columnNames) {
160      RowNames = rowNames;
161    }
[3048]162    public StringMatrix(string[,] elements) {
[2694]163      if (elements == null) throw new ArgumentNullException();
[3054]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];
[2694]168      }
[3308]169      columnNames = new List<string>();
[3310]170      rowNames = new List<string>();
[3320]171      sortableView = false;
[3430]172      readOnly = false;
[2694]173    }
[3308]174    protected StringMatrix(string[,] elements, IEnumerable<string> columnNames)
175      : this(elements) {
176      ColumnNames = columnNames;
177    }
[3310]178    protected StringMatrix(string[,] elements, IEnumerable<string> columnNames,IEnumerable<string> rowNames)
179      : this(elements,columnNames) {
180      RowNames = rowNames;
181    }
[2694]182
183    public override IDeepCloneable Clone(Cloner cloner) {
[3054]184      StringMatrix clone = new StringMatrix();
[2694]185      cloner.RegisterClonedObject(this, clone);
[3054]186      clone.matrix = (string[,])matrix.Clone();
[3310]187      clone.columnNames = new List<string>(columnNames);
188      clone.rowNames = new List<string>(rowNames);
[3430]189      clone.sortableView = sortableView;
190      clone.readOnly = readOnly;
[2694]191      return clone;
192    }
193
[3430]194    public virtual StringMatrix AsReadOnly() {
195      StringMatrix readOnlyStringMatrix = (StringMatrix)this.Clone();
196      readOnlyStringMatrix.readOnly = true;
197      return readOnlyStringMatrix;
198    }
199
[2694]200    public override string ToString() {
201      StringBuilder sb = new StringBuilder();
202      sb.Append("[");
[3054]203      if (matrix.Length > 0) {
[2694]204        for (int i = 0; i < Rows; i++) {
[3054]205          sb.Append("[").Append(matrix[i, 0]);
[2694]206          for (int j = 1; j < Columns; j++)
[3054]207            sb.Append(";").Append(matrix[i, j]);
[2694]208          sb.Append("]");
209        }
210      }
211      sb.Append("]");
212      return sb.ToString();
213    }
214
[3430]215    public virtual IEnumerator<string> GetEnumerator() {
216      return matrix.Cast<string>().GetEnumerator();
[2694]217    }
218
[3430]219    IEnumerator IEnumerable.GetEnumerator() {
220      return GetEnumerator();
221    }
222
[3054]223    protected virtual bool Validate(string value, out string errorMessage) {
[2694]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    }
[3054]232    protected virtual string GetValue(int rowIndex, int columIndex) {
[2694]233      return this[rowIndex, columIndex];
234    }
[3054]235    protected virtual bool SetValue(string value, int rowIndex, int columnIndex) {
[2694]236      if (value != null) {
237        this[rowIndex, columnIndex] = value;
238        return true;
239      } else {
240        return false;
241      }
242    }
[3054]243
[3320]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    }
[2973]262    public event EventHandler<EventArgs<int, int>> ItemChanged;
[3054]263    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
[2694]264      if (ItemChanged != null)
265        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
[2932]266      OnToStringChanged();
[2694]267    }
[2973]268    public event EventHandler Reset;
[3054]269    protected virtual void OnReset() {
[2694]270      if (Reset != null)
271        Reset(this, EventArgs.Empty);
[2932]272      OnToStringChanged();
[2694]273    }
[3054]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    }
[2694]293    #endregion
294  }
295}
Note: See TracBrowser for help on using the repository browser.