Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Analysis/3.3/DataVisualization/IndexedDataTable.cs @ 15428

Last change on this file since 15428 was 15018, checked in by gkronber, 8 years ago

#2520 introduced StorableConstructorFlag type for StorableConstructors

File size: 11.8 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;
32
33namespace HeuristicLab.Analysis {
34  [Item("IndexedDataTable", "A data table where the points are also given with a certain index.")]
35  [StorableType("b49903a7-a7ea-49c7-a13a-8470506eed49")]
36  public class IndexedDataTable<T> : NamedItem, IStringConvertibleMatrix {
37    public static new Image StaticItemImage {
38      get { return HeuristicLab.Common.Resources.VSImageLibrary.Performance; }
39    }
40
41    private DataTableVisualProperties visualProperties;
42    public DataTableVisualProperties VisualProperties {
43      get { return visualProperties; }
44      set {
45        if (visualProperties != value) {
46          if (value == null) throw new ArgumentNullException("VisualProperties");
47          if (visualProperties != null) visualProperties.PropertyChanged -= new PropertyChangedEventHandler(VisualProperties_PropertyChanged);
48          visualProperties = value;
49          visualProperties.PropertyChanged += new PropertyChangedEventHandler(VisualProperties_PropertyChanged);
50          OnVisualPropertiesChanged();
51        }
52      }
53    }
54
55    private NamedItemCollection<IndexedDataRow<T>> rows;
56    public NamedItemCollection<IndexedDataRow<T>> Rows {
57      get { return rows; }
58    }
59
60    #region Persistence Properties
61    [Storable(Name = "VisualProperties")]
62    private DataTableVisualProperties StorableVisualProperties {
63      get { return visualProperties; }
64      set {
65        visualProperties = value;
66        visualProperties.PropertyChanged += new PropertyChangedEventHandler(VisualProperties_PropertyChanged);
67      }
68    }
69    [Storable(Name = "rows")]
70    private IEnumerable<IndexedDataRow<T>> StorableRows {
71      get { return rows; }
72      set { rows = new NamedItemCollection<IndexedDataRow<T>>(value); }
73    }
74    #endregion
75
76    [StorableConstructor]
77    protected IndexedDataTable(StorableConstructorFlag deserializing) : base(deserializing) { }
78    protected IndexedDataTable(IndexedDataTable<T> original, Cloner cloner)
79      : base(original, cloner) {
80      VisualProperties = (DataTableVisualProperties)cloner.Clone(original.visualProperties);
81      rows = cloner.Clone(original.rows);
82      this.RegisterRowsEvents();
83    }
84    public IndexedDataTable()
85      : base() {
86      VisualProperties = new DataTableVisualProperties();
87      rows = new NamedItemCollection<IndexedDataRow<T>>();
88      this.RegisterRowsEvents();
89    }
90    public IndexedDataTable(string name)
91      : base(name) {
92      VisualProperties = new DataTableVisualProperties();
93      rows = new NamedItemCollection<IndexedDataRow<T>>();
94      this.RegisterRowsEvents();
95    }
96    public IndexedDataTable(string name, string description)
97      : base(name, description) {
98      VisualProperties = new DataTableVisualProperties();
99      rows = new NamedItemCollection<IndexedDataRow<T>>();
100      this.RegisterRowsEvents();
101    }
102
103    public override IDeepCloneable Clone(Cloner cloner) {
104      return new IndexedDataTable<T>(this, cloner);
105    }
106
107    public event EventHandler VisualPropertiesChanged;
108    protected virtual void OnVisualPropertiesChanged() {
109      EventHandler handler = VisualPropertiesChanged;
110      if (handler != null) handler(this, EventArgs.Empty);
111    }
112
113    private void VisualProperties_PropertyChanged(object sender, PropertyChangedEventArgs e) {
114      OnVisualPropertiesChanged();
115    }
116
117    protected virtual void RegisterRowsEvents() {
118      rows.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedDataRow<T>>(rows_ItemsAdded);
119      rows.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedDataRow<T>>(rows_ItemsRemoved);
120      rows.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedDataRow<T>>(rows_ItemsReplaced);
121      rows.CollectionReset += new CollectionItemsChangedEventHandler<IndexedDataRow<T>>(rows_CollectionReset);
122    }
123    private void rows_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedDataRow<T>> e) {
124      foreach (var row in e.Items)
125        this.RegisterRowEvents(row);
126
127      this.OnColumnsChanged();
128      this.OnColumnNamesChanged();
129      this.OnReset();
130    }
131    private void rows_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedDataRow<T>> e) {
132      foreach (var row in e.Items)
133        this.DeregisterRowEvents(row);
134
135      this.OnColumnsChanged();
136      this.OnColumnNamesChanged();
137      this.OnReset();
138    }
139    private void rows_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedDataRow<T>> e) {
140      foreach (var row in e.OldItems)
141        this.DeregisterRowEvents(row);
142      foreach (var row in e.Items)
143        this.RegisterRowEvents(row);
144
145      this.OnColumnsChanged();
146      this.OnColumnNamesChanged();
147      this.OnReset();
148    }
149    private void rows_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedDataRow<T>> e) {
150      foreach (var row in e.OldItems)
151        this.DeregisterRowEvents(row);
152      foreach (var row in e.Items)
153        this.RegisterRowEvents(row);
154
155      if (e.OldItems.Count() != e.Items.Count())
156        this.OnColumnsChanged();
157      this.OnColumnNamesChanged();
158      this.OnReset();
159    }
160
161    protected virtual void RegisterRowEvents(IndexedDataRow<T> row) {
162      row.Values.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_ItemsAdded);
163      row.Values.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_ItemsMoved);
164      row.Values.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_ItemsRemoved);
165      row.Values.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_ItemsReplaced);
166      row.Values.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_CollectionReset);
167    }
168    protected virtual void DeregisterRowEvents(IndexedDataRow<T> row) {
169      row.Values.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_ItemsAdded);
170      row.Values.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_ItemsMoved);
171      row.Values.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_ItemsRemoved);
172      row.Values.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_ItemsReplaced);
173      row.Values.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(Values_CollectionReset);
174    }
175
176    private void Values_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<Tuple<T, double>>> e) {
177      this.OnReset();
178    }
179    private void Values_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<Tuple<T, double>>> e) {
180      this.OnReset();
181    }
182    private void Values_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<Tuple<T, double>>> e) {
183      this.OnReset();
184    }
185    private void Values_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<Tuple<T, double>>> e) {
186      this.OnReset();
187    }
188    private void Values_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<Tuple<T, double>>> e) {
189      this.OnReset();
190    }
191
192    #region IStringConvertibleMatrix Members
193
194    int IStringConvertibleMatrix.Rows {
195      get { return rows.Count == 0 ? 0 : rows.Max(r => r.Values.Count); }
196      set { throw new NotSupportedException(); }
197    }
198    int IStringConvertibleMatrix.Columns {
199      get { return rows.Count * 2; }
200      set { throw new NotSupportedException(); }
201    }
202    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
203      get { return rows.Select(r => new string[] { r.Name + " Index", r.Name }).SelectMany(x => x); }
204      set { throw new NotSupportedException(); }
205    }
206    IEnumerable<string> IStringConvertibleMatrix.RowNames {
207      get { return new List<string>(); }
208      set { throw new NotSupportedException(); }
209    }
210
211    bool IStringConvertibleMatrix.SortableView {
212      get { return true; }
213      set { throw new NotSupportedException(); }
214    }
215    bool IStringConvertibleMatrix.ReadOnly {
216      get { return true; }
217    }
218
219    string IStringConvertibleMatrix.GetValue(int rowIndex, int columnIndex) {
220      if (columnIndex < rows.Count * 2) {
221        string columnName = ((IStringConvertibleMatrix)this).ColumnNames.ElementAt(columnIndex);
222        if (!rows.ContainsKey(columnName) && columnName.Length > " Index".Length) {
223          columnName = columnName.Substring(0, columnName.Length - " Index".Length);
224          if (rows.ContainsKey(columnName) && rowIndex < rows[columnName].Values.Count)
225            return rows[columnName].Values[rowIndex].Item1.ToString();
226          else return string.Empty;
227        } else if (rows.ContainsKey(columnName) && rowIndex < rows[columnName].Values.Count)
228          return rows[columnName].Values[rowIndex].Item2.ToString();
229      }
230      return string.Empty;
231    }
232
233    bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
234      throw new NotSupportedException();
235    }
236    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
237      throw new NotSupportedException();
238    }
239
240    public event EventHandler<EventArgs<int, int>> ItemChanged;
241    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
242      var handler = ItemChanged;
243      if (handler != null) handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
244      OnToStringChanged();
245    }
246    public event EventHandler Reset;
247    protected virtual void OnReset() {
248      var handler = Reset;
249      if (handler != null) handler(this, EventArgs.Empty);
250    }
251    public event EventHandler ColumnsChanged;
252    protected virtual void OnColumnsChanged() {
253      var handler = ColumnsChanged;
254      if (handler != null) handler(this, EventArgs.Empty);
255    }
256    public event EventHandler RowsChanged;
257    protected virtual void OnRowsChanged() {
258      var handler = RowsChanged;
259      if (handler != null) handler(this, EventArgs.Empty);
260    }
261    public event EventHandler ColumnNamesChanged;
262    protected virtual void OnColumnNamesChanged() {
263      var handler = ColumnNamesChanged;
264      if (handler != null) handler(this, EventArgs.Empty);
265    }
266    public event EventHandler RowNamesChanged;
267    protected virtual void OnRowNamesChanged() {
268      var handler = RowNamesChanged;
269      if (handler != null) handler(this, EventArgs.Empty);
270    }
271    public event EventHandler SortableViewChanged;
272    protected virtual void OnSortableViewChanged() {
273      var handler = SortableViewChanged;
274      if (handler != null) handler(this, EventArgs.Empty);
275    }
276    #endregion
277  }
278}
Note: See TracBrowser for help on using the repository browser.