Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3390 was 3362, checked in by swagner, 15 years ago

Adapted views of HeuristicLab.Core.Views according the new read-only property and renamed method SetEnableStateOfControls into SetEnabledStateOfControls (#973).

File size: 6.3 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 : AsynchronousContentView {
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    protected override void RegisterContentEvents() {
57      base.RegisterContentEvents();
58      Content.ItemChanged += new EventHandler<EventArgs<int>>(Content_ItemChanged);
59      Content.Reset += new EventHandler(Content_Reset);
60    }
61
62    protected override void OnContentChanged() {
63      base.OnContentChanged();
64      if (Content == null) {
65        Caption = "StringConvertibleArray View";
66        lengthTextBox.Text = "";
67        dataGridView.Rows.Clear();
68        dataGridView.Columns.Clear();
69      } else {
70        Caption = "StringConvertibleArray (" + Content.GetType().Name + ")";
71        UpdateData();
72      }
73      SetEnabledStateOfControls();
74    }
75    protected override void OnReadOnlyChanged() {
76      base.OnReadOnlyChanged();
77      SetEnabledStateOfControls();
78    }
79    private void SetEnabledStateOfControls() {
80      if (Content == null) {
81        lengthTextBox.Enabled = false;
82        dataGridView.Enabled = false;
83      } else {
84        lengthTextBox.Enabled = true;
85        dataGridView.Enabled = true;
86        lengthTextBox.ReadOnly = ReadOnly;
87        dataGridView.ReadOnly = ReadOnly;
88      }
89    }
90
91    private void UpdateData() {
92      lengthTextBox.Text = Content.Length.ToString();
93      lengthTextBox.Enabled = true;
94      dataGridView.Rows.Clear();
95      dataGridView.Columns.Clear();
96      if (Content.Length > 0) {
97        dataGridView.ColumnCount++;
98        dataGridView.Columns[0].FillWeight = float.Epsilon;  // sum of all fill weights must not be larger than 65535
99        dataGridView.RowCount = Content.Length;
100        for (int i = 0; i < Content.Length; i++) {
101          dataGridView.Rows[i].Cells[0].Value = Content.GetValue(i);
102        }
103        dataGridView.Columns[0].Width = dataGridView.Columns[0].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true);
104      }
105      dataGridView.ReadOnly = Content.ReadOnlyView;
106      dataGridView.Enabled = true;
107    }
108
109    private void Content_ItemChanged(object sender, EventArgs<int> e) {
110      if (InvokeRequired)
111        Invoke(new EventHandler<EventArgs<int>>(Content_ItemChanged), sender, e);
112      else {
113        dataGridView.Rows[e.Value].Cells[0].Value = Content.GetValue(e.Value);
114        Size size = dataGridView.Rows[e.Value].Cells[0].PreferredSize;
115        dataGridView.Columns[0].Width = Math.Max(dataGridView.Columns[0].Width, size.Width);
116      }
117    }
118    private void Content_Reset(object sender, EventArgs e) {
119      if (InvokeRequired)
120        Invoke(new EventHandler(Content_Reset), sender, e);
121      else
122        UpdateData();
123    }
124
125    #region TextBox Events
126    private void lengthTextBox_Validating(object sender, CancelEventArgs e) {
127      int i = 0;
128      if (!int.TryParse(lengthTextBox.Text, out i) || (i < 0)) {
129        e.Cancel = true;
130        errorProvider.SetError(lengthTextBox, "Invalid Array Length (Valid Values: Positive Integers Larger or Equal to 0)");
131        lengthTextBox.SelectAll();
132      }
133    }
134    private void lengthTextBox_Validated(object sender, EventArgs e) {
135      Content.Length = int.Parse(lengthTextBox.Text);
136      errorProvider.SetError(lengthTextBox, string.Empty);
137    }
138    private void lengthTextBox_KeyDown(object sender, KeyEventArgs e) {
139      if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
140        lengthLabel.Focus();  // set focus on label to validate data
141      if (e.KeyCode == Keys.Escape) {
142        lengthTextBox.Text = Content.Length.ToString();
143        lengthLabel.Focus();  // set focus on label to validate data
144      }
145    }
146    #endregion
147
148    #region DataGridView Events
149    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
150      string errorMessage;
151      if (!Content.Validate(e.FormattedValue.ToString(), out errorMessage)) {
152        e.Cancel = true;
153        dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
154      }
155    }
156    private void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
157      string value = e.Value.ToString();
158      e.ParsingApplied = Content.SetValue(value, e.RowIndex);
159      if (e.ParsingApplied) e.Value = Content.GetValue(e.RowIndex);
160    }
161    private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
162      dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
163    }
164    #endregion
165  }
166}
Note: See TracBrowser for help on using the repository browser.