[2973] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2973] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.ComponentModel;
|
---|
| 24 | using System.Drawing;
|
---|
[7383] | 25 | using System.Text;
|
---|
[2973] | 26 | using System.Windows.Forms;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.MainForm;
|
---|
| 29 | using HeuristicLab.MainForm.WindowsForms;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Data.Views {
|
---|
[3048] | 32 | [View("StringConvertibleArray View")]
|
---|
| 33 | [Content(typeof(IStringConvertibleArray), true)]
|
---|
[3228] | 34 | public partial class StringConvertibleArrayView : AsynchronousContentView {
|
---|
[3048] | 35 | public new IStringConvertibleArray Content {
|
---|
| 36 | get { return (IStringConvertibleArray)base.Content; }
|
---|
[2973] | 37 | set { base.Content = value; }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[3430] | 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 |
|
---|
[3048] | 48 | public StringConvertibleArrayView() {
|
---|
[2973] | 49 | InitializeComponent();
|
---|
[2974] | 50 | errorProvider.SetIconAlignment(lengthTextBox, ErrorIconAlignment.MiddleLeft);
|
---|
| 51 | errorProvider.SetIconPadding(lengthTextBox, 2);
|
---|
[2973] | 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) {
|
---|
[2974] | 69 | lengthTextBox.Text = "";
|
---|
[2973] | 70 | dataGridView.Rows.Clear();
|
---|
| 71 | dataGridView.Columns.Clear();
|
---|
[3764] | 72 | } else
|
---|
[2973] | 73 | UpdateData();
|
---|
| 74 | }
|
---|
[3904] | 75 |
|
---|
| 76 | protected override void SetEnabledStateOfControls() {
|
---|
| 77 | base.SetEnabledStateOfControls();
|
---|
[3454] | 78 | lengthTextBox.Enabled = Content != null;
|
---|
| 79 | dataGridView.Enabled = Content != null;
|
---|
| 80 | lengthTextBox.ReadOnly = ReadOnly;
|
---|
| 81 | dataGridView.ReadOnly = ReadOnly;
|
---|
[3350] | 82 | }
|
---|
[2973] | 83 |
|
---|
| 84 | private void UpdateData() {
|
---|
[2974] | 85 | lengthTextBox.Text = Content.Length.ToString();
|
---|
| 86 | lengthTextBox.Enabled = true;
|
---|
[2973] | 87 | dataGridView.Rows.Clear();
|
---|
| 88 | dataGridView.Columns.Clear();
|
---|
[2974] | 89 | if (Content.Length > 0) {
|
---|
[2973] | 90 | dataGridView.ColumnCount++;
|
---|
| 91 | dataGridView.Columns[0].FillWeight = float.Epsilon; // sum of all fill weights must not be larger than 65535
|
---|
[2974] | 92 | dataGridView.RowCount = Content.Length;
|
---|
| 93 | for (int i = 0; i < Content.Length; i++) {
|
---|
[2973] | 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
|
---|
[2974] | 118 | private void lengthTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
[2973] | 119 | int i = 0;
|
---|
[2974] | 120 | if (!int.TryParse(lengthTextBox.Text, out i) || (i < 0)) {
|
---|
[2973] | 121 | e.Cancel = true;
|
---|
[2974] | 122 | errorProvider.SetError(lengthTextBox, "Invalid Array Length (Valid Values: Positive Integers Larger or Equal to 0)");
|
---|
| 123 | lengthTextBox.SelectAll();
|
---|
[2973] | 124 | }
|
---|
| 125 | }
|
---|
[2974] | 126 | private void lengthTextBox_Validated(object sender, EventArgs e) {
|
---|
[3430] | 127 | if (!Content.ReadOnly) Content.Length = int.Parse(lengthTextBox.Text);
|
---|
[2974] | 128 | errorProvider.SetError(lengthTextBox, string.Empty);
|
---|
[2973] | 129 | }
|
---|
[2974] | 130 | private void lengthTextBox_KeyDown(object sender, KeyEventArgs e) {
|
---|
[2973] | 131 | if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
|
---|
[2974] | 132 | lengthLabel.Focus(); // set focus on label to validate data
|
---|
[2973] | 133 | if (e.KeyCode == Keys.Escape) {
|
---|
[2974] | 134 | lengthTextBox.Text = Content.Length.ToString();
|
---|
| 135 | lengthLabel.Focus(); // set focus on label to validate data
|
---|
[2973] | 136 | }
|
---|
| 137 | }
|
---|
| 138 | #endregion
|
---|
| 139 |
|
---|
| 140 | #region DataGridView Events
|
---|
| 141 | private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
|
---|
| 142 | string errorMessage;
|
---|
[4030] | 143 | if (Content != null && !Content.Validate(e.FormattedValue.ToString(), out errorMessage)) {
|
---|
[2973] | 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 | }
|
---|
[7383] | 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 | clipboardText = clipboardText.Remove(clipboardText.Length - Environment.NewLine.Length); //remove last newline constant
|
---|
| 198 | return clipboardText.Split(new string[] { Environment.NewLine, "\t" }, StringSplitOptions.None);
|
---|
| 199 | }
|
---|
[2973] | 200 | #endregion
|
---|
| 201 | }
|
---|
| 202 | }
|
---|