Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Analysis/3.3/DataVisualization/DataTable.cs @ 14710

Last change on this file since 14710 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 11.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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    #endregion
116
117    public override IDeepCloneable Clone(Cloner cloner) {
118      return new DataTable(this, cloner);
119    }
120
121    public event EventHandler VisualPropertiesChanged;
122    protected virtual void OnVisualPropertiesChanged() {
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
131    protected virtual void RegisterRowsEvents() {
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);
140
141      this.OnColumnsChanged();
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);
148
149      this.OnColumnsChanged();
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);
158
159      this.OnColumnsChanged();
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);
168
169      if (e.OldItems.Count() != e.Items.Count())
170        this.OnColumnsChanged();
171      this.OnColumnNamesChanged();
172      this.OnReset();
173    }
174
175    protected virtual void RegisterRowEvents(DataRow row) {
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    }
182    protected virtual void DeregisterRowEvents(DataRow row) {
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 {
209      get { return rows.Count == 0 ? 0 : rows.Max(r => r.Values.Count); }
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 {
221      get { return Enumerable.Empty<string>(); }
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;
250    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
251      var handler = ItemChanged;
252      if (handler != null) handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
253      OnToStringChanged();
254    }
255    public event EventHandler Reset;
256    protected virtual void OnReset() {
257      var handler = Reset;
258      if (handler != null) handler(this, EventArgs.Empty);
259    }
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    }
270    public event EventHandler ColumnNamesChanged;
271    protected virtual void OnColumnNamesChanged() {
272      var handler = ColumnNamesChanged;
273      if (handler != null) handler(this, EventArgs.Empty);
274    }
275    public event EventHandler RowNamesChanged;
276    protected virtual void OnRowNamesChanged() {
277      var handler = RowNamesChanged;
278      if (handler != null) handler(this, EventArgs.Empty);
279    }
280    public event EventHandler SortableViewChanged;
281    protected virtual void OnSortableViewChanged() {
282      var handler = SortableViewChanged;
283      if (handler != null) handler(this, EventArgs.Empty);
284    }
285    #endregion
286  }
287}
Note: See TracBrowser for help on using the repository browser.