Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17506 was 17506, checked in by mkommend, 4 years ago

#2971: Further minor refactorings and renaming of members.

File size: 6.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21using System;
22using System.ComponentModel;
23using System.Windows.Forms;
24using HeuristicLab.Core.Views;
25using HeuristicLab.MainForm;
26
27namespace HeuristicLab.Problems.DataAnalysis.Views {
28  [View("Interval Constraint Detail View")]
29  [Content(typeof(IntervalConstraint), true)]
30  public sealed partial class IntervalConstraintView : ItemView {
31
32    public new IntervalConstraint Content {
33      get => (IntervalConstraint) base.Content;
34      set => base.Content = value;
35    }
36
37    public IntervalConstraintView() {
38      InitializeComponent();
39      int [] items = {1, 2, 3};
40      expressionInput.ReadOnly = true;
41      numberderivationInput.DataSource = items;
42    }
43
44    protected override void OnContentChanged() {
45      base.OnContentChanged();
46      UpdateControls();
47    }
48
49    protected override void RegisterContentEvents() {
50      base.RegisterContentEvents();
51      Content.Changed += new EventHandler(Content_Changed);
52    }
53
54    protected override void DeregisterContentEvents() {
55      Content.Changed -= new EventHandler(Content_Changed);
56      base.DeregisterContentEvents();
57    }
58
59    protected override void SetEnabledStateOfControls() {
60      base.SetEnabledStateOfControls();
61      expressionInput.Enabled = Content != null && !Locked && !ReadOnly;
62      variableInput.Enabled = Content != null && !Locked && !ReadOnly;
63      numberderivationInput.Enabled = Content != null && !Locked && !ReadOnly;
64      lowerboundInput.Enabled = Content != null && !Locked && !ReadOnly;
65      upperboundInput.Enabled = Content != null && !Locked && !ReadOnly;
66      ischeckedCheckBox.Enabled = Content != null && !Locked && !ReadOnly;
67    }
68
69
70
71    #region helpers
72
73    private static double ParseDoubleValue(string input, Control control, ErrorProvider errorProvider) {
74      input = input.ToLower();
75      switch (input) {
76        case "inf.":
77        case "+inf.":
78        case "Infinity":
79          return double.PositiveInfinity;
80        case "-inf.":
81        case "-Infinity":
82          return double.NegativeInfinity;
83        default: {
84          if (double.TryParse(input, out var value))
85            return value;
86          else {
87            errorProvider.SetError(control, "Invalid Input: Value must be a double!");
88            return double.NaN;
89          }
90        }
91      }
92    }
93
94    private void UpdateControls() {
95      if (Content == null) {
96        expressionInput.Text = string.Empty;
97        lowerboundInput.Text = string.Empty;
98        upperboundInput.Text = string.Empty;
99        return;
100      }
101
102      expressionInput.Text = Content.Expression;
103      lowerboundInput.Text = Content.Interval.LowerBound.ToString();
104      upperboundInput.Text = Content.Interval.UpperBound.ToString();
105
106      variableInput.Text = Content.Variable;
107      if (!Content.IsDerivative) {
108        numberderivationInput.Enabled = false;
109        numberderivationInput.SelectedItem = null;
110        numberderivationInput.Text = "0";
111      } else {
112        numberderivationLabel.Visible = true;
113        numberderivationInput.Visible = true;
114        numberderivationInput.Enabled = true;
115        numberderivationInput.SelectedItem = Content.NumberOfDerivations;
116      }
117
118      ischeckedCheckBox.Checked = Content.Enabled;
119    }
120
121    #endregion
122
123    #region control event handlers
124    private void lowerboundInput_Validating(object sender, CancelEventArgs e) {
125      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput, this.errorProvider);
126      if (double.IsNaN(value)) {
127        errorProvider.SetError(lowerboundInput, "Invalid Input: Lowerbound must be a double value!");
128        e.Cancel = true;
129        return;
130      }
131
132      if (value > Content.Interval.UpperBound) {
133        errorProvider.SetError(lowerboundInput, "Invalid Input: Lowerbound must be smaller than Upperbound!");
134        e.Cancel = true;
135        return;
136      }
137
138      errorProvider.SetError(lowerboundInput, string.Empty);
139      e.Cancel = false;
140    }
141
142    private void lowerboundInput_Validated(object sender, EventArgs e) {
143      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput, this.errorProvider);
144      if (!double.IsNaN(value)) {
145        Content.Interval = new Interval(value, Content.Interval.UpperBound);
146      }
147    }
148
149    private void upperboundInput_Validating(object sender, CancelEventArgs e) {
150      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput, this.errorProvider);
151      if (double.IsNaN(value)) {
152        errorProvider.SetError(upperboundInput, "Invalid Input: Upperbound must be a double value!");
153        e.Cancel = true;
154        return;
155      }
156
157      if (value < Content.Interval.LowerBound) {
158        errorProvider.SetError(upperboundInput, "Invalid Input: Upperbound must be bigger than Lowerbound!");
159        e.Cancel = true;
160        return;
161      }
162
163      errorProvider.SetError(upperboundInput, string.Empty);
164      e.Cancel = false;
165    }
166
167    private void upperboundInput_Validated(object sender, EventArgs e) {
168      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput, this.errorProvider);
169      if (!double.IsNaN(value)) {
170        Content.Interval = new Interval(Content.Interval.LowerBound, value);
171      }
172    }
173
174    private void ischeckedCheckBox_CheckedChanged(object sender, EventArgs e) {
175        Content.Enabled = ischeckedCheckBox.Checked;
176    }
177
178    private void numberderivationInput_SelectedIndexChanged(object sender, EventArgs e) {
179      if (numberderivationInput.SelectedItem == null) {
180        Content.NumberOfDerivations = 0;
181        numberderivationInput.Enabled = false;
182        return;
183      }
184      if ((int)numberderivationInput.SelectedItem == 1)
185        Content.NumberOfDerivations = 1;
186      else if ((int)numberderivationInput.SelectedItem == 2)
187        Content.NumberOfDerivations = 2;
188      else if ((int)numberderivationInput.SelectedItem == 3)
189        Content.NumberOfDerivations = 3;
190    }
191
192    #endregion
193
194    #region content event handlers
195
196    private void Content_Changed(object sender, EventArgs e) {
197      UpdateControls();
198    }
199    #endregion
200  }
201}
Note: See TracBrowser for help on using the repository browser.