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.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Linq;
|
---|
27 | using HeuristicLab.Collections;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Analysis {
|
---|
34 | /// <summary>
|
---|
35 | /// A table of data values.
|
---|
36 | /// </summary>
|
---|
37 | [Item("DataTable", "A table of data values.")]
|
---|
38 | [StorableClass]
|
---|
39 | public sealed class DataTable : NamedItem, IStringConvertibleMatrix {
|
---|
40 | public override Image ItemImage {
|
---|
41 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Performance; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | private DataTableVisualProperties visualProperties;
|
---|
45 | public DataTableVisualProperties VisualProperties {
|
---|
46 | get { return visualProperties; }
|
---|
47 | set {
|
---|
48 | if (visualProperties != value) {
|
---|
49 | if (value == null) throw new ArgumentNullException("VisualProperties");
|
---|
50 | if (visualProperties != null) visualProperties.PropertyChanged -= new PropertyChangedEventHandler(VisualProperties_PropertyChanged);
|
---|
51 | visualProperties = value;
|
---|
52 | visualProperties.PropertyChanged += new PropertyChangedEventHandler(VisualProperties_PropertyChanged);
|
---|
53 | OnVisualPropertiesChanged();
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | private NamedItemCollection<DataRow> rows;
|
---|
59 | public NamedItemCollection<DataRow> Rows {
|
---|
60 | get { return rows; }
|
---|
61 | }
|
---|
62 |
|
---|
63 | #region Persistence Properties
|
---|
64 | [Storable(Name = "VisualProperties")]
|
---|
65 | private DataTableVisualProperties StorableVisualProperties {
|
---|
66 | get { return VisualProperties; }
|
---|
67 | set { VisualProperties = value; }
|
---|
68 | }
|
---|
69 | [Storable(Name = "rows")]
|
---|
70 | private IEnumerable<DataRow> StorableRows {
|
---|
71 | get { return rows; }
|
---|
72 | set { rows = new NamedItemCollection<DataRow>(value); }
|
---|
73 | }
|
---|
74 | #endregion
|
---|
75 |
|
---|
76 | [StorableConstructor]
|
---|
77 | private DataTable(bool deserializing) : base(deserializing) { }
|
---|
78 | private DataTable(DataTable original, Cloner cloner)
|
---|
79 | : base(original, cloner) {
|
---|
80 | this.VisualProperties = (DataTableVisualProperties)cloner.Clone(original.visualProperties);
|
---|
81 | this.rows = cloner.Clone(original.rows);
|
---|
82 | this.RegisterRowsEvents();
|
---|
83 | }
|
---|
84 | public DataTable()
|
---|
85 | : base() {
|
---|
86 | VisualProperties = new DataTableVisualProperties();
|
---|
87 | rows = new NamedItemCollection<DataRow>();
|
---|
88 | this.RegisterRowsEvents();
|
---|
89 | }
|
---|
90 | public DataTable(string name)
|
---|
91 | : base(name) {
|
---|
92 | VisualProperties = new DataTableVisualProperties();
|
---|
93 | rows = new NamedItemCollection<DataRow>();
|
---|
94 | this.RegisterRowsEvents();
|
---|
95 | }
|
---|
96 | public DataTable(string name, string description)
|
---|
97 | : base(name, description) {
|
---|
98 | VisualProperties = new DataTableVisualProperties();
|
---|
99 | rows = new NamedItemCollection<DataRow>();
|
---|
100 | this.RegisterRowsEvents();
|
---|
101 | }
|
---|
102 |
|
---|
103 | // BackwardsCompatibility3.3
|
---|
104 | #region Backwards compatible code, remove with 3.4
|
---|
105 | [StorableHook(HookType.AfterDeserialization)]
|
---|
106 | private void AfterDeserialization() {
|
---|
107 | if (VisualProperties == null) VisualProperties = new DataTableVisualProperties();
|
---|
108 | }
|
---|
109 | #endregion
|
---|
110 |
|
---|
111 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
112 | return new DataTable(this, cloner);
|
---|
113 | }
|
---|
114 |
|
---|
115 | public event EventHandler VisualPropertiesChanged;
|
---|
116 | private void OnVisualPropertiesChanged() {
|
---|
117 | EventHandler handler = VisualPropertiesChanged;
|
---|
118 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
119 | }
|
---|
120 |
|
---|
121 | private void VisualProperties_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
122 | OnVisualPropertiesChanged();
|
---|
123 | }
|
---|
124 |
|
---|
125 | private void RegisterRowsEvents() {
|
---|
126 | rows.ItemsAdded += new CollectionItemsChangedEventHandler<DataRow>(rows_ItemsAdded);
|
---|
127 | rows.ItemsRemoved += new CollectionItemsChangedEventHandler<DataRow>(rows_ItemsRemoved);
|
---|
128 | rows.ItemsReplaced += new CollectionItemsChangedEventHandler<DataRow>(rows_ItemsReplaced);
|
---|
129 | rows.CollectionReset += new CollectionItemsChangedEventHandler<DataRow>(rows_CollectionReset);
|
---|
130 | }
|
---|
131 | private void rows_ItemsAdded(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
132 | foreach (DataRow row in e.Items)
|
---|
133 | this.RegisterRowEvents(row);
|
---|
134 | this.OnColumnNamesChanged();
|
---|
135 | this.OnReset();
|
---|
136 | }
|
---|
137 | private void rows_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
138 | foreach (DataRow row in e.Items)
|
---|
139 | this.DeregisterRowEvents(row);
|
---|
140 | this.OnColumnNamesChanged();
|
---|
141 | this.OnReset();
|
---|
142 | }
|
---|
143 | private void rows_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
144 | foreach (DataRow row in e.OldItems)
|
---|
145 | this.DeregisterRowEvents(row);
|
---|
146 | foreach (DataRow row in e.Items)
|
---|
147 | this.RegisterRowEvents(row);
|
---|
148 | this.OnColumnNamesChanged();
|
---|
149 | this.OnReset();
|
---|
150 | }
|
---|
151 | private void rows_CollectionReset(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
152 | foreach (DataRow row in e.OldItems)
|
---|
153 | this.DeregisterRowEvents(row);
|
---|
154 | foreach (DataRow row in e.Items)
|
---|
155 | this.RegisterRowEvents(row);
|
---|
156 | this.OnColumnNamesChanged();
|
---|
157 | this.OnReset();
|
---|
158 | }
|
---|
159 |
|
---|
160 | private void RegisterRowEvents(DataRow row) {
|
---|
161 | row.Values.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
|
---|
162 | row.Values.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
|
---|
163 | row.Values.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
|
---|
164 | row.Values.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
|
---|
165 | row.Values.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
|
---|
166 | }
|
---|
167 | private void DeregisterRowEvents(DataRow row) {
|
---|
168 | row.Values.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
|
---|
169 | row.Values.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
|
---|
170 | row.Values.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
|
---|
171 | row.Values.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
|
---|
172 | row.Values.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
|
---|
173 | }
|
---|
174 |
|
---|
175 | private void Values_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
176 | this.OnReset();
|
---|
177 | }
|
---|
178 | private void Values_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
179 | this.OnReset();
|
---|
180 | }
|
---|
181 | private void Values_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
182 | this.OnReset();
|
---|
183 | }
|
---|
184 | private void Values_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
185 | this.OnReset();
|
---|
186 | }
|
---|
187 | private void Values_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
188 | this.OnReset();
|
---|
189 | }
|
---|
190 |
|
---|
191 | #region IStringConvertibleMatrix Members
|
---|
192 |
|
---|
193 | int IStringConvertibleMatrix.Rows {
|
---|
194 | get { return rows.Count == 0 ? 0 : rows.Max(r => r.Values.Count); }
|
---|
195 | set { throw new NotSupportedException(); }
|
---|
196 | }
|
---|
197 | int IStringConvertibleMatrix.Columns {
|
---|
198 | get { return rows.Count; }
|
---|
199 | set { throw new NotSupportedException(); }
|
---|
200 | }
|
---|
201 | IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
|
---|
202 | get { return rows.Select(r => r.Name); }
|
---|
203 | set { throw new NotSupportedException(); }
|
---|
204 | }
|
---|
205 | IEnumerable<string> IStringConvertibleMatrix.RowNames {
|
---|
206 | get { return new List<string>(); }
|
---|
207 | set { throw new NotSupportedException(); }
|
---|
208 | }
|
---|
209 |
|
---|
210 | bool IStringConvertibleMatrix.SortableView {
|
---|
211 | get { return true; }
|
---|
212 | set { throw new NotSupportedException(); }
|
---|
213 | }
|
---|
214 | bool IStringConvertibleMatrix.ReadOnly {
|
---|
215 | get { return true; }
|
---|
216 | }
|
---|
217 |
|
---|
218 | string IStringConvertibleMatrix.GetValue(int rowIndex, int columnIndex) {
|
---|
219 | if (columnIndex < rows.Count) {
|
---|
220 | string columnName = ((IStringConvertibleMatrix)this).ColumnNames.ElementAt(columnIndex);
|
---|
221 | if (rows.ContainsKey(columnName) && rowIndex < rows[columnName].Values.Count)
|
---|
222 | return rows[columnName].Values[rowIndex].ToString();
|
---|
223 | }
|
---|
224 | return string.Empty;
|
---|
225 | }
|
---|
226 |
|
---|
227 | bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
|
---|
228 | throw new NotSupportedException();
|
---|
229 | }
|
---|
230 | bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
231 | throw new NotSupportedException();
|
---|
232 | }
|
---|
233 |
|
---|
234 | public event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
235 | private void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
236 | var handler = ItemChanged;
|
---|
237 | if (handler != null) handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
238 | OnToStringChanged();
|
---|
239 | }
|
---|
240 | public event EventHandler Reset;
|
---|
241 | private void OnReset() {
|
---|
242 | var handler = Reset;
|
---|
243 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
244 | }
|
---|
245 | public event EventHandler ColumnNamesChanged;
|
---|
246 | private void OnColumnNamesChanged() {
|
---|
247 | var handler = ColumnNamesChanged;
|
---|
248 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
249 | }
|
---|
250 | public event EventHandler RowNamesChanged;
|
---|
251 | private void OnRowNamesChanged() {
|
---|
252 | var handler = RowNamesChanged;
|
---|
253 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
254 | }
|
---|
255 | public event EventHandler SortableViewChanged;
|
---|
256 | private void OnSortableViewChanged() {
|
---|
257 | var handler = SortableViewChanged;
|
---|
258 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
259 | }
|
---|
260 | #endregion
|
---|
261 | }
|
---|
262 | }
|
---|