[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;
|
---|
| 24 | using System.Drawing;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.MainForm;
|
---|
[2713] | 28 | using HeuristicLab.MainForm.WindowsForms;
|
---|
[2669] | 29 |
|
---|
| 30 | namespace HeuristicLab.Data.Views {
|
---|
[3048] | 31 | [View("StringConvertibleMatrix View")]
|
---|
| 32 | [Content(typeof(IStringConvertibleMatrix), true)]
|
---|
| 33 | public partial class StringConvertibleMatrixView : ContentView {
|
---|
| 34 | public new IStringConvertibleMatrix Content {
|
---|
| 35 | get { return (IStringConvertibleMatrix)base.Content; }
|
---|
[2713] | 36 | set { base.Content = value; }
|
---|
[2669] | 37 | }
|
---|
| 38 |
|
---|
[3048] | 39 | public StringConvertibleMatrixView() {
|
---|
[2669] | 40 | InitializeComponent();
|
---|
[3048] | 41 | Caption = "StringConvertibleMatrix View";
|
---|
[2677] | 42 | errorProvider.SetIconAlignment(rowsTextBox, ErrorIconAlignment.MiddleLeft);
|
---|
| 43 | errorProvider.SetIconPadding(rowsTextBox, 2);
|
---|
| 44 | errorProvider.SetIconAlignment(columnsTextBox, ErrorIconAlignment.MiddleLeft);
|
---|
| 45 | errorProvider.SetIconPadding(columnsTextBox, 2);
|
---|
[2669] | 46 | }
|
---|
[3048] | 47 | public StringConvertibleMatrixView(IStringConvertibleMatrix content)
|
---|
[2669] | 48 | : this() {
|
---|
[2727] | 49 | Content = content;
|
---|
[2669] | 50 | }
|
---|
| 51 |
|
---|
[2713] | 52 | protected override void DeregisterContentEvents() {
|
---|
| 53 | Content.ItemChanged -= new EventHandler<EventArgs<int, int>>(Content_ItemChanged);
|
---|
| 54 | Content.Reset -= new EventHandler(Content_Reset);
|
---|
| 55 | base.DeregisterContentEvents();
|
---|
[2669] | 56 | }
|
---|
| 57 |
|
---|
| 58 |
|
---|
[2713] | 59 | protected override void RegisterContentEvents() {
|
---|
| 60 | base.RegisterContentEvents();
|
---|
| 61 | Content.ItemChanged += new EventHandler<EventArgs<int, int>>(Content_ItemChanged);
|
---|
| 62 | Content.Reset += new EventHandler(Content_Reset);
|
---|
[2669] | 63 | }
|
---|
| 64 |
|
---|
[2713] | 65 | protected override void OnContentChanged() {
|
---|
| 66 | base.OnContentChanged();
|
---|
| 67 | if (Content == null) {
|
---|
[3048] | 68 | Caption = "StringConvertibleMatrix View";
|
---|
[2677] | 69 | rowsTextBox.Text = "";
|
---|
| 70 | rowsTextBox.Enabled = false;
|
---|
| 71 | columnsTextBox.Text = "";
|
---|
| 72 | columnsTextBox.Enabled = false;
|
---|
[2669] | 73 | dataGridView.Rows.Clear();
|
---|
[2677] | 74 | dataGridView.Columns.Clear();
|
---|
[2669] | 75 | dataGridView.Enabled = false;
|
---|
| 76 | } else {
|
---|
[3048] | 77 | Caption = "StringConvertibleMatrix (" + Content.GetType().Name + ")";
|
---|
[2713] | 78 | UpdateData();
|
---|
[2669] | 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[2713] | 82 | private void UpdateData() {
|
---|
| 83 | rowsTextBox.Text = Content.Rows.ToString();
|
---|
[2973] | 84 | rowsTextBox.Enabled = true;
|
---|
[2713] | 85 | columnsTextBox.Text = Content.Columns.ToString();
|
---|
[2973] | 86 | columnsTextBox.Enabled = true;
|
---|
[2669] | 87 | dataGridView.Rows.Clear();
|
---|
[2677] | 88 | dataGridView.Columns.Clear();
|
---|
[2713] | 89 | if ((Content.Rows > 0) && (Content.Columns > 0)) {
|
---|
| 90 | for (int i = 0; i < Content.Columns; i++) {
|
---|
[2677] | 91 | dataGridView.ColumnCount++;
|
---|
| 92 | dataGridView.Columns[dataGridView.ColumnCount - 1].FillWeight = float.Epsilon; // sum of all fill weights must not be larger than 65535
|
---|
| 93 | }
|
---|
[2713] | 94 | dataGridView.RowCount = Content.Rows;
|
---|
| 95 | for (int i = 0; i < Content.Rows; i++) {
|
---|
| 96 | for (int j = 0; j < Content.Columns; j++)
|
---|
| 97 | dataGridView.Rows[i].Cells[j].Value = Content.GetValue(i, j);
|
---|
[2677] | 98 | }
|
---|
[2713] | 99 | for (int i = 0; i < Content.Columns; i++)
|
---|
[2677] | 100 | dataGridView.Columns[i].Width = dataGridView.Columns[i].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true);
|
---|
| 101 | }
|
---|
[2669] | 102 | dataGridView.Enabled = true;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[2713] | 105 | private void Content_ItemChanged(object sender, EventArgs<int, int> e) {
|
---|
[2669] | 106 | if (InvokeRequired)
|
---|
[2713] | 107 | Invoke(new EventHandler<EventArgs<int, int>>(Content_ItemChanged), sender, e);
|
---|
[2677] | 108 | else {
|
---|
[2713] | 109 | dataGridView.Rows[e.Value].Cells[e.Value2].Value = Content.GetValue(e.Value, e.Value2);
|
---|
[2677] | 110 | Size size = dataGridView.Rows[e.Value].Cells[e.Value2].PreferredSize;
|
---|
| 111 | dataGridView.Columns[e.Value2].Width = Math.Max(dataGridView.Columns[e.Value2].Width, size.Width);
|
---|
| 112 | }
|
---|
[2669] | 113 | }
|
---|
[2713] | 114 | private void Content_Reset(object sender, EventArgs e) {
|
---|
[2669] | 115 | if (InvokeRequired)
|
---|
[2713] | 116 | Invoke(new EventHandler(Content_Reset), sender, e);
|
---|
[2669] | 117 | else
|
---|
[2713] | 118 | UpdateData();
|
---|
[2669] | 119 | }
|
---|
| 120 |
|
---|
[2677] | 121 | #region TextBox Events
|
---|
| 122 | private void rowsTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
[2669] | 123 | int i = 0;
|
---|
[2677] | 124 | if (!int.TryParse(rowsTextBox.Text, out i) || (i < 0)) {
|
---|
[2676] | 125 | e.Cancel = true;
|
---|
[2694] | 126 | errorProvider.SetError(rowsTextBox, "Invalid Number of Rows (Valid Values: Positive Integers Larger or Equal to 0)");
|
---|
[2677] | 127 | rowsTextBox.SelectAll();
|
---|
[2669] | 128 | }
|
---|
| 129 | }
|
---|
[2677] | 130 | private void rowsTextBox_Validated(object sender, EventArgs e) {
|
---|
[2713] | 131 | Content.Rows = int.Parse(rowsTextBox.Text);
|
---|
[2677] | 132 | errorProvider.SetError(rowsTextBox, string.Empty);
|
---|
[2669] | 133 | }
|
---|
[2677] | 134 | private void rowsTextBox_KeyDown(object sender, KeyEventArgs e) {
|
---|
[2669] | 135 | if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
|
---|
[2677] | 136 | rowsLabel.Focus(); // set focus on label to validate data
|
---|
[2676] | 137 | if (e.KeyCode == Keys.Escape) {
|
---|
[2973] | 138 | rowsTextBox.Text = Content.Rows.ToString();
|
---|
[2677] | 139 | rowsLabel.Focus(); // set focus on label to validate data
|
---|
[2676] | 140 | }
|
---|
[2669] | 141 | }
|
---|
[2677] | 142 | private void columnsTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
| 143 | int i = 0;
|
---|
| 144 | if (!int.TryParse(columnsTextBox.Text, out i) || (i < 0)) {
|
---|
| 145 | e.Cancel = true;
|
---|
[2694] | 146 | errorProvider.SetError(columnsTextBox, "Invalid Number of Columns (Valid Values: Positive Integers Larger or Equal to 0)");
|
---|
[2677] | 147 | columnsTextBox.SelectAll();
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 | private void columnsTextBox_Validated(object sender, EventArgs e) {
|
---|
[2713] | 151 | Content.Columns = int.Parse(columnsTextBox.Text);
|
---|
[2677] | 152 | errorProvider.SetError(columnsTextBox, string.Empty);
|
---|
| 153 | }
|
---|
| 154 | private void columnsTextBox_KeyDown(object sender, KeyEventArgs e) {
|
---|
| 155 | if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
|
---|
| 156 | columnsLabel.Focus(); // set focus on label to validate data
|
---|
| 157 | if (e.KeyCode == Keys.Escape) {
|
---|
[2713] | 158 | columnsTextBox.Text = Content.Columns.ToString();
|
---|
[2677] | 159 | columnsLabel.Focus(); // set focus on label to validate data
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 | #endregion
|
---|
| 163 |
|
---|
| 164 | #region DataGridView Events
|
---|
[2669] | 165 | private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
|
---|
[2694] | 166 | string errorMessage;
|
---|
[2713] | 167 | if (!Content.Validate(e.FormattedValue.ToString(), out errorMessage)) {
|
---|
[2676] | 168 | e.Cancel = true;
|
---|
[2694] | 169 | dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
|
---|
[2676] | 170 | }
|
---|
[2669] | 171 | }
|
---|
[2676] | 172 | private void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
|
---|
| 173 | string value = e.Value.ToString();
|
---|
[2713] | 174 | e.ParsingApplied = Content.SetValue(value, e.RowIndex, e.ColumnIndex);
|
---|
| 175 | if (e.ParsingApplied) e.Value = Content.GetValue(e.RowIndex, e.ColumnIndex);
|
---|
[2669] | 176 | }
|
---|
[2676] | 177 | private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
|
---|
| 178 | dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
|
---|
| 179 | }
|
---|
[2677] | 180 | #endregion
|
---|
[2669] | 181 | }
|
---|
| 182 | }
|
---|