[2669] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 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 {
|
---|
[4199] | 37 | private int[] virtualRowIndizes;
|
---|
[3430] | 38 | private List<KeyValuePair<int, SortOrder>> sortedColumnIndizes;
|
---|
| 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 |
|
---|
[3430] | 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 | }
|
---|
[3314] | 53 |
|
---|
[4652] | 54 | private bool showRowsAndColumnsTextBox;
|
---|
| 55 | public bool ShowRowsAndColumnsTextBox {
|
---|
| 56 | get { return showRowsAndColumnsTextBox; }
|
---|
| 57 | set {
|
---|
[4709] | 58 | showRowsAndColumnsTextBox = value;
|
---|
| 59 | UpdateVisibilityOfTextBoxes();
|
---|
[4652] | 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[4709] | 63 | private bool showStatisticalInformation;
|
---|
| 64 | public bool ShowStatisticalInformation {
|
---|
| 65 | get { return showStatisticalInformation; }
|
---|
| 66 | set {
|
---|
| 67 | showStatisticalInformation = value;
|
---|
| 68 | UpdateVisibilityOfStatisticalInformation();
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[3048] | 72 | public StringConvertibleMatrixView() {
|
---|
[2669] | 73 | InitializeComponent();
|
---|
[4709] | 74 | ShowRowsAndColumnsTextBox = true;
|
---|
[4720] | 75 | ShowStatisticalInformation = true;
|
---|
[2677] | 76 | errorProvider.SetIconAlignment(rowsTextBox, ErrorIconAlignment.MiddleLeft);
|
---|
| 77 | errorProvider.SetIconPadding(rowsTextBox, 2);
|
---|
| 78 | errorProvider.SetIconAlignment(columnsTextBox, ErrorIconAlignment.MiddleLeft);
|
---|
| 79 | errorProvider.SetIconPadding(columnsTextBox, 2);
|
---|
[3314] | 80 | sortedColumnIndizes = new List<KeyValuePair<int, SortOrder>>();
|
---|
| 81 | rowComparer = new RowComparer();
|
---|
[2669] | 82 | }
|
---|
| 83 |
|
---|
[2713] | 84 | protected override void DeregisterContentEvents() {
|
---|
| 85 | Content.ItemChanged -= new EventHandler<EventArgs<int, int>>(Content_ItemChanged);
|
---|
| 86 | Content.Reset -= new EventHandler(Content_Reset);
|
---|
[3320] | 87 | Content.ColumnNamesChanged -= new EventHandler(Content_ColumnNamesChanged);
|
---|
| 88 | Content.RowNamesChanged -= new EventHandler(Content_RowNamesChanged);
|
---|
[2713] | 89 | base.DeregisterContentEvents();
|
---|
[2669] | 90 | }
|
---|
[2713] | 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);
|
---|
[3320] | 95 | Content.ColumnNamesChanged += new EventHandler(Content_ColumnNamesChanged);
|
---|
| 96 | Content.RowNamesChanged += new EventHandler(Content_RowNamesChanged);
|
---|
[2669] | 97 | }
|
---|
| 98 |
|
---|
[2713] | 99 | protected override void OnContentChanged() {
|
---|
| 100 | base.OnContentChanged();
|
---|
| 101 | if (Content == null) {
|
---|
[2677] | 102 | rowsTextBox.Text = "";
|
---|
| 103 | columnsTextBox.Text = "";
|
---|
[2669] | 104 | dataGridView.Rows.Clear();
|
---|
[2677] | 105 | dataGridView.Columns.Clear();
|
---|
[3324] | 106 | virtualRowIndizes = new int[0];
|
---|
[3936] | 107 | } else
|
---|
[2713] | 108 | UpdateData();
|
---|
[3904] | 109 | }
|
---|
[3764] | 110 |
|
---|
[3904] | 111 | protected override void SetEnabledStateOfControls() {
|
---|
| 112 | base.SetEnabledStateOfControls();
|
---|
[3454] | 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;
|
---|
[3350] | 119 | }
|
---|
[2669] | 120 |
|
---|
[2713] | 121 | private void UpdateData() {
|
---|
| 122 | rowsTextBox.Text = Content.Rows.ToString();
|
---|
[2973] | 123 | rowsTextBox.Enabled = true;
|
---|
[2713] | 124 | columnsTextBox.Text = Content.Columns.ToString();
|
---|
[2973] | 125 | columnsTextBox.Enabled = true;
|
---|
[4850] | 126 | virtualRowIndizes = Enumerable.Range(0, Content.Rows).ToArray();
|
---|
[4707] | 127 |
|
---|
[4850] | 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 |
|
---|
[4178] | 141 | //DataGridViews with rows but no columns are not allowed !
|
---|
[3431] | 142 | if (Content.Rows == 0 && dataGridView.RowCount != Content.Rows && !Content.ReadOnly)
|
---|
[3335] | 143 | Content.Rows = dataGridView.RowCount;
|
---|
| 144 | else
|
---|
| 145 | dataGridView.RowCount = Content.Rows;
|
---|
[4798] | 146 |
|
---|
[4850] | 147 |
|
---|
[4523] | 148 | ClearSorting();
|
---|
[4199] | 149 | UpdateColumnHeaders();
|
---|
[3328] | 150 | UpdateRowHeaders();
|
---|
[4707] | 151 |
|
---|
[4199] | 152 | dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
|
---|
| 153 | dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
|
---|
[2669] | 154 | dataGridView.Enabled = true;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[4819] | 157 | protected virtual void UpdateColumnHeaders() {
|
---|
[4798] | 158 | HashSet<string> invisibleColumnNames = new HashSet<string>(dataGridView.Columns.OfType<DataGridViewColumn>()
|
---|
| 159 | .Where(c => !c.Visible && !string.IsNullOrEmpty(c.HeaderText)).Select(c => c.HeaderText));
|
---|
[4707] | 160 |
|
---|
[4178] | 161 | for (int i = 0; i < dataGridView.ColumnCount; i++) {
|
---|
[4707] | 162 | if (i < Content.ColumnNames.Count())
|
---|
[3312] | 163 | dataGridView.Columns[i].HeaderText = Content.ColumnNames.ElementAt(i);
|
---|
| 164 | else
|
---|
[3346] | 165 | dataGridView.Columns[i].HeaderText = "Column " + (i + 1);
|
---|
[4798] | 166 | dataGridView.Columns[i].Visible = !invisibleColumnNames.Contains(dataGridView.Columns[i].HeaderText);
|
---|
[3312] | 167 | }
|
---|
| 168 | }
|
---|
[4199] | 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);
|
---|
[3312] | 174 |
|
---|
[4199] | 175 | while (updatedRows < count) {
|
---|
[4707] | 176 | if (virtualRowIndizes[index] < Content.RowNames.Count())
|
---|
[4199] | 177 | dataGridView.Rows[index].HeaderCell.Value = Content.RowNames.ElementAt(virtualRowIndizes[index]);
|
---|
[3314] | 178 | else
|
---|
[4199] | 179 | dataGridView.Rows[index].HeaderCell.Value = "Row " + (index + 1);
|
---|
| 180 | if (dataGridView.Rows[index].Visible)
|
---|
| 181 | updatedRows++;
|
---|
| 182 | index++;
|
---|
[3312] | 183 | }
|
---|
| 184 | }
|
---|
| 185 |
|
---|
[3320] | 186 | private void Content_RowNamesChanged(object sender, EventArgs e) {
|
---|
[3328] | 187 | if (InvokeRequired)
|
---|
| 188 | Invoke(new EventHandler(Content_RowNamesChanged), sender, e);
|
---|
| 189 | else
|
---|
| 190 | UpdateRowHeaders();
|
---|
[3320] | 191 | }
|
---|
| 192 | private void Content_ColumnNamesChanged(object sender, EventArgs e) {
|
---|
[3328] | 193 | if (InvokeRequired)
|
---|
| 194 | Invoke(new EventHandler(Content_ColumnNamesChanged), sender, e);
|
---|
| 195 | else
|
---|
| 196 | UpdateColumnHeaders();
|
---|
[3320] | 197 | }
|
---|
[2713] | 198 | private void Content_ItemChanged(object sender, EventArgs<int, int> e) {
|
---|
[2669] | 199 | if (InvokeRequired)
|
---|
[2713] | 200 | Invoke(new EventHandler<EventArgs<int, int>>(Content_ItemChanged), sender, e);
|
---|
[3328] | 201 | else
|
---|
[3335] | 202 | dataGridView.InvalidateCell(e.Value2, e.Value);
|
---|
[2669] | 203 | }
|
---|
[2713] | 204 | private void Content_Reset(object sender, EventArgs e) {
|
---|
[2669] | 205 | if (InvokeRequired)
|
---|
[2713] | 206 | Invoke(new EventHandler(Content_Reset), sender, e);
|
---|
[2669] | 207 | else
|
---|
[2713] | 208 | UpdateData();
|
---|
[2669] | 209 | }
|
---|
| 210 |
|
---|
[2677] | 211 | #region TextBox Events
|
---|
| 212 | private void rowsTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
[3714] | 213 | if (ReadOnly || Locked)
|
---|
| 214 | return;
|
---|
[2669] | 215 | int i = 0;
|
---|
[3335] | 216 | if (!int.TryParse(rowsTextBox.Text, out i) || (i <= 0)) {
|
---|
[2676] | 217 | e.Cancel = true;
|
---|
[3335] | 218 | errorProvider.SetError(rowsTextBox, "Invalid Number of Rows (Valid values are positive integers larger than 0)");
|
---|
[2677] | 219 | rowsTextBox.SelectAll();
|
---|
[2669] | 220 | }
|
---|
| 221 | }
|
---|
[2677] | 222 | private void rowsTextBox_Validated(object sender, EventArgs e) {
|
---|
[3430] | 223 | if (!Content.ReadOnly) Content.Rows = int.Parse(rowsTextBox.Text);
|
---|
[2677] | 224 | errorProvider.SetError(rowsTextBox, string.Empty);
|
---|
[2669] | 225 | }
|
---|
[2677] | 226 | private void rowsTextBox_KeyDown(object sender, KeyEventArgs e) {
|
---|
[2669] | 227 | if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
|
---|
[2677] | 228 | rowsLabel.Focus(); // set focus on label to validate data
|
---|
[2676] | 229 | if (e.KeyCode == Keys.Escape) {
|
---|
[2973] | 230 | rowsTextBox.Text = Content.Rows.ToString();
|
---|
[2677] | 231 | rowsLabel.Focus(); // set focus on label to validate data
|
---|
[2676] | 232 | }
|
---|
[2669] | 233 | }
|
---|
[2677] | 234 | private void columnsTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
[3714] | 235 | if (ReadOnly || Locked)
|
---|
| 236 | return;
|
---|
[2677] | 237 | int i = 0;
|
---|
[3335] | 238 | if (!int.TryParse(columnsTextBox.Text, out i) || (i <= 0)) {
|
---|
[2677] | 239 | e.Cancel = true;
|
---|
[3335] | 240 | errorProvider.SetError(columnsTextBox, "Invalid Number of Columns (Valid values are positive integers larger than 0)");
|
---|
[2677] | 241 | columnsTextBox.SelectAll();
|
---|
| 242 | }
|
---|
| 243 | }
|
---|
| 244 | private void columnsTextBox_Validated(object sender, EventArgs e) {
|
---|
[3430] | 245 | if (!Content.ReadOnly) Content.Columns = int.Parse(columnsTextBox.Text);
|
---|
[2677] | 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) {
|
---|
[2713] | 252 | columnsTextBox.Text = Content.Columns.ToString();
|
---|
[2677] | 253 | columnsLabel.Focus(); // set focus on label to validate data
|
---|
| 254 | }
|
---|
| 255 | }
|
---|
| 256 | #endregion
|
---|
| 257 |
|
---|
| 258 | #region DataGridView Events
|
---|
[2669] | 259 | private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
|
---|
[3328] | 260 | if (!dataGridView.ReadOnly) {
|
---|
| 261 | string errorMessage;
|
---|
[4030] | 262 | if (Content != null && !Content.Validate(e.FormattedValue.ToString(), out errorMessage)) {
|
---|
[3328] | 263 | e.Cancel = true;
|
---|
| 264 | dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
|
---|
| 265 | }
|
---|
[2676] | 266 | }
|
---|
[2669] | 267 | }
|
---|
[2676] | 268 | private void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
|
---|
[3328] | 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 | }
|
---|
[2669] | 275 | }
|
---|
[2676] | 276 | private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
|
---|
| 277 | dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
|
---|
| 278 | }
|
---|
[3312] | 279 | private void dataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) {
|
---|
[4011] | 280 | if (Content != null && e.RowIndex < Content.Rows && e.ColumnIndex < Content.Columns) {
|
---|
[3337] | 281 | int rowIndex = virtualRowIndizes[e.RowIndex];
|
---|
[3335] | 282 | e.Value = Content.GetValue(rowIndex, e.ColumnIndex);
|
---|
[3337] | 283 | }
|
---|
[3312] | 284 | }
|
---|
[4178] | 285 |
|
---|
| 286 | private void dataGridView_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e) {
|
---|
| 287 | this.UpdateRowHeaders();
|
---|
[3312] | 288 | }
|
---|
| 289 | private void dataGridView_Resize(object sender, EventArgs e) {
|
---|
[4178] | 290 | this.UpdateRowHeaders();
|
---|
[3312] | 291 | }
|
---|
[3314] | 292 |
|
---|
[3643] | 293 | private void dataGridView_KeyDown(object sender, KeyEventArgs e) {
|
---|
[4178] | 294 | if (!ReadOnly && e.Control && e.KeyCode == Keys.V)
|
---|
| 295 | PasteValuesToDataGridView();
|
---|
| 296 | else if (e.Control && e.KeyCode == Keys.C)
|
---|
| 297 | CopyValuesFromDataGridView();
|
---|
| 298 | }
|
---|
[3643] | 299 |
|
---|
[4178] | 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 |
|
---|
[4546] | 319 | bool addRowNames = dataGridView.AreAllCellsSelected(false) && Content.RowNames.Count() > 0;
|
---|
| 320 | bool addColumnNames = dataGridView.AreAllCellsSelected(false) && Content.ColumnNames.Count() > 0;
|
---|
[4178] | 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);
|
---|
[3643] | 332 | }
|
---|
[4178] | 333 | s.Remove(s.Length - 1, 1); //remove last tab
|
---|
| 334 | s.Append(Environment.NewLine);
|
---|
| 335 | }
|
---|
[3643] | 336 |
|
---|
[4178] | 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));
|
---|
[4600] | 349 | s.Append('\t');
|
---|
[3643] | 350 | }
|
---|
[4600] | 351 |
|
---|
[4178] | 352 | column = dataGridView.Columns.GetNextColumn(column, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
|
---|
[3643] | 353 | }
|
---|
[4178] | 354 | s.Remove(s.Length - 1, 1); //remove last tab
|
---|
| 355 | s.Append(Environment.NewLine);
|
---|
| 356 | }
|
---|
| 357 | Clipboard.SetText(s.ToString());
|
---|
| 358 | }
|
---|
[3643] | 359 |
|
---|
[4178] | 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;
|
---|
[3643] | 367 | }
|
---|
[4178] | 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();
|
---|
[3643] | 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 |
|
---|
[3314] | 395 | private void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
[3320] | 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 | }
|
---|
[3314] | 405 |
|
---|
[3320] | 406 | if (!addToSortedIndizes)
|
---|
| 407 | sortedColumnIndizes.Clear();
|
---|
[3314] | 408 |
|
---|
[3320] | 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 | }
|
---|
[3314] | 420 | }
|
---|
| 421 | }
|
---|
| 422 |
|
---|
[4518] | 423 | protected virtual void ClearSorting() {
|
---|
[3643] | 424 | virtualRowIndizes = Enumerable.Range(0, Content.Rows).ToArray();
|
---|
| 425 | sortedColumnIndizes.Clear();
|
---|
| 426 | UpdateSortGlyph();
|
---|
| 427 | }
|
---|
| 428 |
|
---|
[3314] | 429 | private void Sort() {
|
---|
[3444] | 430 | virtualRowIndizes = Sort(sortedColumnIndizes);
|
---|
| 431 | UpdateSortGlyph();
|
---|
[3546] | 432 | UpdateRowHeaders();
|
---|
[3444] | 433 | dataGridView.Invalidate();
|
---|
| 434 | }
|
---|
| 435 | protected virtual int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
|
---|
[3328] | 436 | int[] newSortedIndex = Enumerable.Range(0, Content.Rows).ToArray();
|
---|
[3444] | 437 | if (sortedColumns.Count() != 0) {
|
---|
| 438 | rowComparer.SortedIndizes = sortedColumns;
|
---|
[3346] | 439 | rowComparer.Matrix = Content;
|
---|
[3314] | 440 | Array.Sort(newSortedIndex, rowComparer);
|
---|
| 441 | }
|
---|
[3444] | 442 | return newSortedIndex;
|
---|
[3328] | 443 | }
|
---|
| 444 | private void UpdateSortGlyph() {
|
---|
[3314] | 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 | }
|
---|
[2677] | 450 | #endregion
|
---|
[3312] | 451 |
|
---|
[3346] | 452 | public class RowComparer : IComparer<int> {
|
---|
[3314] | 453 | public RowComparer() {
|
---|
| 454 | }
|
---|
[3312] | 455 |
|
---|
[3346] | 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 |
|
---|
[3314] | 467 | public int Compare(int x, int y) {
|
---|
| 468 | int result = 0;
|
---|
| 469 | double double1, double2;
|
---|
| 470 | DateTime dateTime1, dateTime2;
|
---|
[3444] | 471 | TimeSpan timeSpan1, timeSpan2;
|
---|
[3314] | 472 | string string1, string2;
|
---|
| 473 |
|
---|
[3346] | 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)) {
|
---|
[3350] | 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);
|
---|
[3444] | 486 | else if (TimeSpan.TryParse(string1, out timeSpan1) && TimeSpan.TryParse(string2, out timeSpan2))
|
---|
| 487 | result = timeSpan1.CompareTo(timeSpan2);
|
---|
[3350] | 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;
|
---|
[3314] | 498 | }
|
---|
| 499 | return result;
|
---|
| 500 | }
|
---|
| 501 | }
|
---|
[3316] | 502 |
|
---|
[4213] | 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 | }
|
---|
[3316] | 508 | private void ShowHideColumns_Click(object sender, EventArgs e) {
|
---|
| 509 | new ColumnsVisibilityDialog(this.dataGridView.Columns.Cast<DataGridViewColumn>()).ShowDialog();
|
---|
| 510 | }
|
---|
[4652] | 511 |
|
---|
| 512 | private void UpdateVisibilityOfTextBoxes() {
|
---|
| 513 | rowsTextBox.Visible = columnsTextBox.Visible = showRowsAndColumnsTextBox;
|
---|
| 514 | rowsLabel.Visible = columnsLabel.Visible = showRowsAndColumnsTextBox;
|
---|
[4709] | 515 | UpdateDataGridViewSizeAndLocation();
|
---|
| 516 | }
|
---|
[4652] | 517 |
|
---|
[4709] | 518 | private void UpdateVisibilityOfStatisticalInformation() {
|
---|
[4779] | 519 | statisticsTextBox.Visible = showStatisticalInformation;
|
---|
[4709] | 520 | UpdateDataGridViewSizeAndLocation();
|
---|
| 521 | }
|
---|
| 522 |
|
---|
| 523 | private void UpdateDataGridViewSizeAndLocation() {
|
---|
[4652] | 524 | int headerSize = columnsTextBox.Location.Y + columnsTextBox.Size.Height +
|
---|
[4709] | 525 | columnsTextBox.Margin.Bottom + dataGridView.Margin.Top;
|
---|
[4652] | 526 |
|
---|
| 527 | int offset = showRowsAndColumnsTextBox ? headerSize : 0;
|
---|
| 528 | dataGridView.Location = new Point(0, offset);
|
---|
[4709] | 529 |
|
---|
[4779] | 530 | int statisticsTextBoxHeight = showStatisticalInformation ? statisticsTextBox.Height + statisticsTextBox.Margin.Top + statisticsTextBox.Margin.Bottom : 0;
|
---|
| 531 | dataGridView.Size = new Size(Size.Width, Size.Height - offset - statisticsTextBoxHeight);
|
---|
[4652] | 532 | }
|
---|
[4709] | 533 |
|
---|
| 534 | private void dataGridView_SelectionChanged(object sender, EventArgs e) {
|
---|
[4779] | 535 | string stringFormat = "{0,20:0.0000}";
|
---|
| 536 | statisticsTextBox.Text = string.Empty;
|
---|
[4709] | 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();
|
---|
[4779] | 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()) + " ");
|
---|
[4720] | 552 |
|
---|
[4779] | 553 | statisticsTextBox.Text = labelText.ToString();
|
---|
[4709] | 554 | }
|
---|
| 555 | }
|
---|
| 556 | }
|
---|
[2669] | 557 | }
|
---|
| 558 | }
|
---|