Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Symbols/VariableConditionView.cs @ 7967

Last change on this file since 7967 was 7869, checked in by sforsten, 12 years ago

#1821: it is now possible to select variables for the symbol VariableCondition

File size: 11.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
21
22using System;
23using System.Linq;
24using System.Windows.Forms;
25using HeuristicLab.Collections;
26using HeuristicLab.Core;
27using HeuristicLab.Core.Views;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
30using HeuristicLab.MainForm;
31using HeuristicLab.MainForm.WindowsForms;
32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
34  [View("Variable View")]
35  [Content(typeof(VariableCondition), true)]
36  public partial class VariableConditionView : SymbolView {
37    private CheckedItemCollectionView<StringValue> variableNamesView;
38
39    public new VariableCondition Content {
40      get { return (VariableCondition)base.Content; }
41      set { base.Content = value; }
42    }
43
44    public VariableConditionView() {
45      InitializeComponent();
46      variableNamesView = new CheckedItemCollectionView<StringValue>();
47      variableNamesView.Dock = DockStyle.Fill;
48      variableNamesTabPage.Controls.Add(variableNamesView);
49      variableNamesView.Content = new CheckedItemCollection<StringValue>();
50
51      RegisterVariableNamesViewContentEvents();
52    }
53
54    private void RegisterVariableNamesViewContentEvents() {
55      variableNamesView.Content.ItemsAdded += new CollectionItemsChangedEventHandler<StringValue>(variableNames_Changed);
56      variableNamesView.Content.ItemsRemoved += new CollectionItemsChangedEventHandler<StringValue>(variableNames_Changed);
57      variableNamesView.Content.CheckedItemsChanged += new CollectionItemsChangedEventHandler<StringValue>(variableNames_Changed);
58      variableNamesView.Content.CollectionReset += new CollectionItemsChangedEventHandler<StringValue>(variableNames_Changed);
59      foreach (var variable in variableNamesView.Content) {
60        variable.ValueChanged += new EventHandler(variable_ValueChanged);
61      }
62    }
63
64    private void DeregisterVariableNamesViewContentEvents() {
65      variableNamesView.Content.ItemsAdded -= new CollectionItemsChangedEventHandler<StringValue>(variableNames_Changed);
66      variableNamesView.Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<StringValue>(variableNames_Changed);
67      variableNamesView.Content.CheckedItemsChanged -= new CollectionItemsChangedEventHandler<StringValue>(variableNames_Changed);
68      variableNamesView.Content.CollectionReset -= new CollectionItemsChangedEventHandler<StringValue>(variableNames_Changed);
69      foreach (var variable in variableNamesView.Content) {
70        variable.ValueChanged -= new EventHandler(variable_ValueChanged);
71      }
72    }
73
74    protected override void RegisterContentEvents() {
75      base.RegisterContentEvents();
76      Content.Changed += new EventHandler(Content_Changed);
77    }
78    protected override void DeregisterContentEvents() {
79      base.DeregisterContentEvents();
80      Content.Changed -= new EventHandler(Content_Changed);
81    }
82    protected override void OnContentChanged() {
83      base.OnContentChanged();
84      UpdateControl();
85    }
86    private void Content_Changed(object sender, EventArgs e) {
87      UpdateControl();
88    }
89
90    protected override void SetEnabledStateOfControls() {
91      base.SetEnabledStateOfControls();
92      thresholdInitializationMuTextBox.Enabled = Content != null;
93      thresholdInitializationMuTextBox.ReadOnly = ReadOnly;
94      thresholdInitializationSigmaTextBox.Enabled = Content != null;
95      thresholdInitializationSigmaTextBox.ReadOnly = ReadOnly;
96      slopeInitializationMuTextBox.Enabled = Content != null;
97      slopeInitializationMuTextBox.ReadOnly = ReadOnly;
98      slopeInitializationSigmaTextBox.Enabled = Content != null;
99      slopeInitializationSigmaTextBox.ReadOnly = ReadOnly;
100
101      thresholdChangeMuTextBox.Enabled = Content != null;
102      thresholdChangeMuTextBox.ReadOnly = ReadOnly;
103      thresholdChangeSigmaTextBox.Enabled = Content != null;
104      thresholdChangeSigmaTextBox.ReadOnly = ReadOnly;
105      slopeChangeMuTextBox.Enabled = Content != null;
106      slopeChangeMuTextBox.ReadOnly = ReadOnly;
107      slopeChangeSigmaTextBox.Enabled = Content != null;
108      slopeChangeSigmaTextBox.ReadOnly = ReadOnly;
109    }
110
111    private void UpdateControl() {
112      if (Content == null) {
113        thresholdInitializationMuTextBox.Text = string.Empty;
114        thresholdInitializationSigmaTextBox.Text = string.Empty;
115        slopeInitializationMuTextBox.Text = string.Empty;
116        slopeInitializationSigmaTextBox.Text = string.Empty;
117        thresholdChangeMuTextBox.Text = string.Empty;
118        thresholdChangeSigmaTextBox.Text = string.Empty;
119        slopeChangeMuTextBox.Text = string.Empty;
120        slopeChangeSigmaTextBox.Text = string.Empty;
121        // temporarily deregister to prevent circular calling of events
122        DeregisterVariableNamesViewContentEvents();
123        variableNamesView.Content.Clear();
124        RegisterVariableNamesViewContentEvents();
125      } else {
126        var existingEntries = variableNamesView.Content.ToList();
127
128        // temporarily deregister to prevent circular calling of events
129        DeregisterVariableNamesViewContentEvents();
130        // add additional entries
131        foreach (var variableName in Content.VariableNames.Except(existingEntries.Select(x => x.Value)))
132          variableNamesView.Content.Add(new StringValue(variableName), true);
133        foreach (var oldEntry in existingEntries.Where(x => !Content.VariableNames.Contains(x.Value)))
134          variableNamesView.Content.Remove(oldEntry);
135        RegisterVariableNamesViewContentEvents();
136
137        thresholdInitializationMuTextBox.Text = Content.ThresholdInitializerMu.ToString();
138        thresholdInitializationSigmaTextBox.Text = Content.ThresholdInitializerSigma.ToString();
139        slopeInitializationMuTextBox.Text = Content.SlopeInitializerMu.ToString();
140        slopeInitializationSigmaTextBox.Text = Content.SlopeInitializerSigma.ToString();
141        thresholdChangeMuTextBox.Text = Content.ThresholdManipulatorMu.ToString();
142        thresholdChangeSigmaTextBox.Text = Content.ThresholdManipulatorSigma.ToString();
143        slopeChangeMuTextBox.Text = Content.SlopeManipulatorMu.ToString();
144        slopeChangeSigmaTextBox.Text = Content.SlopeManipulatorSigma.ToString();
145      }
146      SetEnabledStateOfControls();
147    }
148
149    #region control events
150    private void variableNames_Changed(object sender, CollectionItemsChangedEventArgs<StringValue> args) {
151      if (args.Items != null)
152        foreach (var newVar in args.Items)
153          newVar.ValueChanged += new EventHandler(variable_ValueChanged);
154      if (args.OldItems != null)
155        foreach (var oldVar in args.OldItems)
156          oldVar.ValueChanged -= new EventHandler(variable_ValueChanged);
157      UpdateContent();
158    }
159
160    private void variable_ValueChanged(object sender, EventArgs e) {
161      UpdateContent();
162    }
163
164    private void UpdateContent() {
165      if (Content != null) {
166        DeregisterContentEvents();
167        Content.VariableNames = variableNamesView.Content.CheckedItems.Select(x => x.Value).ToList();
168        RegisterContentEvents();
169      }
170    }
171
172    private void thresholdMuTextBox_TextChanged(object sender, EventArgs e) {
173      double value;
174      if (double.TryParse(thresholdInitializationMuTextBox.Text, out value)) {
175        Content.ThresholdInitializerMu = value;
176        errorProvider.SetError(thresholdInitializationMuTextBox, string.Empty);
177      } else errorProvider.SetError(thresholdInitializationMuTextBox, "Invalid value");
178    }
179    private void thresholdInitializationSigmaTextBox_TextChanged(object sender, EventArgs e) {
180      double value;
181      if (double.TryParse(thresholdInitializationSigmaTextBox.Text, out value)) {
182        Content.ThresholdInitializerSigma = value;
183        errorProvider.SetError(thresholdInitializationSigmaTextBox, string.Empty);
184      } else errorProvider.SetError(thresholdInitializationSigmaTextBox, "Invalid value");
185    }
186    private void slopeInitializationMuTextBox_TextChanged(object sender, EventArgs e) {
187      double value;
188      if (double.TryParse(slopeInitializationMuTextBox.Text, out value)) {
189        Content.SlopeInitializerMu = value;
190        errorProvider.SetError(slopeInitializationMuTextBox, string.Empty);
191      } else errorProvider.SetError(slopeInitializationMuTextBox, "Invalid value");
192    }
193    private void slopeInitializationSigmaTextBox_TextChanged(object sender, EventArgs e) {
194      double value;
195      if (double.TryParse(slopeInitializationSigmaTextBox.Text, out value)) {
196        Content.SlopeInitializerSigma = value;
197        errorProvider.SetError(slopeInitializationSigmaTextBox, string.Empty);
198      } else errorProvider.SetError(slopeInitializationSigmaTextBox, "Invalid value");
199    }
200
201    private void thresholdChangeMuTextBox_TextChanged(object sender, EventArgs e) {
202      double value;
203      if (double.TryParse(thresholdChangeMuTextBox.Text, out value)) {
204        Content.ThresholdManipulatorMu = value;
205        errorProvider.SetError(thresholdChangeMuTextBox, string.Empty);
206      } else errorProvider.SetError(thresholdChangeMuTextBox, "Invalid value");
207    }
208
209    private void thresholdChangeSigmaTextBox_TextChanged(object sender, EventArgs e) {
210      double value;
211      if (double.TryParse(thresholdChangeSigmaTextBox.Text, out value)) {
212        Content.ThresholdManipulatorSigma = value;
213        errorProvider.SetError(thresholdChangeSigmaTextBox, string.Empty);
214      } else errorProvider.SetError(thresholdChangeSigmaTextBox, "Invalid value");
215    }
216    private void slopeChangeMuTextBox_TextChanged(object sender, EventArgs e) {
217      double value;
218      if (double.TryParse(slopeChangeMuTextBox.Text, out value)) {
219        Content.SlopeManipulatorMu = value;
220        errorProvider.SetError(slopeChangeMuTextBox, string.Empty);
221      } else errorProvider.SetError(slopeChangeMuTextBox, "Invalid value");
222    }
223
224    private void slopeChangeSigmaTextBox_TextChanged(object sender, EventArgs e) {
225      double value;
226      if (double.TryParse(slopeChangeSigmaTextBox.Text, out value)) {
227        Content.SlopeManipulatorSigma = value;
228        errorProvider.SetError(slopeChangeSigmaTextBox, string.Empty);
229      } else errorProvider.SetError(slopeChangeSigmaTextBox, "Invalid value");
230    }
231    #endregion
232  }
233}
Note: See TracBrowser for help on using the repository browser.