Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Analysis/3.3/DataVisualization/DataTable.cs @ 5119

Last change on this file since 5119 was 5097, checked in by epitzer, 14 years ago

DataTable, DataRow and DataTableView have been unsealed. (#1338)

A possible persistence problem has also been fixed by directly persisting the visualProperties property otherwise an ArgumentException might break backwards compatibility.

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