#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 { get; private set; } private IEnumerable rowNames; 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 DataGridLogic(ITransactionalPreprocessingData preprocessingData) { PreprocessingData = preprocessingData; createRowNames(); } private void createRowNames() { rowNames = Enumerable.Range(1, Rows).Select(n => n.ToString()); } 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; } } } }