#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.Collections.Generic;
using System.Drawing;
using HeuristicLab.Collections;
using HeuristicLab.Core;
using HeuristicLab.MainForm;
using HeuristicLab.MainForm.WindowsForms;
namespace HeuristicLab.Problems.DataAnalysis.Views {
[View("ParsedConstraint View")]
[Content(typeof(ProblemDataConstraint), true)]
public partial class ProblemDataConstraintView : AsynchronousContentView {
private readonly CheckedItemList
intervalConstraints = new CheckedItemList();
public new ProblemDataConstraint Content {
get => (ProblemDataConstraint) base.Content;
set => base.Content = value;
}
public ProblemDataConstraintView() {
InitializeComponent();
errorOutput.Text = "";
}
protected override void OnContentChanged() {
base.OnContentChanged();
UpdateControl();
}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
intervalConstraints.CheckedItemsChanged += constraint_CheckedItemChanged;
intervalConstraints.ItemsAdded += constraints_Updated;
intervalConstraints.ItemsRemoved += constraint_removed;
Content.Changed += Content_Changed;
}
protected override void DeregisterContentEvents() {
intervalConstraints.CheckedItemsChanged -= constraint_CheckedItemChanged;
intervalConstraints.ItemsAdded -= constraints_Updated;
intervalConstraints.ItemsRemoved -= constraint_removed;
Content.Changed -= Content_Changed;
base.DeregisterContentEvents();
}
protected override void SetEnabledStateOfControls() {
constraintsInput.Enabled = Content != null && !Locked && !ReadOnly;
}
private IEnumerable ParseConstraints(string input) {
return IntervalConstraintsParser.ParseInput(input, Content.ProblemData.TargetVariable,
Content.ProblemData.AllowedInputVariables);
}
private void parseBtn_Click(object sender, EventArgs e) {
if (constraintsInput.Text != null) {
intervalConstraints.Clear();
try {
var parsedConstraints = ParseConstraints(constraintsInput.Text);
Content.Constraints = parsedConstraints;
Content.Input = constraintsInput.Text;
Content.InfoText = "Constraints successfully parsed.";
Content.InfoColor = Color.DarkGreen;
//Catch the exception from the constraints parser and show it in the error dialog
}
catch (ArgumentException ex) {
Content.Constraints = new List();
Content.InfoText = ex.Message.Replace("Parameter name", "@Line");
Content.InfoColor = Color.DarkRed;
}
}
else {
errorOutput.Text = "No constraints were found!";
}
}
private void UpdateControl() {
if (Content == null) {
intervalConstraints.Clear();
intervalConstraintsView.Content = intervalConstraints;
}
else {
intervalConstraints.Clear();
constraintsInput.Text = Content.Input;
errorOutput.ForeColor = Content.InfoColor;
errorOutput.Text = Content.InfoText;
foreach (var constraint in Content.Constraints) intervalConstraints.Add(constraint, constraint.Enabled);
intervalConstraintsView.Content = intervalConstraints;
}
}
private void constraint_Changed(object sender, EventArgs e) {
var constraint = (IntervalConstraint) sender;
intervalConstraints.SetItemCheckedState(constraint, constraint.Enabled);
}
private void constraints_Updated(object sender,
CollectionItemsChangedEventArgs> e) {
foreach (var addedItem in e.Items) addedItem.Value.Changed += constraint_Changed;
}
private void constraint_removed(object sender, CollectionItemsChangedEventArgs> e) {
foreach (var removedItem in e.Items) removedItem.Value.Changed -= constraint_Changed;
}
private void constraint_CheckedItemChanged(object sender,
CollectionItemsChangedEventArgs> e) {
ICheckedItemList checkedItemList = (ICheckedItemList) sender;
foreach (var indexedItem in e.Items) indexedItem.Value.Enabled = checkedItemList.ItemChecked(indexedItem.Value);
}
private void constraintsInput_TextChanged(object sender, EventArgs e) {
if (Content.Input != constraintsInput.Text) {
Content.Input = constraintsInput.Text;
Content.InfoText = "Unparsed changes! Press parse button to save changes.";
Content.InfoColor = Color.DarkOrange;
}
else {
errorOutput.Text = "";
}
}
private void Content_Changed(object sender, EventArgs e) {
UpdateControl();
}
}
}