Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.2/ArrayDataBaseView.cs @ 3295

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

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

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