Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Analysis/3.3/DataVisualization/DataTable.cs @ 15344

Last change on this file since 15344 was 9363, checked in by spimming, 12 years ago

#1888:

  • Merged revisions from trunk
File size: 11.4 KB
RevLine 
[2908]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 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; }
[9363]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; }
[9363]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) {
[9363]88      VisualProperties = (DataTableVisualProperties)cloner.Clone(original.visualProperties);
89      Rows = cloner.Clone(original.rows);
[4722]90    }
[2908]91    public DataTable()
92      : base() {
[4870]93      VisualProperties = new DataTableVisualProperties();
[9363]94      Rows = new NamedItemCollection<DataRow>();
[2908]95    }
96    public DataTable(string name)
97      : base(name) {
[7229]98      VisualProperties = new DataTableVisualProperties(name);
[9363]99      Rows = new NamedItemCollection<DataRow>();
[2908]100    }
101    public DataTable(string name, string description)
102      : base(name, description) {
[7229]103      VisualProperties = new DataTableVisualProperties(name);
[9363]104      Rows = new NamedItemCollection<DataRow>();
[2908]105    }
106
[4870]107    // BackwardsCompatibility3.3
108    #region Backwards compatible code, remove with 3.4
109    [StorableHook(HookType.AfterDeserialization)]
110    private void AfterDeserialization() {
[7229]111      if (VisualProperties == null) VisualProperties = new DataTableVisualProperties(name);
112      if (VisualProperties.Title == null) VisualProperties.Title = name;
[4870]113    }
114    #endregion
115
116    public override IDeepCloneable Clone(Cloner cloner) {
117      return new DataTable(this, cloner);
118    }
119
120    public event EventHandler VisualPropertiesChanged;
[5097]121    protected virtual void OnVisualPropertiesChanged() {
[4870]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
[5097]130    protected virtual void RegisterRowsEvents() {
[3767]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);
[5150]139
140      this.OnColumnsChanged();
[3767]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);
[5150]147
148      this.OnColumnsChanged();
[3767]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);
[5150]157
158      this.OnColumnsChanged();
[3767]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);
[5150]167
168      if (e.OldItems.Count() != e.Items.Count())
169        this.OnColumnsChanged();
[3767]170      this.OnColumnNamesChanged();
171      this.OnReset();
172    }
173
[5097]174    protected virtual void RegisterRowEvents(DataRow row) {
[3767]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    }
[5097]181    protected virtual void DeregisterRowEvents(DataRow row) {
[3767]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 {
[4774]208      get { return rows.Count == 0 ? 0 : rows.Max(r => r.Values.Count); }
[3767]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 {
[9363]220      get { return Enumerable.Empty<string>(); }
[3767]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;
[5097]249    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
[4763]250      var handler = ItemChanged;
251      if (handler != null) handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
[3767]252      OnToStringChanged();
253    }
254    public event EventHandler Reset;
[5097]255    protected virtual void OnReset() {
[4763]256      var handler = Reset;
257      if (handler != null) handler(this, EventArgs.Empty);
[3767]258    }
[5150]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    }
[3767]269    public event EventHandler ColumnNamesChanged;
[5097]270    protected virtual void OnColumnNamesChanged() {
[4763]271      var handler = ColumnNamesChanged;
272      if (handler != null) handler(this, EventArgs.Empty);
[3767]273    }
274    public event EventHandler RowNamesChanged;
[5097]275    protected virtual void OnRowNamesChanged() {
[4763]276      var handler = RowNamesChanged;
277      if (handler != null) handler(this, EventArgs.Empty);
[3767]278    }
279    public event EventHandler SortableViewChanged;
[5097]280    protected virtual void OnSortableViewChanged() {
[4763]281      var handler = SortableViewChanged;
282      if (handler != null) handler(this, EventArgs.Empty);
[3767]283    }
284    #endregion
[2908]285  }
286}
Note: See TracBrowser for help on using the repository browser.