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