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