[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections;
|
---|
[3308] | 24 | using System.Collections.Generic;
|
---|
[3306] | 25 | using System.Drawing;
|
---|
[3308] | 26 | using System.Linq;
|
---|
[2694] | 27 | using System.Text;
|
---|
| 28 | using HeuristicLab.Common;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Data {
|
---|
[3048] | 33 | [Item("StringMatrix", "Represents a matrix of strings.")]
|
---|
[3017] | 34 | [StorableClass]
|
---|
[3054] | 35 | public class StringMatrix : Item, IEnumerable, 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]
|
---|
[3310] | 44 | private List<string> columnNames;
|
---|
[3308] | 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)
|
---|
[3310] | 51 | throw new ArgumentException("A column name must be specified for each column .");
|
---|
[3308] | 52 | else
|
---|
| 53 | columnNames = new List<string>(value);
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
[3310] | 56 | [Storable]
|
---|
| 57 | private List<string> rowNames;
|
---|
| 58 | public IEnumerable<string> RowNames {
|
---|
| 59 | get { return this.rowNames; }
|
---|
| 60 | set {
|
---|
| 61 | if (value == null || value.Count() == 0)
|
---|
| 62 | rowNames = new List<string>();
|
---|
| 63 | else if (value.Count() != Rows)
|
---|
| 64 | throw new ArgumentException("A row name must be specified for each row.");
|
---|
| 65 | else
|
---|
| 66 | rowNames = new List<string>(value);
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
[3308] | 69 |
|
---|
[3054] | 70 | public virtual int Rows {
|
---|
| 71 | get { return matrix.GetLength(0); }
|
---|
| 72 | protected set {
|
---|
[2694] | 73 | if (value != Rows) {
|
---|
[3054] | 74 | string[,] newMatrix = new string[value, Columns];
|
---|
| 75 | Array.Copy(matrix, newMatrix, Math.Min(value * Columns, matrix.Length));
|
---|
| 76 | matrix = newMatrix;
|
---|
[2694] | 77 | OnReset();
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
[3054] | 81 | public virtual int Columns {
|
---|
| 82 | get { return matrix.GetLength(1); }
|
---|
| 83 | protected set {
|
---|
[2694] | 84 | if (value != Columns) {
|
---|
[3054] | 85 | string[,] newMatrix = new string[Rows, value];
|
---|
[2694] | 86 | for (int i = 0; i < Rows; i++)
|
---|
[3054] | 87 | Array.Copy(matrix, i * Columns, newMatrix, i * value, Math.Min(value, Columns));
|
---|
| 88 | matrix = newMatrix;
|
---|
[2694] | 89 | OnReset();
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
[3054] | 93 | public virtual string this[int rowIndex, int columnIndex] {
|
---|
| 94 | get { return matrix[rowIndex, columnIndex]; }
|
---|
[2694] | 95 | set {
|
---|
[3054] | 96 | if (value != matrix[rowIndex, columnIndex]) {
|
---|
| 97 | if ((value != null) || (matrix[rowIndex, columnIndex] != string.Empty)) {
|
---|
| 98 | matrix[rowIndex, columnIndex] = value != null ? value : string.Empty;
|
---|
[2694] | 99 | OnItemChanged(rowIndex, columnIndex);
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[3048] | 105 | public StringMatrix() {
|
---|
[3054] | 106 | matrix = new string[0, 0];
|
---|
[3308] | 107 | columnNames = new List<string>();
|
---|
[3310] | 108 | rowNames = new List<string>();
|
---|
[2694] | 109 | }
|
---|
[3048] | 110 | public StringMatrix(int rows, int columns) {
|
---|
[3054] | 111 | matrix = new string[rows, columns];
|
---|
| 112 | for (int i = 0; i < matrix.GetLength(0); i++) {
|
---|
| 113 | for (int j = 0; j < matrix.GetLength(1); j++)
|
---|
| 114 | matrix[i, j] = string.Empty;
|
---|
[2694] | 115 | }
|
---|
[3308] | 116 | columnNames = new List<string>();
|
---|
[3310] | 117 | rowNames = new List<string>();
|
---|
[2694] | 118 | }
|
---|
[3308] | 119 | protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames)
|
---|
| 120 | : this(rows, columns) {
|
---|
| 121 | ColumnNames = columnNames;
|
---|
| 122 | }
|
---|
[3310] | 123 | protected StringMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames)
|
---|
| 124 | : this(rows, columns,columnNames) {
|
---|
| 125 | RowNames = rowNames;
|
---|
| 126 | }
|
---|
[3048] | 127 | public StringMatrix(string[,] elements) {
|
---|
[2694] | 128 | if (elements == null) throw new ArgumentNullException();
|
---|
[3054] | 129 | matrix = new string[elements.GetLength(0), elements.GetLength(1)];
|
---|
| 130 | for (int i = 0; i < matrix.GetLength(0); i++) {
|
---|
| 131 | for (int j = 0; j < matrix.GetLength(1); j++)
|
---|
| 132 | matrix[i, j] = elements[i, j] == null ? string.Empty : elements[i, j];
|
---|
[2694] | 133 | }
|
---|
[3308] | 134 | columnNames = new List<string>();
|
---|
[3310] | 135 | rowNames = new List<string>();
|
---|
[2694] | 136 | }
|
---|
[3308] | 137 | protected StringMatrix(string[,] elements, IEnumerable<string> columnNames)
|
---|
| 138 | : this(elements) {
|
---|
| 139 | ColumnNames = columnNames;
|
---|
| 140 | }
|
---|
[3310] | 141 | protected StringMatrix(string[,] elements, IEnumerable<string> columnNames,IEnumerable<string> rowNames)
|
---|
| 142 | : this(elements,columnNames) {
|
---|
| 143 | RowNames = rowNames;
|
---|
| 144 | }
|
---|
[2694] | 145 |
|
---|
| 146 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[3054] | 147 | StringMatrix clone = new StringMatrix();
|
---|
[2694] | 148 | cloner.RegisterClonedObject(this, clone);
|
---|
[3054] | 149 | clone.matrix = (string[,])matrix.Clone();
|
---|
[3310] | 150 | clone.columnNames = new List<string>(columnNames);
|
---|
| 151 | clone.rowNames = new List<string>(rowNames);
|
---|
[2694] | 152 | return clone;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | public override string ToString() {
|
---|
| 156 | StringBuilder sb = new StringBuilder();
|
---|
| 157 | sb.Append("[");
|
---|
[3054] | 158 | if (matrix.Length > 0) {
|
---|
[2694] | 159 | for (int i = 0; i < Rows; i++) {
|
---|
[3054] | 160 | sb.Append("[").Append(matrix[i, 0]);
|
---|
[2694] | 161 | for (int j = 1; j < Columns; j++)
|
---|
[3054] | 162 | sb.Append(";").Append(matrix[i, j]);
|
---|
[2694] | 163 | sb.Append("]");
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | sb.Append("]");
|
---|
| 167 | return sb.ToString();
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[3054] | 170 | public virtual IEnumerator GetEnumerator() {
|
---|
| 171 | return matrix.GetEnumerator();
|
---|
[2694] | 172 | }
|
---|
| 173 |
|
---|
[3054] | 174 | protected virtual bool Validate(string value, out string errorMessage) {
|
---|
[2694] | 175 | if (value == null) {
|
---|
| 176 | errorMessage = "Invalid Value (string must not be null)";
|
---|
| 177 | return false;
|
---|
| 178 | } else {
|
---|
| 179 | errorMessage = string.Empty;
|
---|
| 180 | return true;
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
[3054] | 183 | protected virtual string GetValue(int rowIndex, int columIndex) {
|
---|
[2694] | 184 | return this[rowIndex, columIndex];
|
---|
| 185 | }
|
---|
[3054] | 186 | protected virtual bool SetValue(string value, int rowIndex, int columnIndex) {
|
---|
[2694] | 187 | if (value != null) {
|
---|
| 188 | this[rowIndex, columnIndex] = value;
|
---|
| 189 | return true;
|
---|
| 190 | } else {
|
---|
| 191 | return false;
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
[3054] | 194 |
|
---|
[2973] | 195 | public event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
[3054] | 196 | protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
[2694] | 197 | if (ItemChanged != null)
|
---|
| 198 | ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
[2932] | 199 | OnToStringChanged();
|
---|
[2694] | 200 | }
|
---|
[2973] | 201 | public event EventHandler Reset;
|
---|
[3054] | 202 | protected virtual void OnReset() {
|
---|
[2694] | 203 | if (Reset != null)
|
---|
| 204 | Reset(this, EventArgs.Empty);
|
---|
[2932] | 205 | OnToStringChanged();
|
---|
[2694] | 206 | }
|
---|
[3054] | 207 |
|
---|
| 208 | #region IStringConvertibleMatrix Members
|
---|
| 209 | int IStringConvertibleMatrix.Rows {
|
---|
| 210 | get { return Rows; }
|
---|
| 211 | set { Rows = value; }
|
---|
| 212 | }
|
---|
| 213 | int IStringConvertibleMatrix.Columns {
|
---|
| 214 | get { return Columns; }
|
---|
| 215 | set { Columns = value; }
|
---|
| 216 | }
|
---|
[3308] | 217 | IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
|
---|
| 218 | get { return this.ColumnNames; }
|
---|
| 219 | set { this.ColumnNames = value; }
|
---|
| 220 | }
|
---|
[3310] | 221 | IEnumerable<string> IStringConvertibleMatrix.RowNames {
|
---|
| 222 | get { return this.RowNames; }
|
---|
| 223 | set { this.RowNames = value; }
|
---|
| 224 | }
|
---|
[3054] | 225 | bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
|
---|
| 226 | return Validate(value, out errorMessage);
|
---|
| 227 | }
|
---|
| 228 | string IStringConvertibleMatrix.GetValue(int rowIndex, int columIndex) {
|
---|
| 229 | return GetValue(rowIndex, columIndex);
|
---|
| 230 | }
|
---|
| 231 | bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
| 232 | return SetValue(value, rowIndex, columnIndex);
|
---|
| 233 | }
|
---|
[2694] | 234 | #endregion
|
---|
| 235 | }
|
---|
| 236 | }
|
---|