Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.4/Implementations/DataGridLogic.cs @ 10978

Last change on this file since 10978 was 10978, checked in by mleitner, 8 years ago

Refactoring

File size: 5.3 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
72      bool valid = false;
73      errorMessage = string.Empty;
74      if (preprocessingData.IsType<double>(columnIndex)) {
75        double val;
76        valid = double.TryParse(value, out val);
77        if (!valid) {
78          errorMessage = "Invalid Value (Valid Value Format: \"" + FormatPatterns.GetDoubleFormatPattern() + "\")";
79        }
80      } else if (preprocessingData.IsType<string>(columnIndex)) {
81        valid = value != null;
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        if (!valid) {
89          errorMessage = "Invalid Value (Valid Value Format: \"" + CultureInfo.CurrentCulture.DateTimeFormat + "\"";
90        }
91      } else {
92        throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
93      }
94
95      return valid;
96    }
97
98    public string GetValue(int columnIndex, int rowIndex) {
99      return preprocessingData.GetCellAsString(columnIndex, rowIndex);
100    }
101
102    public bool SetValue(string value, int columnIndex, int rowIndex) {
103      bool valid = false;
104      if (preprocessingData.IsType<double>(columnIndex)) {
105        double val;
106        valid = double.TryParse(value, out val);
107        SetValueIfValid(columnIndex, rowIndex, valid, val);
108      } else if (preprocessingData.IsType<string>(columnIndex)) {
109        valid = value != null;
110        SetValueIfValid(columnIndex, rowIndex, valid, value);
111      } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
112        DateTime date;
113        valid = DateTime.TryParse(value, out date);
114        SetValueIfValid(columnIndex, rowIndex, valid, date);
115      } else {
116        throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
117      }
118      return valid;
119    }
120
121    private void SetValueIfValid<T>(int columnIndex, int rowIndex, bool valid, T value) {
122      if (valid) {
123        preprocessingData.SetCell<T>(columnIndex, rowIndex, value);
124      }
125    }
126
127    public void DeleteRow(List<int> rows) {
128      preprocessingData.InTransaction(() => {
129        foreach (int rowIndex in rows) {
130          preprocessingData.DeleteRow(rowIndex);
131        }
132      });
133      createRowNames();
134    }
135
136    public event DataPreprocessingChangedEventHandler Changed {
137      add { preprocessingData.Changed += value; }
138      remove { preprocessingData.Changed -= value; }
139    }
140
141    public bool AreAllStringColumns(IEnumerable<int> columnIndices) {
142      return columnIndices.All(x => preprocessingData.IsType<string>(x));
143    }
144
145
146    public void SetSelection(IDictionary<int, IList<int>> selection) {
147      preprocessingData.Selection = selection;
148    }
149
150    public IDictionary<int, IList<int>> GetSelection() {
151      return preprocessingData.Selection;
152    }
153    public void ClearSelection() {
154      preprocessingData.ClearSelection();
155    }
156
157    public event EventHandler SelectionChanged {
158      add { preprocessingData.SelectionChanged += value; }
159      remove { preprocessingData.SelectionChanged -= value; }
160    }
161
162  }
163}
Note: See TracBrowser for help on using the repository browser.