Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/DataGridLogic.cs @ 10804

Last change on this file since 10804 was 10804, checked in by rstoll, 10 years ago
  • SelectionEvent added
File size: 5.4 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.Globalization;
25using System.Linq;
26using HeuristicLab.Data;
27
28namespace HeuristicLab.DataPreprocessing {
29  public class DataGridLogic : IDataGridLogic {
30
31    private ITransactionalPreprocessingData preprocessingData;
32    private IEnumerable<string> rowNames;
33
34    public DataGridLogic(ITransactionalPreprocessingData preprocessingData) {
35      this.preprocessingData = preprocessingData;
36      createRowNames();
37    }
38
39    private void createRowNames() {
40      rowNames = Enumerable.Range(1, Rows).Select(n => n.ToString());
41    }
42
43    public int Rows {
44      get {
45        return preprocessingData.Rows;
46      }
47    }
48
49    public int Columns {
50      get {
51        return preprocessingData.Columns;
52      }
53    }
54
55    public IEnumerable<string> ColumnNames {
56      get {
57        return preprocessingData.VariableNames;
58      }
59    }
60
61    public IEnumerable<string> RowNames {
62      get {
63        return rowNames;
64      }
65    }
66
67    public bool Validate(string value, out string errorMessage, int columnIndex) {
68      if (columnIndex < 0 || columnIndex > preprocessingData.VariableNames.Count()) {
69        throw new ArgumentOutOfRangeException("column index is out of range");
70      }
71      bool valid = false;
72      if (preprocessingData.IsType<double>(columnIndex)) {
73        double val;
74        valid = double.TryParse(value, out val);
75        errorMessage = string.Empty;
76        if (!valid) {
77          errorMessage = "Invalid Value (Valid Value Format: \"" + FormatPatterns.GetDoubleFormatPattern() + "\")";
78        }
79      } else if (preprocessingData.IsType<string>(columnIndex)) {
80        valid = value != null;
81        errorMessage = string.Empty;
82        if (!valid) {
83          errorMessage = "Invalid Value (string must not be null)";
84        }
85      } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
86        DateTime date;
87        valid = DateTime.TryParse(value, out date);
88        errorMessage = string.Empty;
89        if (!valid) {
90          errorMessage = "Invalid Value (Valid Value Format: \"" + CultureInfo.CurrentCulture.DateTimeFormat + "\"";
91        }
92      } else {
93        throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
94      }
95
96      return valid;
97    }
98
99    public string GetValue(int columnIndex, int rowIndex) {
100      return preprocessingData.GetCellAsString(columnIndex, rowIndex);
101    }
102
103    public bool SetValue(string value, int columnIndex, int rowIndex) {
104      bool valid = false;
105      if (preprocessingData.IsType<double>(columnIndex)) {
106        double val;
107        valid = double.TryParse(value, out val);
108        SetValueIfValid(columnIndex, rowIndex, valid, val);
109      } else if (preprocessingData.IsType<string>(columnIndex)) {
110        valid = value != null;
111        SetValueIfValid(columnIndex, rowIndex, valid, value);
112      } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
113        DateTime date;
114        valid = DateTime.TryParse(value, out date);
115        SetValueIfValid(columnIndex, rowIndex, valid, date);
116      } else {
117        throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
118      }
119      return valid;
120    }
121
122    private void SetValueIfValid<T>(int columnIndex, int rowIndex, bool valid, T value) {
123      if (valid) {
124        preprocessingData.SetCell<T>(columnIndex, rowIndex, value);
125      }
126    }
127
128    public void DeleteRow(List<int> rows) {
129      preprocessingData.InTransaction(() => {
130        foreach (int rowIndex in rows) {
131          preprocessingData.DeleteRow(rowIndex);
132        }
133      });
134      createRowNames();
135    }
136
137    public event DataPreprocessingChangedEventHandler Changed {
138      add { preprocessingData.Changed += value; }
139      remove { preprocessingData.Changed -= value; }
140    }
141
142    public bool AreAllStringColumns(IEnumerable<int> columnIndices) {
143      return columnIndices.All(x => preprocessingData.IsType<string>(x));
144    }
145
146
147
148    public void SetSelection(IDictionary<int, IList<int>> selection) {
149      preprocessingData.SetSelection(selection);
150    }
151
152    public IDictionary<int, IList<int>> GetSelection() {
153      return preprocessingData.GetSelection();
154    }
155    public void ClearSelection() {
156      preprocessingData.ClearSelection();
157    }
158
159    public event EventHandler SelectionChanged {
160      add { preprocessingData.SelectionChanged += value; }
161      remove { preprocessingData.SelectionChanged -= value; }
162    }
163
164  }
165}
Note: See TracBrowser for help on using the repository browser.