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
RevLine 
[2908]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
[4068]22using System;
23using System.Collections.Generic;
[4870]24using System.ComponentModel;
[3306]25using System.Drawing;
[3767]26using System.Linq;
[4068]27using HeuristicLab.Collections;
[3376]28using HeuristicLab.Common;
[2908]29using HeuristicLab.Core;
[4068]30using HeuristicLab.Data;
[2908]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.")]
[3017]38  [StorableClass]
[5097]39  public class DataTable : NamedItem, IStringConvertibleMatrix {
[3306]40    public override Image ItemImage {
[4702]41      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Performance; }
[3306]42    }
43
[5097]44    [Storable(Name = "VisualProperties")]
[4870]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
[2908]59    private NamedItemCollection<DataRow> rows;
60    public NamedItemCollection<DataRow> Rows {
61      get { return rows; }
62    }
63
[4870]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
[4722]72    [StorableConstructor]
[5097]73    protected DataTable(bool deserializing) : base(deserializing) { }
74    protected DataTable(DataTable original, Cloner cloner)
[4722]75      : base(original, cloner) {
[4870]76      this.VisualProperties = (DataTableVisualProperties)cloner.Clone(original.visualProperties);
[4722]77      this.rows = cloner.Clone(original.rows);
78      this.RegisterRowsEvents();
79    }
[2908]80    public DataTable()
81      : base() {
[4870]82      VisualProperties = new DataTableVisualProperties();
[2908]83      rows = new NamedItemCollection<DataRow>();
[3767]84      this.RegisterRowsEvents();
[2908]85    }
86    public DataTable(string name)
87      : base(name) {
[4870]88      VisualProperties = new DataTableVisualProperties();
[2908]89      rows = new NamedItemCollection<DataRow>();
[3767]90      this.RegisterRowsEvents();
[2908]91    }
92    public DataTable(string name, string description)
93      : base(name, description) {
[4870]94      VisualProperties = new DataTableVisualProperties();
[2908]95      rows = new NamedItemCollection<DataRow>();
[3767]96      this.RegisterRowsEvents();
[2908]97    }
98
[4870]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;
[5097]112    protected virtual void OnVisualPropertiesChanged() {
[4870]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
[5097]121    protected virtual void RegisterRowsEvents() {
[3767]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
[5097]156    protected virtual void RegisterRowEvents(DataRow row) {
[3767]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    }
[5097]163    protected virtual void DeregisterRowEvents(DataRow row) {
[3767]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 {
[4774]190      get { return rows.Count == 0 ? 0 : rows.Max(r => r.Values.Count); }
[3767]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;
[5097]231    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
[4763]232      var handler = ItemChanged;
233      if (handler != null) handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
[3767]234      OnToStringChanged();
235    }
236    public event EventHandler Reset;
[5097]237    protected virtual void OnReset() {
[4763]238      var handler = Reset;
239      if (handler != null) handler(this, EventArgs.Empty);
[3767]240    }
241    public event EventHandler ColumnNamesChanged;
[5097]242    protected virtual void OnColumnNamesChanged() {
[4763]243      var handler = ColumnNamesChanged;
244      if (handler != null) handler(this, EventArgs.Empty);
[3767]245    }
246    public event EventHandler RowNamesChanged;
[5097]247    protected virtual void OnRowNamesChanged() {
[4763]248      var handler = RowNamesChanged;
249      if (handler != null) handler(this, EventArgs.Empty);
[3767]250    }
251    public event EventHandler SortableViewChanged;
[5097]252    protected virtual void OnSortableViewChanged() {
[4763]253      var handler = SortableViewChanged;
254      if (handler != null) handler(this, EventArgs.Empty);
[3767]255    }
256    #endregion
[2908]257  }
258}
Note: See TracBrowser for help on using the repository browser.