1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 |
|
---|
22 | using System;
|
---|
23 | using System.ComponentModel;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Windows.Forms;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.MainForm;
|
---|
30 | using HeuristicLab.MainForm.WindowsForms;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Data.Views {
|
---|
33 | [View("StringConvertibleMatrix View")]
|
---|
34 | [Content(typeof(IStringConvertibleMatrix), true)]
|
---|
35 | public partial class StringConvertibleMatrixView : AsynchronousContentView {
|
---|
36 | protected int[] virtualRowIndizes;
|
---|
37 | private List<KeyValuePair<int, SortOrder>> sortedColumnIndizes;
|
---|
38 | private RowComparer rowComparer;
|
---|
39 |
|
---|
40 | public new IStringConvertibleMatrix Content {
|
---|
41 | get { return (IStringConvertibleMatrix)base.Content; }
|
---|
42 | set { base.Content = value; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public override bool ReadOnly {
|
---|
46 | get {
|
---|
47 | if ((Content != null) && Content.ReadOnly) return true;
|
---|
48 | return base.ReadOnly;
|
---|
49 | }
|
---|
50 | set { base.ReadOnly = value; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public StringConvertibleMatrixView() {
|
---|
54 | InitializeComponent();
|
---|
55 | Caption = "StringConvertibleMatrix View";
|
---|
56 | errorProvider.SetIconAlignment(rowsTextBox, ErrorIconAlignment.MiddleLeft);
|
---|
57 | errorProvider.SetIconPadding(rowsTextBox, 2);
|
---|
58 | errorProvider.SetIconAlignment(columnsTextBox, ErrorIconAlignment.MiddleLeft);
|
---|
59 | errorProvider.SetIconPadding(columnsTextBox, 2);
|
---|
60 | sortedColumnIndizes = new List<KeyValuePair<int, SortOrder>>();
|
---|
61 | rowComparer = new RowComparer();
|
---|
62 | }
|
---|
63 | public StringConvertibleMatrixView(IStringConvertibleMatrix content)
|
---|
64 | : this() {
|
---|
65 | Content = content;
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected override void DeregisterContentEvents() {
|
---|
69 | Content.ItemChanged -= new EventHandler<EventArgs<int, int>>(Content_ItemChanged);
|
---|
70 | Content.Reset -= new EventHandler(Content_Reset);
|
---|
71 | Content.ColumnNamesChanged -= new EventHandler(Content_ColumnNamesChanged);
|
---|
72 | Content.RowNamesChanged -= new EventHandler(Content_RowNamesChanged);
|
---|
73 | base.DeregisterContentEvents();
|
---|
74 | }
|
---|
75 | protected override void RegisterContentEvents() {
|
---|
76 | base.RegisterContentEvents();
|
---|
77 | Content.ItemChanged += new EventHandler<EventArgs<int, int>>(Content_ItemChanged);
|
---|
78 | Content.Reset += new EventHandler(Content_Reset);
|
---|
79 | Content.ColumnNamesChanged += new EventHandler(Content_ColumnNamesChanged);
|
---|
80 | Content.RowNamesChanged += new EventHandler(Content_RowNamesChanged);
|
---|
81 | }
|
---|
82 |
|
---|
83 | protected override void OnContentChanged() {
|
---|
84 | base.OnContentChanged();
|
---|
85 | if (Content == null) {
|
---|
86 | Caption = "StringConvertibleMatrix View";
|
---|
87 | rowsTextBox.Text = "";
|
---|
88 | columnsTextBox.Text = "";
|
---|
89 | dataGridView.Rows.Clear();
|
---|
90 | dataGridView.Columns.Clear();
|
---|
91 | virtualRowIndizes = new int[0];
|
---|
92 | } else {
|
---|
93 | Caption = "StringConvertibleMatrix (" + Content.GetType().Name + ")";
|
---|
94 | UpdateData();
|
---|
95 | }
|
---|
96 | SetEnabledStateOfControls();
|
---|
97 | }
|
---|
98 | protected override void OnReadOnlyChanged() {
|
---|
99 | base.OnReadOnlyChanged();
|
---|
100 | SetEnabledStateOfControls();
|
---|
101 | }
|
---|
102 | private void SetEnabledStateOfControls() {
|
---|
103 | rowsTextBox.Enabled = Content != null;
|
---|
104 | columnsTextBox.Enabled = Content != null;
|
---|
105 | dataGridView.Enabled = Content != null;
|
---|
106 | rowsTextBox.ReadOnly = ReadOnly;
|
---|
107 | columnsTextBox.ReadOnly = ReadOnly;
|
---|
108 | dataGridView.ReadOnly = ReadOnly;
|
---|
109 | }
|
---|
110 |
|
---|
111 | private void UpdateData() {
|
---|
112 | sortedColumnIndizes.Clear();
|
---|
113 | Sort();
|
---|
114 | rowsTextBox.Text = Content.Rows.ToString();
|
---|
115 | rowsTextBox.Enabled = true;
|
---|
116 | columnsTextBox.Text = Content.Columns.ToString();
|
---|
117 | columnsTextBox.Enabled = true;
|
---|
118 | virtualRowIndizes = Enumerable.Range(0, Content.Rows).ToArray();
|
---|
119 | //DataGridViews with Rows but no columns are not allowed !
|
---|
120 | if (Content.Rows == 0 && dataGridView.RowCount != Content.Rows && !Content.ReadOnly)
|
---|
121 | Content.Rows = dataGridView.RowCount;
|
---|
122 | else
|
---|
123 | dataGridView.RowCount = Content.Rows;
|
---|
124 | if (Content.Columns == 0 && dataGridView.ColumnCount != Content.Columns && !Content.ReadOnly)
|
---|
125 | Content.Columns = dataGridView.ColumnCount;
|
---|
126 | else
|
---|
127 | dataGridView.ColumnCount = Content.Columns;
|
---|
128 |
|
---|
129 | UpdateRowHeaders();
|
---|
130 | UpdateColumnHeaders();
|
---|
131 | dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
|
---|
132 | dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
|
---|
133 | dataGridView.Enabled = true;
|
---|
134 | }
|
---|
135 |
|
---|
136 | private void UpdateColumnHeaders() {
|
---|
137 | for (int i = 0; i < Content.Columns; i++) {
|
---|
138 | if (Content.ColumnNames.Count() != 0)
|
---|
139 | dataGridView.Columns[i].HeaderText = Content.ColumnNames.ElementAt(i);
|
---|
140 | else
|
---|
141 | dataGridView.Columns[i].HeaderText = "Column " + (i + 1);
|
---|
142 | }
|
---|
143 | dataGridView.Invalidate();
|
---|
144 | }
|
---|
145 |
|
---|
146 | private void UpdateRowHeaders() {
|
---|
147 | for (int i = 0; i < dataGridView.RowCount; i++) {
|
---|
148 | if (Content.RowNames.Count() != 0)
|
---|
149 | dataGridView.Rows[i].HeaderCell.Value = Content.RowNames.ElementAt(virtualRowIndizes[i]);
|
---|
150 | else
|
---|
151 | dataGridView.Rows[i].HeaderCell.Value = "Row " + (i + 1);
|
---|
152 | }
|
---|
153 | dataGridView.Invalidate();
|
---|
154 | }
|
---|
155 |
|
---|
156 | private void Content_RowNamesChanged(object sender, EventArgs e) {
|
---|
157 | if (InvokeRequired)
|
---|
158 | Invoke(new EventHandler(Content_RowNamesChanged), sender, e);
|
---|
159 | else
|
---|
160 | UpdateRowHeaders();
|
---|
161 | }
|
---|
162 | private void Content_ColumnNamesChanged(object sender, EventArgs e) {
|
---|
163 | if (InvokeRequired)
|
---|
164 | Invoke(new EventHandler(Content_ColumnNamesChanged), sender, e);
|
---|
165 | else
|
---|
166 | UpdateColumnHeaders();
|
---|
167 | }
|
---|
168 | private void Content_ItemChanged(object sender, EventArgs<int, int> e) {
|
---|
169 | if (InvokeRequired)
|
---|
170 | Invoke(new EventHandler<EventArgs<int, int>>(Content_ItemChanged), sender, e);
|
---|
171 | else
|
---|
172 | dataGridView.InvalidateCell(e.Value2, e.Value);
|
---|
173 | }
|
---|
174 | private void Content_Reset(object sender, EventArgs e) {
|
---|
175 | if (InvokeRequired)
|
---|
176 | Invoke(new EventHandler(Content_Reset), sender, e);
|
---|
177 | else
|
---|
178 | UpdateData();
|
---|
179 | }
|
---|
180 |
|
---|
181 | #region TextBox Events
|
---|
182 | private void rowsTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
183 | int i = 0;
|
---|
184 | if (!int.TryParse(rowsTextBox.Text, out i) || (i <= 0)) {
|
---|
185 | e.Cancel = true;
|
---|
186 | errorProvider.SetError(rowsTextBox, "Invalid Number of Rows (Valid values are positive integers larger than 0)");
|
---|
187 | rowsTextBox.SelectAll();
|
---|
188 | }
|
---|
189 | }
|
---|
190 | private void rowsTextBox_Validated(object sender, EventArgs e) {
|
---|
191 | if (!Content.ReadOnly) Content.Rows = int.Parse(rowsTextBox.Text);
|
---|
192 | errorProvider.SetError(rowsTextBox, string.Empty);
|
---|
193 | }
|
---|
194 | private void rowsTextBox_KeyDown(object sender, KeyEventArgs e) {
|
---|
195 | if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
|
---|
196 | rowsLabel.Focus(); // set focus on label to validate data
|
---|
197 | if (e.KeyCode == Keys.Escape) {
|
---|
198 | rowsTextBox.Text = Content.Rows.ToString();
|
---|
199 | rowsLabel.Focus(); // set focus on label to validate data
|
---|
200 | }
|
---|
201 | }
|
---|
202 | private void columnsTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
203 | int i = 0;
|
---|
204 | if (!int.TryParse(columnsTextBox.Text, out i) || (i <= 0)) {
|
---|
205 | e.Cancel = true;
|
---|
206 | errorProvider.SetError(columnsTextBox, "Invalid Number of Columns (Valid values are positive integers larger than 0)");
|
---|
207 | columnsTextBox.SelectAll();
|
---|
208 | }
|
---|
209 | }
|
---|
210 | private void columnsTextBox_Validated(object sender, EventArgs e) {
|
---|
211 | if (!Content.ReadOnly) Content.Columns = int.Parse(columnsTextBox.Text);
|
---|
212 | errorProvider.SetError(columnsTextBox, string.Empty);
|
---|
213 | }
|
---|
214 | private void columnsTextBox_KeyDown(object sender, KeyEventArgs e) {
|
---|
215 | if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
|
---|
216 | columnsLabel.Focus(); // set focus on label to validate data
|
---|
217 | if (e.KeyCode == Keys.Escape) {
|
---|
218 | columnsTextBox.Text = Content.Columns.ToString();
|
---|
219 | columnsLabel.Focus(); // set focus on label to validate data
|
---|
220 | }
|
---|
221 | }
|
---|
222 | #endregion
|
---|
223 |
|
---|
224 | #region DataGridView Events
|
---|
225 | private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
|
---|
226 | if (!dataGridView.ReadOnly) {
|
---|
227 | string errorMessage;
|
---|
228 | if (!Content.Validate(e.FormattedValue.ToString(), out errorMessage)) {
|
---|
229 | e.Cancel = true;
|
---|
230 | dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
|
---|
231 | }
|
---|
232 | }
|
---|
233 | }
|
---|
234 | private void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
|
---|
235 | if (!dataGridView.ReadOnly) {
|
---|
236 | string value = e.Value.ToString();
|
---|
237 | int rowIndex = virtualRowIndizes[e.RowIndex];
|
---|
238 | e.ParsingApplied = Content.SetValue(value, rowIndex, e.ColumnIndex);
|
---|
239 | if (e.ParsingApplied) e.Value = Content.GetValue(rowIndex, e.ColumnIndex);
|
---|
240 | }
|
---|
241 | }
|
---|
242 | private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
|
---|
243 | dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
|
---|
244 | }
|
---|
245 | private void dataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) {
|
---|
246 | if (e.RowIndex < Content.Rows && e.ColumnIndex < Content.Columns) {
|
---|
247 | int rowIndex = virtualRowIndizes[e.RowIndex];
|
---|
248 | e.Value = Content.GetValue(rowIndex, e.ColumnIndex);
|
---|
249 | }
|
---|
250 | }
|
---|
251 | private void dataGridView_Scroll(object sender, ScrollEventArgs e) {
|
---|
252 | UpdateRowHeaders();
|
---|
253 | }
|
---|
254 | private void dataGridView_Resize(object sender, EventArgs e) {
|
---|
255 | UpdateRowHeaders();
|
---|
256 | }
|
---|
257 |
|
---|
258 | private void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
259 | if (Content != null) {
|
---|
260 | if (e.Button == MouseButtons.Left && Content.SortableView) {
|
---|
261 | bool addToSortedIndizes = (Control.ModifierKeys & Keys.Control) == Keys.Control;
|
---|
262 | SortOrder newSortOrder = SortOrder.Ascending;
|
---|
263 | if (sortedColumnIndizes.Any(x => x.Key == e.ColumnIndex)) {
|
---|
264 | SortOrder oldSortOrder = sortedColumnIndizes.Where(x => x.Key == e.ColumnIndex).First().Value;
|
---|
265 | int enumLength = Enum.GetValues(typeof(SortOrder)).Length;
|
---|
266 | newSortOrder = oldSortOrder = (SortOrder)Enum.Parse(typeof(SortOrder), ((((int)oldSortOrder) + 1) % enumLength).ToString());
|
---|
267 | }
|
---|
268 |
|
---|
269 | if (!addToSortedIndizes)
|
---|
270 | sortedColumnIndizes.Clear();
|
---|
271 |
|
---|
272 | if (sortedColumnIndizes.Any(x => x.Key == e.ColumnIndex)) {
|
---|
273 | int sortedIndex = sortedColumnIndizes.FindIndex(x => x.Key == e.ColumnIndex);
|
---|
274 | if (newSortOrder != SortOrder.None)
|
---|
275 | sortedColumnIndizes[sortedIndex] = new KeyValuePair<int, SortOrder>(e.ColumnIndex, newSortOrder);
|
---|
276 | else
|
---|
277 | sortedColumnIndizes.RemoveAt(sortedIndex);
|
---|
278 | } else
|
---|
279 | if (newSortOrder != SortOrder.None)
|
---|
280 | sortedColumnIndizes.Add(new KeyValuePair<int, SortOrder>(e.ColumnIndex, newSortOrder));
|
---|
281 | Sort();
|
---|
282 | } else if (e.Button == MouseButtons.Right) {
|
---|
283 | if (Content.ColumnNames.Count() != 0)
|
---|
284 | contextMenu.Show(MousePosition);
|
---|
285 | }
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 | private void Sort() {
|
---|
290 | virtualRowIndizes = Sort(sortedColumnIndizes);
|
---|
291 | UpdateSortGlyph();
|
---|
292 | UpdateRowHeaders();
|
---|
293 | dataGridView.Invalidate();
|
---|
294 | }
|
---|
295 | protected virtual int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
|
---|
296 | int[] newSortedIndex = Enumerable.Range(0, Content.Rows).ToArray();
|
---|
297 | if (sortedColumns.Count() != 0) {
|
---|
298 | rowComparer.SortedIndizes = sortedColumns;
|
---|
299 | rowComparer.Matrix = Content;
|
---|
300 | Array.Sort(newSortedIndex, rowComparer);
|
---|
301 | }
|
---|
302 | return newSortedIndex;
|
---|
303 | }
|
---|
304 | private void UpdateSortGlyph() {
|
---|
305 | foreach (DataGridViewColumn col in this.dataGridView.Columns)
|
---|
306 | col.HeaderCell.SortGlyphDirection = SortOrder.None;
|
---|
307 | foreach (KeyValuePair<int, SortOrder> p in sortedColumnIndizes)
|
---|
308 | this.dataGridView.Columns[p.Key].HeaderCell.SortGlyphDirection = p.Value;
|
---|
309 | }
|
---|
310 | #endregion
|
---|
311 |
|
---|
312 | public class RowComparer : IComparer<int> {
|
---|
313 | public RowComparer() {
|
---|
314 | }
|
---|
315 |
|
---|
316 | private List<KeyValuePair<int, SortOrder>> sortedIndizes;
|
---|
317 | public IEnumerable<KeyValuePair<int, SortOrder>> SortedIndizes {
|
---|
318 | get { return this.sortedIndizes; }
|
---|
319 | set { sortedIndizes = new List<KeyValuePair<int, SortOrder>>(value); }
|
---|
320 | }
|
---|
321 | private IStringConvertibleMatrix matrix;
|
---|
322 | public IStringConvertibleMatrix Matrix {
|
---|
323 | get { return this.matrix; }
|
---|
324 | set { this.matrix = value; }
|
---|
325 | }
|
---|
326 |
|
---|
327 | public int Compare(int x, int y) {
|
---|
328 | int result = 0;
|
---|
329 | double double1, double2;
|
---|
330 | DateTime dateTime1, dateTime2;
|
---|
331 | TimeSpan timeSpan1, timeSpan2;
|
---|
332 | string string1, string2;
|
---|
333 |
|
---|
334 | if (matrix == null)
|
---|
335 | throw new InvalidOperationException("Could not sort IStringConvertibleMatrix if the matrix member is null.");
|
---|
336 | if (sortedIndizes == null)
|
---|
337 | return 0;
|
---|
338 |
|
---|
339 | foreach (KeyValuePair<int, SortOrder> pair in sortedIndizes.Where(p => p.Value != SortOrder.None)) {
|
---|
340 | string1 = matrix.GetValue(x, pair.Key);
|
---|
341 | string2 = matrix.GetValue(y, pair.Key);
|
---|
342 | if (double.TryParse(string1, out double1) && double.TryParse(string2, out double2))
|
---|
343 | result = double1.CompareTo(double2);
|
---|
344 | else if (DateTime.TryParse(string1, out dateTime1) && DateTime.TryParse(string2, out dateTime2))
|
---|
345 | result = dateTime1.CompareTo(dateTime2);
|
---|
346 | else if (TimeSpan.TryParse(string1, out timeSpan1) && TimeSpan.TryParse(string2, out timeSpan2))
|
---|
347 | result = timeSpan1.CompareTo(timeSpan2);
|
---|
348 | else {
|
---|
349 | if (string1 != null)
|
---|
350 | result = string1.CompareTo(string2);
|
---|
351 | else if (string2 != null)
|
---|
352 | result = string2.CompareTo(string1) * -1;
|
---|
353 | }
|
---|
354 | if (pair.Value == SortOrder.Descending)
|
---|
355 | result *= -1;
|
---|
356 | if (result != 0)
|
---|
357 | return result;
|
---|
358 | }
|
---|
359 | return result;
|
---|
360 | }
|
---|
361 | }
|
---|
362 |
|
---|
363 | private void ShowHideColumns_Click(object sender, EventArgs e) {
|
---|
364 | new ColumnsVisibilityDialog(this.dataGridView.Columns.Cast<DataGridViewColumn>()).ShowDialog();
|
---|
365 | }
|
---|
366 | }
|
---|
367 | }
|
---|