Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Data/ArrayDataBaseView.cs @ 17456

Last change on this file since 17456 was 344, checked in by gkronber, 16 years ago

merged changes r338 r339 r340 r341 r342 r343 from the ticket-specific branch into the main trunk

File size: 4.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  public partial class ArrayDataBaseView : ViewBase {
33    public ArrayDataBase ArrayDataBase {
34      get { return (ArrayDataBase)Item; }
35      protected set { base.Item = value; }
36    }
37
38    public ArrayDataBaseView() {
39      InitializeComponent();
40    }
41
42    protected override void RemoveItemEvents() {
43      ArrayDataBase.Changed -= new EventHandler(ArrayDataBase_Changed);
44      base.RemoveItemEvents();
45    }
46    protected override void AddItemEvents() {
47      base.AddItemEvents();
48      ArrayDataBase.Changed += new EventHandler(ArrayDataBase_Changed);
49    }
50
51    protected virtual bool ValidateData(string element) {
52      throw new InvalidOperationException("ValidateData has to be overridden in each inherited class");
53    }
54    protected virtual void SetArrayElement(int index, string element) {
55      throw new InvalidOperationException("SetArrayElement has to be overridden in each inherited class");
56    }
57
58    protected override void UpdateControls() {
59      base.UpdateControls();
60      if (ArrayDataBase != null) {
61        int length = ArrayDataBase.Data.Length;
62        lengthTextBox.Text = length + "";
63        dataGridView.ColumnCount = 1;
64        dataGridView.RowCount = length;
65        for (int i = 0; i < length; i++) {
66          dataGridView.Rows[i].Cells[0].Value = ArrayDataBase.Data.GetValue(i);
67        }
68      } else {
69        lengthTextBox.Text = "0";
70        dataGridView.ColumnCount = 1;
71        dataGridView.RowCount = 0;
72      }
73    }
74
75    private void lengthTextBox_Validating(object sender, CancelEventArgs e) {
76      int newLength;
77      if (int.TryParse(lengthTextBox.Text, out newLength)) {
78        if (newLength > 0) {
79          e.Cancel = false;
80          if (newLength != ArrayDataBase.Data.Length) {
81            CreateAndCopyArray(newLength);
82          }
83        } else {
84          // only allow values greater than 0
85          e.Cancel = true;
86        }
87      } else {
88        e.Cancel = true;
89      }
90    }
91
92    private void CreateAndCopyArray(int newLength) {
93      Array newArray = Array.CreateInstance(ArrayDataBase.Data.GetType().GetElementType(), newLength);
94      Array.Copy(ArrayDataBase.Data, newArray, Math.Min(newLength, ArrayDataBase.Data.Length));
95      ArrayDataBase.Data = newArray;
96    }
97
98    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
99      if (ValidateData((string)e.FormattedValue)) {
100        SetArrayElement(e.RowIndex, (string)e.FormattedValue);
101        e.Cancel = false;
102      } else {
103        e.Cancel = true;
104      }
105    }
106
107    private void lengthTextBox_KeyDown(object sender, KeyEventArgs e) {
108      if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return) {
109        e.SuppressKeyPress = true;
110        dataGridView.Focus();
111      }
112    }
113
114    #region ArrayDataBase Events
115    private void ArrayDataBase_Changed(object sender, EventArgs e) {
116      Refresh();
117    }
118    #endregion
119  }
120}
Note: See TracBrowser for help on using the repository browser.