Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6382 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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