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