1 | #region License Information
|
---|
2 |
|
---|
3 | /* HeuristicLab
|
---|
4 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
5 | *
|
---|
6 | * This file is part of HeuristicLab.
|
---|
7 | *
|
---|
8 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
9 | * it under the terms of the GNU General Public License as published by
|
---|
10 | * the Free Software Foundation, either version 3 of the License, or
|
---|
11 | * (at your option) any later version.
|
---|
12 | *
|
---|
13 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | * GNU General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU General Public License
|
---|
19 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #endregion
|
---|
23 |
|
---|
24 | using System;
|
---|
25 | using System.Collections.Generic;
|
---|
26 | using System.Drawing;
|
---|
27 | using HeuristicLab.Collections;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.MainForm;
|
---|
30 | using HeuristicLab.MainForm.WindowsForms;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
33 | [View("ParsedConstraint View")]
|
---|
34 | [Content(typeof(ShapeConstraints), true)]
|
---|
35 | public partial class ProblemDataConstraintView : AsynchronousContentView {
|
---|
36 | public new ShapeConstraints Content {
|
---|
37 | get => (ShapeConstraints)base.Content;
|
---|
38 | set => base.Content = value;
|
---|
39 | }
|
---|
40 |
|
---|
41 | public ProblemDataConstraintView() {
|
---|
42 | InitializeComponent();
|
---|
43 | errorOutput.Text = "";
|
---|
44 | }
|
---|
45 |
|
---|
46 | protected override void OnContentChanged() {
|
---|
47 | base.OnContentChanged();
|
---|
48 | UpdateControl();
|
---|
49 | }
|
---|
50 |
|
---|
51 | protected override void RegisterContentEvents() {
|
---|
52 | base.RegisterContentEvents();
|
---|
53 | Content.ItemsAdded += constraints_Updated;
|
---|
54 | Content.ItemsRemoved += constraint_removed;
|
---|
55 | Content.Changed += Content_Changed;
|
---|
56 | }
|
---|
57 |
|
---|
58 | protected override void DeregisterContentEvents() {
|
---|
59 | Content.ItemsAdded -= constraints_Updated;
|
---|
60 | Content.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 |
|
---|
71 | private void parseBtn_Click(object sender, EventArgs e) {
|
---|
72 | if (constraintsInput.Text != null) {
|
---|
73 | intervalConstraints.Clear();
|
---|
74 | try {
|
---|
75 | var parsedConstraints = ShapeConstraintsParser.ParseConstraints(constraintsInput.Text);
|
---|
76 | Content.Clear();
|
---|
77 | Content.AddRange(parsedConstraints);
|
---|
78 | InfoText = "Constraints successfully parsed.";
|
---|
79 | InfoColor = Color.DarkGreen;
|
---|
80 | //Catch the exception from the constraints parser and show it in the error dialog
|
---|
81 | } catch (ArgumentException ex) {
|
---|
82 | Content.Clear();
|
---|
83 | InfoText = ex.Message.Replace("Parameter name", "@Line");
|
---|
84 | InfoColor = Color.DarkRed;
|
---|
85 | }
|
---|
86 | } else {
|
---|
87 | errorOutput.Text = "No constraints were found!";
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | private void UpdateControl() {
|
---|
92 | if (Content == null) {
|
---|
93 | intervalConstraints.Clear();
|
---|
94 | intervalConstraintsView.Content = intervalConstraints;
|
---|
95 | } else {
|
---|
96 | intervalConstraints.Clear();
|
---|
97 | constraintsInput.Text = Content.Input;
|
---|
98 | errorOutput.ForeColor = InfoColor;
|
---|
99 | errorOutput.Text = InfoText;
|
---|
100 | foreach (var constraint in Content) intervalConstraints.Add(constraint, constraint.Enabled);
|
---|
101 |
|
---|
102 | intervalConstraintsView.Content = intervalConstraints;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | private void constraint_Changed(object sender, EventArgs e) {
|
---|
107 | var constraint = (ShapeConstraint)sender;
|
---|
108 | intervalConstraints.SetItemCheckedState(constraint, constraint.Enabled);
|
---|
109 | }
|
---|
110 |
|
---|
111 | private void constraints_Updated(object sender,
|
---|
112 | CollectionItemsChangedEventArgs<IndexedItem<ShapeConstraint>> e) {
|
---|
113 | foreach (var addedItem in e.Items) addedItem.Value.Changed += constraint_Changed;
|
---|
114 | }
|
---|
115 |
|
---|
116 | private void constraint_removed(object sender, CollectionItemsChangedEventArgs<IndexedItem<ShapeConstraint>> e) {
|
---|
117 | foreach (var removedItem in e.Items) removedItem.Value.Changed -= constraint_Changed;
|
---|
118 | }
|
---|
119 |
|
---|
120 | private void constraint_CheckedItemChanged(object sender,
|
---|
121 | CollectionItemsChangedEventArgs<IndexedItem<ShapeConstraint>> e) {
|
---|
122 | ICheckedItemList<ShapeConstraint> checkedItemList = (ICheckedItemList<ShapeConstraint>)sender;
|
---|
123 | foreach (var indexedItem in e.Items) indexedItem.Value.Enabled = checkedItemList.ItemChecked(indexedItem.Value);
|
---|
124 | }
|
---|
125 |
|
---|
126 | private void constraintsInput_TextChanged(object sender, EventArgs e) {
|
---|
127 | if (Content.Input != constraintsInput.Text) {
|
---|
128 | Content.Input = constraintsInput.Text;
|
---|
129 | Content.InfoText = "Unparsed changes! Press parse button to save changes.";
|
---|
130 | Content.InfoColor = Color.DarkOrange;
|
---|
131 | } else {
|
---|
132 | errorOutput.Text = "";
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | private void Content_Changed(object sender, EventArgs e) {
|
---|
137 | UpdateControl();
|
---|
138 | }
|
---|
139 | }
|
---|
140 | } |
---|