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

Last change on this file since 16936 was 16936, checked in by chaider, 18 months ago

#2971 Added #Derivation combobox and checkboxes to EnableStateOfControls to lock it while running

File size: 7.6 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      incllowerboundInput.Enabled = Content != null && !Locked && !ReadOnly;
67      inclupperboundInput.Enabled = Content != null && !Locked && !ReadOnly;
68      ischeckedCheckBox.Enabled = Content != null && !Locked && !ReadOnly;
69    }
70
71
72
73    #region helpers
74
75    private static double ParseDoubleValue(string input, Control control, ErrorProvider errorProvider) {
76      input = input.ToLower();
77      switch (input) {
78        case "inf.":
79        case "+inf.":
80        case "Infinity":
81          return double.PositiveInfinity;
82        case "-inf.":
83        case "-Infinity":
84          return double.NegativeInfinity;
85        default: {
86          if (double.TryParse(input, out var value))
87            return value;
88          else {
89            errorProvider.SetError(control, "Invalid Input: Value must be a double!");
90            return double.NaN;
91          }
92        }
93      }
94    }
95
96    private void UpdateControls() {
97      if (Content == null) {
98        expressionInput.Text = string.Empty;
99        lowerboundInput.Text = string.Empty;
100        upperboundInput.Text = string.Empty;
101        incllowerboundInput.Checked = true;
102        inclupperboundInput.Checked = true;
103        return;
104      }
105
106      expressionInput.Text = Content.Expression;
107      lowerboundInput.Text = Content.Interval.LowerBound.ToString();
108      upperboundInput.Text = Content.Interval.UpperBound.ToString();
109      incllowerboundInput.Checked = Content.InclusiveLowerBound;
110      inclupperboundInput.Checked = Content.InclusiveUpperBound;
111      variableInput.Text = Content.Variable;
112      if (!Content.IsDerivation) {
113        numberderivationInput.Enabled = false;
114        numberderivationInput.SelectedItem = null;
115        numberderivationInput.Text = "0";
116      } else {
117        numberderivationLabel.Visible = true;
118        numberderivationInput.Visible = true;
119        numberderivationInput.Enabled = true;
120        numberderivationInput.SelectedItem = Content.NumberOfDerivation;
121      }
122
123      ischeckedCheckBox.Checked = Content.Enabled;
124    }
125
126    #endregion
127
128    #region control event handlers
129    private void lowerboundInput_Validating(object sender, CancelEventArgs e) {
130      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput, this.errorProvider);
131      if (double.IsNaN(value)) {
132        errorProvider.SetError(lowerboundInput, "Invalid Input: Lowerbound must be a double value!");
133        e.Cancel = true;
134        return;
135      }
136
137      if (value > Content.Interval.UpperBound) {
138        errorProvider.SetError(lowerboundInput, "Invalid Input: Lowerbound must be smaller than Upperbound!");
139        e.Cancel = true;
140        return;
141      }
142
143      errorProvider.SetError(lowerboundInput, string.Empty);
144      e.Cancel = false;
145    }
146
147    private void lowerboundInput_Validated(object sender, EventArgs e) {
148      var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput, this.errorProvider);
149      if (!double.IsNaN(value)) {
150        Content.Interval = new Interval(value, Content.Interval.UpperBound);
151      }
152    }
153
154    private void upperboundInput_Validating(object sender, CancelEventArgs e) {
155      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput, this.errorProvider);
156      if (double.IsNaN(value)) {
157        errorProvider.SetError(upperboundInput, "Invalid Input: Upperbound must be a double value!");
158        e.Cancel = true;
159        return;
160      }
161
162      if (value < Content.Interval.LowerBound) {
163        errorProvider.SetError(upperboundInput, "Invalid Input: Upperbound must be bigger than Lowerbound!");
164        e.Cancel = true;
165        return;
166      }
167
168      errorProvider.SetError(upperboundInput, string.Empty);
169      e.Cancel = false;
170    }
171
172    private void upperboundInput_Validated(object sender, EventArgs e) {
173      var value = ParseDoubleValue(upperboundInput.Text, upperboundInput, this.errorProvider);
174      if (!double.IsNaN(value)) {
175        Content.Interval = new Interval(Content.Interval.LowerBound, value);
176      }
177    }
178
179    private void incllowerboundInput_CheckedChanged(object sender, EventArgs e) {
180        Content.InclusiveLowerBound = incllowerboundInput.Checked;
181    }
182
183    private void inclupperboundInput_CheckedChanged(object sender, EventArgs e) {
184        Content.InclusiveUpperBound = inclupperboundInput.Checked;
185    }
186
187    private void ischeckedCheckBox_CheckedChanged(object sender, EventArgs e) {
188        Content.Enabled = ischeckedCheckBox.Checked;
189    }
190
191    private void numberderivationInput_SelectedIndexChanged(object sender, EventArgs e) {
192      if (numberderivationInput.SelectedItem == null) {
193        Content.NumberOfDerivation = 0;
194        return;
195      }
196      if ((int)numberderivationInput.SelectedItem == 1)
197        Content.NumberOfDerivation = 1;
198      else if ((int)numberderivationInput.SelectedItem == 2)
199        Content.NumberOfDerivation = 2;
200      else if ((int)numberderivationInput.SelectedItem == 3)
201        Content.NumberOfDerivation = 3;
202    }
203
204    #endregion
205
206    #region content event handlers
207
208    private void Content_Changed(object sender, EventArgs e) {
209      UpdateControls();
210    }
211    #endregion
212  }
213}
Note: See TracBrowser for help on using the repository browser.