Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data.Views/3.3/StringConvertibleArrayView.cs @ 3188

Last change on this file since 3188 was 3048, checked in by swagner, 15 years ago

Renamed classes of HeuristicLab.Data (#909)

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.ComponentModel;
24using System.Drawing;
25using System.Windows.Forms;
26using HeuristicLab.Common;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29
30namespace HeuristicLab.Data.Views {
31  [View("StringConvertibleArray View")]
32  [Content(typeof(IStringConvertibleArray), true)]
33  public partial class StringConvertibleArrayView : ContentView {
34    public new IStringConvertibleArray Content {
35      get { return (IStringConvertibleArray)base.Content; }
36      set { base.Content = value; }
37    }
38
39    public StringConvertibleArrayView() {
40      InitializeComponent();
41      Caption = "StringConvertibleArray View";
42      errorProvider.SetIconAlignment(lengthTextBox, ErrorIconAlignment.MiddleLeft);
43      errorProvider.SetIconPadding(lengthTextBox, 2);
44    }
45    public StringConvertibleArrayView(IStringConvertibleArray content)
46      : this() {
47      Content = content;
48    }
49
50    protected override void DeregisterContentEvents() {
51      Content.ItemChanged -= new EventHandler<EventArgs<int>>(Content_ItemChanged);
52      Content.Reset -= new EventHandler(Content_Reset);
53      base.DeregisterContentEvents();
54    }
55
56
57    protected override void RegisterContentEvents() {
58      base.RegisterContentEvents();
59      Content.ItemChanged += new EventHandler<EventArgs<int>>(Content_ItemChanged);
60      Content.Reset += new EventHandler(Content_Reset);
61    }
62
63    protected override void OnContentChanged() {
64      base.OnContentChanged();
65      if (Content == null) {
66        Caption = "StringConvertibleArray View";
67        lengthTextBox.Text = "";
68        lengthTextBox.Enabled = false;
69        dataGridView.Rows.Clear();
70        dataGridView.Columns.Clear();
71        dataGridView.Enabled = false;
72      } else {
73        Caption = "StringConvertibleArray (" + Content.GetType().Name + ")";
74        UpdateData();
75      }
76    }
77
78    private void UpdateData() {
79      lengthTextBox.Text = Content.Length.ToString();
80      lengthTextBox.Enabled = true;
81      dataGridView.Rows.Clear();
82      dataGridView.Columns.Clear();
83      if (Content.Length > 0) {
84        dataGridView.ColumnCount++;
85        dataGridView.Columns[0].FillWeight = float.Epsilon;  // sum of all fill weights must not be larger than 65535
86        dataGridView.RowCount = Content.Length;
87        for (int i = 0; i < Content.Length; i++) {
88          dataGridView.Rows[i].Cells[0].Value = Content.GetValue(i);
89        }
90        dataGridView.Columns[0].Width = dataGridView.Columns[0].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true);
91      }
92      dataGridView.Enabled = true;
93    }
94
95    private void Content_ItemChanged(object sender, EventArgs<int> e) {
96      if (InvokeRequired)
97        Invoke(new EventHandler<EventArgs<int>>(Content_ItemChanged), sender, e);
98      else {
99        dataGridView.Rows[e.Value].Cells[0].Value = Content.GetValue(e.Value);
100        Size size = dataGridView.Rows[e.Value].Cells[0].PreferredSize;
101        dataGridView.Columns[0].Width = Math.Max(dataGridView.Columns[0].Width, size.Width);
102      }
103    }
104    private void Content_Reset(object sender, EventArgs e) {
105      if (InvokeRequired)
106        Invoke(new EventHandler(Content_Reset), sender, e);
107      else
108        UpdateData();
109    }
110
111    #region TextBox Events
112    private void lengthTextBox_Validating(object sender, CancelEventArgs e) {
113      int i = 0;
114      if (!int.TryParse(lengthTextBox.Text, out i) || (i < 0)) {
115        e.Cancel = true;
116        errorProvider.SetError(lengthTextBox, "Invalid Array Length (Valid Values: Positive Integers Larger or Equal to 0)");
117        lengthTextBox.SelectAll();
118      }
119    }
120    private void lengthTextBox_Validated(object sender, EventArgs e) {
121      Content.Length = int.Parse(lengthTextBox.Text);
122      errorProvider.SetError(lengthTextBox, string.Empty);
123    }
124    private void lengthTextBox_KeyDown(object sender, KeyEventArgs e) {
125      if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
126        lengthLabel.Focus();  // set focus on label to validate data
127      if (e.KeyCode == Keys.Escape) {
128        lengthTextBox.Text = Content.Length.ToString();
129        lengthLabel.Focus();  // set focus on label to validate data
130      }
131    }
132    #endregion
133
134    #region DataGridView Events
135    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
136      string errorMessage;
137      if (!Content.Validate(e.FormattedValue.ToString(), out errorMessage)) {
138        e.Cancel = true;
139        dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
140      }
141    }
142    private void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
143      string value = e.Value.ToString();
144      e.ParsingApplied = Content.SetValue(value, e.RowIndex);
145      if (e.ParsingApplied) e.Value = Content.GetValue(e.RowIndex);
146    }
147    private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
148      dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
149    }
150    #endregion
151  }
152}
Note: See TracBrowser for help on using the repository browser.