using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using HeuristicLab.Data; using HeuristicLab.Core; using HeuristicLab.Common; using System.Drawing; namespace HeuristicLab.DataPreprocessing { [Item("DataGridContent", "Represents a data grid.")] public class DataGridContent : Item,IDataGridContent,IContent { private readonly IPreprocessingData preprocessingData; public DataGridContent(IPreprocessingData thePreprocessingData) { preprocessingData = thePreprocessingData; } public DataGridContent(DataGridContent dataGridContent, Cloner cloner) : base(dataGridContent, cloner) { } public IDataGridLogic DataGridLogic { get; set; } public static new Image StaticItemImage { get { return HeuristicLab.Common.Resources.VSImageLibrary.Table; } } public override IDeepCloneable Clone(Cloner cloner) { return new DataGridContent(this, cloner); } #region IStringConvertibleMatrix Members public int Rows { get { return preprocessingData.Rows; } set { throw new NotImplementedException(); } } public int Columns { get { return preprocessingData.Columns; } set { throw new NotImplementedException(); } } public IEnumerable ColumnNames { get { return preprocessingData.VariableNames; } set { throw new NotImplementedException(); } } public IEnumerable RowNames { get { return Enumerable.Range(1, Rows).Select(n => n.ToString()); } set { throw new NotImplementedException(); } } public bool SortableView { get { return true; } set { throw new NotImplementedException(); } } public bool ReadOnly { get { return false; } } public bool Validate(string value, out string errorMessage) { errorMessage = string.Empty; return true; } 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; string variableName = preprocessingData.VariableNames[columnIndex]; if (preprocessingData.IsType(variableName)) { double val; valid = double.TryParse(value, out val); errorMessage = string.Empty; if (!valid) { errorMessage = "Invalid Value (Valid Value Format: \"" + FormatPatterns.GetDoubleFormatPattern() + "\")"; } } else if (preprocessingData.IsType(variableName)) { valid = value != null; errorMessage = string.Empty; if (!valid) { errorMessage = "Invalid Value (string must not be null)"; } } else if (preprocessingData.IsType(variableName)) { DateTime date; valid = DateTime.TryParse(value, out date); errorMessage = string.Empty; if (!valid) { errorMessage = "Invalid Value (Valid Value Format: \"" + CultureInfo.CurrentCulture.DateTimeFormat + "\""; } } else { throw new ArgumentException("column with variableName: " + variableName + " contains a non supported type."); } return valid; } public string GetValue(int rowIndex, int columnIndex) { return preprocessingData.GetCellAsString(preprocessingData.VariableNames[columnIndex], rowIndex); } public bool SetValue(string value, int rowIndex, int columnIndex) { string variableName = preprocessingData.VariableNames[columnIndex]; bool valid = false; if (preprocessingData.IsType(variableName)) { double val; valid = double.TryParse(value, out val); if (valid) { preprocessingData.SetCell(variableName, rowIndex, val); } } else if (preprocessingData.IsType(variableName)) { valid = value != null; if (valid) { preprocessingData.SetCell(variableName, rowIndex, value); } } else if (preprocessingData.IsType(variableName)) { DateTime date; valid = DateTime.TryParse(value, out date); if (valid) { preprocessingData.SetCell(variableName, rowIndex, date); } } else { throw new ArgumentException("column with variableName: " + variableName + " contains a non supported type."); } return valid; } public event EventHandler ColumnsChanged; public event EventHandler RowsChanged; public event EventHandler ColumnNamesChanged; public event EventHandler RowNamesChanged; public event EventHandler SortableViewChanged; public event EventHandler> ItemChanged; public event EventHandler Reset; #endregion } }