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

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

#2971 Changed some views

File size: 4.3 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      //intervalConstraints.ItemsRemoved
44    }
45
46    protected override void OnContentChanged() {
47      base.OnContentChanged();
48      UpdateControl();
49    }
50    //ParameterCollectionView
51    //Remove method directly at parsebutton event
52    private IEnumerable<IntervalConstraint> ParseConstraints(string input) {
53      return IntervalConstraintsParser.ParseInput(input, Content.ProblemData.TargetVariable,
54        Content.ProblemData.AllowedInputVariables);
55    }
56
57    private void parseBtn_Click(object sender, EventArgs e) {
58      if (constraintsInput.Text != null) {
59        ClearConstraints();
60        var parsedConstraints = ParseConstraints(constraintsInput.Text);
61        foreach (var constraint in parsedConstraints) {
62          constraint.Changed += constraint_Changed;
63          intervalConstraints.Add(constraint, constraint.Enabled);
64        }
65        constraintsOutput.Content = intervalConstraints;
66        Content.Constraints = intervalConstraints;
67        Content.Input = constraintsInput.Text;
68        errorOutput.Text = "";
69      } else {
70        errorOutput.Text = "No constraints were found!";
71      }
72    }
73
74    private void UpdateControl() {
75      if (Content == null) {
76        ClearConstraints();
77        constraintsOutput.Content = intervalConstraints.AsReadOnly();
78      } else {
79        ClearConstraints();
80        constraintsInput.Text = Content.Input;
81        foreach (var constraint in Content.Constraints) {
82          constraint.Changed += constraint_Changed;
83          intervalConstraints.Add(constraint, constraint.Enabled);
84        }
85
86        constraintsOutput.Content = intervalConstraints.AsReadOnly();
87      }
88    }
89
90    private void constraint_Changed(object sender, EventArgs e) {
91      var constraint = (IntervalConstraint) sender;
92      intervalConstraints.SetItemCheckedState(constraint, constraint.Enabled);
93    }
94
95    private void constraint_CheckedItemChanged(object sender,
96      CollectionItemsChangedEventArgs<IndexedItem<IntervalConstraint>> e) {
97      ICheckedItemList<IntervalConstraint> checkedItemList = (ICheckedItemList<IntervalConstraint>) sender;
98      foreach (var indexedItem in e.Items) {
99        indexedItem.Value.Enabled = checkedItemList.ItemChecked(indexedItem.Value);
100      }
101    }
102
103    private void constraintsInput_TextChanged(object sender, EventArgs e) {
104      errorOutput.Text = "Unparsed changes! Press parse button to save changes.";
105    }
106
107    private void ClearConstraints() {
108      foreach (var constraint in intervalConstraints) {
109        constraint.Changed -= constraint_Changed;
110      }
111      intervalConstraints.Clear();
112    }
113
114  }
115}
Note: See TracBrowser for help on using the repository browser.