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