Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Analysis/3.3/DataVisualization/IndexedDataTable.cs @ 15325

Last change on this file since 15325 was 15048, checked in by abeham, 7 years ago

#2634: implemented reviewer's comments

File size: 11.9 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  [Item("IndexedDataTable", "A data table where the points are also given with a certain index.")]
35  [StorableClass]
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(bool 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      var 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    private void RegisterRowsEvents() {
118      rows.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedDataRow<T>>(RowsOnItemsAdded);
119      rows.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedDataRow<T>>(RowsOnItemsRemoved);
120      rows.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedDataRow<T>>(RowsOnItemsReplaced);
121      rows.CollectionReset += new CollectionItemsChangedEventHandler<IndexedDataRow<T>>(RowsOnCollectionReset);
122      foreach (var row in Rows) RegisterRowEvents(row);
123    }
124    protected virtual void RowsOnItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedDataRow<T>> e) {
125      foreach (var row in e.Items)
126        this.RegisterRowEvents(row);
127
128      this.OnColumnsChanged();
129      this.OnColumnNamesChanged();
130      this.OnReset();
131    }
132    protected virtual void RowsOnItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedDataRow<T>> e) {
133      foreach (var row in e.Items)
134        this.DeregisterRowEvents(row);
135
136      this.OnColumnsChanged();
137      this.OnColumnNamesChanged();
138      this.OnReset();
139    }
140    protected virtual void RowsOnItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedDataRow<T>> e) {
141      foreach (var row in e.OldItems)
142        this.DeregisterRowEvents(row);
143      foreach (var row in e.Items)
144        this.RegisterRowEvents(row);
145
146      this.OnColumnsChanged();
147      this.OnColumnNamesChanged();
148      this.OnReset();
149    }
150    protected virtual void RowsOnCollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedDataRow<T>> e) {
151      foreach (var row in e.OldItems)
152        this.DeregisterRowEvents(row);
153      foreach (var row in e.Items)
154        this.RegisterRowEvents(row);
155
156      if (e.OldItems.Count() != e.Items.Count())
157        this.OnColumnsChanged();
158      this.OnColumnNamesChanged();
159      this.OnReset();
160    }
161
162    private void RegisterRowEvents(IndexedDataRow<T> row) {
163      row.Values.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(ValuesOnItemsAdded);
164      row.Values.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(ValuesOnItemsMoved);
165      row.Values.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(ValuesOnItemsRemoved);
166      row.Values.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(ValuesOnItemsReplaced);
167      row.Values.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(ValuesOnCollectionReset);
168    }
169    private void DeregisterRowEvents(IndexedDataRow<T> row) {
170      row.Values.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(ValuesOnItemsAdded);
171      row.Values.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(ValuesOnItemsMoved);
172      row.Values.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(ValuesOnItemsRemoved);
173      row.Values.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(ValuesOnItemsReplaced);
174      row.Values.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<Tuple<T, double>>>(ValuesOnCollectionReset);
175    }
176
177    protected virtual void ValuesOnItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<Tuple<T, double>>> e) {
178      this.OnReset();
179    }
180    protected virtual void ValuesOnItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<Tuple<T, double>>> e) {
181      this.OnReset();
182    }
183    protected virtual void ValuesOnItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<Tuple<T, double>>> e) {
184      this.OnReset();
185    }
186    protected virtual void ValuesOnItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<Tuple<T, double>>> e) {
187      this.OnReset();
188    }
189    protected virtual void ValuesOnCollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<Tuple<T, double>>> e) {
190      this.OnReset();
191    }
192
193    #region IStringConvertibleMatrix Members
194
195    int IStringConvertibleMatrix.Rows {
196      get { return rows.Count == 0 ? 0 : rows.Max(r => r.Values.Count); }
197      set { throw new NotSupportedException(); }
198    }
199    int IStringConvertibleMatrix.Columns {
200      get { return rows.Count * 2; }
201      set { throw new NotSupportedException(); }
202    }
203    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
204      get { return rows.Select(r => new string[] { r.Name + " Index", r.Name }).SelectMany(x => x); }
205      set { throw new NotSupportedException(); }
206    }
207    IEnumerable<string> IStringConvertibleMatrix.RowNames {
208      get { return new List<string>(); }
209      set { throw new NotSupportedException(); }
210    }
211
212    bool IStringConvertibleMatrix.SortableView {
213      get { return true; }
214      set { throw new NotSupportedException(); }
215    }
216    bool IStringConvertibleMatrix.ReadOnly {
217      get { return true; }
218    }
219
220    string IStringConvertibleMatrix.GetValue(int rowIndex, int columnIndex) {
221      if (columnIndex < rows.Count * 2) {
222        string columnName = ((IStringConvertibleMatrix)this).ColumnNames.ElementAt(columnIndex);
223        if (!rows.ContainsKey(columnName) && columnName.Length > " Index".Length) {
224          columnName = columnName.Substring(0, columnName.Length - " Index".Length);
225          if (rows.ContainsKey(columnName) && rowIndex < rows[columnName].Values.Count)
226            return rows[columnName].Values[rowIndex].Item1.ToString();
227          else return string.Empty;
228        } else if (rows.ContainsKey(columnName) && rowIndex < rows[columnName].Values.Count)
229          return rows[columnName].Values[rowIndex].Item2.ToString();
230      }
231      return string.Empty;
232    }
233
234    bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
235      throw new NotSupportedException();
236    }
237    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
238      throw new NotSupportedException();
239    }
240
241    public event EventHandler<EventArgs<int, int>> ItemChanged;
242    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
243      var handler = ItemChanged;
244      if (handler != null) handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
245      OnToStringChanged();
246    }
247    public event EventHandler Reset;
248    protected virtual void OnReset() {
249      var handler = Reset;
250      if (handler != null) handler(this, EventArgs.Empty);
251    }
252    public event EventHandler ColumnsChanged;
253    protected virtual void OnColumnsChanged() {
254      var handler = ColumnsChanged;
255      if (handler != null) handler(this, EventArgs.Empty);
256    }
257    public event EventHandler RowsChanged;
258    protected virtual void OnRowsChanged() {
259      var handler = RowsChanged;
260      if (handler != null) handler(this, EventArgs.Empty);
261    }
262    public event EventHandler ColumnNamesChanged;
263    protected virtual void OnColumnNamesChanged() {
264      var handler = ColumnNamesChanged;
265      if (handler != null) handler(this, EventArgs.Empty);
266    }
267    public event EventHandler RowNamesChanged;
268    protected virtual void OnRowNamesChanged() {
269      var handler = RowNamesChanged;
270      if (handler != null) handler(this, EventArgs.Empty);
271    }
272    public event EventHandler SortableViewChanged;
273    protected virtual void OnSortableViewChanged() {
274      var handler = SortableViewChanged;
275      if (handler != null) handler(this, EventArgs.Empty);
276    }
277    #endregion
278  }
279}
Note: See TracBrowser for help on using the repository browser.