#region License Information /* HeuristicLab * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; using HeuristicLab.Core; using HeuristicLab.Core.Views; namespace HeuristicLab.Data { /// /// The basic visual representation of a two-dimensional matrix. /// public partial class MatrixDataBaseView : ItemViewBase { /// /// Gets or sets the matrix to represent visually. /// /// Uses property of base class . /// No own data storage present. public ArrayDataBase ArrayDataBase { get { return (ArrayDataBase)Item; } protected set { base.Item = value; } } /// /// Initializes a new instance of the class . /// public MatrixDataBaseView() { InitializeComponent(); } /// /// Removes the eventhandler from the underlying . /// /// Calls of base class . /// protected override void RemoveItemEvents() { ArrayDataBase.Changed -= new EventHandler(ArrayDataBase_Changed); base.RemoveItemEvents(); } /// /// Adds an eventhandler to the underlying . /// /// Calls of base class . /// protected override void AddItemEvents() { base.AddItemEvents(); ArrayDataBase.Changed += new EventHandler(ArrayDataBase_Changed); } /// /// Validates the given data. /// Needs to be overriden in each inherited class! /// /// Thrown when method is not /// overridden in inherited class. /// The data to validate. /// true if the data is valid, false otherwise. protected virtual bool ValidateData(string element) { throw new InvalidOperationException("ValidateData has to be overridden in each inherited class"); } /// /// Sets an element of the current instance at the given /// to the given . /// Needs to be overridden in each inherited class! /// /// Thrown when method is not /// overridden in inherited class. /// The row where to substitute the element. /// The column where to substitute the element. /// The element to insert. protected virtual void SetArrayElement(int row, int column, string element) { throw new InvalidOperationException("SetArrayElement has to be overridden in each inherited class"); } /// /// Update all controls with the latest element of the matrix. /// protected override void UpdateControls() { base.UpdateControls(); if (ArrayDataBase != null) { int rows = ArrayDataBase.Data.GetLength(0); int columns = ArrayDataBase.Data.GetLength(1); rowsTextBox.Text = rows + ""; columnsTextBox.Text = columns + ""; dataGridView.ColumnCount = columns; dataGridView.RowCount = rows; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { dataGridView.Rows[i].Cells[j].Value = ArrayDataBase.Data.GetValue(i, j); } } } else { rowsTextBox.Text = "1"; columnsTextBox.Text = "1"; dataGridView.ColumnCount = 1; dataGridView.RowCount = 1; } } private void textBox_Validating(object sender, CancelEventArgs e) { int newValue; TextBox source = (TextBox)sender; if (int.TryParse(source.Text, out newValue)) { if (newValue > 0) { e.Cancel = false; } else { e.Cancel = true; } } else { e.Cancel = true; } } /// /// Creates a new matrix having the specified number () /// of rows and the specified number () of columns of the /// current instance. /// /// The number of rows of the new matrix. /// The number of columns of the new matrix private void CreateAndCopyArray(int newRows, int newColumns) { Array newArray = Array.CreateInstance(ArrayDataBase.Data.GetType().GetElementType(), newRows, newColumns); Array.Copy(ArrayDataBase.Data, newArray, Math.Min(newArray.Length, ArrayDataBase.Data.Length)); ArrayDataBase.Data = newArray; } private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (ValidateData((string)e.FormattedValue)) { SetArrayElement(e.RowIndex, e.ColumnIndex, (string)e.FormattedValue); e.Cancel = false; Refresh(); } else { e.Cancel = true; } } private void textBox_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return) { e.SuppressKeyPress = true; dataGridView.Focus(); } } private void textBox_Validated(object sender, EventArgs e) { int newRows; int newColumns; if (int.TryParse(columnsTextBox.Text, out newColumns) && int.TryParse(rowsTextBox.Text, out newRows)) { CreateAndCopyArray(newRows, newColumns); } else { throw new FormatException(); } } #region ArrayDataBase Events private void ArrayDataBase_Changed(object sender, EventArgs e) { Refresh(); } #endregion } }