Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2913 was 2790, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

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