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.Windows.Forms;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.OKB.ParameterTable {
|
---|
11 | public partial class ParameterTable : UserControl {
|
---|
12 |
|
---|
13 | public ParameterTable() {
|
---|
14 | InitializeComponent();
|
---|
15 | }
|
---|
16 |
|
---|
17 | public void Initialize(IEnumerable<IParameterAccessor> parameters) {
|
---|
18 | SuspendLayout();
|
---|
19 | panel.Controls.Clear();
|
---|
20 | panel.RowStyles.Clear();
|
---|
21 | toolTip.RemoveAll();
|
---|
22 | panel.RowCount = 0;
|
---|
23 | foreach (var p in parameters) {
|
---|
24 | try {
|
---|
25 | Label l = new Label() {
|
---|
26 | Text = p.Name,
|
---|
27 | Dock = DockStyle.Fill,
|
---|
28 | TextAlign = ContentAlignment.MiddleLeft,
|
---|
29 | };
|
---|
30 | Control c = CreateParameterControl(p);
|
---|
31 | toolTip.SetToolTip(l, p.Description);
|
---|
32 | toolTip.SetToolTip(c, p.TypeName);
|
---|
33 | panel.Controls.Add(l);
|
---|
34 | panel.Controls.Add(c);
|
---|
35 | panel.SetCellPosition(l, new TableLayoutPanelCellPosition(0, panel.RowCount));
|
---|
36 | panel.SetCellPosition(c, new TableLayoutPanelCellPosition(1, panel.RowCount));
|
---|
37 | panel.RowCount++;
|
---|
38 | } catch (Exception x) {
|
---|
39 | MessageBox.Show(string.Format("could not display parameter {0}:\n{1}",
|
---|
40 | p.Name, x.ToString()),
|
---|
41 | "Error while creating parameter editor",
|
---|
42 | MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
43 | }
|
---|
44 | }
|
---|
45 | Label filler = new Label() { Dock = DockStyle.Fill };
|
---|
46 | panel.Controls.Add(filler);
|
---|
47 | panel.SetCellPosition(filler, new TableLayoutPanelCellPosition(0, panel.RowCount));
|
---|
48 | panel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
|
---|
49 | ResumeLayout();
|
---|
50 | }
|
---|
51 |
|
---|
52 | private Control CreateParameterControl(IParameterAccessor parameter) {
|
---|
53 | if (parameter is IPrimitiveParameter) {
|
---|
54 | TextBox box = new TextBox() {
|
---|
55 | Dock = DockStyle.Fill,
|
---|
56 | Text = parameter.Value,
|
---|
57 | };
|
---|
58 | box.TextChanged += (s, a) => { UpdateParameter((IPrimitiveParameter)parameter, box); };
|
---|
59 | return box;
|
---|
60 | } else if (parameter is IOperatorParameter) {
|
---|
61 | IOperatorParameter op = (IOperatorParameter)parameter;
|
---|
62 | OperatorParameterSetterControl control = new OperatorParameterSetterControl() {
|
---|
63 | Dock = DockStyle.Fill,
|
---|
64 | Label = op.Value,
|
---|
65 | };
|
---|
66 | control.SetClicked += (s, a) => {
|
---|
67 | op.Set();
|
---|
68 | control.Label = op.Value;
|
---|
69 | };
|
---|
70 | control.ShowClicked += (s, a) => { op.Show(); };
|
---|
71 | return control;
|
---|
72 | } else throw new ArgumentException("unsupported parameter type");
|
---|
73 | }
|
---|
74 |
|
---|
75 | private void UpdateParameter(IPrimitiveParameter parameter, TextBox box) {
|
---|
76 | string value = box.Text;
|
---|
77 | if (parameter.IsValid(value)) {
|
---|
78 | parameter.Value = value;
|
---|
79 | box.BackColor = Color.White;
|
---|
80 | } else {
|
---|
81 | box.BackColor = Color.LightPink;
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|