[2694] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections;
|
---|
| 24 | using System.Collections.Generic;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using System.Xml;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Data {
|
---|
| 32 | [Item("StringMatrixData", "Represents a matrix of strings.")]
|
---|
| 33 | public sealed class StringMatrixData : Item, IEnumerable, IStringConvertibleMatrixData {
|
---|
| 34 | [Storable]
|
---|
| 35 | private string[,] array;
|
---|
| 36 |
|
---|
| 37 | public int Rows {
|
---|
| 38 | get { return array.GetLength(0); }
|
---|
| 39 | private set {
|
---|
| 40 | if (value != Rows) {
|
---|
| 41 | string[,] newArray = new string[value, Columns];
|
---|
| 42 | Array.Copy(array, newArray, Math.Min(value * Columns, array.Length));
|
---|
| 43 | array = newArray;
|
---|
| 44 | OnReset();
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 | public int Columns {
|
---|
| 49 | get { return array.GetLength(1); }
|
---|
| 50 | private set {
|
---|
| 51 | if (value != Columns) {
|
---|
| 52 | string[,] newArray = new string[Rows, value];
|
---|
| 53 | for (int i = 0; i < Rows; i++)
|
---|
| 54 | Array.Copy(array, i * Columns, newArray, i * value, Math.Min(value, Columns));
|
---|
| 55 | array = newArray;
|
---|
| 56 | OnReset();
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 | public string this[int rowIndex, int columnIndex] {
|
---|
| 61 | get { return array[rowIndex, columnIndex]; }
|
---|
| 62 | set {
|
---|
| 63 | if (value != array[rowIndex, columnIndex]) {
|
---|
| 64 | if ((value != null) || (array[rowIndex, columnIndex] != string.Empty)) {
|
---|
| 65 | array[rowIndex, columnIndex] = value != null ? value : string.Empty;
|
---|
| 66 | OnItemChanged(rowIndex, columnIndex);
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | public StringMatrixData() {
|
---|
| 73 | array = new string[0, 0];
|
---|
| 74 | }
|
---|
| 75 | public StringMatrixData(int rows, int columns) {
|
---|
| 76 | array = new string[rows, columns];
|
---|
| 77 | for (int i = 0; i < array.GetLength(0); i++) {
|
---|
| 78 | for (int j = 0; j < array.GetLength(1); j++)
|
---|
| 79 | array[i, j] = string.Empty;
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | public StringMatrixData(string[,] elements) {
|
---|
| 83 | if (elements == null) throw new ArgumentNullException();
|
---|
| 84 | array = new string[elements.GetLength(0), elements.GetLength(1)];
|
---|
| 85 | for (int i = 0; i < array.GetLength(0); i++) {
|
---|
| 86 | for (int j = 0; j < array.GetLength(1); j++)
|
---|
| 87 | array[i, j] = elements[i, j] == null ? string.Empty : elements[i, j];
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | private StringMatrixData(StringMatrixData elements) {
|
---|
| 91 | if (elements == null) throw new ArgumentNullException();
|
---|
| 92 | array = (string[,])elements.array.Clone();
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 96 | StringMatrixData clone = new StringMatrixData(this);
|
---|
| 97 | cloner.RegisterClonedObject(this, clone);
|
---|
| 98 | return clone;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | public override string ToString() {
|
---|
| 102 | StringBuilder sb = new StringBuilder();
|
---|
| 103 | sb.Append("[");
|
---|
| 104 | if (array.Length > 0) {
|
---|
| 105 | for (int i = 0; i < Rows; i++) {
|
---|
| 106 | sb.Append("[").Append(array[i, 0]);
|
---|
| 107 | for (int j = 1; j < Columns; j++)
|
---|
| 108 | sb.Append(";").Append(array[i, j]);
|
---|
| 109 | sb.Append("]");
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | sb.Append("]");
|
---|
| 113 | return sb.ToString();
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | public IEnumerator GetEnumerator() {
|
---|
| 117 | return array.GetEnumerator();
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | #region IStringConvertibleMatrixData Members
|
---|
| 121 | StringConvertibleArrayDataDimensions IStringConvertibleMatrixData.Dimensions {
|
---|
| 122 | get { return StringConvertibleArrayDataDimensions.Both; }
|
---|
| 123 | }
|
---|
| 124 | int IStringConvertibleMatrixData.Rows {
|
---|
| 125 | get { return Rows; }
|
---|
| 126 | set { Rows = value; }
|
---|
| 127 | }
|
---|
| 128 | int IStringConvertibleMatrixData.Columns {
|
---|
| 129 | get { return Columns; }
|
---|
| 130 | set { Columns = value; }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | bool IStringConvertibleMatrixData.Validate(string value, out string errorMessage) {
|
---|
| 134 | if (value == null) {
|
---|
| 135 | errorMessage = "Invalid Value (string must not be null)";
|
---|
| 136 | return false;
|
---|
| 137 | } else {
|
---|
| 138 | errorMessage = string.Empty;
|
---|
| 139 | return true;
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | string IStringConvertibleMatrixData.GetValue(int rowIndex, int columIndex) {
|
---|
| 143 | return this[rowIndex, columIndex];
|
---|
| 144 | }
|
---|
| 145 | bool IStringConvertibleMatrixData.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
| 146 | if (value != null) {
|
---|
| 147 | this[rowIndex, columnIndex] = value;
|
---|
| 148 | return true;
|
---|
| 149 | } else {
|
---|
| 150 | return false;
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | private event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
| 154 | event EventHandler<EventArgs<int, int>> IStringConvertibleMatrixData.ItemChanged {
|
---|
| 155 | add { ItemChanged += value; }
|
---|
| 156 | remove { ItemChanged -= value; }
|
---|
| 157 | }
|
---|
| 158 | private void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
| 159 | if (ItemChanged != null)
|
---|
| 160 | ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
| 161 | OnChanged();
|
---|
| 162 | }
|
---|
| 163 | private event EventHandler Reset;
|
---|
| 164 | event EventHandler IStringConvertibleMatrixData.Reset {
|
---|
| 165 | add { Reset += value; }
|
---|
| 166 | remove { Reset -= value; }
|
---|
| 167 | }
|
---|
| 168 | private void OnReset() {
|
---|
| 169 | if (Reset != null)
|
---|
| 170 | Reset(this, EventArgs.Empty);
|
---|
| 171 | OnChanged();
|
---|
| 172 | }
|
---|
| 173 | #endregion
|
---|
| 174 | }
|
---|
| 175 | }
|
---|