Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6349 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 11.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 override Image ItemImage {
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    }
62
63    #region Persistence Properties
64    [Storable(Name = "VisualProperties")]
65    private DataTableVisualProperties StorableVisualProperties {
66      get { return visualProperties; }
67      set {
68        visualProperties = value;
69        visualProperties.PropertyChanged += new PropertyChangedEventHandler(VisualProperties_PropertyChanged);
70      }
71    }
72    [Storable(Name = "rows")]
73    private IEnumerable<DataRow> StorableRows {
74      get { return rows; }
75      set { rows = new NamedItemCollection<DataRow>(value); }
76    }
77    #endregion
78
79    [StorableConstructor]
80    protected DataTable(bool deserializing) : base(deserializing) { }
81    protected DataTable(DataTable original, Cloner cloner)
82      : base(original, cloner) {
83      this.VisualProperties = (DataTableVisualProperties)cloner.Clone(original.visualProperties);
84      this.rows = cloner.Clone(original.rows);
85      this.RegisterRowsEvents();
86    }
87    public DataTable()
88      : base() {
89      VisualProperties = new DataTableVisualProperties();
90      rows = new NamedItemCollection<DataRow>();
91      this.RegisterRowsEvents();
92    }
93    public DataTable(string name)
94      : base(name) {
95      VisualProperties = new DataTableVisualProperties();
96      rows = new NamedItemCollection<DataRow>();
97      this.RegisterRowsEvents();
98    }
99    public DataTable(string name, string description)
100      : base(name, description) {
101      VisualProperties = new DataTableVisualProperties();
102      rows = new NamedItemCollection<DataRow>();
103      this.RegisterRowsEvents();
104    }
105
106    // BackwardsCompatibility3.3
107    #region Backwards compatible code, remove with 3.4
108    [StorableHook(HookType.AfterDeserialization)]
109    private void AfterDeserialization() {
110      if (VisualProperties == null) VisualProperties = new DataTableVisualProperties();
111    }
112    #endregion
113
114    public override IDeepCloneable Clone(Cloner cloner) {
115      return new DataTable(this, cloner);
116    }
117
118    public event EventHandler VisualPropertiesChanged;
119    protected virtual void OnVisualPropertiesChanged() {
120      EventHandler handler = VisualPropertiesChanged;
121      if (handler != null) handler(this, EventArgs.Empty);
122    }
123
124    private void VisualProperties_PropertyChanged(object sender, PropertyChangedEventArgs e) {
125      OnVisualPropertiesChanged();
126    }
127
128    protected virtual void RegisterRowsEvents() {
129      rows.ItemsAdded += new CollectionItemsChangedEventHandler<DataRow>(rows_ItemsAdded);
130      rows.ItemsRemoved += new CollectionItemsChangedEventHandler<DataRow>(rows_ItemsRemoved);
131      rows.ItemsReplaced += new CollectionItemsChangedEventHandler<DataRow>(rows_ItemsReplaced);
132      rows.CollectionReset += new CollectionItemsChangedEventHandler<DataRow>(rows_CollectionReset);
133    }
134    private void rows_ItemsAdded(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
135      foreach (DataRow row in e.Items)
136        this.RegisterRowEvents(row);
137
138      this.OnColumnsChanged();
139      this.OnColumnNamesChanged();
140      this.OnReset();
141    }
142    private void rows_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
143      foreach (DataRow row in e.Items)
144        this.DeregisterRowEvents(row);
145
146      this.OnColumnsChanged();
147      this.OnColumnNamesChanged();
148      this.OnReset();
149    }
150    private void rows_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
151      foreach (DataRow row in e.OldItems)
152        this.DeregisterRowEvents(row);
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_CollectionReset(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
161      foreach (DataRow row in e.OldItems)
162        this.DeregisterRowEvents(row);
163      foreach (DataRow row in e.Items)
164        this.RegisterRowEvents(row);
165
166      if (e.OldItems.Count() != e.Items.Count())
167        this.OnColumnsChanged();
168      this.OnColumnNamesChanged();
169      this.OnReset();
170    }
171
172    protected virtual void RegisterRowEvents(DataRow row) {
173      row.Values.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
174      row.Values.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
175      row.Values.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
176      row.Values.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
177      row.Values.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
178    }
179    protected virtual void DeregisterRowEvents(DataRow row) {
180      row.Values.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
181      row.Values.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
182      row.Values.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
183      row.Values.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
184      row.Values.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
185    }
186
187    private void Values_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
188      this.OnReset();
189    }
190    private void Values_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
191      this.OnReset();
192    }
193    private void Values_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
194      this.OnReset();
195    }
196    private void Values_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
197      this.OnReset();
198    }
199    private void Values_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
200      this.OnReset();
201    }
202
203    #region IStringConvertibleMatrix Members
204
205    int IStringConvertibleMatrix.Rows {
206      get { return rows.Count == 0 ? 0 : rows.Max(r => r.Values.Count); }
207      set { throw new NotSupportedException(); }
208    }
209    int IStringConvertibleMatrix.Columns {
210      get { return rows.Count; }
211      set { throw new NotSupportedException(); }
212    }
213    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
214      get { return rows.Select(r => r.Name); }
215      set { throw new NotSupportedException(); }
216    }
217    IEnumerable<string> IStringConvertibleMatrix.RowNames {
218      get { return new List<string>(); }
219      set { throw new NotSupportedException(); }
220    }
221
222    bool IStringConvertibleMatrix.SortableView {
223      get { return true; }
224      set { throw new NotSupportedException(); }
225    }
226    bool IStringConvertibleMatrix.ReadOnly {
227      get { return true; }
228    }
229
230    string IStringConvertibleMatrix.GetValue(int rowIndex, int columnIndex) {
231      if (columnIndex < rows.Count) {
232        string columnName = ((IStringConvertibleMatrix)this).ColumnNames.ElementAt(columnIndex);
233        if (rows.ContainsKey(columnName) && rowIndex < rows[columnName].Values.Count)
234          return rows[columnName].Values[rowIndex].ToString();
235      }
236      return string.Empty;
237    }
238
239    bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
240      throw new NotSupportedException();
241    }
242    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
243      throw new NotSupportedException();
244    }
245
246    public event EventHandler<EventArgs<int, int>> ItemChanged;
247    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
248      var handler = ItemChanged;
249      if (handler != null) handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
250      OnToStringChanged();
251    }
252    public event EventHandler Reset;
253    protected virtual void OnReset() {
254      var handler = Reset;
255      if (handler != null) handler(this, EventArgs.Empty);
256    }
257    public event EventHandler ColumnsChanged;
258    protected virtual void OnColumnsChanged() {
259      var handler = ColumnsChanged;
260      if (handler != null) handler(this, EventArgs.Empty);
261    }
262    public event EventHandler RowsChanged;
263    protected virtual void OnRowsChanged() {
264      var handler = RowsChanged;
265      if (handler != null) handler(this, EventArgs.Empty);
266    }
267    public event EventHandler ColumnNamesChanged;
268    protected virtual void OnColumnNamesChanged() {
269      var handler = ColumnNamesChanged;
270      if (handler != null) handler(this, EventArgs.Empty);
271    }
272    public event EventHandler RowNamesChanged;
273    protected virtual void OnRowNamesChanged() {
274      var handler = RowNamesChanged;
275      if (handler != null) handler(this, EventArgs.Empty);
276    }
277    public event EventHandler SortableViewChanged;
278    protected virtual void OnSortableViewChanged() {
279      var handler = SortableViewChanged;
280      if (handler != null) handler(this, EventArgs.Empty);
281    }
282    #endregion
283  }
284}
Note: See TracBrowser for help on using the repository browser.