Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10313 was 10248, checked in by rstoll, 10 years ago

Deleted unnecessary event handlers in DataGridLogic

File size: 3.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.Linq;
5using HeuristicLab.Data;
6
7namespace HeuristicLab.DataPreprocessing {
8  public class DataGridLogic : IDataGridLogic {
9
10    private IPreprocessingData preprocessingData;
11
12    public DataGridLogic(IPreprocessingData preprocessingData) {
13      this.preprocessingData = preprocessingData;
14    }
15
16    public int Rows {
17      get {
18        return preprocessingData.Rows;
19      }
20    }
21
22    public int Columns {
23      get {
24        return preprocessingData.Columns;
25      }
26    }
27
28    public IEnumerable<string> ColumnNames {
29      get {
30        return preprocessingData.VariableNames;
31      }
32    }
33
34    public IEnumerable<string> RowNames {
35      get {
36        return Enumerable.Range(1, Rows).Select(n => n.ToString());
37      }
38    }
39
40    public bool Validate(string value, out string errorMessage, int columnIndex) {
41      if (columnIndex < 0 || columnIndex > preprocessingData.VariableNames.Count()) {
42        throw new ArgumentOutOfRangeException("column index is out of range");
43      }
44      bool valid = false;
45      string variableName = preprocessingData.GetVariableName(columnIndex);
46      if (preprocessingData.IsType<double>(variableName)) {
47        double val;
48        valid = double.TryParse(value, out val);
49        errorMessage = string.Empty;
50        if (!valid) {
51          errorMessage = "Invalid Value (Valid Value Format: \"" + FormatPatterns.GetDoubleFormatPattern() + "\")";
52        }
53      } else if (preprocessingData.IsType<string>(variableName)) {
54        valid = value != null;
55        errorMessage = string.Empty;
56        if (!valid) {
57          errorMessage = "Invalid Value (string must not be null)";
58        }
59      } else if (preprocessingData.IsType<DateTime>(variableName)) {
60        DateTime date;
61        valid = DateTime.TryParse(value, out date);
62        errorMessage = string.Empty;
63        if (!valid) {
64          errorMessage = "Invalid Value (Valid Value Format: \"" + CultureInfo.CurrentCulture.DateTimeFormat + "\"";
65        }
66      } else {
67        throw new ArgumentException("column with variableName: " + variableName + " contains a non supported type.");
68      }
69
70      return valid;
71    }
72
73    public string GetValue(int rowIndex, int columnIndex) {
74      return preprocessingData.GetCellAsString(preprocessingData.GetVariableName(columnIndex), rowIndex);
75    }
76
77    public bool SetValue(string value, int rowIndex, int columnIndex) {
78      string variableName = preprocessingData.GetVariableName(columnIndex);
79      bool valid = false;
80      if (preprocessingData.IsType<double>(variableName)) {
81        double val;
82        valid = double.TryParse(value, out val);
83        if (valid) {
84          preprocessingData.SetCell<double>(variableName, rowIndex, val);
85        }
86      } else if (preprocessingData.IsType<string>(variableName)) {
87        valid = value != null;
88        if (valid) {
89          preprocessingData.SetCell<string>(variableName, rowIndex, value);
90        }
91      } else if (preprocessingData.IsType<DateTime>(variableName)) {
92        DateTime date;
93        valid = DateTime.TryParse(value, out date);
94        if (valid) {
95          preprocessingData.SetCell<DateTime>(variableName, rowIndex, date);
96        }
97      } else {
98        throw new ArgumentException("column with variableName: " + variableName + " contains a non supported type.");
99      }
100
101      return valid;
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.