Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9657 was 9657, checked in by mkommend, 10 years ago

#2075: Added element names for the ValueType- and StringArray classes.

File size: 8.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Linq;
26using System.Text;
27using System.Windows.Forms;
28using HeuristicLab.Common;
29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
31
32namespace HeuristicLab.Data.Views {
33  [View("StringConvertibleArray View")]
34  [Content(typeof(IStringConvertibleArray), true)]
35  public partial class StringConvertibleArrayView : AsynchronousContentView {
36    public new IStringConvertibleArray Content {
37      get { return (IStringConvertibleArray)base.Content; }
38      set { base.Content = value; }
39    }
40
41    public override bool ReadOnly {
42      get {
43        if ((Content != null) && Content.ReadOnly) return true;
44        return base.ReadOnly;
45      }
46      set { base.ReadOnly = value; }
47    }
48
49    public StringConvertibleArrayView() {
50      InitializeComponent();
51      errorProvider.SetIconAlignment(lengthTextBox, ErrorIconAlignment.MiddleLeft);
52      errorProvider.SetIconPadding(lengthTextBox, 2);
53    }
54
55    protected override void DeregisterContentEvents() {
56      Content.ElementNamesChanged -= new EventHandler(Content_ElementNamesChanged);
57      Content.ItemChanged -= new EventHandler<EventArgs<int>>(Content_ItemChanged);
58      Content.Reset -= new EventHandler(Content_Reset);
59      base.DeregisterContentEvents();
60    }
61
62    protected override void RegisterContentEvents() {
63      base.RegisterContentEvents();
64      Content.ItemChanged += new EventHandler<EventArgs<int>>(Content_ItemChanged);
65      Content.Reset += new EventHandler(Content_Reset);
66      Content.ElementNamesChanged += new EventHandler(Content_ElementNamesChanged);
67    }
68
69    protected override void OnContentChanged() {
70      base.OnContentChanged();
71      if (Content == null) {
72        lengthTextBox.Text = "";
73        dataGridView.Rows.Clear();
74        dataGridView.Columns.Clear();
75      } else
76        UpdateData();
77    }
78
79    protected override void SetEnabledStateOfControls() {
80      base.SetEnabledStateOfControls();
81      lengthTextBox.Enabled = Content != null;
82      dataGridView.Enabled = Content != null;
83      lengthTextBox.ReadOnly = ReadOnly;
84      dataGridView.ReadOnly = ReadOnly;
85    }
86
87    private void UpdateData() {
88      lengthTextBox.Text = Content.Length.ToString();
89      lengthTextBox.Enabled = true;
90      dataGridView.Rows.Clear();
91      dataGridView.Columns.Clear();
92      if (Content.Length > 0) {
93        dataGridView.ColumnCount++;
94        dataGridView.Columns[0].FillWeight = float.Epsilon;  // sum of all fill weights must not be larger than 65535
95        dataGridView.RowCount = Content.Length;
96        for (int i = 0; i < Content.Length; i++) {
97          dataGridView.Rows[i].Cells[0].Value = Content.GetValue(i);
98        }
99        dataGridView.Columns[0].Width = dataGridView.Columns[0].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true);
100      }
101      UpdateRowHeaders();
102      dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
103      dataGridView.Enabled = true;
104    }
105
106    protected virtual void UpdateRowHeaders() {
107      for (int i = 0; i < dataGridView.RowCount; i++) {
108        if (i < Content.ElementNames.Count())
109          dataGridView.Rows[i].HeaderCell.Value = Content.ElementNames.ElementAt(i);
110        else
111          dataGridView.Rows[i].HeaderCell.Value = string.Empty;
112      }
113    }
114
115    private void Content_ElementNamesChanged(object sender, EventArgs e) {
116      if (InvokeRequired)
117        Invoke(new EventHandler(Content_ElementNamesChanged), sender, e);
118      else
119        UpdateRowHeaders();
120    }
121
122    private void Content_ItemChanged(object sender, EventArgs<int> e) {
123      if (InvokeRequired)
124        Invoke(new EventHandler<EventArgs<int>>(Content_ItemChanged), sender, e);
125      else {
126        dataGridView.Rows[e.Value].Cells[0].Value = Content.GetValue(e.Value);
127        Size size = dataGridView.Rows[e.Value].Cells[0].PreferredSize;
128        dataGridView.Columns[0].Width = Math.Max(dataGridView.Columns[0].Width, size.Width);
129      }
130    }
131    private void Content_Reset(object sender, EventArgs e) {
132      if (InvokeRequired)
133        Invoke(new EventHandler(Content_Reset), sender, e);
134      else
135        UpdateData();
136    }
137
138    #region TextBox Events
139    private void lengthTextBox_Validating(object sender, CancelEventArgs e) {
140      int i = 0;
141      if (!int.TryParse(lengthTextBox.Text, out i) || (i < 0)) {
142        e.Cancel = true;
143        errorProvider.SetError(lengthTextBox, "Invalid Array Length (Valid Values: Positive Integers Larger or Equal to 0)");
144        lengthTextBox.SelectAll();
145      }
146    }
147    private void lengthTextBox_Validated(object sender, EventArgs e) {
148      if (!Content.ReadOnly) Content.Length = int.Parse(lengthTextBox.Text);
149      errorProvider.SetError(lengthTextBox, string.Empty);
150    }
151    private void lengthTextBox_KeyDown(object sender, KeyEventArgs e) {
152      if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
153        lengthLabel.Focus();  // set focus on label to validate data
154      if (e.KeyCode == Keys.Escape) {
155        lengthTextBox.Text = Content.Length.ToString();
156        lengthLabel.Focus();  // set focus on label to validate data
157      }
158    }
159    #endregion
160
161    #region DataGridView Events
162    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
163      string errorMessage;
164      if (Content != null && !Content.Validate(e.FormattedValue.ToString(), out errorMessage)) {
165        e.Cancel = true;
166        dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
167      }
168    }
169    private void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
170      string value = e.Value.ToString();
171      e.ParsingApplied = Content.SetValue(value, e.RowIndex);
172      if (e.ParsingApplied) e.Value = Content.GetValue(e.RowIndex);
173    }
174    private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
175      dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
176    }
177    private void dataGridView_KeyDown(object sender, KeyEventArgs e) {
178      if (!ReadOnly && e.Control && e.KeyCode == Keys.V)
179        PasteValuesToDataGridView();
180      else if (e.Control && e.KeyCode == Keys.C)
181        CopyValuesFromDataGridView();
182      else if (e.Control && e.KeyCode == Keys.A)
183        dataGridView.SelectAll();
184    }
185    private void CopyValuesFromDataGridView() {
186      if (dataGridView.SelectedCells.Count == 0) return;
187      StringBuilder s = new StringBuilder();
188      int minRowIndex = dataGridView.SelectedCells[0].RowIndex;
189      int maxRowIndex = dataGridView.SelectedCells[dataGridView.SelectedCells.Count - 1].RowIndex;
190
191      if (minRowIndex > maxRowIndex) {
192        int temp = minRowIndex;
193        minRowIndex = maxRowIndex;
194        maxRowIndex = temp;
195      }
196
197      for (int i = minRowIndex; i <= maxRowIndex; i++) {
198        DataGridViewColumn column = dataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
199        DataGridViewCell cell = dataGridView[column.Index, i];
200        if (cell.Selected) {
201          s.Append(Content.GetValue(i));
202          s.Append(Environment.NewLine);
203        }
204      }
205      Clipboard.SetText(s.ToString());
206    }
207    private void PasteValuesToDataGridView() {
208      string[] values = SplitClipboardString(Clipboard.GetText());
209      int rowIndex = 0;
210      if (dataGridView.CurrentCell != null)
211        rowIndex = dataGridView.CurrentCell.RowIndex;
212
213      if (Content.Length < rowIndex + values.Length) Content.Length = rowIndex + values.Length;
214      for (int row = 0; row < values.Length; row++)
215        Content.SetValue(values[row], row + rowIndex);
216    }
217    private string[] SplitClipboardString(string clipboardText) {
218      if (clipboardText.EndsWith(Environment.NewLine))
219        clipboardText = clipboardText.Remove(clipboardText.Length - Environment.NewLine.Length);  //remove last newline constant
220      return clipboardText.Split(new string[] { Environment.NewLine, "\t" }, StringSplitOptions.None);
221    }
222    #endregion
223  }
224}
Note: See TracBrowser for help on using the repository browser.