#region License Information /* HeuristicLab * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using HeuristicLab.Data; namespace HeuristicLab.DataPreprocessing { public class DataGridLogic : IDataGridLogic { private ITransactionalPreprocessingData preprocessingData; private IEnumerable rowNames; public DataGridLogic(ITransactionalPreprocessingData preprocessingData) { this.preprocessingData = preprocessingData; createRowNames(); } private void createRowNames() { rowNames = Enumerable.Range(1, Rows).Select(n => n.ToString()); } public int Rows { get { return preprocessingData.Rows; } } public int Columns { get { return preprocessingData.Columns; } } public IEnumerable ColumnNames { get { return preprocessingData.VariableNames; } } public IEnumerable RowNames { get { return rowNames; } } public bool Validate(string value, out string errorMessage, int columnIndex) { if (columnIndex < 0 || columnIndex > preprocessingData.VariableNames.Count()) { throw new ArgumentOutOfRangeException("column index is out of range"); } bool valid = false; errorMessage = string.Empty; if (preprocessingData.IsType(columnIndex)) { double val; valid = double.TryParse(value, out val); if (!valid) { errorMessage = "Invalid Value (Valid Value Format: \"" + FormatPatterns.GetDoubleFormatPattern() + "\")"; } } else if (preprocessingData.IsType(columnIndex)) { valid = value != null; if (!valid) { errorMessage = "Invalid Value (string must not be null)"; } } else if (preprocessingData.IsType(columnIndex)) { DateTime date; valid = DateTime.TryParse(value, out date); if (!valid) { errorMessage = "Invalid Value (Valid Value Format: \"" + CultureInfo.CurrentCulture.DateTimeFormat + "\""; } } else { throw new ArgumentException("column " + columnIndex + " contains a non supported type."); } return valid; } public string GetValue(int columnIndex, int rowIndex) { return preprocessingData.GetCellAsString(columnIndex, rowIndex); } public bool SetValue(string value, int columnIndex, int rowIndex) { bool valid = false; if (preprocessingData.IsType(columnIndex)) { double val; valid = double.TryParse(value, out val); SetValueIfValid(columnIndex, rowIndex, valid, val); } else if (preprocessingData.IsType(columnIndex)) { valid = value != null; SetValueIfValid(columnIndex, rowIndex, valid, value); } else if (preprocessingData.IsType(columnIndex)) { DateTime date; valid = DateTime.TryParse(value, out date); SetValueIfValid(columnIndex, rowIndex, valid, date); } else { throw new ArgumentException("column " + columnIndex + " contains a non supported type."); } return valid; } private void SetValueIfValid(int columnIndex, int rowIndex, bool valid, T value) { if (valid) { preprocessingData.SetCell(columnIndex, rowIndex, value); } } public void DeleteRow(List rows) { preprocessingData.InTransaction(() => { foreach (int rowIndex in rows) { preprocessingData.DeleteRow(rowIndex); } }); createRowNames(); } public event DataPreprocessingChangedEventHandler Changed { add { preprocessingData.Changed += value; } remove { preprocessingData.Changed -= value; } } public bool AreAllStringColumns(IEnumerable columnIndices) { return columnIndices.All(x => preprocessingData.IsType(x)); } public void SetSelection(IDictionary> selection) { preprocessingData.Selection = selection; } public IDictionary> GetSelection() { return preprocessingData.Selection; } public void ClearSelection() { preprocessingData.ClearSelection(); } public event EventHandler SelectionChanged { add { preprocessingData.SelectionChanged += value; } remove { preprocessingData.SelectionChanged -= value; } } } }