source: branches/3073_IA_constraint_splitting_reintegration/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/ShapeConstraintsView.cs @ 17896

Last change on this file since 17896 was 17896, checked in by gkronber, 17 months ago

#3073: refactoring to prepare for trunk integration

File size: 6.3 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 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
24using System;
25using System.Drawing;
26using System.Text;
27using System.Windows.Forms;
28using HeuristicLab.Collections;
29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
31
32namespace HeuristicLab.Problems.DataAnalysis.Views {
33  [View("ShapeConstraints View")]
34  [Content(typeof(ShapeConstraints), true)]
35  public partial class ShapeConstraintsView : AsynchronousContentView {
36    public new ShapeConstraints Content {
37      get => (ShapeConstraints)base.Content;
38      set => base.Content = value;
39    }
40
41    public bool suspendUpdates = false;
42
43    public ShapeConstraintsView() {
44      InitializeComponent();
45      errorOutput.Text = "";
46    }
47
48    protected override void OnContentChanged() {
49      base.OnContentChanged();
50      this.shapeConstraintsView.Content = Content;
51      UpdateControl();
52    }
53
54    protected override void RegisterContentEvents() {
55      base.RegisterContentEvents();
56      Content.ItemsAdded += constraints_Added;
57      Content.ItemsRemoved += constraint_Removed;
58      Content.Changed += Content_Changed;
59      Content.CollectionReset += constraints_Reset;
60      Content.ItemsMoved += constraints_Moved;
61      Content.ItemsReplaced += Content_ItemsReplaced;
62      Content.CheckedItemsChanged += Content_CheckedItemsChanged;
63    }
64
65
66    protected override void DeregisterContentEvents() {
67      Content.ItemsAdded -= constraints_Added;
68      Content.ItemsRemoved -= constraint_Removed;
69      Content.Changed -= Content_Changed;
70      Content.CollectionReset -= constraints_Reset;
71      Content.ItemsMoved -= constraints_Moved;
72      Content.ItemsReplaced -= Content_ItemsReplaced;
73      Content.CheckedItemsChanged -= Content_CheckedItemsChanged;
74      base.DeregisterContentEvents();
75    }
76
77    protected override void SetEnabledStateOfControls() {
78      constraintsInput.Enabled = Content != null && !Locked && !ReadOnly;
79    }
80
81
82    private void parseBtn_Click(object sender, EventArgs e) {
83      if (constraintsInput.Text != null) {
84        suspendUpdates = true;
85        Content.Clear();
86        try {
87          var parsedConstraints = ShapeConstraintsParser.ParseConstraints(constraintsInput.Text);
88          Content.AddRange(parsedConstraints);
89          errorOutput.Text = "Constraints successfully parsed.";
90          errorOutput.ForeColor = Color.DarkGreen;
91        } catch (ArgumentException ex) {
92          errorOutput.Text = ex.Message.Replace("Parameter name", "@Line");
93          errorOutput.ForeColor = Color.DarkRed;
94        } finally {
95          suspendUpdates = false;
96        }
97      } else {
98        errorOutput.Text = "No constraints were found!";
99      }
100    }
101
102    private void UpdateControl() {
103      if (suspendUpdates) return;
104      if (Content == null) {
105        constraintsInput.Text = string.Empty;
106      } else {
107        var newText = ToString(Content);
108        if (newText != constraintsInput.Text)
109          constraintsInput.Text = newText;
110      }
111    }
112
113    private string ToString(ShapeConstraints constraints) {
114      var sb = new StringBuilder();
115      foreach (var constraint in constraints) {
116        if (!constraints.ItemChecked(constraint)) {
117          sb.Append("# ").AppendLine(constraint.ToString());
118        } else {
119          sb.AppendLine(constraint.ToString());
120        }
121      }
122      return sb.ToString();
123    }
124
125    private void constraint_Changed(object sender, EventArgs e) {
126      UpdateControl();
127    }
128
129    private void constraints_Added(object sender,
130                                     CollectionItemsChangedEventArgs<IndexedItem<ShapeConstraint>> e) {
131      foreach (var addedItem in e.Items) addedItem.Value.Changed += constraint_Changed;
132      UpdateControl();
133    }
134
135    private void constraint_Removed(object sender, CollectionItemsChangedEventArgs<IndexedItem<ShapeConstraint>> e) {
136      foreach (var removedItem in e.Items) removedItem.Value.Changed -= constraint_Changed;
137      UpdateControl();
138    }
139
140    private void constraints_Moved(object sender, CollectionItemsChangedEventArgs<IndexedItem<ShapeConstraint>> e) {
141      UpdateControl();
142    }
143
144    private void constraints_Reset(object sender, CollectionItemsChangedEventArgs<IndexedItem<ShapeConstraint>> e) {
145      foreach (var addedItem in e.Items) addedItem.Value.Changed += constraint_Changed;
146      foreach (var removedItem in e.OldItems) removedItem.Value.Changed -= constraint_Changed;
147      UpdateControl();
148    }
149
150    private void Content_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<ShapeConstraint>> e) {
151      UpdateControl();
152    }
153
154    private void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<ShapeConstraint>> e) {
155      foreach (var addedItem in e.Items) addedItem.Value.Changed += constraint_Changed;
156      foreach (var removedItem in e.OldItems) removedItem.Value.Changed -= constraint_Changed;
157      UpdateControl();
158    }
159
160    private void constraintsInput_TextChanged(object sender, EventArgs e) {
161      errorOutput.Text = "Unparsed changes! Press parse button to save changes.";
162      errorOutput.ForeColor = Color.DarkOrange;
163    }
164
165    private void Content_Changed(object sender, EventArgs e) {
166      UpdateControl();
167    }
168
169    private void helpButton_DoubleClick(object sender, EventArgs e) {
170      using (InfoBox dialog = new InfoBox("Help for shape constraints",
171        "HeuristicLab.Problems.DataAnalysis.Views.Resources.shapeConstraintsHelp.rtf",
172        this)) {
173        dialog.ShowDialog(this);
174      }
175    }
176  }
177}
Note: See TracBrowser for help on using the repository browser.