source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Views/3.4/ParsedConstraintView.cs @ 16830

Last change on this file since 16830 was 16830, checked in by chaider, 5 months ago

#2971 Fixed review comments

File size: 4.2 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.Collections.Generic;
23using HeuristicLab.Collections;
24using HeuristicLab.Core;
25using HeuristicLab.MainForm;
26using HeuristicLab.MainForm.WindowsForms;
27
28namespace HeuristicLab.Problems.DataAnalysis.Views {
29  [View("ParsedConstraint View")]
30  [Content(typeof(ParsedConstraint), true)]
31  public partial class ParsedConstraintView : AsynchronousContentView {
32    private CheckedItemList<IntervalConstraint> intervalConstraints;
33    public new ParsedConstraint Content {
34      get => (ParsedConstraint)base.Content;
35      set => base.Content = value;
36    }
37
38    public ParsedConstraintView() {
39      InitializeComponent();
40      errorOutput.Text = "";
41      intervalConstraints = new CheckedItemList<IntervalConstraint>();
42      intervalConstraints.CheckedItemsChanged += constraint_CheckedItemChanged;
43    }
44
45    protected override void OnContentChanged() {
46      base.OnContentChanged();
47      UpdateControl();
48    }
49
50    private IEnumerable<IntervalConstraint> ParseConstraints(string input) {
51      return IntervalConstraintsParser.ParseInput(input, Content.ProblemData.TargetVariable,
52        Content.ProblemData.AllowedInputVariables);
53    }
54
55    private void parseBtn_Click(object sender, EventArgs e) {
56      if (constraintsInput.Text != null) {
57        ClearConstraints();
58        var parsedConstraints = ParseConstraints(constraintsInput.Text);
59        foreach (var constraint in parsedConstraints) {
60          constraint.Changed += constraint_Changed;
61          intervalConstraints.Add(constraint, constraint.Enabled);
62        }
63        constraintsOutput.Content = intervalConstraints;
64        Content.Constraints = intervalConstraints;
65        Content.Input = constraintsInput.Text;
66        errorOutput.Text = "";
67      } else {
68        errorOutput.Text = "No constraints were found!";
69      }
70    }
71
72    private void UpdateControl() {
73      if (Content == null) {
74        ClearConstraints();
75        constraintsOutput.Content = intervalConstraints.AsReadOnly();
76      } else {
77        ClearConstraints();
78        constraintsInput.Text = Content.Input;
79        foreach (var constraint in Content.Constraints) {
80          constraint.Changed += constraint_Changed;
81          intervalConstraints.Add(constraint, constraint.Enabled);
82        }
83
84        constraintsOutput.Content = intervalConstraints.AsReadOnly();
85      }
86      SetEnabledStateOfControls();
87    }
88
89    private void constraint_Changed(object sender, EventArgs e) {
90      var constraint = (IntervalConstraint) sender;
91      intervalConstraints.SetItemCheckedState(constraint, constraint.Enabled);
92    }
93
94    private void constraint_CheckedItemChanged(object sender,
95      CollectionItemsChangedEventArgs<IndexedItem<IntervalConstraint>> e) {
96      ICheckedItemList<IntervalConstraint> checkedItemList = (ICheckedItemList<IntervalConstraint>) sender;
97      foreach (var indexedItem in e.Items) {
98        indexedItem.Value.Enabled = checkedItemList.ItemChecked(indexedItem.Value);
99      }
100    }
101
102    private void constraintsInput_TextChanged(object sender, EventArgs e) {
103      errorOutput.Text = "Unparsed changes! Press parse button to save changes.";
104    }
105
106    private void ClearConstraints() {
107      foreach (var constraint in intervalConstraints) {
108        constraint.Changed -= constraint_Changed;
109      }
110      intervalConstraints.Clear();
111    }
112
113  }
114}
Note: See TracBrowser for help on using the repository browser.