Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Breadcrumbs/HeuristicLab.Analysis/3.3/DataVisualization/DataTable.cs @ 12858

Last change on this file since 12858 was 11594, checked in by jkarder, 10 years ago

#2116: merged r10041-r11593 from trunk into branch

File size: 11.5 KB
RevLine 
[2908]1#region License Information
2/* HeuristicLab
[11594]3 * Copyright (C) 2002-2014 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;
[4870]114    }
115    #endregion
116
117    public override IDeepCloneable Clone(Cloner cloner) {
118      return new DataTable(this, cloner);
119    }
120
121    public event EventHandler VisualPropertiesChanged;
[5097]122    protected virtual void OnVisualPropertiesChanged() {
[4870]123      EventHandler handler = VisualPropertiesChanged;
124      if (handler != null) handler(this, EventArgs.Empty);
125    }
126
127    private void VisualProperties_PropertyChanged(object sender, PropertyChangedEventArgs e) {
128      OnVisualPropertiesChanged();
129    }
130
[5097]131    protected virtual void RegisterRowsEvents() {
[3767]132      rows.ItemsAdded += new CollectionItemsChangedEventHandler<DataRow>(rows_ItemsAdded);
133      rows.ItemsRemoved += new CollectionItemsChangedEventHandler<DataRow>(rows_ItemsRemoved);
134      rows.ItemsReplaced += new CollectionItemsChangedEventHandler<DataRow>(rows_ItemsReplaced);
135      rows.CollectionReset += new CollectionItemsChangedEventHandler<DataRow>(rows_CollectionReset);
136    }
137    private void rows_ItemsAdded(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
138      foreach (DataRow row in e.Items)
139        this.RegisterRowEvents(row);
[5150]140
141      this.OnColumnsChanged();
[3767]142      this.OnColumnNamesChanged();
143      this.OnReset();
144    }
145    private void rows_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
146      foreach (DataRow row in e.Items)
147        this.DeregisterRowEvents(row);
[5150]148
149      this.OnColumnsChanged();
[3767]150      this.OnColumnNamesChanged();
151      this.OnReset();
152    }
153    private void rows_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
154      foreach (DataRow row in e.OldItems)
155        this.DeregisterRowEvents(row);
156      foreach (DataRow row in e.Items)
157        this.RegisterRowEvents(row);
[5150]158
159      this.OnColumnsChanged();
[3767]160      this.OnColumnNamesChanged();
161      this.OnReset();
162    }
163    private void rows_CollectionReset(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
164      foreach (DataRow row in e.OldItems)
165        this.DeregisterRowEvents(row);
166      foreach (DataRow row in e.Items)
167        this.RegisterRowEvents(row);
[5150]168
169      if (e.OldItems.Count() != e.Items.Count())
170        this.OnColumnsChanged();
[3767]171      this.OnColumnNamesChanged();
172      this.OnReset();
173    }
174
[5097]175    protected virtual void RegisterRowEvents(DataRow row) {
[3767]176      row.Values.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
177      row.Values.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
178      row.Values.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
179      row.Values.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
180      row.Values.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
181    }
[5097]182    protected virtual void DeregisterRowEvents(DataRow row) {
[3767]183      row.Values.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
184      row.Values.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
185      row.Values.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
186      row.Values.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
187      row.Values.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
188    }
189
190    private void Values_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
191      this.OnReset();
192    }
193    private void Values_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
194      this.OnReset();
195    }
196    private void Values_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
197      this.OnReset();
198    }
199    private void Values_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
200      this.OnReset();
201    }
202    private void Values_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
203      this.OnReset();
204    }
205
206    #region IStringConvertibleMatrix Members
207
208    int IStringConvertibleMatrix.Rows {
[4774]209      get { return rows.Count == 0 ? 0 : rows.Max(r => r.Values.Count); }
[3767]210      set { throw new NotSupportedException(); }
211    }
212    int IStringConvertibleMatrix.Columns {
213      get { return rows.Count; }
214      set { throw new NotSupportedException(); }
215    }
216    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
217      get { return rows.Select(r => r.Name); }
218      set { throw new NotSupportedException(); }
219    }
220    IEnumerable<string> IStringConvertibleMatrix.RowNames {
[8280]221      get { return Enumerable.Empty<string>(); }
[3767]222      set { throw new NotSupportedException(); }
223    }
224
225    bool IStringConvertibleMatrix.SortableView {
226      get { return true; }
227      set { throw new NotSupportedException(); }
228    }
229    bool IStringConvertibleMatrix.ReadOnly {
230      get { return true; }
231    }
232
233    string IStringConvertibleMatrix.GetValue(int rowIndex, int columnIndex) {
234      if (columnIndex < rows.Count) {
235        string columnName = ((IStringConvertibleMatrix)this).ColumnNames.ElementAt(columnIndex);
236        if (rows.ContainsKey(columnName) && rowIndex < rows[columnName].Values.Count)
237          return rows[columnName].Values[rowIndex].ToString();
238      }
239      return string.Empty;
240    }
241
242    bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
243      throw new NotSupportedException();
244    }
245    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
246      throw new NotSupportedException();
247    }
248
249    public event EventHandler<EventArgs<int, int>> ItemChanged;
[5097]250    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
[4763]251      var handler = ItemChanged;
252      if (handler != null) handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
[3767]253      OnToStringChanged();
254    }
255    public event EventHandler Reset;
[5097]256    protected virtual void OnReset() {
[4763]257      var handler = Reset;
258      if (handler != null) handler(this, EventArgs.Empty);
[3767]259    }
[5150]260    public event EventHandler ColumnsChanged;
261    protected virtual void OnColumnsChanged() {
262      var handler = ColumnsChanged;
263      if (handler != null) handler(this, EventArgs.Empty);
264    }
265    public event EventHandler RowsChanged;
266    protected virtual void OnRowsChanged() {
267      var handler = RowsChanged;
268      if (handler != null) handler(this, EventArgs.Empty);
269    }
[3767]270    public event EventHandler ColumnNamesChanged;
[5097]271    protected virtual void OnColumnNamesChanged() {
[4763]272      var handler = ColumnNamesChanged;
273      if (handler != null) handler(this, EventArgs.Empty);
[3767]274    }
275    public event EventHandler RowNamesChanged;
[5097]276    protected virtual void OnRowNamesChanged() {
[4763]277      var handler = RowNamesChanged;
278      if (handler != null) handler(this, EventArgs.Empty);
[3767]279    }
280    public event EventHandler SortableViewChanged;
[5097]281    protected virtual void OnSortableViewChanged() {
[4763]282      var handler = SortableViewChanged;
283      if (handler != null) handler(this, EventArgs.Empty);
[3767]284    }
285    #endregion
[2908]286  }
287}
Note: See TracBrowser for help on using the repository browser.