[2] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.ComponentModel;
|
---|
| 25 | using System.Drawing;
|
---|
| 26 | using System.Data;
|
---|
| 27 | using System.Text;
|
---|
| 28 | using System.Windows.Forms;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
[2520] | 30 | using HeuristicLab.Core.Views;
|
---|
[2] | 31 |
|
---|
| 32 | namespace HeuristicLab.Data {
|
---|
[737] | 33 | /// <summary>
|
---|
| 34 | /// The visual representation of the class <see cref="ArrayDataBase"/>.
|
---|
| 35 | /// </summary>
|
---|
[2546] | 36 | public partial class ArrayDataBaseView : ItemViewBase {
|
---|
[737] | 37 | /// <summary>
|
---|
| 38 | /// Gets or sets the instance of the array to represent.
|
---|
| 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>
|
---|
[2] | 42 | public ArrayDataBase ArrayDataBase {
|
---|
| 43 | get { return (ArrayDataBase)Item; }
|
---|
| 44 | protected set { base.Item = value; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[737] | 47 | /// <summary>
|
---|
| 48 | /// Initializes a new instance of <see cref="ArrayDataBaseView"/>.
|
---|
| 49 | /// </summary>
|
---|
[2] | 50 | public ArrayDataBaseView() {
|
---|
| 51 | InitializeComponent();
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[737] | 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>
|
---|
[2] | 59 | protected override void RemoveItemEvents() {
|
---|
| 60 | ArrayDataBase.Changed -= new EventHandler(ArrayDataBase_Changed);
|
---|
| 61 | base.RemoveItemEvents();
|
---|
| 62 | }
|
---|
[737] | 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>
|
---|
[2] | 68 | protected override void AddItemEvents() {
|
---|
| 69 | base.AddItemEvents();
|
---|
| 70 | ArrayDataBase.Changed += new EventHandler(ArrayDataBase_Changed);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[737] | 73 | /// <summary>
|
---|
| 74 | /// Validates the given data.
|
---|
| 75 | /// <note type="caution"> Needs to be overridden 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>
|
---|
[2] | 81 | protected virtual bool ValidateData(string element) {
|
---|
| 82 | throw new InvalidOperationException("ValidateData has to be overridden in each inherited class");
|
---|
| 83 | }
|
---|
[737] | 84 | /// <summary>
|
---|
| 85 | /// Replaces an element at the given <paramref name="index"/>
|
---|
| 86 | /// with 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="index">The position where to substitute the element.</param>
|
---|
| 92 | /// <param name="element">The element to insert.</param>
|
---|
[2] | 93 | protected virtual void SetArrayElement(int index, string element) {
|
---|
| 94 | throw new InvalidOperationException("SetArrayElement has to be overridden in each inherited class");
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[737] | 97 | /// <summary>
|
---|
| 98 | /// Updates all controls and the elements of the table with the latest values.
|
---|
| 99 | /// </summary>
|
---|
[2] | 100 | protected override void UpdateControls() {
|
---|
| 101 | base.UpdateControls();
|
---|
| 102 | if (ArrayDataBase != null) {
|
---|
| 103 | int length = ArrayDataBase.Data.Length;
|
---|
| 104 | lengthTextBox.Text = length + "";
|
---|
| 105 | dataGridView.ColumnCount = 1;
|
---|
| 106 | dataGridView.RowCount = length;
|
---|
| 107 | for (int i = 0; i < length; i++) {
|
---|
[344] | 108 | dataGridView.Rows[i].Cells[0].Value = ArrayDataBase.Data.GetValue(i);
|
---|
[2] | 109 | }
|
---|
| 110 | } else {
|
---|
| 111 | lengthTextBox.Text = "0";
|
---|
| 112 | dataGridView.ColumnCount = 1;
|
---|
| 113 | dataGridView.RowCount = 0;
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | private void lengthTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
| 118 | int newLength;
|
---|
| 119 | if (int.TryParse(lengthTextBox.Text, out newLength)) {
|
---|
| 120 | if (newLength > 0) {
|
---|
| 121 | e.Cancel = false;
|
---|
| 122 | if (newLength != ArrayDataBase.Data.Length) {
|
---|
| 123 | CreateAndCopyArray(newLength);
|
---|
| 124 | }
|
---|
| 125 | } else {
|
---|
| 126 | // only allow values greater than 0
|
---|
| 127 | e.Cancel = true;
|
---|
| 128 | }
|
---|
| 129 | } else {
|
---|
| 130 | e.Cancel = true;
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[737] | 134 | /// <summary>
|
---|
| 135 | /// Creates a new array having the specified number (<paramref name="newLength"/>) of elements of the
|
---|
| 136 | /// current instance (starting from the beginning).
|
---|
| 137 | /// </summary>
|
---|
| 138 | /// <param name="newLength">The size/number of elements of the new array.</param>
|
---|
[2] | 139 | private void CreateAndCopyArray(int newLength) {
|
---|
| 140 | Array newArray = Array.CreateInstance(ArrayDataBase.Data.GetType().GetElementType(), newLength);
|
---|
| 141 | Array.Copy(ArrayDataBase.Data, newArray, Math.Min(newLength, ArrayDataBase.Data.Length));
|
---|
| 142 | ArrayDataBase.Data = newArray;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
|
---|
| 146 | if (ValidateData((string)e.FormattedValue)) {
|
---|
| 147 | SetArrayElement(e.RowIndex, (string)e.FormattedValue);
|
---|
| 148 | e.Cancel = false;
|
---|
| 149 | } else {
|
---|
| 150 | e.Cancel = true;
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | private void lengthTextBox_KeyDown(object sender, KeyEventArgs e) {
|
---|
| 155 | if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return) {
|
---|
| 156 | e.SuppressKeyPress = true;
|
---|
| 157 | dataGridView.Focus();
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | #region ArrayDataBase Events
|
---|
| 162 | private void ArrayDataBase_Changed(object sender, EventArgs e) {
|
---|
| 163 | Refresh();
|
---|
| 164 | }
|
---|
| 165 | #endregion
|
---|
| 166 | }
|
---|
| 167 | }
|
---|