1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.ComponentModel;
|
---|
4 | using System.Drawing;
|
---|
5 | using System.Data;
|
---|
6 | using System.Globalization;
|
---|
7 | using System.Linq;
|
---|
8 | using System.Text;
|
---|
9 | using System.Threading.Tasks;
|
---|
10 | using System.Windows.Forms;
|
---|
11 | using HeuristicLab.Core.Views;
|
---|
12 | using HeuristicLab.MainForm;
|
---|
13 | using HeuristicLab.MainForm.WindowsForms;
|
---|
14 |
|
---|
15 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
16 | [View("Interval Constraint Detail View")]
|
---|
17 | [Content(typeof(IntervalConstraint), true)]
|
---|
18 | public partial class IntervalConstraintView : ItemView {
|
---|
19 |
|
---|
20 | public new IntervalConstraint Content {
|
---|
21 | get => (IntervalConstraint) base.Content;
|
---|
22 | set => base.Content = value;
|
---|
23 | }
|
---|
24 |
|
---|
25 | public IntervalConstraintView() {
|
---|
26 | InitializeComponent();
|
---|
27 | int [] items = {1, 2, 3};
|
---|
28 | expressionInput.ReadOnly = true;
|
---|
29 | definitionInput.ReadOnly = true;
|
---|
30 | derivationInput.Enabled = false;
|
---|
31 | derivationInput.Checked = Content?.IsDerivation ?? false;
|
---|
32 | numberderivationInput.DataSource = items;
|
---|
33 | }
|
---|
34 |
|
---|
35 | protected override void OnContentChanged() {
|
---|
36 | base.OnContentChanged();
|
---|
37 | UpdateControls();
|
---|
38 | }
|
---|
39 |
|
---|
40 | protected override void RegisterContentEvents() {
|
---|
41 | base.RegisterContentEvents();
|
---|
42 | Content.Changed += new EventHandler(Content_Changed);
|
---|
43 | }
|
---|
44 |
|
---|
45 | protected override void DeregisterContentEvents() {
|
---|
46 | base.DeregisterContentEvents();
|
---|
47 | Content.Changed -= new EventHandler(Content_Changed);
|
---|
48 | }
|
---|
49 |
|
---|
50 | protected override void SetEnabledStateOfControls() {
|
---|
51 | base.SetEnabledStateOfControls();
|
---|
52 | lowerboundInput.Enabled = Content != null && !Locked;
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 | #region helpers
|
---|
58 |
|
---|
59 | private double ParseDoubleValue(string input, Control control) {
|
---|
60 | input = input.ToLower();
|
---|
61 | switch (input) {
|
---|
62 | case "inf.":
|
---|
63 | case "+inf.":
|
---|
64 | case "Infinity":
|
---|
65 | return double.PositiveInfinity;
|
---|
66 | case "-inf.":
|
---|
67 | case "-Infinity":
|
---|
68 | return double.NegativeInfinity;
|
---|
69 | default: {
|
---|
70 | if (double.TryParse(input, NumberStyles.Any, CultureInfo.InvariantCulture, out var value))
|
---|
71 | return value;
|
---|
72 | else {
|
---|
73 | errorProvider.SetError(control, "Invalid Input: Value must be a double!");
|
---|
74 | return double.NaN;
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | private void UpdateControls() {
|
---|
81 | if (Content == null) {
|
---|
82 | expressionInput.Text = string.Empty;
|
---|
83 | lowerboundInput.Text = string.Empty;
|
---|
84 | upperboundInput.Text = string.Empty;
|
---|
85 | incllowerboundInput.Checked = true;
|
---|
86 | inclupperboundInput.Checked = true;
|
---|
87 | } else {
|
---|
88 | expressionInput.Text = Content.Expression;
|
---|
89 | definitionInput.Text = Content.Definition;
|
---|
90 | lowerboundInput.Text = Content.Interval.LowerBound.ToString(CultureInfo.InvariantCulture);
|
---|
91 | upperboundInput.Text = Content.Interval.UpperBound.ToString(CultureInfo.InvariantCulture);
|
---|
92 | incllowerboundInput.Checked = Content.InclusiveLowerBound;
|
---|
93 | inclupperboundInput.Checked = Content.InclusiveUpperBound;
|
---|
94 | derivationInput.Checked = Content.IsDerivation;
|
---|
95 | variableInput.Text = Content.Variable;
|
---|
96 | if (!Content.IsDerivation) {
|
---|
97 | numberderivationInput.Enabled = false;
|
---|
98 | } else {
|
---|
99 | numberderivationInput.Enabled = true;
|
---|
100 | numberderivationInput.SelectedItem = Content.NumberOfDerivation;
|
---|
101 | }
|
---|
102 |
|
---|
103 | ischeckedCheckBox.Checked = Content.IsChecked;
|
---|
104 | }
|
---|
105 | SetEnabledStateOfControls();
|
---|
106 | }
|
---|
107 |
|
---|
108 | private void UpdateExpression() {
|
---|
109 | var expression = "";
|
---|
110 |
|
---|
111 | if (!Content.IsDerivation) {
|
---|
112 | expression = string.Format("{0} in {1}{2} .. {3}{4}",
|
---|
113 | Content.Variable,
|
---|
114 | (Content.InclusiveLowerBound) ? "[" : "]",
|
---|
115 | Content.Interval.LowerBound,
|
---|
116 | Content.Interval.UpperBound,
|
---|
117 | (Content.InclusiveUpperBound) ? "]" : "[");
|
---|
118 | } else {
|
---|
119 | expression = string.Format("\u2202{5}Target/\u2202{0}{6} in {1}{2} .. {3}{4}",
|
---|
120 | Content.Variable,
|
---|
121 | (Content.InclusiveLowerBound) ? "[" : "]",
|
---|
122 | Content.Interval.LowerBound,
|
---|
123 | Content.Interval.UpperBound,
|
---|
124 | (Content.InclusiveUpperBound) ? "]" : "[",
|
---|
125 | PrintNumberOfDerivation(Content.numberOfDerivation),
|
---|
126 | PrintNumberOfDerivation(Content.numberOfDerivation));
|
---|
127 | }
|
---|
128 |
|
---|
129 | Content.Expression = expression;
|
---|
130 | Content.Name = expression;
|
---|
131 | UpdateControls();
|
---|
132 | }
|
---|
133 |
|
---|
134 | private string PrintNumberOfDerivation(int derivation) {
|
---|
135 | switch (derivation) {
|
---|
136 | case 1:
|
---|
137 | return "";
|
---|
138 | case 2:
|
---|
139 | return "²";
|
---|
140 | case 3:
|
---|
141 | return "³";
|
---|
142 | default:
|
---|
143 | return "";
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | #endregion
|
---|
148 |
|
---|
149 | #region control event handlers
|
---|
150 | private void lowerboundInput_Validating(object sender, CancelEventArgs e) {
|
---|
151 | var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput);
|
---|
152 | if (!double.IsNaN(value)) {
|
---|
153 | if (value <= Content.Interval.UpperBound) {
|
---|
154 | errorProvider.SetError(lowerboundInput, string.Empty);
|
---|
155 | e.Cancel = false;
|
---|
156 | } else {
|
---|
157 | errorProvider.SetError(lowerboundInput, "Invalid Input: Lowerbound must be smaller than Upperbound!");
|
---|
158 | e.Cancel = true;
|
---|
159 | }
|
---|
160 | } else {
|
---|
161 | e.Cancel = true;
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | private void lowerboundInput_Validated(object sender, EventArgs e) {
|
---|
166 | var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput);
|
---|
167 | if (!double.IsNaN(value)) {
|
---|
168 | Content.Interval = new Interval(value, Content.Interval.UpperBound);
|
---|
169 | UpdateExpression();
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | private void upperboundInput_Validating(object sender, CancelEventArgs e) {
|
---|
174 | var value = ParseDoubleValue(upperboundInput.Text, upperboundInput);
|
---|
175 | if (!double.IsNaN(value)) {
|
---|
176 | if (value >= Content.Interval.LowerBound) {
|
---|
177 | errorProvider.SetError(upperboundInput, string.Empty);
|
---|
178 | e.Cancel = false;
|
---|
179 | } else {
|
---|
180 | errorProvider.SetError(lowerboundInput, "Invalid Input: Upperbound must be bigger than Lowerbound!");
|
---|
181 | e.Cancel = true;
|
---|
182 | }
|
---|
183 | } else {
|
---|
184 | e.Cancel = true;
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | private void upperboundInput_Validated(object sender, EventArgs e) {
|
---|
189 | var value = ParseDoubleValue(upperboundInput.Text, upperboundInput);
|
---|
190 | if (!double.IsNaN(value)) {
|
---|
191 | Content.Interval = new Interval(Content.Interval.LowerBound, value);
|
---|
192 | UpdateExpression();
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | private void incllowerboundInput_CheckedChanged(object sender, EventArgs e) {
|
---|
197 | if (Content.InclusiveLowerBound != incllowerboundInput.Checked) {
|
---|
198 | Content.InclusiveLowerBound = incllowerboundInput.Checked;
|
---|
199 | UpdateExpression();
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | }
|
---|
204 |
|
---|
205 | private void inclupperboundInput_CheckedChanged(object sender, EventArgs e) {
|
---|
206 | if (Content.InclusiveUpperBound != inclupperboundInput.Checked) {
|
---|
207 | Content.InclusiveUpperBound = inclupperboundInput.Checked;
|
---|
208 | UpdateExpression();
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | private void ischeckedCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
213 | if (Content.IsChecked != ischeckedCheckBox.Checked) {
|
---|
214 | Content.IsChecked = ischeckedCheckBox.Checked;
|
---|
215 | UpdateControls();
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | private void numberderivationInput_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
220 | if ((int)numberderivationInput.SelectedItem == 1)
|
---|
221 | Content.numberOfDerivation = 1;
|
---|
222 | else if ((int)numberderivationInput.SelectedItem == 2)
|
---|
223 | Content.numberOfDerivation = 2;
|
---|
224 | else if ((int)numberderivationInput.SelectedItem == 3)
|
---|
225 | Content.numberOfDerivation = 3;
|
---|
226 |
|
---|
227 | UpdateExpression();
|
---|
228 | }
|
---|
229 |
|
---|
230 | #endregion
|
---|
231 |
|
---|
232 | #region content event handlers
|
---|
233 |
|
---|
234 | private void Content_Changed(object sender, EventArgs e) {
|
---|
235 | UpdateControls();
|
---|
236 | }
|
---|
237 |
|
---|
238 |
|
---|
239 | #endregion
|
---|
240 |
|
---|
241 | }
|
---|
242 | }
|
---|