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