[6134] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7267] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6134] | 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 |
|
---|
| 22 | using System;
|
---|
[6133] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Collections;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using System.Text;
|
---|
| 27 | using System.Windows.Forms;
|
---|
| 28 | using System.Xml;
|
---|
| 29 | using HeuristicLab.DataImporter.Data.View;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.DataImporter.Data.Model {
|
---|
| 33 | [StorableClass]
|
---|
| 34 | public class ColumnGroup {
|
---|
| 35 |
|
---|
| 36 | public ColumnGroup()
|
---|
| 37 | : base() {
|
---|
| 38 | this.columns = new List<ColumnBase>();
|
---|
| 39 | this.sortedColumnIndexes = new List<int>();
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public ColumnGroup(string name)
|
---|
| 43 | : this() {
|
---|
| 44 | this.name = name;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | private bool active;
|
---|
| 48 | public bool Active {
|
---|
| 49 | get { return active; }
|
---|
| 50 | set { this.active = value; }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | public IEnumerable<SortOrder> SortOrdersForColumns {
|
---|
| 54 | get { return columns.Select(col => col.SortOrder); }
|
---|
| 55 | set {
|
---|
| 56 | int i = 0;
|
---|
| 57 | foreach (SortOrder order in value) {
|
---|
| 58 | columns[i].SortOrder = order;
|
---|
| 59 | i++;
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | [Storable]
|
---|
| 65 | private ICollection<int> sortedColumnIndexes;
|
---|
| 66 | public ICollection<int> SortedColumnIndexes {
|
---|
| 67 | get { return this.sortedColumnIndexes; }
|
---|
| 68 | set { this.sortedColumnIndexes = value; }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public bool Sorted {
|
---|
| 72 | get { return SortOrdersForColumns.Any(x => x != SortOrder.None); }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | public int SortedColumnsCount {
|
---|
| 76 | get { return SortOrdersForColumns.Count(x => x != SortOrder.None); }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | public void ResetSorting() {
|
---|
| 80 | foreach (ColumnBase column in this.columns)
|
---|
| 81 | column.SortOrder = SortOrder.None;
|
---|
| 82 | sortedColumnIndexes.Clear();
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | public string KeyString(int row) {
|
---|
| 86 | string x = "";
|
---|
| 87 | IComparable val;
|
---|
| 88 | foreach (int colIndex in sortedColumnIndexes) {
|
---|
| 89 | val = columns[colIndex].GetValue(row);
|
---|
| 90 | if (val != null)
|
---|
| 91 | x += val.ToString();
|
---|
| 92 | }
|
---|
| 93 | return x;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | [Storable]
|
---|
| 97 | private string name;
|
---|
| 98 | public string Name {
|
---|
| 99 | get { return this.name; }
|
---|
| 100 | set { this.name = value; }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | [Storable]
|
---|
| 104 | private List<ColumnBase> columns;
|
---|
| 105 | public IEnumerable<ColumnBase> Columns {
|
---|
| 106 | get { return this.columns; }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | public int RowCount {
|
---|
| 110 | get {
|
---|
| 111 | return this.columns.Count == 0 ? 0 :
|
---|
| 112 | this.columns.Max<ColumnBase, int>(x => x.TotalValuesCount);
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | public IEnumerable<ColumnBase> SelectedColumns {
|
---|
| 117 | get { return this.columns.Where<ColumnBase>(col => col.Selected); }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | public int[] SelectedColumnIndexes {
|
---|
| 121 | get {
|
---|
| 122 | List<int> selectedIndexes = new List<int>();
|
---|
| 123 | for (int i = 0; i < this.columns.Count; i++)
|
---|
| 124 | if (columns[i].Selected)
|
---|
| 125 | selectedIndexes.Add(i);
|
---|
| 126 | return selectedIndexes.ToArray();
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | public void ClearSelectedColumns() {
|
---|
| 131 | foreach (ColumnBase col in this.columns)
|
---|
| 132 | col.Selected = false;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | public void SelectAllColumns() {
|
---|
| 136 | foreach (ColumnBase col in this.columns)
|
---|
| 137 | col.Selected = true;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 |
|
---|
| 141 | public void AddColumn(ColumnBase column) {
|
---|
| 142 | column.Changed += ColumnChanged;
|
---|
| 143 | this.columns.Add(column);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | public void AddColumns(IEnumerable<ColumnBase> columns) {
|
---|
| 147 | foreach (ColumnBase col in columns)
|
---|
| 148 | this.AddColumn(col);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | public void InsertColumn(int position, ColumnBase column) {
|
---|
| 152 | column.Changed += ColumnChanged;
|
---|
| 153 | this.columns.Insert(position, column);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | public void RemoveColumn(ColumnBase column) {
|
---|
| 157 | if (sortedColumnIndexes.Contains(IndexOfColumn(column)))
|
---|
| 158 | ResetSorting();
|
---|
| 159 | column.Changed -= ColumnChanged;
|
---|
| 160 | this.columns.Remove(column);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | public void RemoveColumn(int columnIndex) {
|
---|
| 164 | if (sortedColumnIndexes.Contains(columnIndex))
|
---|
| 165 | ResetSorting();
|
---|
| 166 | columns[columnIndex].Changed -= ColumnChanged;
|
---|
| 167 | this.columns.RemoveAt(columnIndex);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | public int IndexOfColumn(ColumnBase column) {
|
---|
| 171 | return this.columns.IndexOf(column);
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | public void ClearColumns() {
|
---|
| 175 | foreach (ColumnBase col in this.columns)
|
---|
| 176 | col.Changed -= ColumnChanged;
|
---|
| 177 | ResetSorting();
|
---|
| 178 | this.columns.Clear();
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | public ColumnBase GetColumn(int position) {
|
---|
| 182 | return this.columns[position];
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | public void ReplaceColumn(int position, ColumnBase column) {
|
---|
| 186 | if (sortedColumnIndexes.Contains(position))
|
---|
| 187 | ResetSorting();
|
---|
| 188 | this.columns[position].Changed -= ColumnChanged;
|
---|
| 189 | column.Changed += ColumnChanged;
|
---|
| 190 | this.columns[position] = column;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | #region methods for row manipulation
|
---|
| 194 | public IComparable[] GetRow(int index) {
|
---|
| 195 | IComparable[] ret = new IComparable[this.columns.Count];
|
---|
| 196 | for (int i = 0; i < this.columns.Count; i++)
|
---|
| 197 | ret[i] = this.columns[i].GetValue(index);
|
---|
| 198 | return ret;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | public IComparable[] GetEmptyRow() {
|
---|
| 202 | IComparable[] ret = new IComparable[this.columns.Count];
|
---|
| 203 | for (int i = 0; i < this.columns.Count; i++)
|
---|
| 204 | ret[i] = null;
|
---|
| 205 | return ret;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | public void AddRow(IComparable[] row) {
|
---|
| 209 | for (int i = 0; i < this.columns.Count; i++)
|
---|
| 210 | this.columns[i].AddValueOrNull(row[i]);
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | public void DeleteRow(int index) {
|
---|
| 214 | for (int i = 0; i < this.columns.Count; i++)
|
---|
| 215 | this.columns[i].RemoveValue(index);
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | public void InsertRow(int index, IComparable[] row) {
|
---|
| 219 | for (int i = 0; i < this.columns.Count; i++)
|
---|
| 220 | this.columns[i].InsertValue(index, row[i]);
|
---|
| 221 | }
|
---|
| 222 | #endregion
|
---|
| 223 |
|
---|
| 224 | public void ColumnChanged(object sender, EventArgs e) {
|
---|
| 225 | this.OnChanged();
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | public override string ToString() {
|
---|
| 229 | return this.name;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | public event EventHandler Changed;
|
---|
| 233 | protected virtual void OnChanged() {
|
---|
| 234 | if (this.Changed != null)
|
---|
| 235 | this.Changed(this, EventArgs.Empty);
|
---|
| 236 | }
|
---|
| 237 | public void FireChanged() {
|
---|
| 238 | this.OnChanged();
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 | }
|
---|