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, 10 years ago

Refactoring

File size: 5.3 KB
RevLine 
[10539]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;
[10239]23using System.Collections.Generic;
[10246]24using System.Globalization;
[10239]25using System.Linq;
[10246]26using HeuristicLab.Data;
[10239]27
28namespace HeuristicLab.DataPreprocessing {
29  public class DataGridLogic : IDataGridLogic {
30
[10586]31    private ITransactionalPreprocessingData preprocessingData;
[10660]32    private IEnumerable<string> rowNames;
[10239]33
[10586]34    public DataGridLogic(ITransactionalPreprocessingData preprocessingData) {
[10239]35      this.preprocessingData = preprocessingData;
[10660]36      createRowNames();
[10239]37    }
38
[10660]39    private void createRowNames() {
40      rowNames = Enumerable.Range(1, Rows).Select(n => n.ToString());
41    }
42
[10246]43    public int Rows {
44      get {
45        return preprocessingData.Rows;
46      }
[10239]47    }
48
[10246]49    public int Columns {
50      get {
51        return preprocessingData.Columns;
52      }
53    }
[10239]54
[10246]55    public IEnumerable<string> ColumnNames {
56      get {
57        return preprocessingData.VariableNames;
58      }
59    }
[10239]60
[10246]61    public IEnumerable<string> RowNames {
62      get {
[10660]63        return rowNames;
[10246]64      }
65    }
[10239]66
[10246]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");
[10239]70      }
[10900]71
[10246]72      bool valid = false;
[10900]73      errorMessage = string.Empty;
[10367]74      if (preprocessingData.IsType<double>(columnIndex)) {
[10246]75        double val;
76        valid = double.TryParse(value, out val);
77        if (!valid) {
78          errorMessage = "Invalid Value (Valid Value Format: \"" + FormatPatterns.GetDoubleFormatPattern() + "\")";
79        }
[10367]80      } else if (preprocessingData.IsType<string>(columnIndex)) {
[10246]81        valid = value != null;
82        if (!valid) {
83          errorMessage = "Invalid Value (string must not be null)";
84        }
[10367]85      } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
[10246]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 {
[10367]92        throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
[10246]93      }
[10239]94
[10246]95      return valid;
96    }
[10239]97
[10616]98    public string GetValue(int columnIndex, int rowIndex) {
[10367]99      return preprocessingData.GetCellAsString(columnIndex, rowIndex);
[10246]100    }
101
[10616]102    public bool SetValue(string value, int columnIndex, int rowIndex) {
[10246]103      bool valid = false;
[10367]104      if (preprocessingData.IsType<double>(columnIndex)) {
[10246]105        double val;
106        valid = double.TryParse(value, out val);
[10634]107        SetValueIfValid(columnIndex, rowIndex, valid, val);
[10367]108      } else if (preprocessingData.IsType<string>(columnIndex)) {
[10246]109        valid = value != null;
[10616]110        SetValueIfValid(columnIndex, rowIndex, valid, value);
[10367]111      } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
[10246]112        DateTime date;
113        valid = DateTime.TryParse(value, out date);
[10634]114        SetValueIfValid(columnIndex, rowIndex, valid, date);
[10246]115      } else {
[10367]116        throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
[10239]117      }
[10246]118      return valid;
[10239]119    }
[10571]120
[10616]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
[10622]127    public void DeleteRow(List<int> rows) {
128      preprocessingData.InTransaction(() => {
129        foreach (int rowIndex in rows) {
130          preprocessingData.DeleteRow(rowIndex);
131        }
132      });
[10660]133      createRowNames();
[10622]134    }
135
[10571]136    public event DataPreprocessingChangedEventHandler Changed {
137      add { preprocessingData.Changed += value; }
138      remove { preprocessingData.Changed -= value; }
139    }
[10621]140
141    public bool AreAllStringColumns(IEnumerable<int> columnIndices) {
142      return columnIndices.All(x => preprocessingData.IsType<string>(x));
143    }
[10804]144
145
146    public void SetSelection(IDictionary<int, IList<int>> selection) {
[10978]147      preprocessingData.Selection = selection;
[10804]148    }
149
150    public IDictionary<int, IList<int>> GetSelection() {
[10978]151      return preprocessingData.Selection;
[10804]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
[10239]162  }
163}
Note: See TracBrowser for help on using the repository browser.