Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data.Views/3.3/StringConvertibleMatrixDataView.cs @ 2971

Last change on this file since 2971 was 2917, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

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