Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3308 was 3308, checked in by mkommend, 14 years ago

added ColumnNames property in IStringConvertibleMatrix and in classes implementing this interface (ticket #968)

File size: 6.9 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, 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 IEnumerable<string> ColumnNames {
46      get { return this.columnNames; }
47      set {
48        if (value == null || value.Count() == 0)
49          columnNames = new List<string>();
50        else if (value.Count() != Columns)
51          throw new ArgumentException("A columnName must be for each column specified.");
52        else
53          columnNames = new List<string>(value);
54      }
55    }
56
57    public virtual int Rows {
58      get { return matrix.GetLength(0); }
59      protected set {
60        if (value != Rows) {
61          string[,] newMatrix = new string[value, Columns];
62          Array.Copy(matrix, newMatrix, Math.Min(value * Columns, matrix.Length));
63          matrix = newMatrix;
64          OnReset();
65        }
66      }
67    }
68    public virtual int Columns {
69      get { return matrix.GetLength(1); }
70      protected set {
71        if (value != Columns) {
72          string[,] newMatrix = new string[Rows, value];
73          for (int i = 0; i < Rows; i++)
74            Array.Copy(matrix, i * Columns, newMatrix, i * value, Math.Min(value, Columns));
75          matrix = newMatrix;
76          OnReset();
77        }
78      }
79    }
80    public virtual string this[int rowIndex, int columnIndex] {
81      get { return matrix[rowIndex, columnIndex]; }
82      set {
83        if (value != matrix[rowIndex, columnIndex]) {
84          if ((value != null) || (matrix[rowIndex, columnIndex] != string.Empty)) {
85            matrix[rowIndex, columnIndex] = value != null ? value : string.Empty;
86            OnItemChanged(rowIndex, columnIndex);
87          }
88        }
89      }
90    }
91
92    public StringMatrix() {
93      matrix = new string[0, 0];
94      columnNames = new List<string>();
95    }
96    public StringMatrix(int rows, int columns) {
97      matrix = new string[rows, columns];
98      for (int i = 0; i < matrix.GetLength(0); i++) {
99        for (int j = 0; j < matrix.GetLength(1); j++)
100          matrix[i, j] = string.Empty;
101      }
102      columnNames = new List<string>();
103    }
104    protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames)
105      : this(rows, columns) {
106      ColumnNames = columnNames;
107    }
108    public StringMatrix(string[,] elements) {
109      if (elements == null) throw new ArgumentNullException();
110      matrix = new string[elements.GetLength(0), elements.GetLength(1)];
111      for (int i = 0; i < matrix.GetLength(0); i++) {
112        for (int j = 0; j < matrix.GetLength(1); j++)
113          matrix[i, j] = elements[i, j] == null ? string.Empty : elements[i, j];
114      }
115      columnNames = new List<string>();
116    }
117    protected StringMatrix(string[,] elements, IEnumerable<string> columnNames)
118      : this(elements) {
119      ColumnNames = columnNames;
120    }
121
122    public override IDeepCloneable Clone(Cloner cloner) {
123      StringMatrix clone = new StringMatrix();
124      cloner.RegisterClonedObject(this, clone);
125      clone.matrix = (string[,])matrix.Clone();
126      return clone;
127    }
128
129    public override string ToString() {
130      StringBuilder sb = new StringBuilder();
131      sb.Append("[");
132      if (matrix.Length > 0) {
133        for (int i = 0; i < Rows; i++) {
134          sb.Append("[").Append(matrix[i, 0]);
135          for (int j = 1; j < Columns; j++)
136            sb.Append(";").Append(matrix[i, j]);
137          sb.Append("]");
138        }
139      }
140      sb.Append("]");
141      return sb.ToString();
142    }
143
144    public virtual IEnumerator GetEnumerator() {
145      return matrix.GetEnumerator();
146    }
147
148    protected virtual bool Validate(string value, out string errorMessage) {
149      if (value == null) {
150        errorMessage = "Invalid Value (string must not be null)";
151        return false;
152      } else {
153        errorMessage = string.Empty;
154        return true;
155      }
156    }
157    protected virtual string GetValue(int rowIndex, int columIndex) {
158      return this[rowIndex, columIndex];
159    }
160    protected virtual bool SetValue(string value, int rowIndex, int columnIndex) {
161      if (value != null) {
162        this[rowIndex, columnIndex] = value;
163        return true;
164      } else {
165        return false;
166      }
167    }
168
169    public event EventHandler<EventArgs<int, int>> ItemChanged;
170    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
171      if (ItemChanged != null)
172        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
173      OnToStringChanged();
174    }
175    public event EventHandler Reset;
176    protected virtual void OnReset() {
177      if (Reset != null)
178        Reset(this, EventArgs.Empty);
179      OnToStringChanged();
180    }
181
182    #region IStringConvertibleMatrix Members
183    int IStringConvertibleMatrix.Rows {
184      get { return Rows; }
185      set { Rows = value; }
186    }
187    int IStringConvertibleMatrix.Columns {
188      get { return Columns; }
189      set { Columns = value; }
190    }
191    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
192      get { return this.ColumnNames; }
193      set { this.ColumnNames = value; }
194    }
195    bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
196      return Validate(value, out errorMessage);
197    }
198    string IStringConvertibleMatrix.GetValue(int rowIndex, int columIndex) {
199      return GetValue(rowIndex, columIndex);
200    }
201    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
202      return SetValue(value, rowIndex, columnIndex);
203    }
204    #endregion
205  }
206}
Note: See TracBrowser for help on using the repository browser.