[17417] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.ComponentModel;
|
---|
| 4 | using System.Drawing;
|
---|
| 5 | using System.Data;
|
---|
| 6 | using System.Linq;
|
---|
| 7 | using System.Text;
|
---|
| 8 | using System.Threading.Tasks;
|
---|
| 9 | using System.Windows.Forms;
|
---|
| 10 |
|
---|
| 11 | namespace HeuristicLab.JsonInterface.OptimizerIntegration {
|
---|
[17464] | 12 |
|
---|
[17431] | 13 | public class JsonItemDoubleMatrixValueControl : JsonItemMultiValueControl<double> {
|
---|
| 14 | public JsonItemDoubleMatrixValueControl(DoubleMatrixValueVM vm) : base(vm, vm.Value) { }
|
---|
[17471] | 15 |
|
---|
[17484] | 16 | protected override void Save() {
|
---|
[17431] | 17 | DoubleMatrixValueVM vm = VM as DoubleMatrixValueVM;
|
---|
[17484] | 18 | vm.Value = Matrix;
|
---|
[17431] | 19 | }
|
---|
[17471] | 20 |
|
---|
[17431] | 21 | }
|
---|
| 22 |
|
---|
| 23 | public class JsonItemIntArrayValueControl : JsonItemMultiValueControl<int> {
|
---|
[17420] | 24 | public JsonItemIntArrayValueControl(IntArrayValueVM vm) : base(vm, vm.Value) { }
|
---|
[17471] | 25 |
|
---|
[17484] | 26 | protected override void Save() {
|
---|
[17431] | 27 | IntArrayValueVM vm = VM as IntArrayValueVM;
|
---|
[17484] | 28 | vm.Value = Matrix[0];
|
---|
[17431] | 29 | }
|
---|
[17471] | 30 |
|
---|
[17420] | 31 | }
|
---|
| 32 |
|
---|
[17431] | 33 | public class JsonItemDoubleArrayValueControl : JsonItemMultiValueControl<double> {
|
---|
[17420] | 34 | public JsonItemDoubleArrayValueControl(DoubleArrayValueVM vm) : base(vm, vm.Value) { }
|
---|
[17471] | 35 |
|
---|
[17484] | 36 | protected override void Save() {
|
---|
[17431] | 37 | DoubleArrayValueVM vm = VM as DoubleArrayValueVM;
|
---|
[17484] | 38 | vm.Value = Matrix[0];
|
---|
[17431] | 39 | }
|
---|
[17471] | 40 |
|
---|
[17420] | 41 | }
|
---|
| 42 |
|
---|
[17464] | 43 | public abstract partial class JsonItemMultiValueControl<T> : UserControl {
|
---|
| 44 | protected IJsonItemVM VM { get; set; }
|
---|
[17420] | 45 | protected NumericRangeControl NumericRangeControl { get; set; }
|
---|
[17471] | 46 | private int Rows { get; set; }
|
---|
| 47 | private int Columns { get; set; }
|
---|
| 48 |
|
---|
[17484] | 49 | protected T[][] Matrix { get; set; }
|
---|
[17471] | 50 |
|
---|
| 51 | protected IEnumerable<string> RowNames {
|
---|
| 52 | get {
|
---|
| 53 | if(VM is IMatrixJsonItemVM mVM)
|
---|
| 54 | return mVM.RowNames;
|
---|
| 55 | return null;
|
---|
| 56 | }
|
---|
[17446] | 57 | set {
|
---|
[17471] | 58 | if (VM is IMatrixJsonItemVM mVM)
|
---|
| 59 | mVM.RowNames = value;
|
---|
[17446] | 60 | }
|
---|
| 61 | }
|
---|
[17471] | 62 | protected IEnumerable<string> ColumnNames {
|
---|
| 63 | get {
|
---|
| 64 | if (VM is IMatrixJsonItemVM mVM)
|
---|
| 65 | return mVM.ColumnNames;
|
---|
| 66 | return null;
|
---|
| 67 | }
|
---|
[17446] | 68 | set {
|
---|
[17471] | 69 | if (VM is IMatrixJsonItemVM mVM)
|
---|
| 70 | mVM.ColumnNames = value;
|
---|
| 71 | }
|
---|
[17446] | 72 | }
|
---|
[17471] | 73 |
|
---|
| 74 | public JsonItemMultiValueControl(IMatrixJsonItemVM vm, T[][] matrix) {
|
---|
[17417] | 75 | InitializeComponent();
|
---|
[17464] | 76 | VM = vm;
|
---|
[17446] | 77 | checkBoxRows.DataBindings.Add("Checked", vm, nameof(IMatrixJsonItemVM.RowsResizable));
|
---|
| 78 | checkBoxColumns.DataBindings.Add("Checked", vm, nameof(IMatrixJsonItemVM.ColumnsResizable));
|
---|
| 79 |
|
---|
[17451] | 80 | int cols = matrix.Length;
|
---|
| 81 | int rows = matrix.Max(x => x.Length);
|
---|
[17519] | 82 |
|
---|
[17446] | 83 | Matrix = matrix;
|
---|
[17471] | 84 | Columns = cols;
|
---|
| 85 | Rows = rows;
|
---|
[17446] | 86 | RefreshMatrix();
|
---|
| 87 | InitSizeConfiguration(rows, cols);
|
---|
[17431] | 88 | dataGridView.CellEndEdit += DataGridView_CellEndEdit;
|
---|
| 89 | InitRangeBinding();
|
---|
| 90 | }
|
---|
[17446] | 91 |
|
---|
[17471] | 92 | public JsonItemMultiValueControl(IArrayJsonItemVM vm, T[] array) {
|
---|
[17446] | 93 | InitializeComponent();
|
---|
[17464] | 94 | VM = vm;
|
---|
[17446] | 95 | checkBoxRows.DataBindings.Add("Checked", vm, nameof(IArrayJsonItemVM.Resizable));
|
---|
| 96 |
|
---|
[17431] | 97 | int length = array.Length;
|
---|
| 98 |
|
---|
[17451] | 99 | Matrix = new T[1][];
|
---|
| 100 | Matrix[0] = array;
|
---|
[17471] | 101 | Columns = 1;
|
---|
| 102 | Rows = length;
|
---|
[17446] | 103 | RefreshMatrix();
|
---|
| 104 |
|
---|
| 105 | InitSizeConfiguration(length, null);
|
---|
[17431] | 106 | dataGridView.CellEndEdit += DataGridView_CellEndEdit;
|
---|
| 107 | InitRangeBinding();
|
---|
| 108 | }
|
---|
[17446] | 109 |
|
---|
[17484] | 110 | protected abstract void Save();
|
---|
[17431] | 111 |
|
---|
[17446] | 112 | private void RefreshMatrix() {
|
---|
| 113 | dataGridView.Rows.Clear();
|
---|
| 114 | dataGridView.Columns.Clear();
|
---|
[17431] | 115 |
|
---|
[17446] | 116 | T[][] tmp = Matrix;
|
---|
[17451] | 117 | Matrix = new T[Columns][];
|
---|
[17464] | 118 |
|
---|
[17484] | 119 | // add columns
|
---|
[17471] | 120 | for (int c = 0; c < Columns; ++c) {
|
---|
| 121 | string name = $"Column {c}";
|
---|
| 122 | if (RowNames != null && c < RowNames.Count())
|
---|
| 123 | name = RowNames.ElementAt(c);
|
---|
| 124 | dataGridView.Columns.Add(name, name);
|
---|
[17446] | 125 | }
|
---|
| 126 |
|
---|
[17484] | 127 | // copy data from old matrix into new
|
---|
[17451] | 128 | for (int c = 0; c < Columns; ++c) {
|
---|
| 129 | T[] newCol = new T[Rows];
|
---|
| 130 | if(c < tmp.Length)
|
---|
| 131 | Array.Copy(tmp[c], 0, newCol, 0, Math.Min(tmp[c].Length, Rows));
|
---|
| 132 | Matrix[c] = newCol;
|
---|
[17446] | 133 | }
|
---|
| 134 |
|
---|
[17484] | 135 | // add rows
|
---|
[17446] | 136 | if(Rows > 0 && Columns > 0) {
|
---|
| 137 | dataGridView.Rows.Add(Rows);
|
---|
| 138 | for (int c = 0; c < Columns; ++c) {
|
---|
| 139 | for (int r = 0; r < Rows; ++r) {
|
---|
| 140 | //col and row is switched for dataGridView
|
---|
[17451] | 141 | dataGridView[c, r].Value = Matrix[c][r];
|
---|
[17471] | 142 | string name = $"Row {r}";
|
---|
[17451] | 143 | if (ColumnNames != null && r < ColumnNames.Count())
|
---|
[17471] | 144 | name = ColumnNames.ElementAt(r);
|
---|
| 145 | dataGridView.Rows[r].HeaderCell.Value = name;
|
---|
[17446] | 146 | }
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 | dataGridView.RowHeadersWidth = 100;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[17484] | 152 | #region Init
|
---|
| 153 | private void InitSizeConfiguration(int? rows, int? columns) {
|
---|
| 154 | if (rows != null) {
|
---|
| 155 | checkBoxRows.CheckedChanged += CheckBoxRows_CheckedChanged;
|
---|
| 156 | textBoxRows.Text = rows.ToString();
|
---|
| 157 | } else {
|
---|
| 158 | checkBoxRows.Enabled = false;
|
---|
| 159 | textBoxRows.ReadOnly = true;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | if (columns != null) {
|
---|
| 163 | checkBoxColumns.CheckedChanged += CheckBoxColumns_CheckedChanged;
|
---|
| 164 | textBoxColumns.Text = columns.ToString();
|
---|
| 165 | } else {
|
---|
| 166 | checkBoxColumns.Enabled = false;
|
---|
| 167 | textBoxColumns.ReadOnly = true;
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[17431] | 171 | private void InitRangeBinding() {
|
---|
[17420] | 172 | NumericRangeControl = numericRangeControl1;
|
---|
[17473] | 173 | NumericRangeControl.TBMinRange.DataBindings.Add("Text", VM, nameof(RangedValueBaseVM<int, IntJsonItem>.MinRange));
|
---|
| 174 | NumericRangeControl.TBMaxRange.DataBindings.Add("Text", VM, nameof(RangedValueBaseVM<int, IntJsonItem>.MaxRange));
|
---|
| 175 | NumericRangeControl.EnableMinRange.DataBindings.Add("Checked", VM, nameof(RangedValueBaseVM<int, IntJsonItem>.EnableMinRange),
|
---|
[17420] | 176 | false, DataSourceUpdateMode.OnPropertyChanged);
|
---|
[17473] | 177 | NumericRangeControl.EnableMaxRange.DataBindings.Add("Checked", VM, nameof(RangedValueBaseVM<int, IntJsonItem>.EnableMaxRange),
|
---|
[17420] | 178 | false, DataSourceUpdateMode.OnPropertyChanged);
|
---|
[17417] | 179 | }
|
---|
[17484] | 180 | #endregion
|
---|
[17446] | 181 |
|
---|
[17484] | 182 | #region Validation
|
---|
[17446] | 183 | private void textBoxRows_Validating(object sender, CancelEventArgs e) {
|
---|
[17485] | 184 | if (textBoxRows.ReadOnly) {
|
---|
| 185 | errorProvider.SetError(textBoxRows, null);
|
---|
[17446] | 186 | } else {
|
---|
[17485] | 187 | if (string.IsNullOrWhiteSpace(textBoxRows.Text)) {
|
---|
| 188 | errorProvider.SetError(textBoxRows, "'Rows' must not be empty.");
|
---|
| 189 | e.Cancel = true;
|
---|
| 190 | } else if (!int.TryParse(textBoxRows.Text, out int r)) {
|
---|
| 191 | errorProvider.SetError(textBoxRows, "Value of 'Rows' must be an integer.");
|
---|
| 192 | e.Cancel = true;
|
---|
| 193 | } else if (r == 0) {
|
---|
| 194 | errorProvider.SetError(textBoxRows, "Value of 'Rows' must be an integer larger than 0.");
|
---|
| 195 | e.Cancel = true;
|
---|
| 196 | } else {
|
---|
| 197 | errorProvider.SetError(textBoxRows, null);
|
---|
| 198 | }
|
---|
[17446] | 199 | }
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | private void textBoxColumns_Validating(object sender, CancelEventArgs e) {
|
---|
[17485] | 203 | if (textBoxColumns.ReadOnly) {
|
---|
| 204 | errorProvider.SetError(textBoxColumns, null);
|
---|
[17446] | 205 | } else {
|
---|
[17485] | 206 | if (string.IsNullOrWhiteSpace(textBoxColumns.Text)) {
|
---|
| 207 | errorProvider.SetError(textBoxColumns, "'Columns' must not be empty.");
|
---|
| 208 | e.Cancel = true;
|
---|
| 209 | } else if (!int.TryParse(textBoxColumns.Text, out int r)) {
|
---|
| 210 | errorProvider.SetError(textBoxColumns, "Value of 'Columns' must be an integer.");
|
---|
| 211 | e.Cancel = true;
|
---|
| 212 | } else if (r == 0) {
|
---|
| 213 | errorProvider.SetError(textBoxColumns, "Value of 'Columns' must be an integer larger than 0.");
|
---|
| 214 | e.Cancel = true;
|
---|
| 215 | } else {
|
---|
| 216 | errorProvider.SetError(textBoxColumns, null);
|
---|
| 217 | }
|
---|
[17446] | 218 | }
|
---|
| 219 | }
|
---|
[17484] | 220 | #endregion
|
---|
| 221 |
|
---|
| 222 | #region Events
|
---|
[17446] | 223 | private void textBoxRows_TextChanged(object sender, EventArgs e) {
|
---|
[17471] | 224 | if(!textBoxRows.ReadOnly && int.TryParse(textBoxRows.Text, out int r) && Rows != r) {
|
---|
[17446] | 225 | Rows = r;
|
---|
[17471] | 226 | RefreshMatrix();
|
---|
[17484] | 227 | Save();
|
---|
[17446] | 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | private void textBoxColumns_TextChanged(object sender, EventArgs e) {
|
---|
[17471] | 232 | if (!textBoxColumns.ReadOnly && int.TryParse(textBoxColumns.Text, out int c) && Columns != c) {
|
---|
[17446] | 233 | Columns = c;
|
---|
[17471] | 234 | RefreshMatrix();
|
---|
[17484] | 235 | Save();
|
---|
[17446] | 236 | }
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | private void CheckBoxColumns_CheckedChanged(object sender, EventArgs e) {
|
---|
| 240 | textBoxColumns.ReadOnly = !checkBoxColumns.Checked;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | private void CheckBoxRows_CheckedChanged(object sender, EventArgs e) {
|
---|
| 244 | textBoxRows.ReadOnly = !checkBoxRows.Checked;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | private void DataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
|
---|
| 248 | object val = dataGridView[e.ColumnIndex, e.RowIndex].Value;
|
---|
[17451] | 249 | Matrix[e.ColumnIndex][e.RowIndex] = (T)Convert.ChangeType(val.ToString().Replace(",", "."),
|
---|
[17446] | 250 | typeof(T),
|
---|
| 251 | System.Globalization.CultureInfo.InvariantCulture);
|
---|
[17484] | 252 | Save();
|
---|
[17446] | 253 | }
|
---|
[17484] | 254 | #endregion
|
---|
[17417] | 255 | }
|
---|
| 256 | }
|
---|