Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2971
-Fixed Dispose problem
-Added event handling for item selection

File size: 7.5 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        ischeckedCheckBox.Checked = Content.IsChecked;
89        //Content.Changed += Content_Changed;
90
91      }
92      SetEnabledStateOfControls();
93    }
94
95    private void UpdateExpression() {
96      var expression = "";
97
98      if (!Content.IsDerivation) {
99        expression = string.Format("{0} in {1}{2} .. {3}{4}",
100          Content.Variable,
101          (Content.InclusiveLowerBound) ? "[" : "]",
102          Content.Interval.LowerBound,
103          Content.Interval.UpperBound,
104          (Content.InclusiveUpperBound) ? "]" : "[");
105      } else {
106        expression = string.Format("\u2202Target/\u2202{0} in {1}{2} .. {3}{4}",
107          Content.Variable,
108          (Content.InclusiveLowerBound) ? "[" : "]",
109          Content.Interval.LowerBound,
110          Content.Interval.UpperBound,
111          (Content.InclusiveUpperBound) ? "]" : "[");
112      }
113
114      Content.Expression = expression;
115      Content.Name = expression;
116      UpdateControls();
117    }
118
119    #endregion
120
121    #region control event handlers
122
123    private void numberderivationInput_Validating(object sender, CancelEventArgs e) {
124      if (int.TryParse(numberderivationInput.Text, out var derivation)) {
125        if (derivation >= 0) {
126          errorProvider.SetError(numberderivationInput, string.Empty);
127          e.Cancel = false;
128        } else {
129          errorProvider.SetError(numberderivationInput, "Invalid Input: Derivation must be positive!");
130          e.Cancel = true;
131        }
132      } else {
133        errorProvider.SetError(numberderivationInput, "Invalid Input: Derivation must be an integer!");
134        e.Cancel = true;
135      }
136    }
137
138    private void numberderivationInput_Validated(object sender, EventArgs e) {
139      if (int.TryParse(numberderivationInput.Text, out var derivation)) {
140        Content.NumberOfDerivation = derivation;
141        UpdateExpression();
142      }
143    }
144
145    private void lowerboundInput_Validating(object sender, CancelEventArgs e) {
146      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput);
147      if (!double.IsNaN(value)) {
148        if (value <= Content.Interval.UpperBound) {
149          errorProvider.SetError(lowerboundInput, string.Empty);
150          e.Cancel = false;
151        } else {
152          errorProvider.SetError(lowerboundInput, "Invalid Input: Lowerbound must be smaller than Upperbound!");
153          e.Cancel = true;
154        }
155      } else {
156        e.Cancel = true;
157      }
158    }
159
160    private void lowerboundInput_Validated(object sender, EventArgs e) {
161      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput);
162      if (!double.IsNaN(value)) {
163        Content.Interval = new Interval(value, Content.Interval.UpperBound);
164        UpdateExpression();
165      }
166    }
167
168    private void upperboundInput_Validating(object sender, CancelEventArgs e) {
169      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput);
170      if (!double.IsNaN(value)) {
171        if (value >= Content.Interval.LowerBound) {
172          errorProvider.SetError(upperboundInput, string.Empty);
173          e.Cancel = false;
174        } else {
175          errorProvider.SetError(lowerboundInput, "Invalid Input: Upperbound must be bigger than Lowerbound!");
176          e.Cancel = true;
177        }
178      } else {
179        e.Cancel = true;
180      }
181    }
182
183    private void upperboundInput_Validated(object sender, EventArgs e) {
184      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput);
185      if (!double.IsNaN(value)) {
186        Content.Interval = new Interval(Content.Interval.LowerBound, value);
187        UpdateExpression();
188      }
189    }
190
191    private void incllowerboundInput_CheckedChanged(object sender, EventArgs e) {
192      if (Content.InclusiveLowerBound != incllowerboundInput.Checked) {
193        Content.InclusiveLowerBound = incllowerboundInput.Checked;
194        UpdateExpression();
195      }
196     
197     
198    }
199
200    private void inclupperboundInput_CheckedChanged(object sender, EventArgs e) {
201      if (Content.InclusiveUpperBound != inclupperboundInput.Checked) {
202        Content.InclusiveUpperBound = inclupperboundInput.Checked;
203        UpdateExpression();
204      }
205    }
206
207    private void ischeckedCheckBox_CheckStateChanged(object sender, EventArgs e) {
208      if (Content.IsChecked != ischeckedCheckBox.Checked) {
209        Content.IsChecked = ischeckedCheckBox.Checked;
210        UpdateControls();
211      }
212    }
213
214    #endregion
215
216    #region content event handlers
217
218    private void Content_Changed(object sender, EventArgs e) {
219      UpdateControls();
220    }
221
222    #endregion
223  }
224}
Note: See TracBrowser for help on using the repository browser.