Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2971: Further minor refactorings and renaming of members.

File size: 6.9 KB
RevLine 
[16830]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;
[16772]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)]
[16862]30  public sealed partial class IntervalConstraintView : ItemView {
[16772]31
32    public new IntervalConstraint Content {
[16776]33      get => (IntervalConstraint) base.Content;
34      set => base.Content = value;
[16772]35    }
[16774]36
[16772]37    public IntervalConstraintView() {
38      InitializeComponent();
[16778]39      int [] items = {1, 2, 3};
[16772]40      expressionInput.ReadOnly = true;
[16778]41      numberderivationInput.DataSource = items;
[16772]42    }
43
44    protected override void OnContentChanged() {
45      base.OnContentChanged();
46      UpdateControls();
47    }
48
[16787]49    protected override void RegisterContentEvents() {
50      base.RegisterContentEvents();
51      Content.Changed += new EventHandler(Content_Changed);
52    }
[16774]53
[16787]54    protected override void DeregisterContentEvents() {
[16862]55      Content.Changed -= new EventHandler(Content_Changed);
[16787]56      base.DeregisterContentEvents();
57    }
58
[16772]59    protected override void SetEnabledStateOfControls() {
60      base.SetEnabledStateOfControls();
[16936]61      expressionInput.Enabled = Content != null && !Locked && !ReadOnly;
62      variableInput.Enabled = Content != null && !Locked && !ReadOnly;
63      numberderivationInput.Enabled = Content != null && !Locked && !ReadOnly;
[16862]64      lowerboundInput.Enabled = Content != null && !Locked && !ReadOnly;
65      upperboundInput.Enabled = Content != null && !Locked && !ReadOnly;
[16936]66      ischeckedCheckBox.Enabled = Content != null && !Locked && !ReadOnly;
[16772]67    }
68
[16776]69
70
71    #region helpers
72
[16862]73    private static double ParseDoubleValue(string input, Control control, ErrorProvider errorProvider) {
[16776]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: {
[16862]84          if (double.TryParse(input, out var value))
[16776]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
[16772]94    private void UpdateControls() {
95      if (Content == null) {
96        expressionInput.Text = string.Empty;
97        lowerboundInput.Text = string.Empty;
98        upperboundInput.Text = string.Empty;
[16862]99        return;
100      }
101
102      expressionInput.Text = Content.Expression;
103      lowerboundInput.Text = Content.Interval.LowerBound.ToString();
104      upperboundInput.Text = Content.Interval.UpperBound.ToString();
[17370]105
[16862]106      variableInput.Text = Content.Variable;
[17506]107      if (!Content.IsDerivative) {
[16898]108        numberderivationInput.Enabled = false;
109        numberderivationInput.SelectedItem = null;
[16920]110        numberderivationInput.Text = "0";
[16772]111      } else {
[16862]112        numberderivationLabel.Visible = true;
113        numberderivationInput.Visible = true;
114        numberderivationInput.Enabled = true;
[17506]115        numberderivationInput.SelectedItem = Content.NumberOfDerivations;
[16862]116      }
[16787]117
[16862]118      ischeckedCheckBox.Checked = Content.Enabled;
[16772]119    }
120
[16778]121    #endregion
[16776]122
[16778]123    #region control event handlers
[16776]124    private void lowerboundInput_Validating(object sender, CancelEventArgs e) {
[16862]125      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput, this.errorProvider);
[16800]126      if (double.IsNaN(value)) {
127        errorProvider.SetError(lowerboundInput, "Invalid Input: Lowerbound must be a double value!");
[16776]128        e.Cancel = true;
[16800]129        return;
[16774]130      }
[16800]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;
[16774]140    }
141
[16776]142    private void lowerboundInput_Validated(object sender, EventArgs e) {
[16862]143      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput, this.errorProvider);
[16776]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) {
[16862]150      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput, this.errorProvider);
[16800]151      if (double.IsNaN(value)) {
152        errorProvider.SetError(upperboundInput, "Invalid Input: Upperbound must be a double value!");
[16776]153        e.Cancel = true;
[16800]154        return;
[16774]155      }
[16800]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;
[16774]165    }
[16776]166
167    private void upperboundInput_Validated(object sender, EventArgs e) {
[16862]168      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput, this.errorProvider);
[16776]169      if (!double.IsNaN(value)) {
170        Content.Interval = new Interval(Content.Interval.LowerBound, value);
171      }
172    }
173
[16787]174    private void ischeckedCheckBox_CheckedChanged(object sender, EventArgs e) {
[16800]175        Content.Enabled = ischeckedCheckBox.Checked;
[16777]176    }
177
[16778]178    private void numberderivationInput_SelectedIndexChanged(object sender, EventArgs e) {
[16898]179      if (numberderivationInput.SelectedItem == null) {
[17506]180        Content.NumberOfDerivations = 0;
[17370]181        numberderivationInput.Enabled = false;
[16898]182        return;
183      }
[16778]184      if ((int)numberderivationInput.SelectedItem == 1)
[17506]185        Content.NumberOfDerivations = 1;
[16778]186      else if ((int)numberderivationInput.SelectedItem == 2)
[17506]187        Content.NumberOfDerivations = 2;
[16778]188      else if ((int)numberderivationInput.SelectedItem == 3)
[17506]189        Content.NumberOfDerivations = 3;
[16778]190    }
191
[16776]192    #endregion
193
[16777]194    #region content event handlers
[16776]195
[16777]196    private void Content_Changed(object sender, EventArgs e) {
197      UpdateControls();
198    }
199    #endregion
[16772]200  }
201}
Note: See TracBrowser for help on using the repository browser.