Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Analysis.Views/3.3/DataTableView.cs @ 3610

Last change on this file since 3610 was 3566, checked in by mkommend, 15 years ago

removed ctors with contents in all views (ticket #972)

File size: 12.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Windows.Forms.DataVisualization.Charting;
25using HeuristicLab.Collections;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28
29namespace HeuristicLab.Analysis.Views {
30  /// <summary>
31  /// The visual representation of a <see cref="Variable"/>.
32  /// </summary>
33  [View("DataTable View")]
34  [Content(typeof(DataTable), true)]
35  public sealed partial class DataTableView : NamedItemView {
36    Dictionary<IObservableList<double>, DataRow> valuesRowsTable;
37    /// <summary>
38    /// Gets or sets the variable to represent visually.
39    /// </summary>
40    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
41    /// No own data storage present.</remarks>
42    public new DataTable Content {
43      get { return (DataTable)base.Content; }
44      set { base.Content = value; }
45    }
46
47    /// <summary>
48    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
49    /// </summary>
50    public DataTableView() {
51      InitializeComponent();
52      Caption = "DataTable";
53      valuesRowsTable = new Dictionary<IObservableList<double>, DataRow>();
54    }
55
56    /// <summary>
57    /// Removes the eventhandlers from the underlying <see cref="Variable"/>.
58    /// </summary>
59    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
60    protected override void DeregisterContentEvents() {
61      foreach (DataRow row in Content.Rows)
62        DeregisterDataRowEvents(row);
63      Content.Rows.ItemsAdded -= new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsAdded);
64      Content.Rows.ItemsRemoved -= new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsRemoved);
65      Content.Rows.ItemsReplaced -= new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsReplaced);
66      Content.Rows.CollectionReset -= new CollectionItemsChangedEventHandler<DataRow>(Rows_CollectionReset);
67      base.DeregisterContentEvents();
68    }
69
70    /// <summary>
71    /// Adds eventhandlers to the underlying <see cref="Variable"/>.
72    /// </summary>
73    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
74    protected override void RegisterContentEvents() {
75      base.RegisterContentEvents();
76      Content.Rows.ItemsAdded += new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsAdded);
77      Content.Rows.ItemsRemoved += new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsRemoved);
78      Content.Rows.ItemsReplaced += new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsReplaced);
79      Content.Rows.CollectionReset += new CollectionItemsChangedEventHandler<DataRow>(Rows_CollectionReset);
80      foreach (DataRow row in Content.Rows)
81        RegisterDataRowEvents(row);
82    }
83
84    protected override void OnContentChanged() {
85      base.OnContentChanged();
86      chart.Titles.Clear();
87      chart.Series.Clear();
88      Caption = "DataTable";
89      if (Content != null) {
90        Caption = Content.Name + " (" + Content.GetType().Name + ")";
91        chart.Titles.Add(new Title(Content.Name, Docking.Top));
92        foreach (DataRow row in Content.Rows)
93          AddDataRow(row);
94      }
95      SetEnabledStateOfControls();
96    }
97
98    protected override void OnReadOnlyChanged() {
99      base.OnReadOnlyChanged();
100      SetEnabledStateOfControls();
101    }
102
103    private void SetEnabledStateOfControls() {
104      chart.Enabled = Content != null;
105    }
106
107    private void AddDataRow(DataRow row) {
108      Series series = new Series(row.Name);
109      series.ChartType = SeriesChartType.FastLine;
110      series.ToolTip = row.Name + " #VAL";
111      for (int i = 0; i < row.Values.Count; i++) {
112        var value = row.Values[i];
113        if (IsInvalidValue(value)) {
114          DataPoint point = new DataPoint();
115          point.IsEmpty = true;
116          series.Points.Add(point);
117        } else {
118          series.Points.Add(value);
119        }
120      }
121      chart.Series.Add(series);
122    }
123    private void RemoveDataRow(DataRow row) {
124      Series series = chart.Series[row.Name];
125      chart.Series.Remove(series);
126    }
127
128    #region Content Events
129    private void RegisterDataRowEvents(DataRow row) {
130      row.NameChanged += new EventHandler(Row_NameChanged);
131      valuesRowsTable.Add(row.Values, row);
132      row.Values.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
133      row.Values.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
134      row.Values.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
135      row.Values.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
136      row.Values.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
137    }
138    private void DeregisterDataRowEvents(DataRow row) {
139      row.Values.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
140      row.Values.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
141      row.Values.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
142      row.Values.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
143      row.Values.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
144      valuesRowsTable.Remove(row.Values);
145      row.NameChanged -= new EventHandler(Row_NameChanged);
146    }
147    protected override void Content_NameChanged(object sender, EventArgs e) {
148      if (InvokeRequired)
149        Invoke(new EventHandler(Content_NameChanged), sender, e);
150      else {
151        chart.Titles[0].Text = Content.Name;
152        base.Content_NameChanged(sender, e);
153      }
154    }
155    private void Rows_ItemsAdded(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
156      if (InvokeRequired)
157        Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsAdded), sender, e);
158      else {
159        foreach (DataRow row in e.Items) {
160          AddDataRow(row);
161          RegisterDataRowEvents(row);
162        }
163      }
164    }
165    private void Rows_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
166      if (InvokeRequired)
167        Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsRemoved), sender, e);
168      else {
169        foreach (DataRow row in e.Items) {
170          DeregisterDataRowEvents(row);
171          RemoveDataRow(row);
172        }
173      }
174    }
175    private void Rows_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
176      if (InvokeRequired)
177        Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsReplaced), sender, e);
178      else {
179        foreach (DataRow row in e.OldItems) {
180          DeregisterDataRowEvents(row);
181          RemoveDataRow(row);
182        }
183        foreach (DataRow row in e.Items) {
184          AddDataRow(row);
185          RegisterDataRowEvents(row);
186        }
187      }
188    }
189    private void Rows_CollectionReset(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
190      if (InvokeRequired)
191        Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_CollectionReset), sender, e);
192      else {
193        foreach (DataRow row in e.OldItems) {
194          DeregisterDataRowEvents(row);
195          RemoveDataRow(row);
196        }
197        foreach (DataRow row in e.Items) {
198          AddDataRow(row);
199          RegisterDataRowEvents(row);
200        }
201      }
202    }
203    private void Row_NameChanged(object sender, EventArgs e) {
204      if (InvokeRequired)
205        Invoke(new EventHandler(Row_NameChanged), sender, e);
206      else {
207        DataRow row = (DataRow)sender;
208        chart.Series[row.Name].Name = row.Name;
209      }
210    }
211    private void Values_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
212      if (InvokeRequired)
213        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded), sender, e);
214      else {
215        DataRow row = null;
216        valuesRowsTable.TryGetValue((IObservableList<double>)sender, out row);
217        if (row != null) {
218          foreach (IndexedItem<double> item in e.Items) {
219            var value = item.Value;
220            if (IsInvalidValue(item.Value)) {
221              DataPoint point = new DataPoint();
222              point.IsEmpty = true;
223              chart.Series[row.Name].Points.Insert(item.Index, point);
224            } else {
225              chart.Series[row.Name].Points.InsertY(item.Index, value);
226            }
227          }
228        }
229      }
230    }
231    private void Values_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
232      if (InvokeRequired)
233        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved), sender, e);
234      else {
235        DataRow row = null;
236        valuesRowsTable.TryGetValue((IObservableList<double>)sender, out row);
237        if (row != null) {
238          List<DataPoint> points = new List<DataPoint>();
239          foreach (IndexedItem<double> item in e.Items)
240            points.Add(chart.Series[row.Name].Points[item.Index]);
241          foreach (DataPoint point in points)
242            chart.Series[row.Name].Points.Remove(point);
243        }
244      }
245    }
246    private void Values_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
247      if (InvokeRequired)
248        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced), sender, e);
249      else {
250        DataRow row = null;
251        valuesRowsTable.TryGetValue((IObservableList<double>)sender, out row);
252        if (row != null) {
253          foreach (IndexedItem<double> item in e.Items) {
254            if (IsInvalidValue(item.Value))
255              chart.Series[row.Name].Points[item.Index].IsEmpty = true;
256            else {
257              chart.Series[row.Name].Points[item.Index].YValues = new double[] { item.Value };
258              chart.Series[row.Name].Points[item.Index].IsEmpty = false;
259            }
260          }
261        }
262      }
263    }
264    private void Values_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
265      if (InvokeRequired)
266        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved), sender, e);
267      else {
268        DataRow row = null;
269        valuesRowsTable.TryGetValue((IObservableList<double>)sender, out row);
270        if (row != null) {
271          foreach (IndexedItem<double> item in e.Items) {
272            if (IsInvalidValue(item.Value))
273              chart.Series[row.Name].Points[item.Index].IsEmpty = true;
274            else {
275              chart.Series[row.Name].Points[item.Index].YValues = new double[] { item.Value };
276              chart.Series[row.Name].Points[item.Index].IsEmpty = false;
277            }
278          }
279        }
280      }
281    }
282
283    private void Values_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
284      if (InvokeRequired)
285        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset), sender, e);
286      else {
287        DataRow row = null;
288        valuesRowsTable.TryGetValue((IObservableList<double>)sender, out row);
289        if (row != null) {
290          chart.Series[row.Name].Points.Clear();
291          foreach (IndexedItem<double> item in e.Items) {
292            if (IsInvalidValue(item.Value))
293              chart.Series[row.Name].Points[item.Index].IsEmpty = true;
294            else {
295              chart.Series[row.Name].Points[item.Index].YValues = new double[] { item.Value };
296              chart.Series[row.Name].Points[item.Index].IsEmpty = false;
297            }
298          }
299        }
300      }
301    }
302    #endregion
303
304    private bool IsInvalidValue(double x) {
305      return double.IsNaN(x) || x < (double)decimal.MinValue || x > (double)decimal.MaxValue;
306    }
307  }
308}
Note: See TracBrowser for help on using the repository browser.