Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

  • implemented reviewers' comments
File size: 7.8 KB
RevLine 
[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
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;
[2713]33using HeuristicLab.MainForm.WindowsForms;
[2669]34
35namespace HeuristicLab.Data.Views {
[2917]36  [View("StringConvertibleMatrix View")]
[2677]37  [Content(typeof(IStringConvertibleMatrixData), true)]
[2713]38  public partial class StringConvertibleMatrixDataView : ContentView {
39    public new IStringConvertibleMatrixData Content {
40      get { return (IStringConvertibleMatrixData)base.Content; }
41      set { base.Content = value; }
[2669]42    }
43
[2677]44    public StringConvertibleMatrixDataView() {
[2669]45      InitializeComponent();
[2677]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);
[2669]51    }
[2727]52    public StringConvertibleMatrixDataView(IStringConvertibleMatrixData content)
[2669]53      : this() {
[2727]54      Content = content;
[2669]55    }
56
[2713]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();
[2669]61    }
62
63
[2713]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);
[2669]68    }
69
[2713]70    protected override void OnContentChanged() {
71      base.OnContentChanged();
72      if (Content == null) {
[2677]73        Caption = "StringConvertibleMatrixData View";
74        rowsTextBox.Text = "";
75        rowsTextBox.Enabled = false;
76        columnsTextBox.Text = "";
77        columnsTextBox.Enabled = false;
[2669]78        dataGridView.Rows.Clear();
[2677]79        dataGridView.Columns.Clear();
[2669]80        dataGridView.Enabled = false;
81      } else {
[2713]82        Caption = "StringConvertibleMatrixData (" + Content.GetType().Name + ")";
83        UpdateData();
[2669]84      }
85    }
86
[2713]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;
[2669]92      dataGridView.Rows.Clear();
[2677]93      dataGridView.Columns.Clear();
[2713]94      if ((Content.Rows > 0) && (Content.Columns > 0)) {
95        for (int i = 0; i < Content.Columns; i++) {
[2677]96          dataGridView.ColumnCount++;
97          dataGridView.Columns[dataGridView.ColumnCount - 1].FillWeight = float.Epsilon;  // sum of all fill weights must not be larger than 65535
98        }
[2713]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);
[2677]103        }
[2713]104        for (int i = 0; i < Content.Columns; i++)
[2677]105          dataGridView.Columns[i].Width = dataGridView.Columns[i].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true);
106      }
[2669]107      dataGridView.Enabled = true;
108    }
109
[2713]110    private void Content_ItemChanged(object sender, EventArgs<int, int> e) {
[2669]111      if (InvokeRequired)
[2713]112        Invoke(new EventHandler<EventArgs<int, int>>(Content_ItemChanged), sender, e);
[2677]113      else {
[2713]114        dataGridView.Rows[e.Value].Cells[e.Value2].Value = Content.GetValue(e.Value, e.Value2);
[2677]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      }
[2669]118    }
[2713]119    private void Content_Reset(object sender, EventArgs e) {
[2669]120      if (InvokeRequired)
[2713]121        Invoke(new EventHandler(Content_Reset), sender, e);
[2669]122      else
[2713]123        UpdateData();
[2669]124    }
125
[2677]126    #region TextBox Events
127    private void rowsTextBox_Validating(object sender, CancelEventArgs e) {
[2669]128      int i = 0;
[2677]129      if (!int.TryParse(rowsTextBox.Text, out i) || (i < 0)) {
[2676]130        e.Cancel = true;
[2694]131        errorProvider.SetError(rowsTextBox, "Invalid Number of Rows (Valid Values: Positive Integers Larger or Equal to 0)");
[2677]132        rowsTextBox.SelectAll();
[2669]133      }
134    }
[2677]135    private void rowsTextBox_Validated(object sender, EventArgs e) {
[2713]136      Content.Rows = int.Parse(rowsTextBox.Text);
[2677]137      errorProvider.SetError(rowsTextBox, string.Empty);
[2669]138    }
[2677]139    private void rowsTextBox_KeyDown(object sender, KeyEventArgs e) {
[2669]140      if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
[2677]141        rowsLabel.Focus();  // set focus on label to validate data
[2676]142      if (e.KeyCode == Keys.Escape) {
[2713]143        rowsTextBox.Text = Content.Columns.ToString();
[2677]144        rowsLabel.Focus();  // set focus on label to validate data
[2676]145      }
[2669]146    }
[2677]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;
[2694]151        errorProvider.SetError(columnsTextBox, "Invalid Number of Columns (Valid Values: Positive Integers Larger or Equal to 0)");
[2677]152        columnsTextBox.SelectAll();
153      }
154    }
155    private void columnsTextBox_Validated(object sender, EventArgs e) {
[2713]156      Content.Columns = int.Parse(columnsTextBox.Text);
[2677]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) {
[2713]163        columnsTextBox.Text = Content.Columns.ToString();
[2677]164        columnsLabel.Focus();  // set focus on label to validate data
165      }
166    }
167    #endregion
168
169    #region DataGridView Events
[2669]170    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
[2694]171      string errorMessage;
[2713]172      if (!Content.Validate(e.FormattedValue.ToString(), out errorMessage)) {
[2676]173        e.Cancel = true;
[2694]174        dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
[2676]175      }
[2669]176    }
[2676]177    private void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
178      string value = e.Value.ToString();
[2713]179      e.ParsingApplied = Content.SetValue(value, e.RowIndex, e.ColumnIndex);
180      if (e.ParsingApplied) e.Value = Content.GetValue(e.RowIndex, e.ColumnIndex);
[2669]181    }
[2676]182    private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
183      dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
184    }
[2677]185    #endregion
[2669]186  }
187}
Note: See TracBrowser for help on using the repository browser.