Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/JsonItemMultiValueControl.cs @ 17471

Last change on this file since 17471 was 17471, checked in by dpiringe, 4 years ago

#3026:

  • deleted INamedMatrixJsonItem and all corresponding classes/views, because of bad design
  • added ILookupJsonItem and IValueLookupJsonItem (incl. all corresponding implementations, VMs, Views)
  • added IResultJsonItem
  • changed type of property Control from JsonItemBaseControl to UserControl in IJsonItemVM (because the details control now builds up with linked user controls -> allows better construction of dynamic controls)
  • added all properties of INamedMatrixJsonItem in IMatrixJsonItem
  • refactored a lot of views for better usage (TableLayoutPanel is used a lot now -> for better item positioning)
  • property ActualName is now located in ILookupJsonItem instead of IJsonItem
File size: 8.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9using System.Windows.Forms;
10
11namespace HeuristicLab.JsonInterface.OptimizerIntegration {
12 
13  public class JsonItemDoubleMatrixValueControl : JsonItemMultiValueControl<double> {
14    public JsonItemDoubleMatrixValueControl(DoubleMatrixValueVM vm) : base(vm, vm.Value) { }
15   
16    protected override void SaveCellData(double data, int row, int col) {
17      DoubleMatrixValueVM vm = VM as DoubleMatrixValueVM;
18      vm.SetCellValue(data, row, col);
19    }
20   
21  }
22
23  public class JsonItemIntArrayValueControl : JsonItemMultiValueControl<int> {
24    public JsonItemIntArrayValueControl(IntArrayValueVM vm) : base(vm, vm.Value) { }
25   
26    protected override void SaveCellData(int data, int row, int col) {
27      IntArrayValueVM vm = VM as IntArrayValueVM;
28      vm.SetIndexValue(data, row);
29    }
30   
31  }
32
33  public class JsonItemDoubleArrayValueControl : JsonItemMultiValueControl<double> {
34    public JsonItemDoubleArrayValueControl(DoubleArrayValueVM vm) : base(vm, vm.Value) { }
35   
36    protected override void SaveCellData(double data, int row, int col) {
37      DoubleArrayValueVM vm = VM as DoubleArrayValueVM;
38      vm.SetIndexValue(data, row);
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    private 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 SaveCellData(T data, int row, int col);
111
112    private void InitSizeConfiguration(int? rows, int? columns) {
113      if(rows != null) {
114        checkBoxRows.CheckedChanged += CheckBoxRows_CheckedChanged;
115        textBoxRows.Text = rows.ToString();
116      } else {
117        checkBoxRows.Enabled = false;
118        textBoxRows.ReadOnly = true;
119      }
120
121      if (columns != null) {
122        checkBoxColumns.CheckedChanged += CheckBoxColumns_CheckedChanged;
123        textBoxColumns.Text = columns.ToString();
124      } else {
125        checkBoxColumns.Enabled = false;
126        textBoxColumns.ReadOnly = true;
127      }
128    }
129
130    private void RefreshMatrix() {
131      dataGridView.Rows.Clear();
132      dataGridView.Columns.Clear();
133
134      T[][] tmp = Matrix;
135      Matrix = new T[Columns][];
136     
137      for (int c = 0; c < Columns; ++c) {
138        string name = $"Column {c}";
139        if (RowNames != null && c < RowNames.Count())
140          name = RowNames.ElementAt(c);
141        dataGridView.Columns.Add(name, name);
142      }
143
144      for (int c = 0; c < Columns; ++c) {
145        T[] newCol = new T[Rows];
146        if(c < tmp.Length)
147          Array.Copy(tmp[c], 0, newCol, 0, Math.Min(tmp[c].Length, Rows));
148        Matrix[c] = newCol;
149      }
150
151      if(Rows > 0 && Columns > 0) {
152        dataGridView.Rows.Add(Rows);
153        for (int c = 0; c < Columns; ++c) {
154          for (int r = 0; r < Rows; ++r) {
155            //col and row is switched for dataGridView
156            dataGridView[c, r].Value = Matrix[c][r];
157            string name = $"Row {r}";
158            if (ColumnNames != null && r < ColumnNames.Count())
159              name = ColumnNames.ElementAt(r);
160            dataGridView.Rows[r].HeaderCell.Value = name;
161          }
162        }
163      }
164      dataGridView.RowHeadersWidth = 100;
165    }
166
167    private void InitRangeBinding() {
168      NumericRangeControl = numericRangeControl1;
169      NumericRangeControl.TBMinRange.DataBindings.Add("Text", VM, nameof(RangedValueBaseVM.MinRange));
170      NumericRangeControl.TBMaxRange.DataBindings.Add("Text", VM, nameof(RangedValueBaseVM.MaxRange));
171      NumericRangeControl.EnableMinRange.DataBindings.Add("Checked", VM, nameof(RangedValueBaseVM.EnableMinRange),
172        false, DataSourceUpdateMode.OnPropertyChanged);
173      NumericRangeControl.EnableMaxRange.DataBindings.Add("Checked", VM, nameof(RangedValueBaseVM.EnableMaxRange),
174        false, DataSourceUpdateMode.OnPropertyChanged);
175    }
176
177    private void textBoxRows_Validating(object sender, CancelEventArgs e) {
178      if (string.IsNullOrWhiteSpace(textBoxRows.Text)) {
179        errorProvider.SetError(textBoxRows, "'Rows' must not be empty.");
180        e.Cancel = true;
181      } else if (!int.TryParse(textBoxRows.Text, out int r)) {
182        errorProvider.SetError(textBoxRows, "Value of 'Rows' must be an integer.");
183        e.Cancel = true;
184      } else if (r == 0) {
185        errorProvider.SetError(textBoxRows, "Value of 'Rows' must be an integer larger than 0.");
186        e.Cancel = true;
187      } else {
188        errorProvider.SetError(textBoxRows, null);
189      }
190    }
191
192    private void textBoxColumns_Validating(object sender, CancelEventArgs e) {
193      if (string.IsNullOrWhiteSpace(textBoxColumns.Text)) {
194        errorProvider.SetError(textBoxColumns, "'Columns' must not be empty.");
195        e.Cancel = true;
196      } else if (!int.TryParse(textBoxColumns.Text, out int r)) {
197        errorProvider.SetError(textBoxColumns, "Value of 'Columns' must be an integer.");
198        e.Cancel = true;
199      } else if (r == 0) {
200        errorProvider.SetError(textBoxColumns, "Value of 'Columns' must be an integer larger than 0.");
201        e.Cancel = true;
202      } else {
203        errorProvider.SetError(textBoxColumns, null);
204      }
205    }
206   
207    private void textBoxRows_TextChanged(object sender, EventArgs e) {
208      if(!textBoxRows.ReadOnly && int.TryParse(textBoxRows.Text, out int r) && Rows != r) {
209        Rows = r;
210        RefreshMatrix();
211      }
212    }
213
214    private void textBoxColumns_TextChanged(object sender, EventArgs e) {
215      if (!textBoxColumns.ReadOnly && int.TryParse(textBoxColumns.Text, out int c) && Columns != c) {
216        Columns = c;
217        RefreshMatrix();
218      }
219    }
220
221    private void CheckBoxColumns_CheckedChanged(object sender, EventArgs e) {
222      textBoxColumns.ReadOnly = !checkBoxColumns.Checked;
223    }
224
225    private void CheckBoxRows_CheckedChanged(object sender, EventArgs e) {
226      textBoxRows.ReadOnly = !checkBoxRows.Checked;
227    }
228
229    private void DataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
230      object val = dataGridView[e.ColumnIndex, e.RowIndex].Value;
231      Matrix[e.ColumnIndex][e.RowIndex] = (T)Convert.ChangeType(val.ToString().Replace(",", "."),
232                                            typeof(T),
233                                            System.Globalization.CultureInfo.InvariantCulture);
234      SaveCellData(Matrix[e.ColumnIndex][e.RowIndex], e.ColumnIndex, e.RowIndex);
235    }
236  }
237}
Note: See TracBrowser for help on using the repository browser.