Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2929_PrioritizedGrammarEnumeration/HeuristicLab.Analysis/3.3/DataVisualization/DataTable.cs @ 16793

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

#2640: Updated year of copyrights in license headers

File size: 12.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 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      Name = "DataTable";
94      VisualProperties = new DataTableVisualProperties();
95      Rows = new NamedItemCollection<DataRow>();
96    }
97    public DataTable(string name)
98      : base(name) {
99      VisualProperties = new DataTableVisualProperties(name);
100      Rows = new NamedItemCollection<DataRow>();
101    }
102    public DataTable(string name, string description)
103      : base(name, description) {
104      VisualProperties = new DataTableVisualProperties(name);
105      Rows = new NamedItemCollection<DataRow>();
106    }
107
108    // BackwardsCompatibility3.3
109    #region Backwards compatible code, remove with 3.4
110    [StorableHook(HookType.AfterDeserialization)]
111    private void AfterDeserialization() {
112      if (VisualProperties == null) VisualProperties = new DataTableVisualProperties(name);
113      if (VisualProperties.Title == null) VisualProperties.Title = name;
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
129    }
130    #endregion
131
132    public override IDeepCloneable Clone(Cloner cloner) {
133      return new DataTable(this, cloner);
134    }
135
136    public event EventHandler VisualPropertiesChanged;
137    protected virtual void OnVisualPropertiesChanged() {
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
146    protected virtual void RegisterRowsEvents() {
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);
155
156      this.OnColumnsChanged();
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);
163
164      this.OnColumnsChanged();
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);
173
174      this.OnColumnsChanged();
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);
183
184      if (e.OldItems.Count() != e.Items.Count())
185        this.OnColumnsChanged();
186      this.OnColumnNamesChanged();
187      this.OnReset();
188    }
189
190    protected virtual void RegisterRowEvents(DataRow row) {
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    }
197    protected virtual void DeregisterRowEvents(DataRow row) {
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 {
224      get { return rows.Count == 0 ? 0 : rows.Max(r => r.Values.Count); }
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 {
236      get { return Enumerable.Empty<string>(); }
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;
265    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
266      var handler = ItemChanged;
267      if (handler != null) handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
268      OnToStringChanged();
269    }
270    public event EventHandler Reset;
271    protected virtual void OnReset() {
272      var handler = Reset;
273      if (handler != null) handler(this, EventArgs.Empty);
274    }
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    }
285    public event EventHandler ColumnNamesChanged;
286    protected virtual void OnColumnNamesChanged() {
287      var handler = ColumnNamesChanged;
288      if (handler != null) handler(this, EventArgs.Empty);
289    }
290    public event EventHandler RowNamesChanged;
291    protected virtual void OnRowNamesChanged() {
292      var handler = RowNamesChanged;
293      if (handler != null) handler(this, EventArgs.Empty);
294    }
295    public event EventHandler SortableViewChanged;
296    protected virtual void OnSortableViewChanged() {
297      var handler = SortableViewChanged;
298      if (handler != null) handler(this, EventArgs.Empty);
299    }
300    #endregion
301  }
302}
Note: See TracBrowser for help on using the repository browser.