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