Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Data/Model/ColumnGroup.cs @ 16567

Last change on this file since 16567 was 16567, checked in by gkronber, 5 years ago

#2520: changed StorableConstructors and added StorableType attributes in HeuristicLab.DataImporter addon

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