Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2062 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

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