Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/MatrixDataBaseView.cs @ 2604

Last change on this file since 2604 was 2546, checked in by swagner, 15 years ago

Continued work on Optimizer and on adapting all views to the new MainForm concept (#770)

File size: 7.0 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;
30using HeuristicLab.Core.Views;
31
32namespace HeuristicLab.Data {
33  /// <summary>
34  /// The basic visual representation of a two-dimensional matrix.
35  /// </summary>
36  public partial class MatrixDataBaseView : ItemViewBase {
37
38    /// <summary>
39    /// Gets or sets the matrix to represent visually.
40    /// </summary>
41    /// <remarks>Uses property <see cref="HeuristicLab.Core.ViewBase.Item"/> of base class <see cref="ViewBase"/>.
42    /// No own data storage present.</remarks>
43    public ArrayDataBase ArrayDataBase {
44      get { return (ArrayDataBase)Item; }
45      protected set { base.Item = value; }
46    }
47
48    /// <summary>
49    /// Initializes a new instance of the class <see cref="MatrixDataBaseView"/>.
50    /// </summary>
51    public MatrixDataBaseView() {
52      InitializeComponent();
53    }
54
55    /// <summary>
56    /// Removes the eventhandler from the underlying <see cref="ArrayDataBase"/>.
57    /// </summary>
58    /// <remarks>Calls <see cref="HeuristicLab.Core.ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.
59    /// </remarks>
60    protected override void RemoveItemEvents() {
61      ArrayDataBase.Changed -= new EventHandler(ArrayDataBase_Changed);
62      base.RemoveItemEvents();
63    }
64    /// <summary>
65    /// Adds an eventhandler to the underlying <see cref="ArrayDataBase"/>.
66    /// </summary>
67    /// <remarks>Calls <see cref="HeuristicLab.Core.ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.
68    /// </remarks>
69    protected override void AddItemEvents() {
70      base.AddItemEvents();
71      ArrayDataBase.Changed += new EventHandler(ArrayDataBase_Changed);
72    }
73
74    /// <summary>
75    /// Validates the given data.
76    /// <note type="caution"> Needs to be overriden in each inherited class!</note>
77    /// </summary>
78    /// <exception cref="InvalidOperationException">Thrown when method is not
79    /// overridden in inherited class.</exception>
80    /// <param name="element">The data to validate.</param>
81    /// <returns><c>true</c> if the data is valid, <c>false</c> otherwise.</returns>
82    protected virtual bool ValidateData(string element) {
83      throw new InvalidOperationException("ValidateData has to be overridden in each inherited class");
84    }
85    /// <summary>
86    /// Sets an element of the current instance at the given <paramref name="index"/>
87    /// to the given <paramref name="element"/>.
88    /// <note type="caution"> Needs to be overridden in each inherited class!</note>
89    /// </summary>
90    /// <exception cref="InvalidOperationException">Thrown when method is not
91    /// overridden in inherited class.</exception>
92    /// <param name="row">The row where to substitute the element.</param>
93    /// <param name="column">The column where to substitute the element.</param>
94    /// <param name="element">The element to insert.</param>       
95    protected virtual void SetArrayElement(int row, int column, string element) {
96      throw new InvalidOperationException("SetArrayElement has to be overridden in each inherited class");
97    }
98
99    /// <summary>
100    /// Update all controls with the latest element of the matrix.
101    /// </summary>
102    protected override void UpdateControls() {
103      base.UpdateControls();
104      if (ArrayDataBase != null) {
105        int rows = ArrayDataBase.Data.GetLength(0);
106        int columns = ArrayDataBase.Data.GetLength(1);
107
108        rowsTextBox.Text = rows + "";
109        columnsTextBox.Text = columns + "";
110        dataGridView.ColumnCount = columns;
111        dataGridView.RowCount = rows;
112        for (int i = 0; i < rows; i++) {
113          for (int j = 0; j < columns; j++) {
114            dataGridView.Rows[i].Cells[j].Value = ArrayDataBase.Data.GetValue(i, j);
115          }
116        }
117      } else {
118        rowsTextBox.Text = "1";
119        columnsTextBox.Text = "1";
120        dataGridView.ColumnCount = 1;
121        dataGridView.RowCount = 1;
122      }
123    }
124
125    private void textBox_Validating(object sender, CancelEventArgs e) {
126      int newValue;
127      TextBox source = (TextBox)sender;
128      if (int.TryParse(source.Text, out newValue)) {
129        if (newValue > 0) {
130          e.Cancel = false;
131        } else {
132          e.Cancel = true;
133        }
134      } else {
135        e.Cancel = true;
136      }
137    }
138
139    /// <summary>
140    /// Creates a new matrix having the specified number (<paramref name="newRows"/>)
141    /// of rows and the specified number (<paramref name="newColumns"/>) of columns of the
142    /// current instance.
143    /// </summary>
144    /// <param name="newRows">The number of rows of the new matrix.</param>
145    /// <param name="newColumns">The number of columns of the new matrix</param>
146    private void CreateAndCopyArray(int newRows, int newColumns) {
147      Array newArray = Array.CreateInstance(ArrayDataBase.Data.GetType().GetElementType(), newRows, newColumns);
148      Array.Copy(ArrayDataBase.Data, newArray, Math.Min(newArray.Length, ArrayDataBase.Data.Length));
149      ArrayDataBase.Data = newArray;
150    }
151
152    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
153      if (ValidateData((string)e.FormattedValue)) {
154        SetArrayElement(e.RowIndex, e.ColumnIndex, (string)e.FormattedValue);
155        e.Cancel = false;
156        Refresh();
157      } else {
158        e.Cancel = true;
159      }
160    }
161
162    private void textBox_KeyDown(object sender, KeyEventArgs e) {
163      if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return) {
164        e.SuppressKeyPress = true;
165        dataGridView.Focus();
166      }
167    }
168
169    private void textBox_Validated(object sender, EventArgs e) {
170      int newRows;
171      int newColumns;
172      if (int.TryParse(columnsTextBox.Text, out newColumns) && int.TryParse(rowsTextBox.Text, out newRows)) {
173        CreateAndCopyArray(newRows, newColumns);
174      } else {
175        throw new FormatException();
176      }
177    }
178
179    #region ArrayDataBase Events
180    private void ArrayDataBase_Changed(object sender, EventArgs e) {
181      Refresh();
182    }
183    #endregion
184  }
185}
Note: See TracBrowser for help on using the repository browser.