Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15612 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

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