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

Last change on this file since 17894 was 17894, checked in by gkronber, 17 months ago

#3073: moved views into Regression folder because they are only used within RegressionProblemData

File size: 6.8 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      this.regionView.Content = Content.Regions;
48      UpdateControls();
49    }
50
51    protected override void RegisterContentEvents() {
52      base.RegisterContentEvents();
53      Content.Changed += Content_Changed;
54    }
55
56    protected override void DeregisterContentEvents() {
57      Content.Changed -= Content_Changed;
58      base.DeregisterContentEvents();
59    }
60
61    protected override void SetEnabledStateOfControls() {
62      base.SetEnabledStateOfControls();
63      variableInput.Enabled = Content != null && !Locked && !ReadOnly;
64      numberderivationInput.Enabled = Content != null && !Locked && !ReadOnly;
65      lowerboundInput.Enabled = Content != null && !Locked && !ReadOnly;
66      upperboundInput.Enabled = Content != null && !Locked && !ReadOnly;
67      weightInput.Enabled = Content != null && !Locked && !ReadOnly;
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            }
87
88            errorProvider.SetError(control, "Invalid input: value must be a double.");
89            return double.NaN;
90          }
91      }
92    }
93
94    private void UpdateControls() {
95      if (Content == null) {
96        lowerboundInput.Text = string.Empty;
97        upperboundInput.Text = string.Empty;
98        weightInput.Text = string.Empty;
99        return;
100      }
101
102      lowerboundInput.Text = Content.Interval.LowerBound.ToString();
103      upperboundInput.Text = Content.Interval.UpperBound.ToString();
104      weightInput.Text = Content.Weight.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      regionView.Content = Content.Regions;
119    }
120
121    #endregion
122
123    #region control event handlers
124
125    private void lowerboundInput_Validating(object sender, CancelEventArgs e) {
126      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput, errorProvider);
127      if (double.IsNaN(value)) {
128        errorProvider.SetError(lowerboundInput, "Invalid input: lower bound must be a double value.");
129        e.Cancel = true;
130        return;
131      }
132
133      if (value > Content.Interval.UpperBound) {
134        errorProvider.SetError(lowerboundInput, "Invalid input: lower bound must be smaller than upper bound.");
135        e.Cancel = true;
136        return;
137      }
138
139      errorProvider.SetError(lowerboundInput, string.Empty);
140      e.Cancel = false;
141    }
142
143    private void lowerboundInput_Validated(object sender, EventArgs e) {
144      var value =
145        ParseDoubleValue(lowerboundInput.Text, lowerboundInput, errorProvider);
146      if (!double.IsNaN(value)) Content.Interval = new Interval(value, Content.Interval.UpperBound);
147    }
148
149    private void upperboundInput_Validating(object sender, CancelEventArgs e) {
150      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput, errorProvider);
151      if (double.IsNaN(value)) {
152        errorProvider.SetError(upperboundInput, "Invalid Input: upper bound 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: upper bound must be bigger than lower bound.");
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 =
169        ParseDoubleValue(upperboundInput.Text, upperboundInput, errorProvider);
170      if (!double.IsNaN(value)) Content.Interval = new Interval(Content.Interval.LowerBound, value);
171    }
172
173    private void numberderivationInput_SelectedIndexChanged(object sender, EventArgs e) {
174      if (numberderivationInput.SelectedItem == null) {
175        Content.NumberOfDerivations = 0;
176        numberderivationInput.Enabled = false;
177        return;
178      }
179
180      if ((int)numberderivationInput.SelectedItem == 1)
181        Content.NumberOfDerivations = 1;
182      else if ((int)numberderivationInput.SelectedItem == 2)
183        Content.NumberOfDerivations = 2;
184      else if ((int)numberderivationInput.SelectedItem == 3)
185        Content.NumberOfDerivations = 3;
186    }
187
188    private void weightInput_TextChanged(object sender, EventArgs e) {
189      var value = ParseDoubleValue(weightInput.Text, weightInput, errorProvider);
190      if (!double.IsNaN(value)) Content.Weight = value;
191    }
192
193
194    #endregion
195
196    #region content event handlers
197
198    private void Content_Changed(object sender, EventArgs e) {
199      UpdateControls();
200    }
201
202   #endregion
203  }
204}
Note: See TracBrowser for help on using the repository browser.