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

Last change on this file since 16777 was 16777, checked in by chaider, 11 months ago

#2971
-Fixed Dispose problem
-Added event handling for item selection

File size: 3.4 KB
Line 
1using System;
2using HeuristicLab.Collections;
3using HeuristicLab.Core;
4using HeuristicLab.MainForm;
5using HeuristicLab.MainForm.WindowsForms;
6
7namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
8  [View("ParsedConstraint View")]
9  [Content(typeof(ParsedConstraint), true)]
10  public partial class ParsedConstraintView : AsynchronousContentView {
11    private CheckedItemList<IntervalConstraint> intervalConstraints;
12    public new ParsedConstraint Content {
13      get => (ParsedConstraint)base.Content;
14      set => base.Content = value;
15    }
16
17    public ParsedConstraintView() {
18      InitializeComponent();
19      errorOutput.Text = "";
20      intervalConstraints = new CheckedItemList<IntervalConstraint>();
21      intervalConstraints.CheckedItemsChanged += constraints_CheckedItemsChanged;
22    }
23
24    protected override void OnContentChanged() {
25      base.OnContentChanged();
26      UpdateControl();
27    }
28
29    private CheckedItemList<IntervalConstraint> ParseConstraints(string input) {
30      CheckedItemList<IntervalConstraint> constraints = new CheckedItemList<IntervalConstraint>(IntervalConstraintsParser.ParseInput(input, Content.ProblemData.TargetVariable, Content.ProblemData.AllowedInputVariables));
31      return constraints;
32    }
33
34    private void parseBtn_Click(object sender, EventArgs e) {
35      if (constraintsInput.Text != null) {
36        intervalConstraints = ParseConstraints(constraintsInput.Text);
37        intervalConstraints.CheckedItemsChanged += constraints_CheckedItemsChanged;
38        constraintsOutput.Content = intervalConstraints;
39        Content.Constraints = intervalConstraints;
40        Content.Input = constraintsInput.Text;
41        errorOutput.Text = "";
42      } else {
43        errorOutput.Text = "No constraints were found!";
44      }
45    }
46
47    private void UpdateControl() {
48      if (Content == null) {
49        ClearConstraints();
50        constraintsInput.Text = string.Empty;
51        errorOutput.Text = string.Empty;
52        constraintsOutput.Content = intervalConstraints;
53      } else {
54        constraintsInput.Text = Content.Input;
55        foreach (var constraint in Content.Constraints) {
56          constraint.Changed += new EventHandler(Content_Changed);
57          intervalConstraints.Add(constraint, constraint.IsChecked);
58        }
59      }
60      constraintsOutput.Content = intervalConstraints;
61    }
62
63    private void constraintsInput_TextChanged(object sender, EventArgs e) {
64      errorOutput.Text = "Unparsed changes! Press parse button to save changes.";
65    }
66
67    private void Content_Changed(object sender, EventArgs e) {
68      var constraint = (IntervalConstraint) sender;
69      intervalConstraints.SetItemCheckedState(constraint, constraint.IsChecked);
70      constraintsOutput.Content = intervalConstraints;
71    }
72
73    private void constraints_CheckedItemsChanged(object sender,
74      CollectionItemsChangedEventArgs<IndexedItem<IntervalConstraint>> e) {
75      var checkedItemList = (ICheckedItemList<IntervalConstraint>) sender;
76      foreach (var indexedItem in e.Items) {
77        indexedItem.Value.IsChecked = checkedItemList.ItemChecked(indexedItem.Value);
78      }
79    }
80
81    private void ClearConstraints() {
82      foreach (var constraint in intervalConstraints) {
83        constraint.Changed -= new EventHandler(Content_Changed);
84      }
85      intervalConstraints.Clear();
86    }
87
88  }
89}
Note: See TracBrowser for help on using the repository browser.