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