[2908] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7268] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2908] | 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 |
|
---|
[4068] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[4870] | 24 | using System.ComponentModel;
|
---|
[3306] | 25 | using System.Drawing;
|
---|
[3767] | 26 | using System.Linq;
|
---|
[4068] | 27 | using HeuristicLab.Collections;
|
---|
[3376] | 28 | using HeuristicLab.Common;
|
---|
[2908] | 29 | using HeuristicLab.Core;
|
---|
[4068] | 30 | using HeuristicLab.Data;
|
---|
[2908] | 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Analysis {
|
---|
| 34 | /// <summary>
|
---|
| 35 | /// A table of data values.
|
---|
| 36 | /// </summary>
|
---|
| 37 | [Item("DataTable", "A table of data values.")]
|
---|
[3017] | 38 | [StorableClass]
|
---|
[5097] | 39 | public class DataTable : NamedItem, IStringConvertibleMatrix {
|
---|
[7213] | 40 | public static new Image StaticItemImage {
|
---|
[5287] | 41 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Performance; }
|
---|
[3306] | 42 | }
|
---|
| 43 |
|
---|
[4870] | 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 |
|
---|
[2908] | 58 | private NamedItemCollection<DataRow> rows;
|
---|
| 59 | public NamedItemCollection<DataRow> Rows {
|
---|
| 60 | get { return rows; }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[4870] | 63 | #region Persistence Properties
|
---|
[5261] | 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 | }
|
---|
[4870] | 72 | [Storable(Name = "rows")]
|
---|
| 73 | private IEnumerable<DataRow> StorableRows {
|
---|
| 74 | get { return rows; }
|
---|
| 75 | set { rows = new NamedItemCollection<DataRow>(value); }
|
---|
| 76 | }
|
---|
| 77 | #endregion
|
---|
| 78 |
|
---|
[4722] | 79 | [StorableConstructor]
|
---|
[5097] | 80 | protected DataTable(bool deserializing) : base(deserializing) { }
|
---|
| 81 | protected DataTable(DataTable original, Cloner cloner)
|
---|
[4722] | 82 | : base(original, cloner) {
|
---|
[4870] | 83 | this.VisualProperties = (DataTableVisualProperties)cloner.Clone(original.visualProperties);
|
---|
[4722] | 84 | this.rows = cloner.Clone(original.rows);
|
---|
| 85 | this.RegisterRowsEvents();
|
---|
| 86 | }
|
---|
[2908] | 87 | public DataTable()
|
---|
| 88 | : base() {
|
---|
[4870] | 89 | VisualProperties = new DataTableVisualProperties();
|
---|
[2908] | 90 | rows = new NamedItemCollection<DataRow>();
|
---|
[3767] | 91 | this.RegisterRowsEvents();
|
---|
[2908] | 92 | }
|
---|
| 93 | public DataTable(string name)
|
---|
| 94 | : base(name) {
|
---|
[7268] | 95 | VisualProperties = new DataTableVisualProperties(name);
|
---|
[2908] | 96 | rows = new NamedItemCollection<DataRow>();
|
---|
[3767] | 97 | this.RegisterRowsEvents();
|
---|
[2908] | 98 | }
|
---|
| 99 | public DataTable(string name, string description)
|
---|
| 100 | : base(name, description) {
|
---|
[7268] | 101 | VisualProperties = new DataTableVisualProperties(name);
|
---|
[2908] | 102 | rows = new NamedItemCollection<DataRow>();
|
---|
[3767] | 103 | this.RegisterRowsEvents();
|
---|
[2908] | 104 | }
|
---|
| 105 |
|
---|
[4870] | 106 | // BackwardsCompatibility3.3
|
---|
| 107 | #region Backwards compatible code, remove with 3.4
|
---|
| 108 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 109 | private void AfterDeserialization() {
|
---|
[7268] | 110 | if (VisualProperties == null) VisualProperties = new DataTableVisualProperties(name);
|
---|
| 111 | if (VisualProperties.Title == null) VisualProperties.Title = name;
|
---|
[4870] | 112 | }
|
---|
| 113 | #endregion
|
---|
| 114 |
|
---|
| 115 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 116 | return new DataTable(this, cloner);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | public event EventHandler VisualPropertiesChanged;
|
---|
[5097] | 120 | protected virtual void OnVisualPropertiesChanged() {
|
---|
[4870] | 121 | EventHandler handler = VisualPropertiesChanged;
|
---|
| 122 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | private void VisualProperties_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
| 126 | OnVisualPropertiesChanged();
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[5097] | 129 | protected virtual void RegisterRowsEvents() {
|
---|
[3767] | 130 | rows.ItemsAdded += new CollectionItemsChangedEventHandler<DataRow>(rows_ItemsAdded);
|
---|
| 131 | rows.ItemsRemoved += new CollectionItemsChangedEventHandler<DataRow>(rows_ItemsRemoved);
|
---|
| 132 | rows.ItemsReplaced += new CollectionItemsChangedEventHandler<DataRow>(rows_ItemsReplaced);
|
---|
| 133 | rows.CollectionReset += new CollectionItemsChangedEventHandler<DataRow>(rows_CollectionReset);
|
---|
| 134 | }
|
---|
| 135 | private void rows_ItemsAdded(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
| 136 | foreach (DataRow row in e.Items)
|
---|
| 137 | this.RegisterRowEvents(row);
|
---|
[5150] | 138 |
|
---|
| 139 | this.OnColumnsChanged();
|
---|
[3767] | 140 | this.OnColumnNamesChanged();
|
---|
| 141 | this.OnReset();
|
---|
| 142 | }
|
---|
| 143 | private void rows_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
| 144 | foreach (DataRow row in e.Items)
|
---|
| 145 | this.DeregisterRowEvents(row);
|
---|
[5150] | 146 |
|
---|
| 147 | this.OnColumnsChanged();
|
---|
[3767] | 148 | this.OnColumnNamesChanged();
|
---|
| 149 | this.OnReset();
|
---|
| 150 | }
|
---|
| 151 | private void rows_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
| 152 | foreach (DataRow row in e.OldItems)
|
---|
| 153 | this.DeregisterRowEvents(row);
|
---|
| 154 | foreach (DataRow row in e.Items)
|
---|
| 155 | this.RegisterRowEvents(row);
|
---|
[5150] | 156 |
|
---|
| 157 | this.OnColumnsChanged();
|
---|
[3767] | 158 | this.OnColumnNamesChanged();
|
---|
| 159 | this.OnReset();
|
---|
| 160 | }
|
---|
| 161 | private void rows_CollectionReset(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);
|
---|
[5150] | 166 |
|
---|
| 167 | if (e.OldItems.Count() != e.Items.Count())
|
---|
| 168 | this.OnColumnsChanged();
|
---|
[3767] | 169 | this.OnColumnNamesChanged();
|
---|
| 170 | this.OnReset();
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[5097] | 173 | protected virtual void RegisterRowEvents(DataRow row) {
|
---|
[3767] | 174 | row.Values.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
|
---|
| 175 | row.Values.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
|
---|
| 176 | row.Values.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
|
---|
| 177 | row.Values.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
|
---|
| 178 | row.Values.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
|
---|
| 179 | }
|
---|
[5097] | 180 | protected virtual void DeregisterRowEvents(DataRow row) {
|
---|
[3767] | 181 | row.Values.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
|
---|
| 182 | row.Values.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
|
---|
| 183 | row.Values.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
|
---|
| 184 | row.Values.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
|
---|
| 185 | row.Values.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | private void Values_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
| 189 | this.OnReset();
|
---|
| 190 | }
|
---|
| 191 | private void Values_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
| 192 | this.OnReset();
|
---|
| 193 | }
|
---|
| 194 | private void Values_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
| 195 | this.OnReset();
|
---|
| 196 | }
|
---|
| 197 | private void Values_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
| 198 | this.OnReset();
|
---|
| 199 | }
|
---|
| 200 | private void Values_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
| 201 | this.OnReset();
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | #region IStringConvertibleMatrix Members
|
---|
| 205 |
|
---|
| 206 | int IStringConvertibleMatrix.Rows {
|
---|
[4774] | 207 | get { return rows.Count == 0 ? 0 : rows.Max(r => r.Values.Count); }
|
---|
[3767] | 208 | set { throw new NotSupportedException(); }
|
---|
| 209 | }
|
---|
| 210 | int IStringConvertibleMatrix.Columns {
|
---|
| 211 | get { return rows.Count; }
|
---|
| 212 | set { throw new NotSupportedException(); }
|
---|
| 213 | }
|
---|
| 214 | IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
|
---|
| 215 | get { return rows.Select(r => r.Name); }
|
---|
| 216 | set { throw new NotSupportedException(); }
|
---|
| 217 | }
|
---|
| 218 | IEnumerable<string> IStringConvertibleMatrix.RowNames {
|
---|
| 219 | get { return new List<string>(); }
|
---|
| 220 | set { throw new NotSupportedException(); }
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | bool IStringConvertibleMatrix.SortableView {
|
---|
| 224 | get { return true; }
|
---|
| 225 | set { throw new NotSupportedException(); }
|
---|
| 226 | }
|
---|
| 227 | bool IStringConvertibleMatrix.ReadOnly {
|
---|
| 228 | get { return true; }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | string IStringConvertibleMatrix.GetValue(int rowIndex, int columnIndex) {
|
---|
| 232 | if (columnIndex < rows.Count) {
|
---|
| 233 | string columnName = ((IStringConvertibleMatrix)this).ColumnNames.ElementAt(columnIndex);
|
---|
| 234 | if (rows.ContainsKey(columnName) && rowIndex < rows[columnName].Values.Count)
|
---|
| 235 | return rows[columnName].Values[rowIndex].ToString();
|
---|
| 236 | }
|
---|
| 237 | return string.Empty;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
|
---|
| 241 | throw new NotSupportedException();
|
---|
| 242 | }
|
---|
| 243 | bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
| 244 | throw new NotSupportedException();
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | public event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
[5097] | 248 | protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
[4763] | 249 | var handler = ItemChanged;
|
---|
| 250 | if (handler != null) handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
[3767] | 251 | OnToStringChanged();
|
---|
| 252 | }
|
---|
| 253 | public event EventHandler Reset;
|
---|
[5097] | 254 | protected virtual void OnReset() {
|
---|
[4763] | 255 | var handler = Reset;
|
---|
| 256 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3767] | 257 | }
|
---|
[5150] | 258 | public event EventHandler ColumnsChanged;
|
---|
| 259 | protected virtual void OnColumnsChanged() {
|
---|
| 260 | var handler = ColumnsChanged;
|
---|
| 261 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 262 | }
|
---|
| 263 | public event EventHandler RowsChanged;
|
---|
| 264 | protected virtual void OnRowsChanged() {
|
---|
| 265 | var handler = RowsChanged;
|
---|
| 266 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 267 | }
|
---|
[3767] | 268 | public event EventHandler ColumnNamesChanged;
|
---|
[5097] | 269 | protected virtual void OnColumnNamesChanged() {
|
---|
[4763] | 270 | var handler = ColumnNamesChanged;
|
---|
| 271 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3767] | 272 | }
|
---|
| 273 | public event EventHandler RowNamesChanged;
|
---|
[5097] | 274 | protected virtual void OnRowNamesChanged() {
|
---|
[4763] | 275 | var handler = RowNamesChanged;
|
---|
| 276 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3767] | 277 | }
|
---|
| 278 | public event EventHandler SortableViewChanged;
|
---|
[5097] | 279 | protected virtual void OnSortableViewChanged() {
|
---|
[4763] | 280 | var handler = SortableViewChanged;
|
---|
| 281 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3767] | 282 | }
|
---|
| 283 | #endregion
|
---|
[2908] | 284 | }
|
---|
| 285 | }
|
---|