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

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

#2971: Corrected view name of ProblemDataConstraintView.

File size: 5.8 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 System.Drawing;
24using HeuristicLab.Collections;
25using HeuristicLab.Core;
26using HeuristicLab.MainForm;
27using HeuristicLab.MainForm.WindowsForms;
28
29namespace HeuristicLab.Problems.DataAnalysis.Views {
30  [View("ProblemDataConstraint View")]
31  [Content(typeof(ProblemDataConstraint), true)]
32  public partial class ProblemDataConstraintView : AsynchronousContentView {
33    private readonly CheckedItemList<IntervalConstraint> intervalConstraints = new CheckedItemList<IntervalConstraint>();
34    public new ProblemDataConstraint Content {
35      get => (ProblemDataConstraint)base.Content;
36      set => base.Content = value;
37    }
38
39    public ProblemDataConstraintView() {
40      InitializeComponent();
41      errorOutput.Text = "";
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        try {
79          var parsedConstraints = ParseConstraints(constraintsInput.Text);
80          Content.Constraints = parsedConstraints;
81          Content.Input = constraintsInput.Text;
82          Content.InfoText = "Constraints successfully parsed.";
83          Content.InfoColor = Color.DarkGreen;
84          //Catch the exception from the constraints parser and show it in the error dialog
85        } catch(ArgumentException ex) {
86          Content.Constraints = new List<IntervalConstraint>();
87          Content.InfoText = ex.Message.Replace("Parameter name", "@Line");
88          Content.InfoColor = Color.DarkRed;
89        }
90      } else {
91        errorOutput.Text = "No constraints were found!";
92      }
93    }
94
95    private void UpdateControl() {
96      if (Content == null) {
97        intervalConstraints.Clear();
98        intervalConstraintsView.Content = intervalConstraints;
99      } else {
100        intervalConstraints.Clear();
101        constraintsInput.Text = Content.Input;
102        errorOutput.ForeColor = Content.InfoColor;
103        errorOutput.Text = Content.InfoText;
104        foreach (var constraint in Content.Constraints) {
105          intervalConstraints.Add(constraint, constraint.Enabled);
106        }
107
108        intervalConstraintsView.Content = intervalConstraints;
109      }
110    }
111
112    private void constraint_Changed(object sender, EventArgs e) {
113      var constraint = (IntervalConstraint)sender;
114      intervalConstraints.SetItemCheckedState(constraint, constraint.Enabled);
115    }
116
117    private void constraints_Updated(object sender, CollectionItemsChangedEventArgs<IndexedItem<IntervalConstraint>> e) {
118      foreach (var addedItem in e.Items) {
119        addedItem.Value.Changed += constraint_Changed;
120      }
121    }
122
123    private void constraint_removed(object sender, CollectionItemsChangedEventArgs<IndexedItem<IntervalConstraint>> e) {
124      foreach (var removedItem in e.Items) {
125        removedItem.Value.Changed -= constraint_Changed;
126      }
127    }
128
129    private void constraint_CheckedItemChanged(object sender,
130      CollectionItemsChangedEventArgs<IndexedItem<IntervalConstraint>> e) {
131      ICheckedItemList<IntervalConstraint> checkedItemList = (ICheckedItemList<IntervalConstraint>)sender;
132      foreach (var indexedItem in e.Items) {
133        indexedItem.Value.Enabled = checkedItemList.ItemChecked(indexedItem.Value);
134      }
135    }
136
137    private void constraintsInput_TextChanged(object sender, EventArgs e) {
138      if (Content.Input != constraintsInput.Text) {
139       Content.Input = constraintsInput.Text;
140       Content.InfoText = "Unparsed changes! Press parse button to save changes.";
141       Content.InfoColor = Color.DarkOrange;
142      } else {
143        errorOutput.Text = "";
144      }
145
146    }
147
148    private void Content_Changed(object sender, EventArgs e) {
149      UpdateControl();
150    }
151
152  }
153}
Note: See TracBrowser for help on using the repository browser.