Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Analysis/3.3/DataVisualization/DataTable.cs @ 7221

Last change on this file since 7221 was 7221, checked in by ascheibe, 13 years ago

#1611

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