source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Views/3.4/ProblemDataConstraintView.cs @ 17146

Last change on this file since 17146 was 17146, checked in by mkommend, 2 months ago

#2971: Renamed views and updated error messages in constraint parser.

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