Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5955 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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