Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16936 was 16918, checked in by chaider, 6 years ago

#2971

  • changed VariableIntervals from ObservableDictionary to IDictionary
  • Added methods to get single intervals and all intervals
File size: 4.9 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(ProblemDataConstraint), true)]
31  public partial class ParsedConstraintView : AsynchronousContentView {
32    private CheckedItemList<IntervalConstraint> intervalConstraints;
33    public new ProblemDataConstraint Content {
34      get => (ProblemDataConstraint)base.Content;
35      set => base.Content = value;
36    }
37
38    public ParsedConstraintView() {
39      InitializeComponent();
40      errorOutput.Text = "";
41      intervalConstraints = new CheckedItemList<IntervalConstraint>();
42    }
43
44    protected override void OnContentChanged() {
45      base.OnContentChanged();
46      UpdateControl();
47    }
48
49    protected override void RegisterContentEvents() {
50      base.RegisterContentEvents();
51      intervalConstraints.CheckedItemsChanged += constraint_CheckedItemChanged;
52      intervalConstraints.ItemsAdded += constraints_Updated;
53      intervalConstraints.ItemsRemoved += constraint_removed;
54    }
55
56    protected override void DeregisterContentEvents() {
57      intervalConstraints.CheckedItemsChanged -= constraint_CheckedItemChanged;
58      intervalConstraints.ItemsAdded -= constraints_Updated;
59      intervalConstraints.ItemsRemoved -= constraint_removed;
60      base.DeregisterContentEvents();
61    }
62
63
64    private IEnumerable<IntervalConstraint> ParseConstraints(string input) {
65      return IntervalConstraintsParser.ParseInput(input, Content.ProblemData.TargetVariable,
66        Content.ProblemData.AllowedInputVariables);
67    }
68
69    private void parseBtn_Click(object sender, EventArgs e) {
70      if (constraintsInput.Text != null) {
71        intervalConstraints.Clear();
72        var parsedConstraints = ParseConstraints(constraintsInput.Text);
73        foreach (var constraint in parsedConstraints) {
74          intervalConstraints.Add(constraint, constraint.Enabled);
75        }
76        constraintsOutput.Content = intervalConstraints;
77        Content.Constraints = intervalConstraints;
78        Content.Input = constraintsInput.Text;
79        errorOutput.Text = "";
80      } else {
81        errorOutput.Text = "No constraints were found!";
82      }
83    }
84
85    private void UpdateControl() {
86      if (Content == null) {
87        intervalConstraints.Clear();
88        constraintsOutput.Content = intervalConstraints.AsReadOnly();
89      } else {
90        intervalConstraints.Clear();
91        constraintsInput.Text = Content.Input;
92        foreach (var constraint in Content.Constraints) {
93          intervalConstraints.Add(constraint, constraint.Enabled);
94        }
95
96        constraintsOutput.Content = intervalConstraints.AsReadOnly();
97      }
98    }
99
100    private void constraint_Changed(object sender, EventArgs e) {
101      var constraint = (IntervalConstraint) sender;
102      intervalConstraints.SetItemCheckedState(constraint, constraint.Enabled);
103    }
104
105    private void constraints_Updated(object sender, CollectionItemsChangedEventArgs<IndexedItem<IntervalConstraint>> e) {
106      foreach (var addedItem in e.Items) {
107        addedItem.Value.Changed += constraint_Changed;
108      }
109    }
110
111    private void constraint_removed(object sender, CollectionItemsChangedEventArgs<IndexedItem<IntervalConstraint>> e) {
112      foreach (var removedItem in e.Items) {
113        removedItem.Value.Changed -= constraint_Changed;
114      }
115    }
116
117    private void constraint_CheckedItemChanged(object sender,
118      CollectionItemsChangedEventArgs<IndexedItem<IntervalConstraint>> e) {
119      ICheckedItemList<IntervalConstraint> checkedItemList = (ICheckedItemList<IntervalConstraint>) sender;
120      foreach (var indexedItem in e.Items) {
121        indexedItem.Value.Enabled = checkedItemList.ItemChecked(indexedItem.Value);
122      }
123    }
124
125    private void constraintsInput_TextChanged(object sender, EventArgs e) {
126      errorOutput.Text = "Unparsed changes! Press parse button to save changes.";
127    }
128
129  }
130}
Note: See TracBrowser for help on using the repository browser.