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

Last change on this file since 16963 was 16963, checked in by chaider, 4 months ago

#2971 Added OnTextChange event for constraints input; Added lock of constraints input

File size: 5.1 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      Content.Changed += Content_Changed;
55    }
56
57    protected override void DeregisterContentEvents() {
58      intervalConstraints.CheckedItemsChanged -= constraint_CheckedItemChanged;
59      intervalConstraints.ItemsAdded -= constraints_Updated;
60      intervalConstraints.ItemsRemoved -= constraint_removed;
61      Content.Changed -= Content_Changed;
62      base.DeregisterContentEvents();
63    }
64
65    protected override void SetEnabledStateOfControls() {
66      constraintsInput.Enabled = Content != null && !Locked && !ReadOnly;
67    }
68
69
70    private IEnumerable<IntervalConstraint> ParseConstraints(string input) {
71      return IntervalConstraintsParser.ParseInput(input, Content.ProblemData.TargetVariable,
72        Content.ProblemData.AllowedInputVariables);
73    }
74
75    private void parseBtn_Click(object sender, EventArgs e) {
76      if (constraintsInput.Text != null) {
77        intervalConstraints.Clear();
78        var parsedConstraints = ParseConstraints(constraintsInput.Text);
79        Content.Constraints = parsedConstraints;
80        Content.Input = constraintsInput.Text;
81        errorOutput.Text = "";
82      } else {
83        errorOutput.Text = "No constraints were found!";
84      }
85    }
86
87    private void UpdateControl() {
88      if (Content == null) {
89        intervalConstraints.Clear();
90        constraintsOutput.Content = intervalConstraints;
91      } else {
92        intervalConstraints.Clear();
93        constraintsInput.Text = Content.Input;
94        foreach (var constraint in Content.Constraints) {
95          intervalConstraints.Add(constraint, constraint.Enabled);
96        }
97
98        constraintsOutput.Content = intervalConstraints;
99      }
100    }
101
102    private void constraint_Changed(object sender, EventArgs e) {
103      var constraint = (IntervalConstraint) sender;
104      intervalConstraints.SetItemCheckedState(constraint, constraint.Enabled);
105    }
106
107    private void constraints_Updated(object sender, CollectionItemsChangedEventArgs<IndexedItem<IntervalConstraint>> e) {
108      foreach (var addedItem in e.Items) {
109        addedItem.Value.Changed += constraint_Changed;
110      }
111    }
112
113    private void constraint_removed(object sender, CollectionItemsChangedEventArgs<IndexedItem<IntervalConstraint>> e) {
114      foreach (var removedItem in e.Items) {
115        removedItem.Value.Changed -= constraint_Changed;
116      }
117    }
118
119    private void constraint_CheckedItemChanged(object sender,
120      CollectionItemsChangedEventArgs<IndexedItem<IntervalConstraint>> e) {
121      ICheckedItemList<IntervalConstraint> checkedItemList = (ICheckedItemList<IntervalConstraint>) sender;
122      foreach (var indexedItem in e.Items) {
123        indexedItem.Value.Enabled = checkedItemList.ItemChecked(indexedItem.Value);
124      }
125    }
126
127    private void constraintsInput_TextChanged(object sender, EventArgs e) {
128      errorOutput.Text = "Unparsed changes! Press parse button to save changes.";
129      Content.Input = constraintsInput.Text;
130    }
131
132    private void Content_Changed(object sender, EventArgs e) {
133      UpdateControl();
134    }
135
136  }
137}
Note: See TracBrowser for help on using the repository browser.