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