Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Data/Model/ColumnGroup.cs @ 14906

Last change on this file since 14906 was 9615, checked in by mkommend, 12 years ago

#1734: Updated copyright information in all DataImporter classes.

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