#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.Globalization; 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 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; definitionInput.ReadOnly = true; derivationInput.Enabled = false; derivationInput.Checked = false; 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() { base.DeregisterContentEvents(); Content.Changed -= new EventHandler(Content_Changed); } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); lowerboundInput.Enabled = Content != null && !Locked; } #region helpers private double ParseDoubleValue(string input, Control control) { 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, NumberStyles.Any, CultureInfo.InvariantCulture, 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; } else { expressionInput.Text = Content.Expression; definitionInput.Text = Content.Definition; lowerboundInput.Text = Content.Interval.LowerBound.ToString(CultureInfo.InvariantCulture); upperboundInput.Text = Content.Interval.UpperBound.ToString(CultureInfo.InvariantCulture); incllowerboundInput.Checked = Content.InclusiveLowerBound; inclupperboundInput.Checked = Content.InclusiveUpperBound; derivationInput.Checked = Content.IsDerivation; variableInput.Text = Content.Variable; if (!Content.IsDerivation) { numberderivationInput.Enabled = false; } else { numberderivationInput.Enabled = true; numberderivationInput.SelectedItem = Content.NumberOfDerivation; } ischeckedCheckBox.Checked = Content.Enabled; } SetEnabledStateOfControls(); } private static string UpdateExpression(IntervalConstraint constraint) { var expression = ""; if (!constraint.IsDerivation) { expression = string.Format("{0} in {1}{2} .. {3}{4}", constraint.Variable, (constraint.InclusiveLowerBound) ? "[" : "]", constraint.Interval.LowerBound, constraint.Interval.UpperBound, (constraint.InclusiveUpperBound) ? "]" : "["); } else { expression = string.Format("\u2202{5}Target/\u2202{0}{6} in {1}{2} .. {3}{4}", constraint.Variable, (constraint.InclusiveLowerBound) ? "[" : "]", constraint.Interval.LowerBound, constraint.Interval.UpperBound, (constraint.InclusiveUpperBound) ? "]" : "[", PrintNumberOfDerivation(constraint.numberOfDerivation), PrintNumberOfDerivation(constraint.numberOfDerivation)); } return expression; } private static string PrintNumberOfDerivation(int derivation) { switch (derivation) { case 1: return ""; case 2: return "²"; case 3: return "³"; default: return ""; } } #endregion #region control event handlers private void lowerboundInput_Validating(object sender, CancelEventArgs e) { var value = ParseDoubleValue(lowerboundInput.Text, lowerboundInput); 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); if (!double.IsNaN(value)) { Content.Interval = new Interval(value, Content.Interval.UpperBound); var exp = UpdateExpression(Content); Content.Name = exp; Content.Expression = exp; } } private void upperboundInput_Validating(object sender, CancelEventArgs e) { var value = ParseDoubleValue(upperboundInput.Text, upperboundInput); 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); if (!double.IsNaN(value)) { Content.Interval = new Interval(Content.Interval.LowerBound, value); var exp = UpdateExpression(Content); Content.Name = exp; Content.Expression = exp; } } private void incllowerboundInput_CheckedChanged(object sender, EventArgs e) { if (Content.InclusiveLowerBound != incllowerboundInput.Checked) { Content.InclusiveLowerBound = incllowerboundInput.Checked; var exp = UpdateExpression(Content); Content.Name = exp; Content.Expression = exp; } } private void inclupperboundInput_CheckedChanged(object sender, EventArgs e) { if (Content.InclusiveUpperBound != inclupperboundInput.Checked) { Content.InclusiveUpperBound = inclupperboundInput.Checked; var exp = UpdateExpression(Content); Content.Name = exp; Content.Expression = exp; } } private void ischeckedCheckBox_CheckedChanged(object sender, EventArgs e) { if (Content.Enabled != ischeckedCheckBox.Checked) { Content.Enabled = ischeckedCheckBox.Checked; //UpdateControls(); } } private void numberderivationInput_SelectedIndexChanged(object sender, EventArgs e) { 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; var exp = UpdateExpression(Content); Content.Name = exp; Content.Expression = exp; } #endregion #region content event handlers private void Content_Changed(object sender, EventArgs e) { UpdateControls(); } #endregion } }