1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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 class DataTable : NamedItem, IStringConvertibleMatrix {
|
---|
40 | public static new Image StaticItemImage {
|
---|
41 | get { return HeuristicLab.Common.Resources.VSImageLibrary.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 | private set {
|
---|
62 | if (rows != null) throw new InvalidOperationException("Rows already set");
|
---|
63 | rows = value;
|
---|
64 | if (rows != null) RegisterRowsEvents();
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | #region Persistence Properties
|
---|
69 | [Storable(Name = "VisualProperties")]
|
---|
70 | private DataTableVisualProperties StorableVisualProperties {
|
---|
71 | get { return visualProperties; }
|
---|
72 | set {
|
---|
73 | visualProperties = value;
|
---|
74 | visualProperties.PropertyChanged += new PropertyChangedEventHandler(VisualProperties_PropertyChanged);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | [Storable(Name = "rows")]
|
---|
78 | private IEnumerable<DataRow> StorableRows {
|
---|
79 | get { return rows; }
|
---|
80 | set { Rows = new NamedItemCollection<DataRow>(value); }
|
---|
81 | }
|
---|
82 | #endregion
|
---|
83 |
|
---|
84 | [StorableConstructor]
|
---|
85 | protected DataTable(bool deserializing) : base(deserializing) { }
|
---|
86 | protected DataTable(DataTable original, Cloner cloner)
|
---|
87 | : base(original, cloner) {
|
---|
88 | VisualProperties = (DataTableVisualProperties)cloner.Clone(original.visualProperties);
|
---|
89 | Rows = cloner.Clone(original.rows);
|
---|
90 | }
|
---|
91 | public DataTable()
|
---|
92 | : base() {
|
---|
93 | VisualProperties = new DataTableVisualProperties();
|
---|
94 | Rows = new NamedItemCollection<DataRow>();
|
---|
95 | }
|
---|
96 | public DataTable(string name)
|
---|
97 | : base(name) {
|
---|
98 | VisualProperties = new DataTableVisualProperties(name);
|
---|
99 | Rows = new NamedItemCollection<DataRow>();
|
---|
100 | }
|
---|
101 | public DataTable(string name, string description)
|
---|
102 | : base(name, description) {
|
---|
103 | VisualProperties = new DataTableVisualProperties(name);
|
---|
104 | Rows = new NamedItemCollection<DataRow>();
|
---|
105 | }
|
---|
106 |
|
---|
107 | // BackwardsCompatibility3.3
|
---|
108 | #region Backwards compatible code, remove with 3.4
|
---|
109 | [StorableHook(HookType.AfterDeserialization)]
|
---|
110 | private void AfterDeserialization() {
|
---|
111 | if (VisualProperties == null) VisualProperties = new DataTableVisualProperties(name);
|
---|
112 | if (VisualProperties.Title == null) VisualProperties.Title = name;
|
---|
113 | }
|
---|
114 | #endregion
|
---|
115 |
|
---|
116 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
117 | return new DataTable(this, cloner);
|
---|
118 | }
|
---|
119 |
|
---|
120 | public event EventHandler VisualPropertiesChanged;
|
---|
121 | protected virtual void OnVisualPropertiesChanged() {
|
---|
122 | EventHandler handler = VisualPropertiesChanged;
|
---|
123 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
124 | }
|
---|
125 |
|
---|
126 | private void VisualProperties_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
127 | OnVisualPropertiesChanged();
|
---|
128 | }
|
---|
129 |
|
---|
130 | protected virtual void RegisterRowsEvents() {
|
---|
131 | rows.ItemsAdded += new CollectionItemsChangedEventHandler<DataRow>(rows_ItemsAdded);
|
---|
132 | rows.ItemsRemoved += new CollectionItemsChangedEventHandler<DataRow>(rows_ItemsRemoved);
|
---|
133 | rows.ItemsReplaced += new CollectionItemsChangedEventHandler<DataRow>(rows_ItemsReplaced);
|
---|
134 | rows.CollectionReset += new CollectionItemsChangedEventHandler<DataRow>(rows_CollectionReset);
|
---|
135 | }
|
---|
136 | private void rows_ItemsAdded(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
137 | foreach (DataRow row in e.Items)
|
---|
138 | this.RegisterRowEvents(row);
|
---|
139 |
|
---|
140 | this.OnColumnsChanged();
|
---|
141 | this.OnColumnNamesChanged();
|
---|
142 | this.OnReset();
|
---|
143 | }
|
---|
144 | private void rows_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
145 | foreach (DataRow row in e.Items)
|
---|
146 | this.DeregisterRowEvents(row);
|
---|
147 |
|
---|
148 | this.OnColumnsChanged();
|
---|
149 | this.OnColumnNamesChanged();
|
---|
150 | this.OnReset();
|
---|
151 | }
|
---|
152 | private void rows_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
153 | foreach (DataRow row in e.OldItems)
|
---|
154 | this.DeregisterRowEvents(row);
|
---|
155 | foreach (DataRow row in e.Items)
|
---|
156 | this.RegisterRowEvents(row);
|
---|
157 |
|
---|
158 | this.OnColumnsChanged();
|
---|
159 | this.OnColumnNamesChanged();
|
---|
160 | this.OnReset();
|
---|
161 | }
|
---|
162 | private void rows_CollectionReset(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
163 | foreach (DataRow row in e.OldItems)
|
---|
164 | this.DeregisterRowEvents(row);
|
---|
165 | foreach (DataRow row in e.Items)
|
---|
166 | this.RegisterRowEvents(row);
|
---|
167 |
|
---|
168 | if (e.OldItems.Count() != e.Items.Count())
|
---|
169 | this.OnColumnsChanged();
|
---|
170 | this.OnColumnNamesChanged();
|
---|
171 | this.OnReset();
|
---|
172 | }
|
---|
173 |
|
---|
174 | protected virtual void RegisterRowEvents(DataRow row) {
|
---|
175 | row.Values.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
|
---|
176 | row.Values.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
|
---|
177 | row.Values.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
|
---|
178 | row.Values.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
|
---|
179 | row.Values.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
|
---|
180 | }
|
---|
181 | protected virtual void DeregisterRowEvents(DataRow row) {
|
---|
182 | row.Values.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
|
---|
183 | row.Values.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
|
---|
184 | row.Values.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
|
---|
185 | row.Values.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
|
---|
186 | row.Values.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
|
---|
187 | }
|
---|
188 |
|
---|
189 | private void Values_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
190 | this.OnReset();
|
---|
191 | }
|
---|
192 | private void Values_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
193 | this.OnReset();
|
---|
194 | }
|
---|
195 | private void Values_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
196 | this.OnReset();
|
---|
197 | }
|
---|
198 | private void Values_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
199 | this.OnReset();
|
---|
200 | }
|
---|
201 | private void Values_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
202 | this.OnReset();
|
---|
203 | }
|
---|
204 |
|
---|
205 | #region IStringConvertibleMatrix Members
|
---|
206 |
|
---|
207 | int IStringConvertibleMatrix.Rows {
|
---|
208 | get { return rows.Count == 0 ? 0 : rows.Max(r => r.Values.Count); }
|
---|
209 | set { throw new NotSupportedException(); }
|
---|
210 | }
|
---|
211 | int IStringConvertibleMatrix.Columns {
|
---|
212 | get { return rows.Count; }
|
---|
213 | set { throw new NotSupportedException(); }
|
---|
214 | }
|
---|
215 | IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
|
---|
216 | get { return rows.Select(r => r.Name); }
|
---|
217 | set { throw new NotSupportedException(); }
|
---|
218 | }
|
---|
219 | IEnumerable<string> IStringConvertibleMatrix.RowNames {
|
---|
220 | get { return Enumerable.Empty<string>(); }
|
---|
221 | set { throw new NotSupportedException(); }
|
---|
222 | }
|
---|
223 |
|
---|
224 | bool IStringConvertibleMatrix.SortableView {
|
---|
225 | get { return true; }
|
---|
226 | set { throw new NotSupportedException(); }
|
---|
227 | }
|
---|
228 | bool IStringConvertibleMatrix.ReadOnly {
|
---|
229 | get { return true; }
|
---|
230 | }
|
---|
231 |
|
---|
232 | string IStringConvertibleMatrix.GetValue(int rowIndex, int columnIndex) {
|
---|
233 | if (columnIndex < rows.Count) {
|
---|
234 | string columnName = ((IStringConvertibleMatrix)this).ColumnNames.ElementAt(columnIndex);
|
---|
235 | if (rows.ContainsKey(columnName) && rowIndex < rows[columnName].Values.Count)
|
---|
236 | return rows[columnName].Values[rowIndex].ToString();
|
---|
237 | }
|
---|
238 | return string.Empty;
|
---|
239 | }
|
---|
240 |
|
---|
241 | bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
|
---|
242 | throw new NotSupportedException();
|
---|
243 | }
|
---|
244 | bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
245 | throw new NotSupportedException();
|
---|
246 | }
|
---|
247 |
|
---|
248 | public event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
249 | protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
250 | var handler = ItemChanged;
|
---|
251 | if (handler != null) handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
252 | OnToStringChanged();
|
---|
253 | }
|
---|
254 | public event EventHandler Reset;
|
---|
255 | protected virtual void OnReset() {
|
---|
256 | var handler = Reset;
|
---|
257 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
258 | }
|
---|
259 | public event EventHandler ColumnsChanged;
|
---|
260 | protected virtual void OnColumnsChanged() {
|
---|
261 | var handler = ColumnsChanged;
|
---|
262 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
263 | }
|
---|
264 | public event EventHandler RowsChanged;
|
---|
265 | protected virtual void OnRowsChanged() {
|
---|
266 | var handler = RowsChanged;
|
---|
267 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
268 | }
|
---|
269 | public event EventHandler ColumnNamesChanged;
|
---|
270 | protected virtual void OnColumnNamesChanged() {
|
---|
271 | var handler = ColumnNamesChanged;
|
---|
272 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
273 | }
|
---|
274 | public event EventHandler RowNamesChanged;
|
---|
275 | protected virtual void OnRowNamesChanged() {
|
---|
276 | var handler = RowNamesChanged;
|
---|
277 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
278 | }
|
---|
279 | public event EventHandler SortableViewChanged;
|
---|
280 | protected virtual void OnSortableViewChanged() {
|
---|
281 | var handler = SortableViewChanged;
|
---|
282 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
283 | }
|
---|
284 | #endregion
|
---|
285 | }
|
---|
286 | }
|
---|