Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.Data/MatrixDataBaseView.cs @ 6089

Last change on this file since 6089 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Core;
30
31namespace HeuristicLab.Data {
32  public partial class MatrixDataBaseView : ViewBase {
33    public ArrayDataBase ArrayDataBase {
34      get { return (ArrayDataBase)Item; }
35      protected set { base.Item = value; }
36    }
37
38    public MatrixDataBaseView() {
39      InitializeComponent();
40    }
41
42    protected override void RemoveItemEvents() {
43      ArrayDataBase.Changed -= new EventHandler(ArrayDataBase_Changed);
44      base.RemoveItemEvents();
45    }
46    protected override void AddItemEvents() {
47      base.AddItemEvents();
48      ArrayDataBase.Changed += new EventHandler(ArrayDataBase_Changed);
49    }
50
51    protected virtual bool ValidateData(string element) {
52      throw new InvalidOperationException("ValidateData has to be overridden in each inherited class");
53    }
54    protected virtual void SetArrayElement(int row, int column, string element) {
55      throw new InvalidOperationException("SetArrayElement has to be overridden in each inherited class");
56    }
57
58    protected override void UpdateControls() {
59      base.UpdateControls();
60      if (ArrayDataBase != null) {
61        int rows = ArrayDataBase.Data.GetLength(0);
62        int columns = ArrayDataBase.Data.GetLength(1);
63
64        rowsTextBox.Text = rows + "";
65        columnsTextBox.Text = columns + "";
66        dataGridView.ColumnCount = columns;
67        dataGridView.RowCount = rows;
68        for (int i = 0; i < rows; i++) {
69          for (int j = 0; j < columns; j++) {
70            dataGridView.Rows[i].Cells[j].Value = ArrayDataBase.Data.GetValue(i, j);
71          }
72        }
73      } else {
74        rowsTextBox.Text = "1";
75        columnsTextBox.Text = "1";
76        dataGridView.ColumnCount = 1;
77        dataGridView.RowCount = 1;
78      }
79    }
80
81    private void textBox_Validating(object sender, CancelEventArgs e) {
82      int newValue;
83      TextBox source = (TextBox)sender;
84      if (int.TryParse(source.Text, out newValue)) {
85        if (newValue > 0) {
86          e.Cancel = false;
87        } else {
88          e.Cancel = true;
89        }
90      } else {
91        e.Cancel = true;
92      }
93    }
94
95    private void CreateAndCopyArray(int newRows, int newColumns) {
96      Array newArray = Array.CreateInstance(ArrayDataBase.Data.GetType().GetElementType(), newRows, newColumns);
97      Array.Copy(ArrayDataBase.Data, newArray, Math.Min(newArray.Length, ArrayDataBase.Data.Length));
98      ArrayDataBase.Data = newArray;
99    }
100
101    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
102      if (ValidateData((string)e.FormattedValue)) {
103        SetArrayElement(e.RowIndex, e.ColumnIndex, (string)e.FormattedValue);
104        e.Cancel = false;
105        Refresh();
106      } else {
107        e.Cancel = true;
108      }
109    }
110
111    private void textBox_KeyDown(object sender, KeyEventArgs e) {
112      if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return) {
113        e.SuppressKeyPress = true;
114        dataGridView.Focus();
115      }
116    }
117
118    private void textBox_Validated(object sender, EventArgs e) {
119      int newRows;
120      int newColumns;
121      if (int.TryParse(columnsTextBox.Text, out newColumns) && int.TryParse(rowsTextBox.Text, out newRows)) {
122        CreateAndCopyArray(newRows, newColumns);
123      } else {
124        throw new FormatException();
125      }
126    }
127
128    #region ArrayDataBase Events
129    private void ArrayDataBase_Changed(object sender, EventArgs e) {
130      Refresh();
131    }
132    #endregion
133  }
134}
Note: See TracBrowser for help on using the repository browser.