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