Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ImprovingStringConvertibleMatrix/HeuristicLab.Data/3.3/StringMatrix.cs @ 9286

Last change on this file since 9286 was 9286, checked in by sforsten, 12 years ago

#2018:

  • added new methods to the interface IStringConvertibleMatrix as well as two structs. Also the event ItemChanged has been changed to ItemsChanged
  • class ValueTypeMatrix now implements IStringConvertibleMatrix instead of the classes which inherit from it
  • small changes have been applied to a lot of classes to correctly implement the changed interface IStringConvertibleMatrix
  • solution file, Build.cmd and PreBuildEvent.cmd have been added
File size: 12.1 KB
RevLine 
[2694]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 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 {
[7201]36    public static new Image StaticItemImage {
[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;
[9286]130            OnItemsChanged(new List<Position>(1) { new Position(rowIndex, columnIndex) });
[2694]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    }
[9286]252    protected virtual bool SetValue(IEnumerable<RowColumnValue> rowColumnValues) {
253      if (ReadOnly) throw new NotSupportedException("Item cannot be set. StringMatrix is read-only.");
254      if (rowColumnValues.Any(x => x.Value == null)) { return false; }
255      Position pos;
256      foreach (var curValue in rowColumnValues) {
257        pos = curValue.Position;
258        matrix[pos.Row, pos.Column] = curValue.Value;
259      }
260      OnItemsChanged(rowColumnValues.Select(x => x.Position));
261      return true;
262    }
[3054]263
[5150]264    #region events
265    public event EventHandler ColumnsChanged;
266    protected virtual void OnColumnsChanged() {
267      EventHandler handler = ColumnsChanged;
268      if (handler != null)
269        handler(this, EventArgs.Empty);
270    }
271    public event EventHandler RowsChanged;
272    protected virtual void OnRowsChanged() {
273      EventHandler handler = RowsChanged;
274      if (handler != null)
275        handler(this, EventArgs.Empty);
276    }
[3320]277    public event EventHandler ColumnNamesChanged;
278    protected virtual void OnColumnNamesChanged() {
279      EventHandler handler = ColumnNamesChanged;
280      if (handler != null)
281        handler(this, EventArgs.Empty);
282    }
283    public event EventHandler RowNamesChanged;
284    protected virtual void OnRowNamesChanged() {
285      EventHandler handler = RowNamesChanged;
286      if (handler != null)
287        handler(this, EventArgs.Empty);
288    }
289    public event EventHandler SortableViewChanged;
290    protected virtual void OnSortableViewChanged() {
291      EventHandler handler = SortableViewChanged;
292      if (handler != null)
293        handler(this, EventArgs.Empty);
294    }
[9286]295    public event EventHandler<EventArgs<IEnumerable<Position>>> ItemsChanged;
296    protected virtual void OnItemsChanged(IEnumerable<Position> positions) {
297      if (ItemsChanged != null)
298        ItemsChanged(this, new EventArgs<IEnumerable<Position>>(positions));
[2932]299      OnToStringChanged();
[2694]300    }
[2973]301    public event EventHandler Reset;
[3054]302    protected virtual void OnReset() {
[2694]303      if (Reset != null)
304        Reset(this, EventArgs.Empty);
[2932]305      OnToStringChanged();
[2694]306    }
[5150]307    #endregion
[3054]308
309    #region IStringConvertibleMatrix Members
310    int IStringConvertibleMatrix.Rows {
311      get { return Rows; }
312      set { Rows = value; }
313    }
314    int IStringConvertibleMatrix.Columns {
315      get { return Columns; }
316      set { Columns = value; }
317    }
318    bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
319      return Validate(value, out errorMessage);
320    }
321    string IStringConvertibleMatrix.GetValue(int rowIndex, int columIndex) {
322      return GetValue(rowIndex, columIndex);
323    }
324    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
325      return SetValue(value, rowIndex, columnIndex);
326    }
[9286]327    public bool SetValue(RowColumnValue rowColumnValue) {
328      return SetValue(rowColumnValue.Value, rowColumnValue.Position.Row, rowColumnValue.Position.Column);
329    }
330    bool IStringConvertibleMatrix.SetValue(IEnumerable<RowColumnValue> rowColumnValues) {
331      return SetValue(rowColumnValues);
332    }
[2694]333    #endregion
334  }
335}
Note: See TracBrowser for help on using the repository browser.