Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive.Azure/HeuristicLab.Analysis/3.3/DataVisualization/DataTable.cs @ 7270

Last change on this file since 7270 was 7270, checked in by spimming, 13 years ago

#1680:

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