Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Data.Views/3.3/StringConvertibleArrayView.cs @ 9771

Last change on this file since 9771 was 9737, checked in by mkommend, 11 years ago

#2075: Integrated the performed changes in the stable branch.

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.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.ElementNamesChanged -= new EventHandler(Content_ElementNamesChanged);
56      Content.ItemChanged -= new EventHandler<EventArgs<int>>(Content_ItemChanged);
57      Content.Reset -= new EventHandler(Content_Reset);
58      base.DeregisterContentEvents();
59    }
60
61    protected override void RegisterContentEvents() {
62      base.RegisterContentEvents();
63      Content.ItemChanged += new EventHandler<EventArgs<int>>(Content_ItemChanged);
64      Content.Reset += new EventHandler(Content_Reset);
65      Content.ElementNamesChanged += new EventHandler(Content_ElementNamesChanged);
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        dataGridView.Columns[0].Width = dataGridView.Columns[0].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true);
99      }
100      UpdateRowHeaders();
101      dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
102      dataGridView.Enabled = true;
103    }
104
105    protected virtual void UpdateRowHeaders() {
106      int i = 0;
107      foreach (string elementName in Content.ElementNames) {
108        dataGridView.Rows[i].HeaderCell.Value = elementName;
109        i++;
110      }
111      for (; i < dataGridView.RowCount; i++) {
112        dataGridView.Rows[i].HeaderCell.Value = string.Empty;
113      }
114    }
115
116    private void Content_ElementNamesChanged(object sender, EventArgs e) {
117      if (InvokeRequired)
118        Invoke(new EventHandler(Content_ElementNamesChanged), sender, e);
119      else
120        UpdateRowHeaders();
121    }
122
123    private void Content_ItemChanged(object sender, EventArgs<int> e) {
124      if (InvokeRequired)
125        Invoke(new EventHandler<EventArgs<int>>(Content_ItemChanged), sender, e);
126      else {
127        dataGridView.Rows[e.Value].Cells[0].Value = Content.GetValue(e.Value);
128        Size size = dataGridView.Rows[e.Value].Cells[0].PreferredSize;
129        dataGridView.Columns[0].Width = Math.Max(dataGridView.Columns[0].Width, size.Width);
130      }
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.