1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Globalization;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Text;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Common;
|
---|
9 | using System.Drawing;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.DataPreprocessing {
|
---|
12 |
|
---|
13 | [Item("DataGridContent", "Represents a data grid.")]
|
---|
14 | public class DataGridContent : Item,IDataGridContent,IContent
|
---|
15 | {
|
---|
16 | private readonly IPreprocessingData preprocessingData;
|
---|
17 |
|
---|
18 | public DataGridContent(IPreprocessingData thePreprocessingData) {
|
---|
19 | preprocessingData = thePreprocessingData;
|
---|
20 | }
|
---|
21 |
|
---|
22 | public DataGridContent(DataGridContent dataGridContent, Cloner cloner)
|
---|
23 | : base(dataGridContent, cloner)
|
---|
24 | {
|
---|
25 |
|
---|
26 | }
|
---|
27 |
|
---|
28 | public IDataGridLogic DataGridLogic { get; set; }
|
---|
29 |
|
---|
30 | public static new Image StaticItemImage
|
---|
31 | {
|
---|
32 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Table; }
|
---|
33 | }
|
---|
34 |
|
---|
35 | public override IDeepCloneable Clone(Cloner cloner)
|
---|
36 | {
|
---|
37 | return new DataGridContent(this, cloner);
|
---|
38 | }
|
---|
39 |
|
---|
40 | #region IStringConvertibleMatrix Members
|
---|
41 |
|
---|
42 | public int Rows {
|
---|
43 | get {
|
---|
44 | return preprocessingData.Rows;
|
---|
45 | }
|
---|
46 | set {
|
---|
47 | //does nothing, use something else :P
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public int Columns {
|
---|
52 | get {
|
---|
53 | return preprocessingData.Columns;
|
---|
54 | }
|
---|
55 | set {
|
---|
56 | //does nothing, use something else :P
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | public IEnumerable<string> ColumnNames {
|
---|
61 | get {
|
---|
62 | return preprocessingData.VariableNames;
|
---|
63 | }
|
---|
64 | set {
|
---|
65 | //not supported
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | public IEnumerable<string> RowNames {
|
---|
70 | get {
|
---|
71 | return Enumerable.Range(1, Rows).Select(n => n.ToString());
|
---|
72 | }
|
---|
73 | set {
|
---|
74 | //not supported
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | public bool SortableView {
|
---|
79 | get {
|
---|
80 | return true;
|
---|
81 | }
|
---|
82 | set {
|
---|
83 | //not supported
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | public bool ReadOnly {
|
---|
88 | get { return false; }
|
---|
89 | }
|
---|
90 |
|
---|
91 | public bool Validate(string value, out string errorMessage) {
|
---|
92 | errorMessage = string.Empty;
|
---|
93 | return true;
|
---|
94 | }
|
---|
95 |
|
---|
96 | public bool Validate(string value, out string errorMessage, int columnIndex) {
|
---|
97 | if (columnIndex < 0 || columnIndex > preprocessingData.VariableNames.Count()) {
|
---|
98 | throw new ArgumentOutOfRangeException("column index is out of range");
|
---|
99 | }
|
---|
100 | bool valid = false;
|
---|
101 | string variableName = preprocessingData.GetVariableName(columnIndex);
|
---|
102 | if (preprocessingData.IsType<double>(variableName)) {
|
---|
103 | double val;
|
---|
104 | valid = double.TryParse(value, out val);
|
---|
105 | errorMessage = string.Empty;
|
---|
106 | if (!valid) {
|
---|
107 | errorMessage = "Invalid Value (Valid Value Format: \"" + FormatPatterns.GetDoubleFormatPattern() + "\")";
|
---|
108 | }
|
---|
109 | } else if (preprocessingData.IsType<string>(variableName)) {
|
---|
110 | valid = value != null;
|
---|
111 | errorMessage = string.Empty;
|
---|
112 | if (!valid) {
|
---|
113 | errorMessage = "Invalid Value (string must not be null)";
|
---|
114 | }
|
---|
115 | } else if (preprocessingData.IsType<DateTime>(variableName)) {
|
---|
116 | DateTime date;
|
---|
117 | valid = DateTime.TryParse(value, out date);
|
---|
118 | errorMessage = string.Empty;
|
---|
119 | if (!valid) {
|
---|
120 | errorMessage = "Invalid Value (Valid Value Format: \"" + CultureInfo.CurrentCulture.DateTimeFormat + "\"";
|
---|
121 | }
|
---|
122 | } else {
|
---|
123 | throw new ArgumentException("column with variableName: " + variableName + " contains a non supported type.");
|
---|
124 | }
|
---|
125 |
|
---|
126 | return valid;
|
---|
127 | }
|
---|
128 |
|
---|
129 | public string GetValue(int rowIndex, int columnIndex) {
|
---|
130 | return preprocessingData.GetCellAsString(preprocessingData.GetVariableName(columnIndex), rowIndex);
|
---|
131 | }
|
---|
132 |
|
---|
133 | public bool SetValue(string value, int rowIndex, int columnIndex) {
|
---|
134 | string variableName = preprocessingData.GetVariableName(columnIndex);
|
---|
135 | bool valid = false;
|
---|
136 | if (preprocessingData.IsType<double>(variableName)) {
|
---|
137 | double val;
|
---|
138 | valid = double.TryParse(value, out val);
|
---|
139 | if (valid) {
|
---|
140 | preprocessingData.SetCell<double>(variableName, rowIndex, val);
|
---|
141 | }
|
---|
142 | } else if (preprocessingData.IsType<string>(variableName)) {
|
---|
143 | valid = value != null;
|
---|
144 | if (valid) {
|
---|
145 | preprocessingData.SetCell<string>(variableName, rowIndex, value);
|
---|
146 | }
|
---|
147 | } else if (preprocessingData.IsType<DateTime>(variableName)) {
|
---|
148 | DateTime date;
|
---|
149 | valid = DateTime.TryParse(value, out date);
|
---|
150 | if (valid) {
|
---|
151 | preprocessingData.SetCell<DateTime>(variableName, rowIndex, date);
|
---|
152 | }
|
---|
153 | } else {
|
---|
154 | throw new ArgumentException("column with variableName: " + variableName + " contains a non supported type.");
|
---|
155 | }
|
---|
156 |
|
---|
157 | return valid;
|
---|
158 | }
|
---|
159 |
|
---|
160 | public event EventHandler ColumnsChanged;
|
---|
161 |
|
---|
162 | public event EventHandler RowsChanged;
|
---|
163 |
|
---|
164 | public event EventHandler ColumnNamesChanged;
|
---|
165 |
|
---|
166 | public event EventHandler RowNamesChanged;
|
---|
167 |
|
---|
168 | public event EventHandler SortableViewChanged;
|
---|
169 |
|
---|
170 | public event EventHandler<Common.EventArgs<int, int>> ItemChanged;
|
---|
171 |
|
---|
172 | public event EventHandler Reset;
|
---|
173 |
|
---|
174 | #endregion
|
---|
175 | }
|
---|
176 | }
|
---|