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 {
|
---|
12 |
|
---|
13 | public class JsonItemDoubleMatrixValueControl : JsonItemMultiValueControl<double> {
|
---|
14 | public JsonItemDoubleMatrixValueControl(DoubleMatrixValueVM vm) : base(vm, vm.Value) { }
|
---|
15 |
|
---|
16 | protected override void Save() {
|
---|
17 | DoubleMatrixValueVM vm = VM as DoubleMatrixValueVM;
|
---|
18 | vm.Value = Matrix;
|
---|
19 | }
|
---|
20 |
|
---|
21 | }
|
---|
22 |
|
---|
23 | public class JsonItemIntArrayValueControl : JsonItemMultiValueControl<int> {
|
---|
24 | public JsonItemIntArrayValueControl(IntArrayValueVM vm) : base(vm, vm.Value) { }
|
---|
25 |
|
---|
26 | protected override void Save() {
|
---|
27 | IntArrayValueVM vm = VM as IntArrayValueVM;
|
---|
28 | vm.Value = Matrix[0];
|
---|
29 | }
|
---|
30 |
|
---|
31 | }
|
---|
32 |
|
---|
33 | public class JsonItemDoubleArrayValueControl : JsonItemMultiValueControl<double> {
|
---|
34 | public JsonItemDoubleArrayValueControl(DoubleArrayValueVM vm) : base(vm, vm.Value) { }
|
---|
35 |
|
---|
36 | protected override void Save() {
|
---|
37 | DoubleArrayValueVM vm = VM as DoubleArrayValueVM;
|
---|
38 | vm.Value = Matrix[0];
|
---|
39 | }
|
---|
40 |
|
---|
41 | }
|
---|
42 |
|
---|
43 | public abstract partial class JsonItemMultiValueControl<T> : UserControl {
|
---|
44 | protected IJsonItemVM VM { get; set; }
|
---|
45 | protected NumericRangeControl NumericRangeControl { get; set; }
|
---|
46 | private int Rows { get; set; }
|
---|
47 | private int Columns { get; set; }
|
---|
48 |
|
---|
49 | protected T[][] Matrix { get; set; }
|
---|
50 |
|
---|
51 | protected IEnumerable<string> RowNames {
|
---|
52 | get {
|
---|
53 | if(VM is IMatrixJsonItemVM mVM)
|
---|
54 | return mVM.RowNames;
|
---|
55 | return null;
|
---|
56 | }
|
---|
57 | set {
|
---|
58 | if (VM is IMatrixJsonItemVM mVM)
|
---|
59 | mVM.RowNames = value;
|
---|
60 | }
|
---|
61 | }
|
---|
62 | protected IEnumerable<string> ColumnNames {
|
---|
63 | get {
|
---|
64 | if (VM is IMatrixJsonItemVM mVM)
|
---|
65 | return mVM.ColumnNames;
|
---|
66 | return null;
|
---|
67 | }
|
---|
68 | set {
|
---|
69 | if (VM is IMatrixJsonItemVM mVM)
|
---|
70 | mVM.ColumnNames = value;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | public JsonItemMultiValueControl(IMatrixJsonItemVM vm, T[][] matrix) {
|
---|
75 | InitializeComponent();
|
---|
76 | VM = vm;
|
---|
77 | checkBoxRows.DataBindings.Add("Checked", vm, nameof(IMatrixJsonItemVM.RowsResizable));
|
---|
78 | checkBoxColumns.DataBindings.Add("Checked", vm, nameof(IMatrixJsonItemVM.ColumnsResizable));
|
---|
79 |
|
---|
80 | int cols = matrix.Length;
|
---|
81 | int rows = matrix.Max(x => x.Length);
|
---|
82 |
|
---|
83 | Matrix = matrix;
|
---|
84 | Columns = cols;
|
---|
85 | Rows = rows;
|
---|
86 | RefreshMatrix();
|
---|
87 | InitSizeConfiguration(rows, cols);
|
---|
88 | dataGridView.CellEndEdit += DataGridView_CellEndEdit;
|
---|
89 | InitRangeBinding();
|
---|
90 | }
|
---|
91 |
|
---|
92 | public JsonItemMultiValueControl(IArrayJsonItemVM vm, T[] array) {
|
---|
93 | InitializeComponent();
|
---|
94 | VM = vm;
|
---|
95 | checkBoxRows.DataBindings.Add("Checked", vm, nameof(IArrayJsonItemVM.Resizable));
|
---|
96 |
|
---|
97 | int length = array.Length;
|
---|
98 |
|
---|
99 | Matrix = new T[1][];
|
---|
100 | Matrix[0] = array;
|
---|
101 | Columns = 1;
|
---|
102 | Rows = length;
|
---|
103 | RefreshMatrix();
|
---|
104 |
|
---|
105 | InitSizeConfiguration(length, null);
|
---|
106 | dataGridView.CellEndEdit += DataGridView_CellEndEdit;
|
---|
107 | InitRangeBinding();
|
---|
108 | }
|
---|
109 |
|
---|
110 | protected abstract void Save();
|
---|
111 |
|
---|
112 | private void RefreshMatrix() {
|
---|
113 | dataGridView.Rows.Clear();
|
---|
114 | dataGridView.Columns.Clear();
|
---|
115 |
|
---|
116 | T[][] tmp = Matrix;
|
---|
117 | Matrix = new T[Columns][];
|
---|
118 |
|
---|
119 | // add columns
|
---|
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);
|
---|
125 | }
|
---|
126 |
|
---|
127 | // copy data from old matrix into new
|
---|
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;
|
---|
133 | }
|
---|
134 |
|
---|
135 | // add rows
|
---|
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
|
---|
141 | dataGridView[c, r].Value = Matrix[c][r];
|
---|
142 | string name = $"Row {r}";
|
---|
143 | if (ColumnNames != null && r < ColumnNames.Count())
|
---|
144 | name = ColumnNames.ElementAt(r);
|
---|
145 | dataGridView.Rows[r].HeaderCell.Value = name;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 | dataGridView.RowHeadersWidth = 100;
|
---|
150 | }
|
---|
151 |
|
---|
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 |
|
---|
171 | private void InitRangeBinding() {
|
---|
172 | NumericRangeControl = numericRangeControl1;
|
---|
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),
|
---|
176 | false, DataSourceUpdateMode.OnPropertyChanged);
|
---|
177 | NumericRangeControl.EnableMaxRange.DataBindings.Add("Checked", VM, nameof(RangedValueBaseVM<int, IntJsonItem>.EnableMaxRange),
|
---|
178 | false, DataSourceUpdateMode.OnPropertyChanged);
|
---|
179 | }
|
---|
180 | #endregion
|
---|
181 |
|
---|
182 | #region Validation
|
---|
183 | private void textBoxRows_Validating(object sender, CancelEventArgs e) {
|
---|
184 | if (textBoxRows.ReadOnly) {
|
---|
185 | errorProvider.SetError(textBoxRows, null);
|
---|
186 | } else {
|
---|
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 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | private void textBoxColumns_Validating(object sender, CancelEventArgs e) {
|
---|
203 | if (textBoxColumns.ReadOnly) {
|
---|
204 | errorProvider.SetError(textBoxColumns, null);
|
---|
205 | } else {
|
---|
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 | }
|
---|
218 | }
|
---|
219 | }
|
---|
220 | #endregion
|
---|
221 |
|
---|
222 | #region Events
|
---|
223 | private void textBoxRows_TextChanged(object sender, EventArgs e) {
|
---|
224 | if(!textBoxRows.ReadOnly && int.TryParse(textBoxRows.Text, out int r) && Rows != r) {
|
---|
225 | Rows = r;
|
---|
226 | RefreshMatrix();
|
---|
227 | Save();
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | private void textBoxColumns_TextChanged(object sender, EventArgs e) {
|
---|
232 | if (!textBoxColumns.ReadOnly && int.TryParse(textBoxColumns.Text, out int c) && Columns != c) {
|
---|
233 | Columns = c;
|
---|
234 | RefreshMatrix();
|
---|
235 | Save();
|
---|
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;
|
---|
249 | Matrix[e.ColumnIndex][e.RowIndex] = (T)Convert.ChangeType(val.ToString().Replace(",", "."),
|
---|
250 | typeof(T),
|
---|
251 | System.Globalization.CultureInfo.InvariantCulture);
|
---|
252 | Save();
|
---|
253 | }
|
---|
254 | #endregion
|
---|
255 | }
|
---|
256 | }
|
---|