[2669] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 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;
|
---|
[4707] | 126 |
|
---|
[4178] | 127 | //DataGridViews with rows but no columns are not allowed !
|
---|
[3431] | 128 | if (Content.Rows == 0 && dataGridView.RowCount != Content.Rows && !Content.ReadOnly)
|
---|
[3335] | 129 | Content.Rows = dataGridView.RowCount;
|
---|
| 130 | else
|
---|
| 131 | dataGridView.RowCount = Content.Rows;
|
---|
[3431] | 132 | if (Content.Columns == 0 && dataGridView.ColumnCount != Content.Columns && !Content.ReadOnly)
|
---|
[3335] | 133 | Content.Columns = dataGridView.ColumnCount;
|
---|
| 134 | else
|
---|
| 135 | dataGridView.ColumnCount = Content.Columns;
|
---|
[4523] | 136 | ClearSorting();
|
---|
[3346] | 137 |
|
---|
[4199] | 138 | UpdateColumnHeaders();
|
---|
[3328] | 139 | UpdateRowHeaders();
|
---|
[4707] | 140 |
|
---|
[4199] | 141 | dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
|
---|
| 142 | dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
|
---|
[2669] | 143 | dataGridView.Enabled = true;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[4199] | 146 | protected void UpdateColumnHeaders() {
|
---|
[4707] | 147 | HashSet<string> visibleColumnNames = new HashSet<string>(dataGridView.Columns.OfType<DataGridViewColumn>()
|
---|
| 148 | .Where(c => c.Visible && !string.IsNullOrEmpty(c.HeaderText)).Select(c => c.HeaderText));
|
---|
[4717] | 149 | if (!visibleColumnNames.Intersect(Content.ColumnNames).Any())
|
---|
| 150 | visibleColumnNames.Clear();
|
---|
[4707] | 151 |
|
---|
[4178] | 152 | for (int i = 0; i < dataGridView.ColumnCount; i++) {
|
---|
[4707] | 153 | if (i < Content.ColumnNames.Count())
|
---|
[3312] | 154 | dataGridView.Columns[i].HeaderText = Content.ColumnNames.ElementAt(i);
|
---|
| 155 | else
|
---|
[3346] | 156 | dataGridView.Columns[i].HeaderText = "Column " + (i + 1);
|
---|
[3312] | 157 | }
|
---|
[4707] | 158 |
|
---|
| 159 | foreach (DataGridViewColumn column in dataGridView.Columns)
|
---|
| 160 | column.Visible = visibleColumnNames.Contains(column.HeaderText) || visibleColumnNames.Count == 0;
|
---|
[3312] | 161 | }
|
---|
[4199] | 162 | protected void UpdateRowHeaders() {
|
---|
| 163 | int index = dataGridView.FirstDisplayedScrollingRowIndex;
|
---|
| 164 | if (index == -1) index = 0;
|
---|
| 165 | int updatedRows = 0;
|
---|
| 166 | int count = dataGridView.DisplayedRowCount(true);
|
---|
[3312] | 167 |
|
---|
[4199] | 168 | while (updatedRows < count) {
|
---|
[4707] | 169 | if (virtualRowIndizes[index] < Content.RowNames.Count())
|
---|
[4199] | 170 | dataGridView.Rows[index].HeaderCell.Value = Content.RowNames.ElementAt(virtualRowIndizes[index]);
|
---|
[3314] | 171 | else
|
---|
[4199] | 172 | dataGridView.Rows[index].HeaderCell.Value = "Row " + (index + 1);
|
---|
| 173 | if (dataGridView.Rows[index].Visible)
|
---|
| 174 | updatedRows++;
|
---|
| 175 | index++;
|
---|
[3312] | 176 | }
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[3320] | 179 | private void Content_RowNamesChanged(object sender, EventArgs e) {
|
---|
[3328] | 180 | if (InvokeRequired)
|
---|
| 181 | Invoke(new EventHandler(Content_RowNamesChanged), sender, e);
|
---|
| 182 | else
|
---|
| 183 | UpdateRowHeaders();
|
---|
[3320] | 184 | }
|
---|
| 185 | private void Content_ColumnNamesChanged(object sender, EventArgs e) {
|
---|
[3328] | 186 | if (InvokeRequired)
|
---|
| 187 | Invoke(new EventHandler(Content_ColumnNamesChanged), sender, e);
|
---|
| 188 | else
|
---|
| 189 | UpdateColumnHeaders();
|
---|
[3320] | 190 | }
|
---|
[2713] | 191 | private void Content_ItemChanged(object sender, EventArgs<int, int> e) {
|
---|
[2669] | 192 | if (InvokeRequired)
|
---|
[2713] | 193 | Invoke(new EventHandler<EventArgs<int, int>>(Content_ItemChanged), sender, e);
|
---|
[3328] | 194 | else
|
---|
[3335] | 195 | dataGridView.InvalidateCell(e.Value2, e.Value);
|
---|
[2669] | 196 | }
|
---|
[2713] | 197 | private void Content_Reset(object sender, EventArgs e) {
|
---|
[2669] | 198 | if (InvokeRequired)
|
---|
[2713] | 199 | Invoke(new EventHandler(Content_Reset), sender, e);
|
---|
[2669] | 200 | else
|
---|
[2713] | 201 | UpdateData();
|
---|
[2669] | 202 | }
|
---|
| 203 |
|
---|
[2677] | 204 | #region TextBox Events
|
---|
| 205 | private void rowsTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
[3714] | 206 | if (ReadOnly || Locked)
|
---|
| 207 | return;
|
---|
[2669] | 208 | int i = 0;
|
---|
[3335] | 209 | if (!int.TryParse(rowsTextBox.Text, out i) || (i <= 0)) {
|
---|
[2676] | 210 | e.Cancel = true;
|
---|
[3335] | 211 | errorProvider.SetError(rowsTextBox, "Invalid Number of Rows (Valid values are positive integers larger than 0)");
|
---|
[2677] | 212 | rowsTextBox.SelectAll();
|
---|
[2669] | 213 | }
|
---|
| 214 | }
|
---|
[2677] | 215 | private void rowsTextBox_Validated(object sender, EventArgs e) {
|
---|
[3430] | 216 | if (!Content.ReadOnly) Content.Rows = int.Parse(rowsTextBox.Text);
|
---|
[2677] | 217 | errorProvider.SetError(rowsTextBox, string.Empty);
|
---|
[2669] | 218 | }
|
---|
[2677] | 219 | private void rowsTextBox_KeyDown(object sender, KeyEventArgs e) {
|
---|
[2669] | 220 | if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
|
---|
[2677] | 221 | rowsLabel.Focus(); // set focus on label to validate data
|
---|
[2676] | 222 | if (e.KeyCode == Keys.Escape) {
|
---|
[2973] | 223 | rowsTextBox.Text = Content.Rows.ToString();
|
---|
[2677] | 224 | rowsLabel.Focus(); // set focus on label to validate data
|
---|
[2676] | 225 | }
|
---|
[2669] | 226 | }
|
---|
[2677] | 227 | private void columnsTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
[3714] | 228 | if (ReadOnly || Locked)
|
---|
| 229 | return;
|
---|
[2677] | 230 | int i = 0;
|
---|
[3335] | 231 | if (!int.TryParse(columnsTextBox.Text, out i) || (i <= 0)) {
|
---|
[2677] | 232 | e.Cancel = true;
|
---|
[3335] | 233 | errorProvider.SetError(columnsTextBox, "Invalid Number of Columns (Valid values are positive integers larger than 0)");
|
---|
[2677] | 234 | columnsTextBox.SelectAll();
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
| 237 | private void columnsTextBox_Validated(object sender, EventArgs e) {
|
---|
[3430] | 238 | if (!Content.ReadOnly) Content.Columns = int.Parse(columnsTextBox.Text);
|
---|
[2677] | 239 | errorProvider.SetError(columnsTextBox, string.Empty);
|
---|
| 240 | }
|
---|
| 241 | private void columnsTextBox_KeyDown(object sender, KeyEventArgs e) {
|
---|
| 242 | if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
|
---|
| 243 | columnsLabel.Focus(); // set focus on label to validate data
|
---|
| 244 | if (e.KeyCode == Keys.Escape) {
|
---|
[2713] | 245 | columnsTextBox.Text = Content.Columns.ToString();
|
---|
[2677] | 246 | columnsLabel.Focus(); // set focus on label to validate data
|
---|
| 247 | }
|
---|
| 248 | }
|
---|
| 249 | #endregion
|
---|
| 250 |
|
---|
| 251 | #region DataGridView Events
|
---|
[2669] | 252 | private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
|
---|
[3328] | 253 | if (!dataGridView.ReadOnly) {
|
---|
| 254 | string errorMessage;
|
---|
[4030] | 255 | if (Content != null && !Content.Validate(e.FormattedValue.ToString(), out errorMessage)) {
|
---|
[3328] | 256 | e.Cancel = true;
|
---|
| 257 | dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
|
---|
| 258 | }
|
---|
[2676] | 259 | }
|
---|
[2669] | 260 | }
|
---|
[2676] | 261 | private void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
|
---|
[3328] | 262 | if (!dataGridView.ReadOnly) {
|
---|
| 263 | string value = e.Value.ToString();
|
---|
| 264 | int rowIndex = virtualRowIndizes[e.RowIndex];
|
---|
| 265 | e.ParsingApplied = Content.SetValue(value, rowIndex, e.ColumnIndex);
|
---|
| 266 | if (e.ParsingApplied) e.Value = Content.GetValue(rowIndex, e.ColumnIndex);
|
---|
| 267 | }
|
---|
[2669] | 268 | }
|
---|
[2676] | 269 | private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
|
---|
| 270 | dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
|
---|
| 271 | }
|
---|
[3312] | 272 | private void dataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) {
|
---|
[4011] | 273 | if (Content != null && e.RowIndex < Content.Rows && e.ColumnIndex < Content.Columns) {
|
---|
[3337] | 274 | int rowIndex = virtualRowIndizes[e.RowIndex];
|
---|
[3335] | 275 | e.Value = Content.GetValue(rowIndex, e.ColumnIndex);
|
---|
[3337] | 276 | }
|
---|
[3312] | 277 | }
|
---|
[4178] | 278 |
|
---|
| 279 | private void dataGridView_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e) {
|
---|
| 280 | this.UpdateRowHeaders();
|
---|
[3312] | 281 | }
|
---|
| 282 | private void dataGridView_Resize(object sender, EventArgs e) {
|
---|
[4178] | 283 | this.UpdateRowHeaders();
|
---|
[3312] | 284 | }
|
---|
[3314] | 285 |
|
---|
[3643] | 286 | private void dataGridView_KeyDown(object sender, KeyEventArgs e) {
|
---|
[4178] | 287 | if (!ReadOnly && e.Control && e.KeyCode == Keys.V)
|
---|
| 288 | PasteValuesToDataGridView();
|
---|
| 289 | else if (e.Control && e.KeyCode == Keys.C)
|
---|
| 290 | CopyValuesFromDataGridView();
|
---|
| 291 | }
|
---|
[3643] | 292 |
|
---|
[4178] | 293 | private void CopyValuesFromDataGridView() {
|
---|
| 294 | if (dataGridView.SelectedCells.Count == 0) return;
|
---|
| 295 | StringBuilder s = new StringBuilder();
|
---|
| 296 | int minRowIndex = dataGridView.SelectedCells[0].RowIndex;
|
---|
| 297 | int maxRowIndex = dataGridView.SelectedCells[dataGridView.SelectedCells.Count - 1].RowIndex;
|
---|
| 298 | int minColIndex = dataGridView.SelectedCells[0].ColumnIndex;
|
---|
| 299 | int maxColIndex = dataGridView.SelectedCells[dataGridView.SelectedCells.Count - 1].ColumnIndex;
|
---|
| 300 |
|
---|
| 301 | if (minRowIndex > maxRowIndex) {
|
---|
| 302 | int temp = minRowIndex;
|
---|
| 303 | minRowIndex = maxRowIndex;
|
---|
| 304 | maxRowIndex = temp;
|
---|
| 305 | }
|
---|
| 306 | if (minColIndex > maxColIndex) {
|
---|
| 307 | int temp = minColIndex;
|
---|
| 308 | minColIndex = maxColIndex;
|
---|
| 309 | maxColIndex = temp;
|
---|
| 310 | }
|
---|
| 311 |
|
---|
[4546] | 312 | bool addRowNames = dataGridView.AreAllCellsSelected(false) && Content.RowNames.Count() > 0;
|
---|
| 313 | bool addColumnNames = dataGridView.AreAllCellsSelected(false) && Content.ColumnNames.Count() > 0;
|
---|
[4178] | 314 |
|
---|
| 315 | //add colum names
|
---|
| 316 | if (addColumnNames) {
|
---|
| 317 | if (addRowNames)
|
---|
| 318 | s.Append('\t');
|
---|
| 319 |
|
---|
| 320 | DataGridViewColumn column = dataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
|
---|
| 321 | while (column != null) {
|
---|
| 322 | s.Append(column.HeaderText);
|
---|
| 323 | s.Append('\t');
|
---|
| 324 | column = dataGridView.Columns.GetNextColumn(column, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
|
---|
[3643] | 325 | }
|
---|
[4178] | 326 | s.Remove(s.Length - 1, 1); //remove last tab
|
---|
| 327 | s.Append(Environment.NewLine);
|
---|
| 328 | }
|
---|
[3643] | 329 |
|
---|
[4178] | 330 | for (int i = minRowIndex; i <= maxRowIndex; i++) {
|
---|
| 331 | int rowIndex = this.virtualRowIndizes[i];
|
---|
| 332 | if (addRowNames) {
|
---|
| 333 | s.Append(Content.RowNames.ElementAt(rowIndex));
|
---|
| 334 | s.Append('\t');
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | DataGridViewColumn column = dataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
|
---|
| 338 | while (column != null) {
|
---|
| 339 | DataGridViewCell cell = dataGridView[column.Index, i];
|
---|
| 340 | if (cell.Selected) {
|
---|
| 341 | s.Append(Content.GetValue(rowIndex, column.Index));
|
---|
[4600] | 342 | s.Append('\t');
|
---|
[3643] | 343 | }
|
---|
[4600] | 344 |
|
---|
[4178] | 345 | column = dataGridView.Columns.GetNextColumn(column, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
|
---|
[3643] | 346 | }
|
---|
[4178] | 347 | s.Remove(s.Length - 1, 1); //remove last tab
|
---|
| 348 | s.Append(Environment.NewLine);
|
---|
| 349 | }
|
---|
| 350 | Clipboard.SetText(s.ToString());
|
---|
| 351 | }
|
---|
[3643] | 352 |
|
---|
[4178] | 353 | private void PasteValuesToDataGridView() {
|
---|
| 354 | string[,] values = SplitClipboardString(Clipboard.GetText());
|
---|
| 355 | int rowIndex = 0;
|
---|
| 356 | int columnIndex = 0;
|
---|
| 357 | if (dataGridView.CurrentCell != null) {
|
---|
| 358 | rowIndex = dataGridView.CurrentCell.RowIndex;
|
---|
| 359 | columnIndex = dataGridView.CurrentCell.ColumnIndex;
|
---|
[3643] | 360 | }
|
---|
[4178] | 361 |
|
---|
| 362 | for (int row = 0; row < values.GetLength(1); row++) {
|
---|
| 363 | if (row + rowIndex >= Content.Rows)
|
---|
| 364 | Content.Rows = Content.Rows + 1;
|
---|
| 365 | for (int col = 0; col < values.GetLength(0); col++) {
|
---|
| 366 | if (col + columnIndex >= Content.Columns)
|
---|
| 367 | Content.Columns = Content.Columns + 1;
|
---|
| 368 | Content.SetValue(values[col, row], row + rowIndex, col + columnIndex);
|
---|
| 369 | }
|
---|
| 370 | }
|
---|
| 371 | ClearSorting();
|
---|
[3643] | 372 | }
|
---|
| 373 | private string[,] SplitClipboardString(string clipboardText) {
|
---|
| 374 | clipboardText = clipboardText.Remove(clipboardText.Length - Environment.NewLine.Length); //remove last newline constant
|
---|
| 375 | string[,] values = null;
|
---|
| 376 | string[] lines = clipboardText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
|
---|
| 377 | string[] cells;
|
---|
| 378 | for (int i = 0; i < lines.Length; i++) {
|
---|
| 379 | cells = lines[i].Split('\t');
|
---|
| 380 | if (values == null)
|
---|
| 381 | values = new string[cells.Length, lines.Length];
|
---|
| 382 | for (int j = 0; j < cells.Length; j++)
|
---|
| 383 | values[j, i] = string.IsNullOrEmpty(cells[j]) ? string.Empty : cells[j];
|
---|
| 384 | }
|
---|
| 385 | return values;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
[3314] | 388 | private void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
[3320] | 389 | if (Content != null) {
|
---|
| 390 | if (e.Button == MouseButtons.Left && Content.SortableView) {
|
---|
| 391 | bool addToSortedIndizes = (Control.ModifierKeys & Keys.Control) == Keys.Control;
|
---|
| 392 | SortOrder newSortOrder = SortOrder.Ascending;
|
---|
| 393 | if (sortedColumnIndizes.Any(x => x.Key == e.ColumnIndex)) {
|
---|
| 394 | SortOrder oldSortOrder = sortedColumnIndizes.Where(x => x.Key == e.ColumnIndex).First().Value;
|
---|
| 395 | int enumLength = Enum.GetValues(typeof(SortOrder)).Length;
|
---|
| 396 | newSortOrder = oldSortOrder = (SortOrder)Enum.Parse(typeof(SortOrder), ((((int)oldSortOrder) + 1) % enumLength).ToString());
|
---|
| 397 | }
|
---|
[3314] | 398 |
|
---|
[3320] | 399 | if (!addToSortedIndizes)
|
---|
| 400 | sortedColumnIndizes.Clear();
|
---|
[3314] | 401 |
|
---|
[3320] | 402 | if (sortedColumnIndizes.Any(x => x.Key == e.ColumnIndex)) {
|
---|
| 403 | int sortedIndex = sortedColumnIndizes.FindIndex(x => x.Key == e.ColumnIndex);
|
---|
| 404 | if (newSortOrder != SortOrder.None)
|
---|
| 405 | sortedColumnIndizes[sortedIndex] = new KeyValuePair<int, SortOrder>(e.ColumnIndex, newSortOrder);
|
---|
| 406 | else
|
---|
| 407 | sortedColumnIndizes.RemoveAt(sortedIndex);
|
---|
| 408 | } else
|
---|
| 409 | if (newSortOrder != SortOrder.None)
|
---|
| 410 | sortedColumnIndizes.Add(new KeyValuePair<int, SortOrder>(e.ColumnIndex, newSortOrder));
|
---|
| 411 | Sort();
|
---|
| 412 | }
|
---|
[3314] | 413 | }
|
---|
| 414 | }
|
---|
| 415 |
|
---|
[4518] | 416 | protected virtual void ClearSorting() {
|
---|
[3643] | 417 | virtualRowIndizes = Enumerable.Range(0, Content.Rows).ToArray();
|
---|
| 418 | sortedColumnIndizes.Clear();
|
---|
| 419 | UpdateSortGlyph();
|
---|
| 420 | }
|
---|
| 421 |
|
---|
[3314] | 422 | private void Sort() {
|
---|
[3444] | 423 | virtualRowIndizes = Sort(sortedColumnIndizes);
|
---|
| 424 | UpdateSortGlyph();
|
---|
[3546] | 425 | UpdateRowHeaders();
|
---|
[3444] | 426 | dataGridView.Invalidate();
|
---|
| 427 | }
|
---|
| 428 | protected virtual int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
|
---|
[3328] | 429 | int[] newSortedIndex = Enumerable.Range(0, Content.Rows).ToArray();
|
---|
[3444] | 430 | if (sortedColumns.Count() != 0) {
|
---|
| 431 | rowComparer.SortedIndizes = sortedColumns;
|
---|
[3346] | 432 | rowComparer.Matrix = Content;
|
---|
[3314] | 433 | Array.Sort(newSortedIndex, rowComparer);
|
---|
| 434 | }
|
---|
[3444] | 435 | return newSortedIndex;
|
---|
[3328] | 436 | }
|
---|
| 437 | private void UpdateSortGlyph() {
|
---|
[3314] | 438 | foreach (DataGridViewColumn col in this.dataGridView.Columns)
|
---|
| 439 | col.HeaderCell.SortGlyphDirection = SortOrder.None;
|
---|
| 440 | foreach (KeyValuePair<int, SortOrder> p in sortedColumnIndizes)
|
---|
| 441 | this.dataGridView.Columns[p.Key].HeaderCell.SortGlyphDirection = p.Value;
|
---|
| 442 | }
|
---|
[2677] | 443 | #endregion
|
---|
[3312] | 444 |
|
---|
[3346] | 445 | public class RowComparer : IComparer<int> {
|
---|
[3314] | 446 | public RowComparer() {
|
---|
| 447 | }
|
---|
[3312] | 448 |
|
---|
[3346] | 449 | private List<KeyValuePair<int, SortOrder>> sortedIndizes;
|
---|
| 450 | public IEnumerable<KeyValuePair<int, SortOrder>> SortedIndizes {
|
---|
| 451 | get { return this.sortedIndizes; }
|
---|
| 452 | set { sortedIndizes = new List<KeyValuePair<int, SortOrder>>(value); }
|
---|
| 453 | }
|
---|
| 454 | private IStringConvertibleMatrix matrix;
|
---|
| 455 | public IStringConvertibleMatrix Matrix {
|
---|
| 456 | get { return this.matrix; }
|
---|
| 457 | set { this.matrix = value; }
|
---|
| 458 | }
|
---|
| 459 |
|
---|
[3314] | 460 | public int Compare(int x, int y) {
|
---|
| 461 | int result = 0;
|
---|
| 462 | double double1, double2;
|
---|
| 463 | DateTime dateTime1, dateTime2;
|
---|
[3444] | 464 | TimeSpan timeSpan1, timeSpan2;
|
---|
[3314] | 465 | string string1, string2;
|
---|
| 466 |
|
---|
[3346] | 467 | if (matrix == null)
|
---|
| 468 | throw new InvalidOperationException("Could not sort IStringConvertibleMatrix if the matrix member is null.");
|
---|
| 469 | if (sortedIndizes == null)
|
---|
| 470 | return 0;
|
---|
| 471 |
|
---|
| 472 | foreach (KeyValuePair<int, SortOrder> pair in sortedIndizes.Where(p => p.Value != SortOrder.None)) {
|
---|
[3350] | 473 | string1 = matrix.GetValue(x, pair.Key);
|
---|
| 474 | string2 = matrix.GetValue(y, pair.Key);
|
---|
| 475 | if (double.TryParse(string1, out double1) && double.TryParse(string2, out double2))
|
---|
| 476 | result = double1.CompareTo(double2);
|
---|
| 477 | else if (DateTime.TryParse(string1, out dateTime1) && DateTime.TryParse(string2, out dateTime2))
|
---|
| 478 | result = dateTime1.CompareTo(dateTime2);
|
---|
[3444] | 479 | else if (TimeSpan.TryParse(string1, out timeSpan1) && TimeSpan.TryParse(string2, out timeSpan2))
|
---|
| 480 | result = timeSpan1.CompareTo(timeSpan2);
|
---|
[3350] | 481 | else {
|
---|
| 482 | if (string1 != null)
|
---|
| 483 | result = string1.CompareTo(string2);
|
---|
| 484 | else if (string2 != null)
|
---|
| 485 | result = string2.CompareTo(string1) * -1;
|
---|
| 486 | }
|
---|
| 487 | if (pair.Value == SortOrder.Descending)
|
---|
| 488 | result *= -1;
|
---|
| 489 | if (result != 0)
|
---|
| 490 | return result;
|
---|
[3314] | 491 | }
|
---|
| 492 | return result;
|
---|
| 493 | }
|
---|
| 494 | }
|
---|
[3316] | 495 |
|
---|
[4213] | 496 | private void dataGridView_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e) {
|
---|
| 497 | if (Content == null) return;
|
---|
| 498 | if (e.Button == MouseButtons.Right && Content.ColumnNames.Count() != 0)
|
---|
| 499 | contextMenu.Show(MousePosition);
|
---|
| 500 | }
|
---|
[3316] | 501 | private void ShowHideColumns_Click(object sender, EventArgs e) {
|
---|
| 502 | new ColumnsVisibilityDialog(this.dataGridView.Columns.Cast<DataGridViewColumn>()).ShowDialog();
|
---|
| 503 | }
|
---|
[4652] | 504 |
|
---|
| 505 | private void UpdateVisibilityOfTextBoxes() {
|
---|
| 506 | rowsTextBox.Visible = columnsTextBox.Visible = showRowsAndColumnsTextBox;
|
---|
| 507 | rowsLabel.Visible = columnsLabel.Visible = showRowsAndColumnsTextBox;
|
---|
[4709] | 508 | UpdateDataGridViewSizeAndLocation();
|
---|
| 509 | }
|
---|
[4652] | 510 |
|
---|
[4709] | 511 | private void UpdateVisibilityOfStatisticalInformation() {
|
---|
| 512 | statusStrip.Visible = showStatisticalInformation;
|
---|
| 513 | UpdateDataGridViewSizeAndLocation();
|
---|
| 514 | }
|
---|
| 515 |
|
---|
| 516 | private void UpdateDataGridViewSizeAndLocation() {
|
---|
[4652] | 517 | int headerSize = columnsTextBox.Location.Y + columnsTextBox.Size.Height +
|
---|
[4709] | 518 | columnsTextBox.Margin.Bottom + dataGridView.Margin.Top;
|
---|
[4652] | 519 |
|
---|
| 520 | int offset = showRowsAndColumnsTextBox ? headerSize : 0;
|
---|
| 521 | dataGridView.Location = new Point(0, offset);
|
---|
[4709] | 522 |
|
---|
| 523 | int statusStripHeight = showStatisticalInformation ? statusStrip.Height : 0;
|
---|
| 524 | dataGridView.Size = new Size(Size.Width, Size.Height - offset - statusStripHeight);
|
---|
[4652] | 525 | }
|
---|
[4709] | 526 |
|
---|
| 527 | private void dataGridView_SelectionChanged(object sender, EventArgs e) {
|
---|
| 528 | toolStripStatusLabel.Text = string.Empty;
|
---|
| 529 | if (dataGridView.SelectedCells.Count > 1) {
|
---|
| 530 | List<double> selectedValues = new List<double>();
|
---|
| 531 | foreach (DataGridViewCell cell in dataGridView.SelectedCells) {
|
---|
| 532 | double value;
|
---|
| 533 | if (!double.TryParse(cell.Value.ToString(), out value)) return;
|
---|
| 534 | selectedValues.Add(value);
|
---|
| 535 | }
|
---|
| 536 | if (selectedValues.Count > 1) {
|
---|
| 537 | StringBuilder labelText = new StringBuilder();
|
---|
[4720] | 538 | labelText.Append("Count: " + selectedValues.Count + " ");
|
---|
| 539 | labelText.Append("Sum: " + selectedValues.Sum() + " ");
|
---|
| 540 | labelText.Append("Min: " + selectedValues.Min() + " ");
|
---|
| 541 | labelText.Append("Max: " + selectedValues.Max() + " ");
|
---|
[4709] | 542 | labelText.Append("Average: " + selectedValues.Average() + " ");
|
---|
[4720] | 543 | labelText.Append("Standard Deviation: " + selectedValues.StandardDeviation() + " ");
|
---|
| 544 |
|
---|
[4709] | 545 | toolStripStatusLabel.Text = labelText.ToString();
|
---|
| 546 | }
|
---|
| 547 | }
|
---|
| 548 | }
|
---|
[2669] | 549 | }
|
---|
| 550 | }
|
---|