[2908] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 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 {
|
---|
[13656] | 40 | public static new Image StaticItemImage
|
---|
| 41 | {
|
---|
| 42 | get { return new Bitmap(25, 25); }
|
---|
[3306] | 43 | }
|
---|
| 44 |
|
---|
[4870] | 45 | private DataTableVisualProperties visualProperties;
|
---|
[13656] | 46 | public DataTableVisualProperties VisualProperties
|
---|
| 47 | {
|
---|
[4870] | 48 | get { return visualProperties; }
|
---|
[13656] | 49 | set
|
---|
| 50 | {
|
---|
[4870] | 51 | if (visualProperties != value) {
|
---|
| 52 | if (value == null) throw new ArgumentNullException("VisualProperties");
|
---|
| 53 | if (visualProperties != null) visualProperties.PropertyChanged -= new PropertyChangedEventHandler(VisualProperties_PropertyChanged);
|
---|
| 54 | visualProperties = value;
|
---|
| 55 | visualProperties.PropertyChanged += new PropertyChangedEventHandler(VisualProperties_PropertyChanged);
|
---|
| 56 | OnVisualPropertiesChanged();
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[2908] | 61 | private NamedItemCollection<DataRow> rows;
|
---|
[13656] | 62 | public NamedItemCollection<DataRow> Rows
|
---|
| 63 | {
|
---|
[2908] | 64 | get { return rows; }
|
---|
[13656] | 65 | private set
|
---|
| 66 | {
|
---|
[8280] | 67 | if (rows != null) throw new InvalidOperationException("Rows already set");
|
---|
| 68 | rows = value;
|
---|
| 69 | if (rows != null) RegisterRowsEvents();
|
---|
| 70 | }
|
---|
[2908] | 71 | }
|
---|
| 72 |
|
---|
[4870] | 73 | #region Persistence Properties
|
---|
[5261] | 74 | [Storable(Name = "VisualProperties")]
|
---|
[13656] | 75 | private DataTableVisualProperties StorableVisualProperties
|
---|
| 76 | {
|
---|
[5261] | 77 | get { return visualProperties; }
|
---|
[13656] | 78 | set
|
---|
| 79 | {
|
---|
[5261] | 80 | visualProperties = value;
|
---|
| 81 | visualProperties.PropertyChanged += new PropertyChangedEventHandler(VisualProperties_PropertyChanged);
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
[4870] | 84 | [Storable(Name = "rows")]
|
---|
[13656] | 85 | private IEnumerable<DataRow> StorableRows
|
---|
| 86 | {
|
---|
[4870] | 87 | get { return rows; }
|
---|
[8280] | 88 | set { Rows = new NamedItemCollection<DataRow>(value); }
|
---|
[4870] | 89 | }
|
---|
| 90 | #endregion
|
---|
| 91 |
|
---|
[4722] | 92 | [StorableConstructor]
|
---|
[5097] | 93 | protected DataTable(bool deserializing) : base(deserializing) { }
|
---|
| 94 | protected DataTable(DataTable original, Cloner cloner)
|
---|
[4722] | 95 | : base(original, cloner) {
|
---|
[8280] | 96 | VisualProperties = (DataTableVisualProperties)cloner.Clone(original.visualProperties);
|
---|
| 97 | Rows = cloner.Clone(original.rows);
|
---|
[4722] | 98 | }
|
---|
[2908] | 99 | public DataTable()
|
---|
| 100 | : base() {
|
---|
[9816] | 101 | Name = "DataTable";
|
---|
[4870] | 102 | VisualProperties = new DataTableVisualProperties();
|
---|
[8280] | 103 | Rows = new NamedItemCollection<DataRow>();
|
---|
[2908] | 104 | }
|
---|
| 105 | public DataTable(string name)
|
---|
| 106 | : base(name) {
|
---|
[7229] | 107 | VisualProperties = new DataTableVisualProperties(name);
|
---|
[8280] | 108 | Rows = new NamedItemCollection<DataRow>();
|
---|
[2908] | 109 | }
|
---|
| 110 | public DataTable(string name, string description)
|
---|
| 111 | : base(name, description) {
|
---|
[7229] | 112 | VisualProperties = new DataTableVisualProperties(name);
|
---|
[8280] | 113 | Rows = new NamedItemCollection<DataRow>();
|
---|
[2908] | 114 | }
|
---|
| 115 |
|
---|
[4870] | 116 | // BackwardsCompatibility3.3
|
---|
| 117 | #region Backwards compatible code, remove with 3.4
|
---|
| 118 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 119 | private void AfterDeserialization() {
|
---|
[7229] | 120 | if (VisualProperties == null) VisualProperties = new DataTableVisualProperties(name);
|
---|
| 121 | if (VisualProperties.Title == null) VisualProperties.Title = name;
|
---|
[4870] | 122 | }
|
---|
| 123 | #endregion
|
---|
| 124 |
|
---|
| 125 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 126 | return new DataTable(this, cloner);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | public event EventHandler VisualPropertiesChanged;
|
---|
[5097] | 130 | protected virtual void OnVisualPropertiesChanged() {
|
---|
[4870] | 131 | EventHandler handler = VisualPropertiesChanged;
|
---|
| 132 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | private void VisualProperties_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
| 136 | OnVisualPropertiesChanged();
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[5097] | 139 | protected virtual void RegisterRowsEvents() {
|
---|
[3767] | 140 | rows.ItemsAdded += new CollectionItemsChangedEventHandler<DataRow>(rows_ItemsAdded);
|
---|
| 141 | rows.ItemsRemoved += new CollectionItemsChangedEventHandler<DataRow>(rows_ItemsRemoved);
|
---|
| 142 | rows.ItemsReplaced += new CollectionItemsChangedEventHandler<DataRow>(rows_ItemsReplaced);
|
---|
| 143 | rows.CollectionReset += new CollectionItemsChangedEventHandler<DataRow>(rows_CollectionReset);
|
---|
| 144 | }
|
---|
| 145 | private void rows_ItemsAdded(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
| 146 | foreach (DataRow row in e.Items)
|
---|
| 147 | this.RegisterRowEvents(row);
|
---|
[5150] | 148 |
|
---|
| 149 | this.OnColumnsChanged();
|
---|
[3767] | 150 | this.OnColumnNamesChanged();
|
---|
| 151 | this.OnReset();
|
---|
| 152 | }
|
---|
| 153 | private void rows_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
| 154 | foreach (DataRow row in e.Items)
|
---|
| 155 | this.DeregisterRowEvents(row);
|
---|
[5150] | 156 |
|
---|
| 157 | this.OnColumnsChanged();
|
---|
[3767] | 158 | this.OnColumnNamesChanged();
|
---|
| 159 | this.OnReset();
|
---|
| 160 | }
|
---|
| 161 | private void rows_ItemsReplaced(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 | this.OnColumnsChanged();
|
---|
[3767] | 168 | this.OnColumnNamesChanged();
|
---|
| 169 | this.OnReset();
|
---|
| 170 | }
|
---|
| 171 | private void rows_CollectionReset(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
| 172 | foreach (DataRow row in e.OldItems)
|
---|
| 173 | this.DeregisterRowEvents(row);
|
---|
| 174 | foreach (DataRow row in e.Items)
|
---|
| 175 | this.RegisterRowEvents(row);
|
---|
[5150] | 176 |
|
---|
| 177 | if (e.OldItems.Count() != e.Items.Count())
|
---|
| 178 | this.OnColumnsChanged();
|
---|
[3767] | 179 | this.OnColumnNamesChanged();
|
---|
| 180 | this.OnReset();
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[5097] | 183 | protected virtual void RegisterRowEvents(DataRow row) {
|
---|
[3767] | 184 | row.Values.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
|
---|
| 185 | row.Values.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
|
---|
| 186 | row.Values.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
|
---|
| 187 | row.Values.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
|
---|
| 188 | row.Values.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
|
---|
| 189 | }
|
---|
[5097] | 190 | protected virtual void DeregisterRowEvents(DataRow row) {
|
---|
[3767] | 191 | row.Values.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
|
---|
| 192 | row.Values.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
|
---|
| 193 | row.Values.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
|
---|
| 194 | row.Values.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
|
---|
| 195 | row.Values.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | private void Values_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
| 199 | this.OnReset();
|
---|
| 200 | }
|
---|
| 201 | private void Values_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
| 202 | this.OnReset();
|
---|
| 203 | }
|
---|
| 204 | private void Values_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
| 205 | this.OnReset();
|
---|
| 206 | }
|
---|
| 207 | private void Values_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
| 208 | this.OnReset();
|
---|
| 209 | }
|
---|
| 210 | private void Values_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
| 211 | this.OnReset();
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | #region IStringConvertibleMatrix Members
|
---|
| 215 |
|
---|
[13656] | 216 | int IStringConvertibleMatrix.Rows
|
---|
| 217 | {
|
---|
[4774] | 218 | get { return rows.Count == 0 ? 0 : rows.Max(r => r.Values.Count); }
|
---|
[3767] | 219 | set { throw new NotSupportedException(); }
|
---|
| 220 | }
|
---|
[13656] | 221 | int IStringConvertibleMatrix.Columns
|
---|
| 222 | {
|
---|
[3767] | 223 | get { return rows.Count; }
|
---|
| 224 | set { throw new NotSupportedException(); }
|
---|
| 225 | }
|
---|
[13656] | 226 | IEnumerable<string> IStringConvertibleMatrix.ColumnNames
|
---|
| 227 | {
|
---|
[3767] | 228 | get { return rows.Select(r => r.Name); }
|
---|
| 229 | set { throw new NotSupportedException(); }
|
---|
| 230 | }
|
---|
[13656] | 231 | IEnumerable<string> IStringConvertibleMatrix.RowNames
|
---|
| 232 | {
|
---|
[8280] | 233 | get { return Enumerable.Empty<string>(); }
|
---|
[3767] | 234 | set { throw new NotSupportedException(); }
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[13656] | 237 | bool IStringConvertibleMatrix.SortableView
|
---|
| 238 | {
|
---|
[3767] | 239 | get { return true; }
|
---|
| 240 | set { throw new NotSupportedException(); }
|
---|
| 241 | }
|
---|
[13656] | 242 | bool IStringConvertibleMatrix.ReadOnly
|
---|
| 243 | {
|
---|
[3767] | 244 | get { return true; }
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | string IStringConvertibleMatrix.GetValue(int rowIndex, int columnIndex) {
|
---|
| 248 | if (columnIndex < rows.Count) {
|
---|
| 249 | string columnName = ((IStringConvertibleMatrix)this).ColumnNames.ElementAt(columnIndex);
|
---|
| 250 | if (rows.ContainsKey(columnName) && rowIndex < rows[columnName].Values.Count)
|
---|
| 251 | return rows[columnName].Values[rowIndex].ToString();
|
---|
| 252 | }
|
---|
| 253 | return string.Empty;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
|
---|
| 257 | throw new NotSupportedException();
|
---|
| 258 | }
|
---|
| 259 | bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
| 260 | throw new NotSupportedException();
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | public event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
[5097] | 264 | protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
[4763] | 265 | var handler = ItemChanged;
|
---|
| 266 | if (handler != null) handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
[3767] | 267 | OnToStringChanged();
|
---|
| 268 | }
|
---|
| 269 | public event EventHandler Reset;
|
---|
[5097] | 270 | protected virtual void OnReset() {
|
---|
[4763] | 271 | var handler = Reset;
|
---|
| 272 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3767] | 273 | }
|
---|
[5150] | 274 | public event EventHandler ColumnsChanged;
|
---|
| 275 | protected virtual void OnColumnsChanged() {
|
---|
| 276 | var handler = ColumnsChanged;
|
---|
| 277 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 278 | }
|
---|
| 279 | public event EventHandler RowsChanged;
|
---|
| 280 | protected virtual void OnRowsChanged() {
|
---|
| 281 | var handler = RowsChanged;
|
---|
| 282 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 283 | }
|
---|
[3767] | 284 | public event EventHandler ColumnNamesChanged;
|
---|
[5097] | 285 | protected virtual void OnColumnNamesChanged() {
|
---|
[4763] | 286 | var handler = ColumnNamesChanged;
|
---|
| 287 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3767] | 288 | }
|
---|
| 289 | public event EventHandler RowNamesChanged;
|
---|
[5097] | 290 | protected virtual void OnRowNamesChanged() {
|
---|
[4763] | 291 | var handler = RowNamesChanged;
|
---|
| 292 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3767] | 293 | }
|
---|
| 294 | public event EventHandler SortableViewChanged;
|
---|
[5097] | 295 | protected virtual void OnSortableViewChanged() {
|
---|
[4763] | 296 | var handler = SortableViewChanged;
|
---|
| 297 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3767] | 298 | }
|
---|
| 299 | #endregion
|
---|
[2908] | 300 | }
|
---|
| 301 | }
|
---|