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.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Text;
|
---|
28 | using System.Windows.Forms;
|
---|
29 | using HeuristicLab.Common;
|
---|
30 | using HeuristicLab.MainForm;
|
---|
31 | using HeuristicLab.MainForm.WindowsForms;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Data.Views {
|
---|
34 | [View("StringConvertibleMatrix View")]
|
---|
35 | [Content(typeof(IStringConvertibleMatrix), true)]
|
---|
36 | public partial class StringConvertibleMatrixView : AsynchronousContentView {
|
---|
37 | private int[] virtualRowIndizes;
|
---|
38 | private List<KeyValuePair<int, SortOrder>> sortedColumnIndizes;
|
---|
39 | private RowComparer rowComparer;
|
---|
40 |
|
---|
41 | public new IStringConvertibleMatrix Content {
|
---|
42 | get { return (IStringConvertibleMatrix)base.Content; }
|
---|
43 | set { base.Content = value; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public override bool ReadOnly {
|
---|
47 | get {
|
---|
48 | if ((Content != null) && Content.ReadOnly) return true;
|
---|
49 | return base.ReadOnly;
|
---|
50 | }
|
---|
51 | set { base.ReadOnly = value; }
|
---|
52 | }
|
---|
53 |
|
---|
54 | private bool showRowsAndColumnsTextBox;
|
---|
55 | public bool ShowRowsAndColumnsTextBox {
|
---|
56 | get { return showRowsAndColumnsTextBox; }
|
---|
57 | set {
|
---|
58 | showRowsAndColumnsTextBox = value;
|
---|
59 | UpdateVisibilityOfTextBoxes();
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | private bool showStatisticalInformation;
|
---|
64 | public bool ShowStatisticalInformation {
|
---|
65 | get { return showStatisticalInformation; }
|
---|
66 | set {
|
---|
67 | showStatisticalInformation = value;
|
---|
68 | UpdateVisibilityOfStatisticalInformation();
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | public StringConvertibleMatrixView() {
|
---|
73 | InitializeComponent();
|
---|
74 | ShowRowsAndColumnsTextBox = true;
|
---|
75 | ShowStatisticalInformation = true;
|
---|
76 | errorProvider.SetIconAlignment(rowsTextBox, ErrorIconAlignment.MiddleLeft);
|
---|
77 | errorProvider.SetIconPadding(rowsTextBox, 2);
|
---|
78 | errorProvider.SetIconAlignment(columnsTextBox, ErrorIconAlignment.MiddleLeft);
|
---|
79 | errorProvider.SetIconPadding(columnsTextBox, 2);
|
---|
80 | sortedColumnIndizes = new List<KeyValuePair<int, SortOrder>>();
|
---|
81 | rowComparer = new RowComparer();
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected override void DeregisterContentEvents() {
|
---|
85 | Content.ItemChanged -= new EventHandler<EventArgs<int, int>>(Content_ItemChanged);
|
---|
86 | Content.Reset -= new EventHandler(Content_Reset);
|
---|
87 | Content.ColumnNamesChanged -= new EventHandler(Content_ColumnNamesChanged);
|
---|
88 | Content.RowNamesChanged -= new EventHandler(Content_RowNamesChanged);
|
---|
89 | base.DeregisterContentEvents();
|
---|
90 | }
|
---|
91 | protected override void RegisterContentEvents() {
|
---|
92 | base.RegisterContentEvents();
|
---|
93 | Content.ItemChanged += new EventHandler<EventArgs<int, int>>(Content_ItemChanged);
|
---|
94 | Content.Reset += new EventHandler(Content_Reset);
|
---|
95 | Content.ColumnNamesChanged += new EventHandler(Content_ColumnNamesChanged);
|
---|
96 | Content.RowNamesChanged += new EventHandler(Content_RowNamesChanged);
|
---|
97 | }
|
---|
98 |
|
---|
99 | protected override void OnContentChanged() {
|
---|
100 | base.OnContentChanged();
|
---|
101 | if (Content == null) {
|
---|
102 | rowsTextBox.Text = "";
|
---|
103 | columnsTextBox.Text = "";
|
---|
104 | dataGridView.Rows.Clear();
|
---|
105 | dataGridView.Columns.Clear();
|
---|
106 | virtualRowIndizes = new int[0];
|
---|
107 | } else
|
---|
108 | UpdateData();
|
---|
109 | }
|
---|
110 |
|
---|
111 | protected override void SetEnabledStateOfControls() {
|
---|
112 | base.SetEnabledStateOfControls();
|
---|
113 | rowsTextBox.Enabled = Content != null;
|
---|
114 | columnsTextBox.Enabled = Content != null;
|
---|
115 | dataGridView.Enabled = Content != null;
|
---|
116 | rowsTextBox.ReadOnly = ReadOnly;
|
---|
117 | columnsTextBox.ReadOnly = ReadOnly;
|
---|
118 | dataGridView.ReadOnly = ReadOnly;
|
---|
119 | }
|
---|
120 |
|
---|
121 | private void UpdateData() {
|
---|
122 | rowsTextBox.Text = Content.Rows.ToString();
|
---|
123 | rowsTextBox.Enabled = true;
|
---|
124 | columnsTextBox.Text = Content.Columns.ToString();
|
---|
125 | columnsTextBox.Enabled = true;
|
---|
126 | virtualRowIndizes = Enumerable.Range(0, Content.Rows).ToArray();
|
---|
127 |
|
---|
128 | if (Content.Columns == 0 && dataGridView.ColumnCount != Content.Columns && !Content.ReadOnly)
|
---|
129 | Content.Columns = dataGridView.ColumnCount;
|
---|
130 | else {
|
---|
131 | DataGridViewColumn[] columns = new DataGridViewColumn[Content.Columns];
|
---|
132 | for (int i = 0; i < columns.Length; ++i) {
|
---|
133 | var column = new DataGridViewTextBoxColumn();
|
---|
134 | column.FillWeight = 1;
|
---|
135 | columns[i] = column;
|
---|
136 | }
|
---|
137 | dataGridView.Columns.Clear();
|
---|
138 | dataGridView.Columns.AddRange(columns);
|
---|
139 | }
|
---|
140 |
|
---|
141 | //DataGridViews with rows but no columns are not allowed !
|
---|
142 | if (Content.Rows == 0 && dataGridView.RowCount != Content.Rows && !Content.ReadOnly)
|
---|
143 | Content.Rows = dataGridView.RowCount;
|
---|
144 | else
|
---|
145 | dataGridView.RowCount = Content.Rows;
|
---|
146 |
|
---|
147 |
|
---|
148 | ClearSorting();
|
---|
149 | UpdateColumnHeaders();
|
---|
150 | UpdateRowHeaders();
|
---|
151 |
|
---|
152 | dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
|
---|
153 | dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
|
---|
154 | dataGridView.Enabled = true;
|
---|
155 | }
|
---|
156 |
|
---|
157 | protected virtual void UpdateColumnHeaders() {
|
---|
158 | HashSet<string> invisibleColumnNames = new HashSet<string>(dataGridView.Columns.OfType<DataGridViewColumn>()
|
---|
159 | .Where(c => !c.Visible && !string.IsNullOrEmpty(c.HeaderText)).Select(c => c.HeaderText));
|
---|
160 |
|
---|
161 | for (int i = 0; i < dataGridView.ColumnCount; i++) {
|
---|
162 | if (i < Content.ColumnNames.Count())
|
---|
163 | dataGridView.Columns[i].HeaderText = Content.ColumnNames.ElementAt(i);
|
---|
164 | else
|
---|
165 | dataGridView.Columns[i].HeaderText = "Column " + (i + 1);
|
---|
166 | dataGridView.Columns[i].Visible = !invisibleColumnNames.Contains(dataGridView.Columns[i].HeaderText);
|
---|
167 | }
|
---|
168 | }
|
---|
169 | protected void UpdateRowHeaders() {
|
---|
170 | int index = dataGridView.FirstDisplayedScrollingRowIndex;
|
---|
171 | if (index == -1) index = 0;
|
---|
172 | int updatedRows = 0;
|
---|
173 | int count = dataGridView.DisplayedRowCount(true);
|
---|
174 |
|
---|
175 | while (updatedRows < count) {
|
---|
176 | if (virtualRowIndizes[index] < Content.RowNames.Count())
|
---|
177 | dataGridView.Rows[index].HeaderCell.Value = Content.RowNames.ElementAt(virtualRowIndizes[index]);
|
---|
178 | else
|
---|
179 | dataGridView.Rows[index].HeaderCell.Value = "Row " + (index + 1);
|
---|
180 | if (dataGridView.Rows[index].Visible)
|
---|
181 | updatedRows++;
|
---|
182 | index++;
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | private void Content_RowNamesChanged(object sender, EventArgs e) {
|
---|
187 | if (InvokeRequired)
|
---|
188 | Invoke(new EventHandler(Content_RowNamesChanged), sender, e);
|
---|
189 | else
|
---|
190 | UpdateRowHeaders();
|
---|
191 | }
|
---|
192 | private void Content_ColumnNamesChanged(object sender, EventArgs e) {
|
---|
193 | if (InvokeRequired)
|
---|
194 | Invoke(new EventHandler(Content_ColumnNamesChanged), sender, e);
|
---|
195 | else
|
---|
196 | UpdateColumnHeaders();
|
---|
197 | }
|
---|
198 | private void Content_ItemChanged(object sender, EventArgs<int, int> e) {
|
---|
199 | if (InvokeRequired)
|
---|
200 | Invoke(new EventHandler<EventArgs<int, int>>(Content_ItemChanged), sender, e);
|
---|
201 | else
|
---|
202 | dataGridView.InvalidateCell(e.Value2, e.Value);
|
---|
203 | }
|
---|
204 | private void Content_Reset(object sender, EventArgs e) {
|
---|
205 | if (InvokeRequired)
|
---|
206 | Invoke(new EventHandler(Content_Reset), sender, e);
|
---|
207 | else
|
---|
208 | UpdateData();
|
---|
209 | }
|
---|
210 |
|
---|
211 | #region TextBox Events
|
---|
212 | private void rowsTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
213 | if (ReadOnly || Locked)
|
---|
214 | return;
|
---|
215 | int i = 0;
|
---|
216 | if (!int.TryParse(rowsTextBox.Text, out i) || (i <= 0)) {
|
---|
217 | e.Cancel = true;
|
---|
218 | errorProvider.SetError(rowsTextBox, "Invalid Number of Rows (Valid values are positive integers larger than 0)");
|
---|
219 | rowsTextBox.SelectAll();
|
---|
220 | }
|
---|
221 | }
|
---|
222 | private void rowsTextBox_Validated(object sender, EventArgs e) {
|
---|
223 | if (!Content.ReadOnly) Content.Rows = int.Parse(rowsTextBox.Text);
|
---|
224 | errorProvider.SetError(rowsTextBox, string.Empty);
|
---|
225 | }
|
---|
226 | private void rowsTextBox_KeyDown(object sender, KeyEventArgs e) {
|
---|
227 | if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
|
---|
228 | rowsLabel.Focus(); // set focus on label to validate data
|
---|
229 | if (e.KeyCode == Keys.Escape) {
|
---|
230 | rowsTextBox.Text = Content.Rows.ToString();
|
---|
231 | rowsLabel.Focus(); // set focus on label to validate data
|
---|
232 | }
|
---|
233 | }
|
---|
234 | private void columnsTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
235 | if (ReadOnly || Locked)
|
---|
236 | return;
|
---|
237 | int i = 0;
|
---|
238 | if (!int.TryParse(columnsTextBox.Text, out i) || (i <= 0)) {
|
---|
239 | e.Cancel = true;
|
---|
240 | errorProvider.SetError(columnsTextBox, "Invalid Number of Columns (Valid values are positive integers larger than 0)");
|
---|
241 | columnsTextBox.SelectAll();
|
---|
242 | }
|
---|
243 | }
|
---|
244 | private void columnsTextBox_Validated(object sender, EventArgs e) {
|
---|
245 | if (!Content.ReadOnly) Content.Columns = int.Parse(columnsTextBox.Text);
|
---|
246 | errorProvider.SetError(columnsTextBox, string.Empty);
|
---|
247 | }
|
---|
248 | private void columnsTextBox_KeyDown(object sender, KeyEventArgs e) {
|
---|
249 | if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
|
---|
250 | columnsLabel.Focus(); // set focus on label to validate data
|
---|
251 | if (e.KeyCode == Keys.Escape) {
|
---|
252 | columnsTextBox.Text = Content.Columns.ToString();
|
---|
253 | columnsLabel.Focus(); // set focus on label to validate data
|
---|
254 | }
|
---|
255 | }
|
---|
256 | #endregion
|
---|
257 |
|
---|
258 | #region DataGridView Events
|
---|
259 | private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
|
---|
260 | if (!dataGridView.ReadOnly) {
|
---|
261 | string errorMessage;
|
---|
262 | if (Content != null && !Content.Validate(e.FormattedValue.ToString(), out errorMessage)) {
|
---|
263 | e.Cancel = true;
|
---|
264 | dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
|
---|
265 | }
|
---|
266 | }
|
---|
267 | }
|
---|
268 | private void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
|
---|
269 | if (!dataGridView.ReadOnly) {
|
---|
270 | string value = e.Value.ToString();
|
---|
271 | int rowIndex = virtualRowIndizes[e.RowIndex];
|
---|
272 | e.ParsingApplied = Content.SetValue(value, rowIndex, e.ColumnIndex);
|
---|
273 | if (e.ParsingApplied) e.Value = Content.GetValue(rowIndex, e.ColumnIndex);
|
---|
274 | }
|
---|
275 | }
|
---|
276 | private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
|
---|
277 | dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
|
---|
278 | }
|
---|
279 | private void dataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) {
|
---|
280 | if (Content != null && e.RowIndex < Content.Rows && e.ColumnIndex < Content.Columns) {
|
---|
281 | int rowIndex = virtualRowIndizes[e.RowIndex];
|
---|
282 | e.Value = Content.GetValue(rowIndex, e.ColumnIndex);
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | private void dataGridView_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e) {
|
---|
287 | this.UpdateRowHeaders();
|
---|
288 | }
|
---|
289 | private void dataGridView_Resize(object sender, EventArgs e) {
|
---|
290 | this.UpdateRowHeaders();
|
---|
291 | }
|
---|
292 |
|
---|
293 | private void dataGridView_KeyDown(object sender, KeyEventArgs e) {
|
---|
294 | if (!ReadOnly && e.Control && e.KeyCode == Keys.V)
|
---|
295 | PasteValuesToDataGridView();
|
---|
296 | else if (e.Control && e.KeyCode == Keys.C)
|
---|
297 | CopyValuesFromDataGridView();
|
---|
298 | }
|
---|
299 |
|
---|
300 | private void CopyValuesFromDataGridView() {
|
---|
301 | if (dataGridView.SelectedCells.Count == 0) return;
|
---|
302 | StringBuilder s = new StringBuilder();
|
---|
303 | int minRowIndex = dataGridView.SelectedCells[0].RowIndex;
|
---|
304 | int maxRowIndex = dataGridView.SelectedCells[dataGridView.SelectedCells.Count - 1].RowIndex;
|
---|
305 | int minColIndex = dataGridView.SelectedCells[0].ColumnIndex;
|
---|
306 | int maxColIndex = dataGridView.SelectedCells[dataGridView.SelectedCells.Count - 1].ColumnIndex;
|
---|
307 |
|
---|
308 | if (minRowIndex > maxRowIndex) {
|
---|
309 | int temp = minRowIndex;
|
---|
310 | minRowIndex = maxRowIndex;
|
---|
311 | maxRowIndex = temp;
|
---|
312 | }
|
---|
313 | if (minColIndex > maxColIndex) {
|
---|
314 | int temp = minColIndex;
|
---|
315 | minColIndex = maxColIndex;
|
---|
316 | maxColIndex = temp;
|
---|
317 | }
|
---|
318 |
|
---|
319 | bool addRowNames = dataGridView.AreAllCellsSelected(false) && Content.RowNames.Count() > 0;
|
---|
320 | bool addColumnNames = dataGridView.AreAllCellsSelected(false) && Content.ColumnNames.Count() > 0;
|
---|
321 |
|
---|
322 | //add colum names
|
---|
323 | if (addColumnNames) {
|
---|
324 | if (addRowNames)
|
---|
325 | s.Append('\t');
|
---|
326 |
|
---|
327 | DataGridViewColumn column = dataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
|
---|
328 | while (column != null) {
|
---|
329 | s.Append(column.HeaderText);
|
---|
330 | s.Append('\t');
|
---|
331 | column = dataGridView.Columns.GetNextColumn(column, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
|
---|
332 | }
|
---|
333 | s.Remove(s.Length - 1, 1); //remove last tab
|
---|
334 | s.Append(Environment.NewLine);
|
---|
335 | }
|
---|
336 |
|
---|
337 | for (int i = minRowIndex; i <= maxRowIndex; i++) {
|
---|
338 | int rowIndex = this.virtualRowIndizes[i];
|
---|
339 | if (addRowNames) {
|
---|
340 | s.Append(Content.RowNames.ElementAt(rowIndex));
|
---|
341 | s.Append('\t');
|
---|
342 | }
|
---|
343 |
|
---|
344 | DataGridViewColumn column = dataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
|
---|
345 | while (column != null) {
|
---|
346 | DataGridViewCell cell = dataGridView[column.Index, i];
|
---|
347 | if (cell.Selected) {
|
---|
348 | s.Append(Content.GetValue(rowIndex, column.Index));
|
---|
349 | s.Append('\t');
|
---|
350 | }
|
---|
351 |
|
---|
352 | column = dataGridView.Columns.GetNextColumn(column, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
|
---|
353 | }
|
---|
354 | s.Remove(s.Length - 1, 1); //remove last tab
|
---|
355 | s.Append(Environment.NewLine);
|
---|
356 | }
|
---|
357 | Clipboard.SetText(s.ToString());
|
---|
358 | }
|
---|
359 |
|
---|
360 | private void PasteValuesToDataGridView() {
|
---|
361 | string[,] values = SplitClipboardString(Clipboard.GetText());
|
---|
362 | int rowIndex = 0;
|
---|
363 | int columnIndex = 0;
|
---|
364 | if (dataGridView.CurrentCell != null) {
|
---|
365 | rowIndex = dataGridView.CurrentCell.RowIndex;
|
---|
366 | columnIndex = dataGridView.CurrentCell.ColumnIndex;
|
---|
367 | }
|
---|
368 |
|
---|
369 | for (int row = 0; row < values.GetLength(1); row++) {
|
---|
370 | if (row + rowIndex >= Content.Rows)
|
---|
371 | Content.Rows = Content.Rows + 1;
|
---|
372 | for (int col = 0; col < values.GetLength(0); col++) {
|
---|
373 | if (col + columnIndex >= Content.Columns)
|
---|
374 | Content.Columns = Content.Columns + 1;
|
---|
375 | Content.SetValue(values[col, row], row + rowIndex, col + columnIndex);
|
---|
376 | }
|
---|
377 | }
|
---|
378 | ClearSorting();
|
---|
379 | }
|
---|
380 | private string[,] SplitClipboardString(string clipboardText) {
|
---|
381 | clipboardText = clipboardText.Remove(clipboardText.Length - Environment.NewLine.Length); //remove last newline constant
|
---|
382 | string[,] values = null;
|
---|
383 | string[] lines = clipboardText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
|
---|
384 | string[] cells;
|
---|
385 | for (int i = 0; i < lines.Length; i++) {
|
---|
386 | cells = lines[i].Split('\t');
|
---|
387 | if (values == null)
|
---|
388 | values = new string[cells.Length, lines.Length];
|
---|
389 | for (int j = 0; j < cells.Length; j++)
|
---|
390 | values[j, i] = string.IsNullOrEmpty(cells[j]) ? string.Empty : cells[j];
|
---|
391 | }
|
---|
392 | return values;
|
---|
393 | }
|
---|
394 |
|
---|
395 | private void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
396 | if (Content != null) {
|
---|
397 | if (e.Button == MouseButtons.Left && Content.SortableView) {
|
---|
398 | bool addToSortedIndizes = (Control.ModifierKeys & Keys.Control) == Keys.Control;
|
---|
399 | SortOrder newSortOrder = SortOrder.Ascending;
|
---|
400 | if (sortedColumnIndizes.Any(x => x.Key == e.ColumnIndex)) {
|
---|
401 | SortOrder oldSortOrder = sortedColumnIndizes.Where(x => x.Key == e.ColumnIndex).First().Value;
|
---|
402 | int enumLength = Enum.GetValues(typeof(SortOrder)).Length;
|
---|
403 | newSortOrder = oldSortOrder = (SortOrder)Enum.Parse(typeof(SortOrder), ((((int)oldSortOrder) + 1) % enumLength).ToString());
|
---|
404 | }
|
---|
405 |
|
---|
406 | if (!addToSortedIndizes)
|
---|
407 | sortedColumnIndizes.Clear();
|
---|
408 |
|
---|
409 | if (sortedColumnIndizes.Any(x => x.Key == e.ColumnIndex)) {
|
---|
410 | int sortedIndex = sortedColumnIndizes.FindIndex(x => x.Key == e.ColumnIndex);
|
---|
411 | if (newSortOrder != SortOrder.None)
|
---|
412 | sortedColumnIndizes[sortedIndex] = new KeyValuePair<int, SortOrder>(e.ColumnIndex, newSortOrder);
|
---|
413 | else
|
---|
414 | sortedColumnIndizes.RemoveAt(sortedIndex);
|
---|
415 | } else
|
---|
416 | if (newSortOrder != SortOrder.None)
|
---|
417 | sortedColumnIndizes.Add(new KeyValuePair<int, SortOrder>(e.ColumnIndex, newSortOrder));
|
---|
418 | Sort();
|
---|
419 | }
|
---|
420 | }
|
---|
421 | }
|
---|
422 |
|
---|
423 | protected virtual void ClearSorting() {
|
---|
424 | virtualRowIndizes = Enumerable.Range(0, Content.Rows).ToArray();
|
---|
425 | sortedColumnIndizes.Clear();
|
---|
426 | UpdateSortGlyph();
|
---|
427 | }
|
---|
428 |
|
---|
429 | private void Sort() {
|
---|
430 | virtualRowIndizes = Sort(sortedColumnIndizes);
|
---|
431 | UpdateSortGlyph();
|
---|
432 | UpdateRowHeaders();
|
---|
433 | dataGridView.Invalidate();
|
---|
434 | }
|
---|
435 | protected virtual int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
|
---|
436 | int[] newSortedIndex = Enumerable.Range(0, Content.Rows).ToArray();
|
---|
437 | if (sortedColumns.Count() != 0) {
|
---|
438 | rowComparer.SortedIndizes = sortedColumns;
|
---|
439 | rowComparer.Matrix = Content;
|
---|
440 | Array.Sort(newSortedIndex, rowComparer);
|
---|
441 | }
|
---|
442 | return newSortedIndex;
|
---|
443 | }
|
---|
444 | private void UpdateSortGlyph() {
|
---|
445 | foreach (DataGridViewColumn col in this.dataGridView.Columns)
|
---|
446 | col.HeaderCell.SortGlyphDirection = SortOrder.None;
|
---|
447 | foreach (KeyValuePair<int, SortOrder> p in sortedColumnIndizes)
|
---|
448 | this.dataGridView.Columns[p.Key].HeaderCell.SortGlyphDirection = p.Value;
|
---|
449 | }
|
---|
450 | #endregion
|
---|
451 |
|
---|
452 | public class RowComparer : IComparer<int> {
|
---|
453 | public RowComparer() {
|
---|
454 | }
|
---|
455 |
|
---|
456 | private List<KeyValuePair<int, SortOrder>> sortedIndizes;
|
---|
457 | public IEnumerable<KeyValuePair<int, SortOrder>> SortedIndizes {
|
---|
458 | get { return this.sortedIndizes; }
|
---|
459 | set { sortedIndizes = new List<KeyValuePair<int, SortOrder>>(value); }
|
---|
460 | }
|
---|
461 | private IStringConvertibleMatrix matrix;
|
---|
462 | public IStringConvertibleMatrix Matrix {
|
---|
463 | get { return this.matrix; }
|
---|
464 | set { this.matrix = value; }
|
---|
465 | }
|
---|
466 |
|
---|
467 | public int Compare(int x, int y) {
|
---|
468 | int result = 0;
|
---|
469 | double double1, double2;
|
---|
470 | DateTime dateTime1, dateTime2;
|
---|
471 | TimeSpan timeSpan1, timeSpan2;
|
---|
472 | string string1, string2;
|
---|
473 |
|
---|
474 | if (matrix == null)
|
---|
475 | throw new InvalidOperationException("Could not sort IStringConvertibleMatrix if the matrix member is null.");
|
---|
476 | if (sortedIndizes == null)
|
---|
477 | return 0;
|
---|
478 |
|
---|
479 | foreach (KeyValuePair<int, SortOrder> pair in sortedIndizes.Where(p => p.Value != SortOrder.None)) {
|
---|
480 | string1 = matrix.GetValue(x, pair.Key);
|
---|
481 | string2 = matrix.GetValue(y, pair.Key);
|
---|
482 | if (double.TryParse(string1, out double1) && double.TryParse(string2, out double2))
|
---|
483 | result = double1.CompareTo(double2);
|
---|
484 | else if (DateTime.TryParse(string1, out dateTime1) && DateTime.TryParse(string2, out dateTime2))
|
---|
485 | result = dateTime1.CompareTo(dateTime2);
|
---|
486 | else if (TimeSpan.TryParse(string1, out timeSpan1) && TimeSpan.TryParse(string2, out timeSpan2))
|
---|
487 | result = timeSpan1.CompareTo(timeSpan2);
|
---|
488 | else {
|
---|
489 | if (string1 != null)
|
---|
490 | result = string1.CompareTo(string2);
|
---|
491 | else if (string2 != null)
|
---|
492 | result = string2.CompareTo(string1) * -1;
|
---|
493 | }
|
---|
494 | if (pair.Value == SortOrder.Descending)
|
---|
495 | result *= -1;
|
---|
496 | if (result != 0)
|
---|
497 | return result;
|
---|
498 | }
|
---|
499 | return result;
|
---|
500 | }
|
---|
501 | }
|
---|
502 |
|
---|
503 | private void dataGridView_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e) {
|
---|
504 | if (Content == null) return;
|
---|
505 | if (e.Button == MouseButtons.Right && Content.ColumnNames.Count() != 0)
|
---|
506 | contextMenu.Show(MousePosition);
|
---|
507 | }
|
---|
508 | private void ShowHideColumns_Click(object sender, EventArgs e) {
|
---|
509 | new ColumnsVisibilityDialog(this.dataGridView.Columns.Cast<DataGridViewColumn>()).ShowDialog();
|
---|
510 | }
|
---|
511 |
|
---|
512 | private void UpdateVisibilityOfTextBoxes() {
|
---|
513 | rowsTextBox.Visible = columnsTextBox.Visible = showRowsAndColumnsTextBox;
|
---|
514 | rowsLabel.Visible = columnsLabel.Visible = showRowsAndColumnsTextBox;
|
---|
515 | UpdateDataGridViewSizeAndLocation();
|
---|
516 | }
|
---|
517 |
|
---|
518 | private void UpdateVisibilityOfStatisticalInformation() {
|
---|
519 | statisticsTextBox.Visible = showStatisticalInformation;
|
---|
520 | UpdateDataGridViewSizeAndLocation();
|
---|
521 | }
|
---|
522 |
|
---|
523 | private void UpdateDataGridViewSizeAndLocation() {
|
---|
524 | int headerSize = columnsTextBox.Location.Y + columnsTextBox.Size.Height +
|
---|
525 | columnsTextBox.Margin.Bottom + dataGridView.Margin.Top;
|
---|
526 |
|
---|
527 | int offset = showRowsAndColumnsTextBox ? headerSize : 0;
|
---|
528 | dataGridView.Location = new Point(0, offset);
|
---|
529 |
|
---|
530 | int statisticsTextBoxHeight = showStatisticalInformation ? statisticsTextBox.Height + statisticsTextBox.Margin.Top + statisticsTextBox.Margin.Bottom : 0;
|
---|
531 | dataGridView.Size = new Size(Size.Width, Size.Height - offset - statisticsTextBoxHeight);
|
---|
532 | }
|
---|
533 |
|
---|
534 | private void dataGridView_SelectionChanged(object sender, EventArgs e) {
|
---|
535 | string stringFormat = "{0,20:0.0000}";
|
---|
536 | statisticsTextBox.Text = string.Empty;
|
---|
537 | if (dataGridView.SelectedCells.Count > 1) {
|
---|
538 | List<double> selectedValues = new List<double>();
|
---|
539 | foreach (DataGridViewCell cell in dataGridView.SelectedCells) {
|
---|
540 | double value;
|
---|
541 | if (!double.TryParse(cell.Value.ToString(), out value)) return;
|
---|
542 | selectedValues.Add(value);
|
---|
543 | }
|
---|
544 | if (selectedValues.Count > 1) {
|
---|
545 | StringBuilder labelText = new StringBuilder();
|
---|
546 | labelText.Append("Count: " + string.Format(stringFormat, selectedValues.Count) + " ");
|
---|
547 | labelText.Append("Sum: " + string.Format(stringFormat, selectedValues.Sum()) + " ");
|
---|
548 | labelText.Append("Min: " + string.Format(stringFormat, selectedValues.Min()) + " ");
|
---|
549 | labelText.Append("Max: " + string.Format(stringFormat, selectedValues.Max()) + " ");
|
---|
550 | labelText.Append("Average: " + string.Format(stringFormat, selectedValues.Average()) + " ");
|
---|
551 | labelText.Append("Standard Deviation: " + string.Format(stringFormat, selectedValues.StandardDeviation()) + " ");
|
---|
552 |
|
---|
553 | statisticsTextBox.Text = labelText.ToString();
|
---|
554 | }
|
---|
555 | }
|
---|
556 | }
|
---|
557 | }
|
---|
558 | }
|
---|