#region License Information /* HeuristicLab * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.ComponentModel; using System.Windows.Forms; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; namespace HeuristicLab.Problems.DataAnalysis.Views { [View("Interval Constraint Detail View")] [Content(typeof(IntervalConstraint), true)] public sealed partial class IntervalConstraintView : ItemView { public new IntervalConstraint Content { get => (IntervalConstraint) base.Content; set => base.Content = value; } public IntervalConstraintView() { InitializeComponent(); int [] items = {1, 2, 3}; expressionInput.ReadOnly = true; numberderivationInput.DataSource = items; } protected override void OnContentChanged() { base.OnContentChanged(); UpdateControls(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.Changed += new EventHandler(Content_Changed); } protected override void DeregisterContentEvents() { Content.Changed -= new EventHandler(Content_Changed); base.DeregisterContentEvents(); } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); expressionInput.Enabled = Content != null && !Locked && !ReadOnly; variableInput.Enabled = Content != null && !Locked && !ReadOnly; numberderivationInput.Enabled = Content != null && !Locked && !ReadOnly; lowerboundInput.Enabled = Content != null && !Locked && !ReadOnly; upperboundInput.Enabled = Content != null && !Locked && !ReadOnly; incllowerboundInput.Enabled = Content != null && !Locked && !ReadOnly; inclupperboundInput.Enabled = Content != null && !Locked && !ReadOnly; ischeckedCheckBox.Enabled = Content != null && !Locked && !ReadOnly; } #region helpers private static double ParseDoubleValue(string input, Control control, ErrorProvider errorProvider) { input = input.ToLower(); switch (input) { case "inf.": case "+inf.": case "Infinity": return double.PositiveInfinity; case "-inf.": case "-Infinity": return double.NegativeInfinity; default: { if (double.TryParse(input, out var value)) return value; else { errorProvider.SetError(control, "Invalid Input: Value must be a double!"); return double.NaN; } } } } private void UpdateControls() { if (Content == null) { expressionInput.Text = string.Empty; lowerboundInput.Text = string.Empty; upperboundInput.Text = string.Empty; incllowerboundInput.Checked = true; inclupperboundInput.Checked = true; return; } expressionInput.Text = Content.Expression; lowerboundInput.Text = Content.Interval.LowerBound.ToString(); upperboundInput.Text = Content.Interval.UpperBound.ToString(); incllowerboundInput.Checked = Content.InclusiveLowerBound; inclupperboundInput.Checked = Content.InclusiveUpperBound; variableInput.Text = Content.Variable; if (!Content.IsDerivation) { numberderivationInput.Enabled = false; numberderivationInput.SelectedItem = null; numberderivationInput.Text = "0"; } else { numberderivationLabel.Visible = true; numberderivationInput.Visible = true; numberderivationInput.Enabled = true; numberderivationInput.SelectedItem = Content.NumberOfDerivation; } ischeckedCheckBox.Checked = Content.Enabled; } #endregion #region control event handlers private void lowerboundInput_Validating(object sender, CancelEventArgs e) { var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput, this.errorProvider); if (double.IsNaN(value)) { errorProvider.SetError(lowerboundInput, "Invalid Input: Lowerbound must be a double value!"); e.Cancel = true; return; } if (value > Content.Interval.UpperBound) { errorProvider.SetError(lowerboundInput, "Invalid Input: Lowerbound must be smaller than Upperbound!"); e.Cancel = true; return; } errorProvider.SetError(lowerboundInput, string.Empty); e.Cancel = false; } private void lowerboundInput_Validated(object sender, EventArgs e) { var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput, this.errorProvider); if (!double.IsNaN(value)) { Content.Interval = new Interval(value, Content.Interval.UpperBound); } } private void upperboundInput_Validating(object sender, CancelEventArgs e) { var value = ParseDoubleValue(upperboundInput.Text, upperboundInput, this.errorProvider); if (double.IsNaN(value)) { errorProvider.SetError(upperboundInput, "Invalid Input: Upperbound must be a double value!"); e.Cancel = true; return; } if (value < Content.Interval.LowerBound) { errorProvider.SetError(upperboundInput, "Invalid Input: Upperbound must be bigger than Lowerbound!"); e.Cancel = true; return; } errorProvider.SetError(upperboundInput, string.Empty); e.Cancel = false; } private void upperboundInput_Validated(object sender, EventArgs e) { var value = ParseDoubleValue(upperboundInput.Text, upperboundInput, this.errorProvider); if (!double.IsNaN(value)) { Content.Interval = new Interval(Content.Interval.LowerBound, value); } } private void incllowerboundInput_CheckedChanged(object sender, EventArgs e) { Content.InclusiveLowerBound = incllowerboundInput.Checked; } private void inclupperboundInput_CheckedChanged(object sender, EventArgs e) { Content.InclusiveUpperBound = inclupperboundInput.Checked; } private void ischeckedCheckBox_CheckedChanged(object sender, EventArgs e) { Content.Enabled = ischeckedCheckBox.Checked; } private void numberderivationInput_SelectedIndexChanged(object sender, EventArgs e) { if (numberderivationInput.SelectedItem == null) { Content.NumberOfDerivation = 0; return; } if ((int)numberderivationInput.SelectedItem == 1) Content.NumberOfDerivation = 1; else if ((int)numberderivationInput.SelectedItem == 2) Content.NumberOfDerivation = 2; else if ((int)numberderivationInput.SelectedItem == 3) Content.NumberOfDerivation = 3; } #endregion #region content event handlers private void Content_Changed(object sender, EventArgs e) { UpdateControls(); } #endregion } }