Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ImprovingStringConvertibleMatrix/HeuristicLab.Data.Views/3.3/StringConvertibleArrayView.cs @ 9308

Last change on this file since 9308 was 9308, checked in by sforsten, 12 years ago

#2018:

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