Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Views/3.4/IntervalConstraintView.cs @ 16776

Last change on this file since 16776 was 16776, checked in by chaider, 5 years ago

#2971

  • Added Expression change event
  • Added errorProvider Validation
File size: 7.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Globalization;
7using System.Linq;
8using System.Text;
9using System.Threading.Tasks;
10using System.Windows.Forms;
11using HeuristicLab.Core.Views;
12using HeuristicLab.MainForm;
13using HeuristicLab.MainForm.WindowsForms;
14
15namespace 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      expressionInput.ReadOnly = true;
28      definitionInput.ReadOnly = true;
29      derivationInput.Enabled = false;
30      derivationInput.Checked = Content?.IsDerivation ?? false;
31      if (Content != null && !Content.IsDerivation)
32        numberderivationInput.ReadOnly = true;
33    }
34
35    protected override void OnContentChanged() {
36      base.OnContentChanged();
37      UpdateControls();
38    }
39
40
41    protected override void SetEnabledStateOfControls() {
42      base.SetEnabledStateOfControls();
43      lowerboundInput.Enabled = Content != null && !Locked;
44    }
45
46
47
48    #region helpers
49
50    private double ParseDoubleValue(string input, Control control) {
51      input = input.ToLower();
52      switch (input) {
53        case "inf.":
54        case "+inf.":
55        case "Infinity":
56          return double.PositiveInfinity;
57        case "-inf.":
58        case "-Infinity":
59          return double.NegativeInfinity;
60        default: {
61          if (double.TryParse(input, NumberStyles.Any, CultureInfo.InvariantCulture, out var value))
62            return value;
63          else {
64            errorProvider.SetError(control, "Invalid Input: Value must be a double!");
65            return double.NaN;
66          }
67        }
68      }
69    }
70
71    private void UpdateControls() {
72      if (Content == null) {
73        expressionInput.Text = string.Empty;
74        lowerboundInput.Text = string.Empty;
75        upperboundInput.Text = string.Empty;
76        incllowerboundInput.Checked = true;
77        inclupperboundInput.Checked = true;
78      } else {
79        expressionInput.Text = Content.Expression;
80        definitionInput.Text = Content.Definition;
81        lowerboundInput.Text = Content.Interval.LowerBound.ToString(CultureInfo.InvariantCulture);
82        upperboundInput.Text = Content.Interval.UpperBound.ToString(CultureInfo.InvariantCulture);
83        incllowerboundInput.Checked = Content.InclusiveLowerBound;
84        inclupperboundInput.Checked = Content.InclusiveUpperBound;
85        derivationInput.Checked = Content.IsDerivation;
86        variableInput.Text = Content.Variable;
87        numberderivationInput.Text = Content.NumberOfDerivation.ToString();
88      }
89      SetEnabledStateOfControls();
90    }
91
92    private void UpdateExpression() {
93      var expression = "";
94
95      if (!Content.IsDerivation) {
96        expression = string.Format("{0} in {1}{2} .. {3}{4}",
97          Content.Variable,
98          (Content.InclusiveLowerBound) ? "[" : "]",
99          Content.Interval.LowerBound,
100          Content.Interval.UpperBound,
101          (Content.InclusiveUpperBound) ? "]" : "[");
102      } else {
103        expression = string.Format("\u2202Target/\u2202{0} in {1}{2} .. {3}{4}",
104          Content.Variable,
105          (Content.InclusiveLowerBound) ? "[" : "]",
106          Content.Interval.LowerBound,
107          Content.Interval.UpperBound,
108          (Content.InclusiveUpperBound) ? "]" : "[");
109      }
110
111      Content.Expression = expression;
112      Content.Name = expression;
113      UpdateControls();
114    }
115
116    #endregion
117
118    #region content event handlers
119
120    private void numberderivationInput_Validating(object sender, CancelEventArgs e) {
121      if (int.TryParse(numberderivationInput.Text, out var derivation)) {
122        if (derivation >= 0) {
123          errorProvider.SetError(numberderivationInput, string.Empty);
124          e.Cancel = false;
125        } else {
126          errorProvider.SetError(numberderivationInput, "Invalid Input: Derivation must be positive!");
127          e.Cancel = true;
128        }
129      } else {
130        errorProvider.SetError(numberderivationInput, "Invalid Input: Derivation must be an integer!");
131        e.Cancel = true;
132      }
133    }
134
135    private void numberderivationInput_Validated(object sender, EventArgs e) {
136      if (int.TryParse(numberderivationInput.Text, out var derivation)) {
137        Content.NumberOfDerivation = derivation;
138        UpdateExpression();
139      }
140    }
141
142    private void lowerboundInput_Validating(object sender, CancelEventArgs e) {
143      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput);
144      if (!double.IsNaN(value)) {
145        if (value <= Content.Interval.UpperBound) {
146          errorProvider.SetError(lowerboundInput, string.Empty);
147          e.Cancel = false;
148        } else {
149          errorProvider.SetError(lowerboundInput, "Invalid Input: Lowerbound must be smaller than Upperbound!");
150          e.Cancel = true;
151        }
152      } else {
153        e.Cancel = true;
154      }
155    }
156
157    private void lowerboundInput_Validated(object sender, EventArgs e) {
158      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput);
159      if (!double.IsNaN(value)) {
160        Content.Interval = new Interval(value, Content.Interval.UpperBound);
161        UpdateExpression();
162      }
163    }
164
165    private void upperboundInput_Validating(object sender, CancelEventArgs e) {
166      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput);
167      if (!double.IsNaN(value)) {
168        if (value >= Content.Interval.LowerBound) {
169          errorProvider.SetError(upperboundInput, string.Empty);
170          e.Cancel = false;
171        } else {
172          errorProvider.SetError(lowerboundInput, "Invalid Input: Upperbound must be bigger than Lowerbound!");
173          e.Cancel = true;
174        }
175      } else {
176        e.Cancel = true;
177      }
178    }
179
180    private void upperboundInput_Validated(object sender, EventArgs e) {
181      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput);
182      if (!double.IsNaN(value)) {
183        Content.Interval = new Interval(Content.Interval.LowerBound, value);
184        UpdateExpression();
185      }
186    }
187
188    private void incllowerboundInput_CheckedChanged(object sender, EventArgs e) {
189      if (Content.InclusiveLowerBound != incllowerboundInput.Checked) {
190        Content.InclusiveLowerBound = incllowerboundInput.Checked;
191        UpdateExpression();
192      }
193     
194     
195    }
196
197    private void inclupperboundInput_CheckedChanged(object sender, EventArgs e) {
198      if (Content.InclusiveUpperBound != incllowerboundInput.Checked) {
199        Content.InclusiveUpperBound = incllowerboundInput.Checked;
200        UpdateExpression();
201      }
202    }
203    #endregion
204
205
206  }
207}
Note: See TracBrowser for help on using the repository browser.