Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data.Views/3.3/StringConvertibleMatrixView.cs @ 3155

Last change on this file since 3155 was 3048, checked in by swagner, 15 years ago

Renamed classes of HeuristicLab.Data (#909)

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