[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;
|
---|
| 23 | using System.ComponentModel;
|
---|
[3314] | 24 | using System.Collections.Generic;
|
---|
[2669] | 25 | using System.Drawing;
|
---|
[3312] | 26 | using System.Linq;
|
---|
[2669] | 27 | using System.Windows.Forms;
|
---|
| 28 | using HeuristicLab.Common;
|
---|
| 29 | using HeuristicLab.MainForm;
|
---|
[2713] | 30 | using HeuristicLab.MainForm.WindowsForms;
|
---|
[2669] | 31 |
|
---|
| 32 | namespace HeuristicLab.Data.Views {
|
---|
[3048] | 33 | [View("StringConvertibleMatrix View")]
|
---|
| 34 | [Content(typeof(IStringConvertibleMatrix), true)]
|
---|
[3228] | 35 | public partial class StringConvertibleMatrixView : AsynchronousContentView {
|
---|
[3048] | 36 | public new IStringConvertibleMatrix Content {
|
---|
| 37 | get { return (IStringConvertibleMatrix)base.Content; }
|
---|
[2713] | 38 | set { base.Content = value; }
|
---|
[2669] | 39 | }
|
---|
| 40 |
|
---|
[3314] | 41 | private int[] virtualRowIndizes;
|
---|
| 42 | private List<KeyValuePair<int, SortOrder>> sortedColumnIndizes;
|
---|
| 43 | RowComparer rowComparer;
|
---|
| 44 |
|
---|
[3048] | 45 | public StringConvertibleMatrixView() {
|
---|
[2669] | 46 | InitializeComponent();
|
---|
[3048] | 47 | Caption = "StringConvertibleMatrix View";
|
---|
[2677] | 48 | errorProvider.SetIconAlignment(rowsTextBox, ErrorIconAlignment.MiddleLeft);
|
---|
| 49 | errorProvider.SetIconPadding(rowsTextBox, 2);
|
---|
| 50 | errorProvider.SetIconAlignment(columnsTextBox, ErrorIconAlignment.MiddleLeft);
|
---|
| 51 | errorProvider.SetIconPadding(columnsTextBox, 2);
|
---|
[3314] | 52 | sortedColumnIndizes = new List<KeyValuePair<int, SortOrder>>();
|
---|
| 53 | rowComparer = new RowComparer();
|
---|
[2669] | 54 | }
|
---|
[3048] | 55 | public StringConvertibleMatrixView(IStringConvertibleMatrix content)
|
---|
[2669] | 56 | : this() {
|
---|
[2727] | 57 | Content = content;
|
---|
[2669] | 58 | }
|
---|
| 59 |
|
---|
[2713] | 60 | protected override void DeregisterContentEvents() {
|
---|
| 61 | Content.ItemChanged -= new EventHandler<EventArgs<int, int>>(Content_ItemChanged);
|
---|
| 62 | Content.Reset -= new EventHandler(Content_Reset);
|
---|
[3320] | 63 | Content.ColumnNamesChanged -= new EventHandler(Content_ColumnNamesChanged);
|
---|
| 64 | Content.RowNamesChanged -= new EventHandler(Content_RowNamesChanged);
|
---|
[2713] | 65 | base.DeregisterContentEvents();
|
---|
[2669] | 66 | }
|
---|
| 67 |
|
---|
| 68 |
|
---|
[2713] | 69 | protected override void RegisterContentEvents() {
|
---|
| 70 | base.RegisterContentEvents();
|
---|
| 71 | Content.ItemChanged += new EventHandler<EventArgs<int, int>>(Content_ItemChanged);
|
---|
| 72 | Content.Reset += new EventHandler(Content_Reset);
|
---|
[3320] | 73 | Content.ColumnNamesChanged += new EventHandler(Content_ColumnNamesChanged);
|
---|
| 74 | Content.RowNamesChanged += new EventHandler(Content_RowNamesChanged);
|
---|
[2669] | 75 | }
|
---|
| 76 |
|
---|
[3320] | 77 |
|
---|
| 78 |
|
---|
[2713] | 79 | protected override void OnContentChanged() {
|
---|
| 80 | base.OnContentChanged();
|
---|
[3314] | 81 | sortedColumnIndizes.Clear();
|
---|
| 82 | virtualRowIndizes = new int[0];
|
---|
[2713] | 83 | if (Content == null) {
|
---|
[3048] | 84 | Caption = "StringConvertibleMatrix View";
|
---|
[2677] | 85 | rowsTextBox.Text = "";
|
---|
| 86 | rowsTextBox.Enabled = false;
|
---|
| 87 | columnsTextBox.Text = "";
|
---|
| 88 | columnsTextBox.Enabled = false;
|
---|
[2669] | 89 | dataGridView.Rows.Clear();
|
---|
[2677] | 90 | dataGridView.Columns.Clear();
|
---|
[2669] | 91 | dataGridView.Enabled = false;
|
---|
| 92 | } else {
|
---|
[3048] | 93 | Caption = "StringConvertibleMatrix (" + Content.GetType().Name + ")";
|
---|
[2713] | 94 | UpdateData();
|
---|
[2669] | 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[2713] | 98 | private void UpdateData() {
|
---|
| 99 | rowsTextBox.Text = Content.Rows.ToString();
|
---|
[2973] | 100 | rowsTextBox.Enabled = true;
|
---|
[2713] | 101 | columnsTextBox.Text = Content.Columns.ToString();
|
---|
[2973] | 102 | columnsTextBox.Enabled = true;
|
---|
[3312] | 103 | dataGridView.RowCount = 0;
|
---|
| 104 | dataGridView.ColumnCount = 0;
|
---|
[2713] | 105 | if ((Content.Rows > 0) && (Content.Columns > 0)) {
|
---|
[3314] | 106 | virtualRowIndizes = Enumerable.Range(0, Content.Rows - 1).ToArray();
|
---|
[2713] | 107 | dataGridView.RowCount = Content.Rows;
|
---|
[3312] | 108 | dataGridView.ColumnCount = Content.Columns;
|
---|
| 109 | UpdateRowHeaders();
|
---|
| 110 | UpdateColumnHeaders();
|
---|
[2677] | 111 | }
|
---|
[3317] | 112 | dataGridView.ReadOnly = Content.ReadOnlyView;
|
---|
[2669] | 113 | dataGridView.Enabled = true;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[3312] | 116 | private void UpdateColumnHeaders() {
|
---|
| 117 | for (int i = 0; i < Content.Columns; i++) {
|
---|
| 118 | if (Content.ColumnNames.Count() != 0)
|
---|
| 119 | dataGridView.Columns[i].HeaderText = Content.ColumnNames.ElementAt(i);
|
---|
| 120 | else
|
---|
| 121 | dataGridView.Columns[i].HeaderText = "Column " + i;
|
---|
| 122 | }
|
---|
| 123 | dataGridView.Invalidate();
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | private void UpdateRowHeaders() {
|
---|
| 127 | for (int i = dataGridView.FirstDisplayedScrollingRowIndex; i < dataGridView.FirstDisplayedScrollingRowIndex + dataGridView.DisplayedRowCount(true); i++) {
|
---|
| 128 | if (Content.RowNames.Count() != 0)
|
---|
| 129 | dataGridView.Rows[i].HeaderCell.Value = Content.RowNames.ElementAt(i);
|
---|
[3314] | 130 | else
|
---|
[3312] | 131 | dataGridView.Rows[i].HeaderCell.Value = i.ToString();
|
---|
| 132 | }
|
---|
| 133 | dataGridView.Invalidate();
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[3320] | 136 | private void Content_RowNamesChanged(object sender, EventArgs e) {
|
---|
| 137 | UpdateColumnHeaders();
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | private void Content_ColumnNamesChanged(object sender, EventArgs e) {
|
---|
| 141 | UpdateRowHeaders();
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[2713] | 144 | private void Content_ItemChanged(object sender, EventArgs<int, int> e) {
|
---|
[2669] | 145 | if (InvokeRequired)
|
---|
[2713] | 146 | Invoke(new EventHandler<EventArgs<int, int>>(Content_ItemChanged), sender, e);
|
---|
[2677] | 147 | else {
|
---|
[3314] | 148 | dataGridView.InvalidateCell(e.Value, e.Value2);
|
---|
[2677] | 149 | }
|
---|
[2669] | 150 | }
|
---|
[2713] | 151 | private void Content_Reset(object sender, EventArgs e) {
|
---|
[2669] | 152 | if (InvokeRequired)
|
---|
[2713] | 153 | Invoke(new EventHandler(Content_Reset), sender, e);
|
---|
[2669] | 154 | else
|
---|
[2713] | 155 | UpdateData();
|
---|
[2669] | 156 | }
|
---|
| 157 |
|
---|
[2677] | 158 | #region TextBox Events
|
---|
| 159 | private void rowsTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
[2669] | 160 | int i = 0;
|
---|
[2677] | 161 | if (!int.TryParse(rowsTextBox.Text, out i) || (i < 0)) {
|
---|
[2676] | 162 | e.Cancel = true;
|
---|
[2694] | 163 | errorProvider.SetError(rowsTextBox, "Invalid Number of Rows (Valid Values: Positive Integers Larger or Equal to 0)");
|
---|
[2677] | 164 | rowsTextBox.SelectAll();
|
---|
[2669] | 165 | }
|
---|
| 166 | }
|
---|
[2677] | 167 | private void rowsTextBox_Validated(object sender, EventArgs e) {
|
---|
[2713] | 168 | Content.Rows = int.Parse(rowsTextBox.Text);
|
---|
[2677] | 169 | errorProvider.SetError(rowsTextBox, string.Empty);
|
---|
[2669] | 170 | }
|
---|
[2677] | 171 | private void rowsTextBox_KeyDown(object sender, KeyEventArgs e) {
|
---|
[2669] | 172 | if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
|
---|
[2677] | 173 | rowsLabel.Focus(); // set focus on label to validate data
|
---|
[2676] | 174 | if (e.KeyCode == Keys.Escape) {
|
---|
[2973] | 175 | rowsTextBox.Text = Content.Rows.ToString();
|
---|
[2677] | 176 | rowsLabel.Focus(); // set focus on label to validate data
|
---|
[2676] | 177 | }
|
---|
[2669] | 178 | }
|
---|
[2677] | 179 | private void columnsTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
| 180 | int i = 0;
|
---|
| 181 | if (!int.TryParse(columnsTextBox.Text, out i) || (i < 0)) {
|
---|
| 182 | e.Cancel = true;
|
---|
[2694] | 183 | errorProvider.SetError(columnsTextBox, "Invalid Number of Columns (Valid Values: Positive Integers Larger or Equal to 0)");
|
---|
[2677] | 184 | columnsTextBox.SelectAll();
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 | private void columnsTextBox_Validated(object sender, EventArgs e) {
|
---|
[2713] | 188 | Content.Columns = int.Parse(columnsTextBox.Text);
|
---|
[2677] | 189 | errorProvider.SetError(columnsTextBox, string.Empty);
|
---|
| 190 | }
|
---|
| 191 | private void columnsTextBox_KeyDown(object sender, KeyEventArgs e) {
|
---|
| 192 | if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
|
---|
| 193 | columnsLabel.Focus(); // set focus on label to validate data
|
---|
| 194 | if (e.KeyCode == Keys.Escape) {
|
---|
[2713] | 195 | columnsTextBox.Text = Content.Columns.ToString();
|
---|
[2677] | 196 | columnsLabel.Focus(); // set focus on label to validate data
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 | #endregion
|
---|
| 200 |
|
---|
| 201 | #region DataGridView Events
|
---|
[2669] | 202 | private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
|
---|
[2694] | 203 | string errorMessage;
|
---|
[2713] | 204 | if (!Content.Validate(e.FormattedValue.ToString(), out errorMessage)) {
|
---|
[2676] | 205 | e.Cancel = true;
|
---|
[2694] | 206 | dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
|
---|
[2676] | 207 | }
|
---|
[2669] | 208 | }
|
---|
[2676] | 209 | private void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
|
---|
| 210 | string value = e.Value.ToString();
|
---|
[3314] | 211 | int rowIndex = virtualRowIndizes[e.RowIndex];
|
---|
| 212 | e.ParsingApplied = Content.SetValue(value, rowIndex, e.ColumnIndex);
|
---|
| 213 | if (e.ParsingApplied) e.Value = Content.GetValue(rowIndex, e.ColumnIndex);
|
---|
[2669] | 214 | }
|
---|
[2676] | 215 | private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
|
---|
| 216 | dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
|
---|
| 217 | }
|
---|
[3312] | 218 | private void dataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) {
|
---|
[3314] | 219 | if (e.RowIndex < virtualRowIndizes.Length) {
|
---|
| 220 | int rowIndex = virtualRowIndizes[e.RowIndex];
|
---|
| 221 | e.Value = Content.GetValue(rowIndex, e.ColumnIndex);
|
---|
| 222 | }
|
---|
[3312] | 223 | }
|
---|
| 224 |
|
---|
| 225 | private void dataGridView_Scroll(object sender, ScrollEventArgs e) {
|
---|
| 226 | UpdateRowHeaders();
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | private void dataGridView_Resize(object sender, EventArgs e) {
|
---|
| 230 | UpdateRowHeaders();
|
---|
| 231 | }
|
---|
[3314] | 232 |
|
---|
| 233 | private void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
[3320] | 234 | if (Content != null) {
|
---|
| 235 | if (e.Button == MouseButtons.Left && Content.SortableView) {
|
---|
| 236 | bool addToSortedIndizes = (Control.ModifierKeys & Keys.Control) == Keys.Control;
|
---|
| 237 | SortOrder newSortOrder = SortOrder.Ascending;
|
---|
| 238 | if (sortedColumnIndizes.Any(x => x.Key == e.ColumnIndex)) {
|
---|
| 239 | SortOrder oldSortOrder = sortedColumnIndizes.Where(x => x.Key == e.ColumnIndex).First().Value;
|
---|
| 240 | int enumLength = Enum.GetValues(typeof(SortOrder)).Length;
|
---|
| 241 | newSortOrder = oldSortOrder = (SortOrder)Enum.Parse(typeof(SortOrder), ((((int)oldSortOrder) + 1) % enumLength).ToString());
|
---|
| 242 | }
|
---|
[3314] | 243 |
|
---|
[3320] | 244 | if (!addToSortedIndizes)
|
---|
| 245 | sortedColumnIndizes.Clear();
|
---|
[3314] | 246 |
|
---|
[3320] | 247 | if (sortedColumnIndizes.Any(x => x.Key == e.ColumnIndex)) {
|
---|
| 248 | int sortedIndex = sortedColumnIndizes.FindIndex(x => x.Key == e.ColumnIndex);
|
---|
| 249 | if (newSortOrder != SortOrder.None)
|
---|
| 250 | sortedColumnIndizes[sortedIndex] = new KeyValuePair<int, SortOrder>(e.ColumnIndex, newSortOrder);
|
---|
| 251 | else
|
---|
| 252 | sortedColumnIndizes.RemoveAt(sortedIndex);
|
---|
| 253 | } else
|
---|
| 254 | if (newSortOrder != SortOrder.None)
|
---|
| 255 | sortedColumnIndizes.Add(new KeyValuePair<int, SortOrder>(e.ColumnIndex, newSortOrder));
|
---|
| 256 | Sort();
|
---|
| 257 | } else if (e.Button == MouseButtons.Right) {
|
---|
| 258 | if (Content.ColumnNames.Count() != 0)
|
---|
| 259 | contextMenu.Show(MousePosition);
|
---|
| 260 | }
|
---|
[3314] | 261 | }
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | private void Sort() {
|
---|
| 265 | int[] newSortedIndex = Enumerable.Range(0, Content.Rows - 1).ToArray();
|
---|
| 266 | if (sortedColumnIndizes.Count != 0) {
|
---|
| 267 | rowComparer.sortedIndizes = sortedColumnIndizes;
|
---|
| 268 | rowComparer.matrix = Content;
|
---|
| 269 | Array.Sort(newSortedIndex, rowComparer);
|
---|
| 270 | }
|
---|
| 271 | virtualRowIndizes = newSortedIndex;
|
---|
| 272 | dataGridView.Invalidate();
|
---|
| 273 | foreach (DataGridViewColumn col in this.dataGridView.Columns)
|
---|
| 274 | col.HeaderCell.SortGlyphDirection = SortOrder.None;
|
---|
| 275 | foreach (KeyValuePair<int, SortOrder> p in sortedColumnIndizes)
|
---|
| 276 | this.dataGridView.Columns[p.Key].HeaderCell.SortGlyphDirection = p.Value;
|
---|
| 277 | }
|
---|
[2677] | 278 | #endregion
|
---|
[3312] | 279 |
|
---|
[3314] | 280 | private class RowComparer : IComparer<int> {
|
---|
| 281 | public List<KeyValuePair<int, SortOrder>> sortedIndizes;
|
---|
| 282 | public IStringConvertibleMatrix matrix;
|
---|
| 283 | public RowComparer() {
|
---|
| 284 | }
|
---|
[3312] | 285 |
|
---|
[3314] | 286 | public int Compare(int x, int y) {
|
---|
| 287 | int result = 0;
|
---|
| 288 | double double1, double2;
|
---|
| 289 | DateTime dateTime1, dateTime2;
|
---|
| 290 | string string1, string2;
|
---|
| 291 |
|
---|
| 292 | foreach (KeyValuePair<int, SortOrder> pair in sortedIndizes) {
|
---|
| 293 | string1 = matrix.GetValue(x, pair.Key);
|
---|
| 294 | string2 = matrix.GetValue(y, pair.Key);
|
---|
| 295 | if (double.TryParse(string1, out double1) && double.TryParse(string2, out double2))
|
---|
| 296 | result = double1.CompareTo(double2);
|
---|
| 297 | else if (DateTime.TryParse(string1, out dateTime1) && DateTime.TryParse(string2, out dateTime2))
|
---|
| 298 | result = dateTime1.CompareTo(dateTime2);
|
---|
| 299 | else {
|
---|
| 300 | if (string1 != null)
|
---|
| 301 | result = string1.CompareTo(string2);
|
---|
| 302 | else if (string2 != null)
|
---|
| 303 | result = string2.CompareTo(string1) * -1;
|
---|
| 304 | }
|
---|
| 305 | if (pair.Value == SortOrder.Descending)
|
---|
| 306 | result *= -1;
|
---|
| 307 | if (result != 0)
|
---|
| 308 | return result;
|
---|
| 309 | }
|
---|
| 310 | return result;
|
---|
| 311 | }
|
---|
| 312 | }
|
---|
[3316] | 313 |
|
---|
| 314 | private void ShowHideColumns_Click(object sender, EventArgs e) {
|
---|
| 315 | new ColumnsVisibilityDialog(this.dataGridView.Columns.Cast<DataGridViewColumn>()).ShowDialog();
|
---|
| 316 | }
|
---|
[2669] | 317 | }
|
---|
| 318 | }
|
---|