Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3073_IA_constraint_splitting_reintegration/HeuristicLab.Problems.DataAnalysis.Views/3.4/IntervalConstraintView.cs @ 17891

Last change on this file since 17891 was 17891, checked in by gkronber, 3 years ago

#3073 refactoring to prepare for trunk reintegration

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