Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3434 was 3431, checked in by swagner, 15 years ago

Removed property ReadOnlyView (#969)

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