Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Data.Views/3.3/StringConvertibleArrayView.cs @ 15287

Last change on this file since 15287 was 7984, checked in by abeham, 13 years ago

#1870:

  • Added a check to remove the last newline only when there's actually a newline at the end
  • Improved speed of StringConvertibleMatrixView with respect to paste operations that would extend the number of rows and columns
File size: 8.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.Text;
26using System.Windows.Forms;
27using HeuristicLab.Common;
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.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        lengthTextBox.Text = "";
70        dataGridView.Rows.Clear();
71        dataGridView.Columns.Clear();
72      } else
73        UpdateData();
74    }
75
76    protected override void SetEnabledStateOfControls() {
77      base.SetEnabledStateOfControls();
78      lengthTextBox.Enabled = Content != null;
79      dataGridView.Enabled = Content != null;
80      lengthTextBox.ReadOnly = ReadOnly;
81      dataGridView.ReadOnly = ReadOnly;
82    }
83
84    private void UpdateData() {
85      lengthTextBox.Text = Content.Length.ToString();
86      lengthTextBox.Enabled = true;
87      dataGridView.Rows.Clear();
88      dataGridView.Columns.Clear();
89      if (Content.Length > 0) {
90        dataGridView.ColumnCount++;
91        dataGridView.Columns[0].FillWeight = float.Epsilon;  // sum of all fill weights must not be larger than 65535
92        dataGridView.RowCount = Content.Length;
93        for (int i = 0; i < Content.Length; i++) {
94          dataGridView.Rows[i].Cells[0].Value = Content.GetValue(i);
95        }
96        dataGridView.Columns[0].Width = dataGridView.Columns[0].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true);
97      }
98      dataGridView.Enabled = true;
99    }
100
101    private void Content_ItemChanged(object sender, EventArgs<int> e) {
102      if (InvokeRequired)
103        Invoke(new EventHandler<EventArgs<int>>(Content_ItemChanged), sender, e);
104      else {
105        dataGridView.Rows[e.Value].Cells[0].Value = Content.GetValue(e.Value);
106        Size size = dataGridView.Rows[e.Value].Cells[0].PreferredSize;
107        dataGridView.Columns[0].Width = Math.Max(dataGridView.Columns[0].Width, size.Width);
108      }
109    }
110    private void Content_Reset(object sender, EventArgs e) {
111      if (InvokeRequired)
112        Invoke(new EventHandler(Content_Reset), sender, e);
113      else
114        UpdateData();
115    }
116
117    #region TextBox Events
118    private void lengthTextBox_Validating(object sender, CancelEventArgs e) {
119      int i = 0;
120      if (!int.TryParse(lengthTextBox.Text, out i) || (i < 0)) {
121        e.Cancel = true;
122        errorProvider.SetError(lengthTextBox, "Invalid Array Length (Valid Values: Positive Integers Larger or Equal to 0)");
123        lengthTextBox.SelectAll();
124      }
125    }
126    private void lengthTextBox_Validated(object sender, EventArgs e) {
127      if (!Content.ReadOnly) Content.Length = int.Parse(lengthTextBox.Text);
128      errorProvider.SetError(lengthTextBox, string.Empty);
129    }
130    private void lengthTextBox_KeyDown(object sender, KeyEventArgs e) {
131      if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
132        lengthLabel.Focus();  // set focus on label to validate data
133      if (e.KeyCode == Keys.Escape) {
134        lengthTextBox.Text = Content.Length.ToString();
135        lengthLabel.Focus();  // set focus on label to validate data
136      }
137    }
138    #endregion
139
140    #region DataGridView Events
141    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
142      string errorMessage;
143      if (Content != null && !Content.Validate(e.FormattedValue.ToString(), out errorMessage)) {
144        e.Cancel = true;
145        dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
146      }
147    }
148    private void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
149      string value = e.Value.ToString();
150      e.ParsingApplied = Content.SetValue(value, e.RowIndex);
151      if (e.ParsingApplied) e.Value = Content.GetValue(e.RowIndex);
152    }
153    private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
154      dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
155    }
156    private void dataGridView_KeyDown(object sender, KeyEventArgs e) {
157      if (!ReadOnly && e.Control && e.KeyCode == Keys.V)
158        PasteValuesToDataGridView();
159      else if (e.Control && e.KeyCode == Keys.C)
160        CopyValuesFromDataGridView();
161      else if (e.Control && e.KeyCode == Keys.A)
162        dataGridView.SelectAll();
163    }
164    private void CopyValuesFromDataGridView() {
165      if (dataGridView.SelectedCells.Count == 0) return;
166      StringBuilder s = new StringBuilder();
167      int minRowIndex = dataGridView.SelectedCells[0].RowIndex;
168      int maxRowIndex = dataGridView.SelectedCells[dataGridView.SelectedCells.Count - 1].RowIndex;
169
170      if (minRowIndex > maxRowIndex) {
171        int temp = minRowIndex;
172        minRowIndex = maxRowIndex;
173        maxRowIndex = temp;
174      }
175
176      for (int i = minRowIndex; i <= maxRowIndex; i++) {
177        DataGridViewColumn column = dataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
178        DataGridViewCell cell = dataGridView[column.Index, i];
179        if (cell.Selected) {
180          s.Append(Content.GetValue(i));
181          s.Append(Environment.NewLine);
182        }
183      }
184      Clipboard.SetText(s.ToString());
185    }
186    private void PasteValuesToDataGridView() {
187      string[] values = SplitClipboardString(Clipboard.GetText());
188      int rowIndex = 0;
189      if (dataGridView.CurrentCell != null)
190        rowIndex = dataGridView.CurrentCell.RowIndex;
191
192      if (Content.Length < rowIndex + values.Length) Content.Length = rowIndex + values.Length;
193      for (int row = 0; row < values.Length; row++)
194        Content.SetValue(values[row], row + rowIndex);
195    }
196    private string[] SplitClipboardString(string clipboardText) {
197      if (clipboardText.EndsWith(Environment.NewLine))
198        clipboardText = clipboardText.Remove(clipboardText.Length - Environment.NewLine.Length);  //remove last newline constant
199      return clipboardText.Split(new string[] { Environment.NewLine, "\t" }, StringSplitOptions.None);
200    }
201    #endregion
202  }
203}
Note: See TracBrowser for help on using the repository browser.