#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 visual representation of the class . /// public partial class ArrayDataBaseView : ItemViewBase { /// /// Gets or sets the instance of the array to represent. /// /// 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 . /// public ArrayDataBaseView() { 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 overridden 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"); } /// /// Replaces an element at the given /// with the given . /// Needs to be overridden in each inherited class! /// /// Thrown when method is not /// overridden in inherited class. /// The position where to substitute the element. /// The element to insert. protected virtual void SetArrayElement(int index, string element) { throw new InvalidOperationException("SetArrayElement has to be overridden in each inherited class"); } /// /// Updates all controls and the elements of the table with the latest values. /// protected override void UpdateControls() { base.UpdateControls(); if (ArrayDataBase != null) { int length = ArrayDataBase.Data.Length; lengthTextBox.Text = length + ""; dataGridView.ColumnCount = 1; dataGridView.RowCount = length; for (int i = 0; i < length; i++) { dataGridView.Rows[i].Cells[0].Value = ArrayDataBase.Data.GetValue(i); } } else { lengthTextBox.Text = "0"; dataGridView.ColumnCount = 1; dataGridView.RowCount = 0; } } private void lengthTextBox_Validating(object sender, CancelEventArgs e) { int newLength; if (int.TryParse(lengthTextBox.Text, out newLength)) { if (newLength > 0) { e.Cancel = false; if (newLength != ArrayDataBase.Data.Length) { CreateAndCopyArray(newLength); } } else { // only allow values greater than 0 e.Cancel = true; } } else { e.Cancel = true; } } /// /// Creates a new array having the specified number () of elements of the /// current instance (starting from the beginning). /// /// The size/number of elements of the new array. private void CreateAndCopyArray(int newLength) { Array newArray = Array.CreateInstance(ArrayDataBase.Data.GetType().GetElementType(), newLength); Array.Copy(ArrayDataBase.Data, newArray, Math.Min(newLength, ArrayDataBase.Data.Length)); ArrayDataBase.Data = newArray; } private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (ValidateData((string)e.FormattedValue)) { SetArrayElement(e.RowIndex, (string)e.FormattedValue); e.Cancel = false; } else { e.Cancel = true; } } private void lengthTextBox_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return) { e.SuppressKeyPress = true; dataGridView.Focus(); } } #region ArrayDataBase Events private void ArrayDataBase_Changed(object sender, EventArgs e) { Refresh(); } #endregion } }