Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11996 was 11170, checked in by ascheibe, 10 years ago

#2115 updated copyright year in stable branch

File size: 9.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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        // if a resize of the array occurs and some other class handles the event and provides default values
128        //then the itemChanged will occur before the reset event. hence the check was added
129        if (dataGridView.RowCount <= e.Value) return;
130        dataGridView.Rows[e.Value].Cells[0].Value = Content.GetValue(e.Value);
131        Size size = dataGridView.Rows[e.Value].Cells[0].PreferredSize;
132        dataGridView.Columns[0].Width = Math.Max(dataGridView.Columns[0].Width, size.Width);
133      }
134    }
135    private void Content_Reset(object sender, EventArgs e) {
136      if (InvokeRequired)
137        Invoke(new EventHandler(Content_Reset), sender, e);
138      else
139        UpdateData();
140    }
141
142    #region TextBox Events
143    private void lengthTextBox_Validating(object sender, CancelEventArgs e) {
144      int i = 0;
145      if (!int.TryParse(lengthTextBox.Text, out i) || (i < 0)) {
146        e.Cancel = true;
147        errorProvider.SetError(lengthTextBox, "Invalid Array Length (Valid Values: Positive Integers Larger or Equal to 0)");
148        lengthTextBox.SelectAll();
149      }
150    }
151    private void lengthTextBox_Validated(object sender, EventArgs e) {
152      if (!Content.ReadOnly) Content.Length = int.Parse(lengthTextBox.Text);
153      errorProvider.SetError(lengthTextBox, string.Empty);
154    }
155    private void lengthTextBox_KeyDown(object sender, KeyEventArgs e) {
156      if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
157        lengthLabel.Focus();  // set focus on label to validate data
158      if (e.KeyCode == Keys.Escape) {
159        lengthTextBox.Text = Content.Length.ToString();
160        lengthLabel.Focus();  // set focus on label to validate data
161      }
162    }
163    #endregion
164
165    #region DataGridView Events
166    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
167      string errorMessage;
168      if (Content != null && !Content.Validate(e.FormattedValue.ToString(), out errorMessage)) {
169        e.Cancel = true;
170        dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
171      }
172    }
173    private void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
174      string value = e.Value.ToString();
175      e.ParsingApplied = Content.SetValue(value, e.RowIndex);
176      if (e.ParsingApplied) e.Value = Content.GetValue(e.RowIndex);
177    }
178    private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
179      dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
180    }
181    private void dataGridView_KeyDown(object sender, KeyEventArgs e) {
182      if (!ReadOnly && e.Control && e.KeyCode == Keys.V)
183        PasteValuesToDataGridView();
184      else if (e.Control && e.KeyCode == Keys.C)
185        CopyValuesFromDataGridView();
186      else if (e.Control && e.KeyCode == Keys.A)
187        dataGridView.SelectAll();
188    }
189    private void CopyValuesFromDataGridView() {
190      if (dataGridView.SelectedCells.Count == 0) return;
191      StringBuilder s = new StringBuilder();
192      int minRowIndex = dataGridView.SelectedCells[0].RowIndex;
193      int maxRowIndex = dataGridView.SelectedCells[dataGridView.SelectedCells.Count - 1].RowIndex;
194
195      if (minRowIndex > maxRowIndex) {
196        int temp = minRowIndex;
197        minRowIndex = maxRowIndex;
198        maxRowIndex = temp;
199      }
200
201      for (int i = minRowIndex; i <= maxRowIndex; i++) {
202        DataGridViewColumn column = dataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
203        DataGridViewCell cell = dataGridView[column.Index, i];
204        if (cell.Selected) {
205          s.Append(Content.GetValue(i));
206          s.Append(Environment.NewLine);
207        }
208      }
209      Clipboard.SetText(s.ToString());
210    }
211    private void PasteValuesToDataGridView() {
212      string[] values = SplitClipboardString(Clipboard.GetText());
213      int rowIndex = 0;
214      if (dataGridView.CurrentCell != null)
215        rowIndex = dataGridView.CurrentCell.RowIndex;
216
217      if (Content.Length < rowIndex + values.Length) Content.Length = rowIndex + values.Length;
218      for (int row = 0; row < values.Length; row++)
219        Content.SetValue(values[row], row + rowIndex);
220    }
221    private string[] SplitClipboardString(string clipboardText) {
222      if (clipboardText.EndsWith(Environment.NewLine))
223        clipboardText = clipboardText.Remove(clipboardText.Length - Environment.NewLine.Length);  //remove last newline constant
224      return clipboardText.Split(new string[] { Environment.NewLine, "\t" }, StringSplitOptions.None);
225    }
226    #endregion
227  }
228}
Note: See TracBrowser for help on using the repository browser.