[2973] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 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;
|
---|
[7318] | 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() {
|
---|
[9657] | 55 | Content.ElementNamesChanged -= new EventHandler(Content_ElementNamesChanged);
|
---|
[2973] | 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);
|
---|
[9657] | 65 | Content.ElementNamesChanged += new EventHandler(Content_ElementNamesChanged);
|
---|
[2973] | 66 | }
|
---|
| 67 |
|
---|
| 68 | protected override void OnContentChanged() {
|
---|
| 69 | base.OnContentChanged();
|
---|
| 70 | if (Content == null) {
|
---|
[2974] | 71 | lengthTextBox.Text = "";
|
---|
[2973] | 72 | dataGridView.Rows.Clear();
|
---|
| 73 | dataGridView.Columns.Clear();
|
---|
[3764] | 74 | } else
|
---|
[2973] | 75 | UpdateData();
|
---|
| 76 | }
|
---|
[3904] | 77 |
|
---|
| 78 | protected override void SetEnabledStateOfControls() {
|
---|
| 79 | base.SetEnabledStateOfControls();
|
---|
[3454] | 80 | lengthTextBox.Enabled = Content != null;
|
---|
| 81 | dataGridView.Enabled = Content != null;
|
---|
[14029] | 82 | lengthTextBox.ReadOnly = ReadOnly || (Content != null && !Content.Resizable);
|
---|
[3454] | 83 | dataGridView.ReadOnly = ReadOnly;
|
---|
[3350] | 84 | }
|
---|
[2973] | 85 |
|
---|
| 86 | private void UpdateData() {
|
---|
[2974] | 87 | lengthTextBox.Text = Content.Length.ToString();
|
---|
| 88 | lengthTextBox.Enabled = true;
|
---|
[2973] | 89 | dataGridView.Rows.Clear();
|
---|
| 90 | dataGridView.Columns.Clear();
|
---|
[2974] | 91 | if (Content.Length > 0) {
|
---|
[2973] | 92 | dataGridView.ColumnCount++;
|
---|
| 93 | dataGridView.Columns[0].FillWeight = float.Epsilon; // sum of all fill weights must not be larger than 65535
|
---|
[2974] | 94 | dataGridView.RowCount = Content.Length;
|
---|
| 95 | for (int i = 0; i < Content.Length; i++) {
|
---|
[2973] | 96 | dataGridView.Rows[i].Cells[0].Value = Content.GetValue(i);
|
---|
| 97 | }
|
---|
| 98 | dataGridView.Columns[0].Width = dataGridView.Columns[0].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true);
|
---|
| 99 | }
|
---|
[9657] | 100 | UpdateRowHeaders();
|
---|
| 101 | dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
|
---|
[2973] | 102 | dataGridView.Enabled = true;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[14029] | 105 | public virtual void UpdateRowHeaders() {
|
---|
[9695] | 106 | int i = 0;
|
---|
| 107 | foreach (string elementName in Content.ElementNames) {
|
---|
| 108 | dataGridView.Rows[i].HeaderCell.Value = elementName;
|
---|
| 109 | i++;
|
---|
[9657] | 110 | }
|
---|
[9695] | 111 | for (; i < dataGridView.RowCount; i++) {
|
---|
| 112 | dataGridView.Rows[i].HeaderCell.Value = string.Empty;
|
---|
| 113 | }
|
---|
[9657] | 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 |
|
---|
[2973] | 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 {
|
---|
[9714] | 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;
|
---|
[2973] | 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
|
---|
[2974] | 143 | private void lengthTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
[2973] | 144 | int i = 0;
|
---|
[2974] | 145 | if (!int.TryParse(lengthTextBox.Text, out i) || (i < 0)) {
|
---|
[2973] | 146 | e.Cancel = true;
|
---|
[2974] | 147 | errorProvider.SetError(lengthTextBox, "Invalid Array Length (Valid Values: Positive Integers Larger or Equal to 0)");
|
---|
| 148 | lengthTextBox.SelectAll();
|
---|
[2973] | 149 | }
|
---|
| 150 | }
|
---|
[2974] | 151 | private void lengthTextBox_Validated(object sender, EventArgs e) {
|
---|
[3430] | 152 | if (!Content.ReadOnly) Content.Length = int.Parse(lengthTextBox.Text);
|
---|
[2974] | 153 | errorProvider.SetError(lengthTextBox, string.Empty);
|
---|
[2973] | 154 | }
|
---|
[2974] | 155 | private void lengthTextBox_KeyDown(object sender, KeyEventArgs e) {
|
---|
[2973] | 156 | if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
|
---|
[2974] | 157 | lengthLabel.Focus(); // set focus on label to validate data
|
---|
[2973] | 158 | if (e.KeyCode == Keys.Escape) {
|
---|
[2974] | 159 | lengthTextBox.Text = Content.Length.ToString();
|
---|
| 160 | lengthLabel.Focus(); // set focus on label to validate data
|
---|
[2973] | 161 | }
|
---|
| 162 | }
|
---|
| 163 | #endregion
|
---|
| 164 |
|
---|
| 165 | #region DataGridView Events
|
---|
| 166 | private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
|
---|
| 167 | string errorMessage;
|
---|
[4030] | 168 | if (Content != null && !Content.Validate(e.FormattedValue.ToString(), out errorMessage)) {
|
---|
[2973] | 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 | }
|
---|
[7318] | 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) {
|
---|
[7984] | 222 | if (clipboardText.EndsWith(Environment.NewLine))
|
---|
| 223 | clipboardText = clipboardText.Remove(clipboardText.Length - Environment.NewLine.Length); //remove last newline constant
|
---|
[7318] | 224 | return clipboardText.Split(new string[] { Environment.NewLine, "\t" }, StringSplitOptions.None);
|
---|
| 225 | }
|
---|
[2973] | 226 | #endregion
|
---|
| 227 | }
|
---|
| 228 | }
|
---|