Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Analysis/3.3/DataVisualization/DataTable.cs @ 14113

Last change on this file since 14113 was 13656, checked in by ascheibe, 9 years ago

#2582 created branch for Hive Web Job Manager

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