Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Symbols/VariableView.cs @ 8528

Last change on this file since 8528 was 8206, checked in by gkronber, 12 years ago

#1847: merged r8084:8205 from trunk into GP move operators branch

File size: 8.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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
33
34namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
35  [View("Variable View")]
36  [Content(typeof(Variable), true)]
37  public partial class VariableView : SymbolView {
38    private CheckedItemCollectionView<StringValue> variableNamesView;
39
40    public new Variable Content {
41      get { return (Variable)base.Content; }
42      set { base.Content = value; }
43    }
44
45    public VariableView() {
46      InitializeComponent();
47      variableNamesView = new CheckedItemCollectionView<StringValue>();
48      variableNamesView.Dock = DockStyle.Fill;
49      variableNamesTabPage.Controls.Add(variableNamesView);
50      variableNamesView.Content = new CheckedItemCollection<StringValue>();
51
52      RegisterVariableNamesViewContentEvents();
53    }
54
55    private void RegisterVariableNamesViewContentEvents() {
56      variableNamesView.Content.ItemsAdded += new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Changed);
57      variableNamesView.Content.ItemsRemoved += new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Changed);
58      variableNamesView.Content.CheckedItemsChanged += new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Changed);
59      variableNamesView.Content.CollectionReset += new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Changed);
60      foreach (var variable in variableNamesView.Content) {
61        variable.ValueChanged += new EventHandler(Variable_ValueChanged);
62      }
63    }
64
65
66    private void DeregisterVariableNamesViewContentEvents() {
67      variableNamesView.Content.ItemsAdded -= new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Changed);
68      variableNamesView.Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Changed);
69      variableNamesView.Content.CheckedItemsChanged -= new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Changed);
70      variableNamesView.Content.CollectionReset -= new CollectionItemsChangedEventHandler<StringValue>(VariableNames_Changed);
71      foreach (var variable in variableNamesView.Content) {
72        variable.ValueChanged -= new EventHandler(Variable_ValueChanged);
73      }
74    }
75
76
77    protected override void RegisterContentEvents() {
78      base.RegisterContentEvents();
79      Content.Changed += new EventHandler(Content_Changed);
80    }
81
82    protected override void DeregisterContentEvents() {
83      base.DeregisterContentEvents();
84      Content.Changed -= new EventHandler(Content_Changed);
85    }
86
87    protected override void OnContentChanged() {
88      base.OnContentChanged();
89      UpdateControl();
90    }
91
92    protected override void SetEnabledStateOfControls() {
93      base.SetEnabledStateOfControls();
94      weightInitializationMuTextBox.Enabled = Content != null;
95      weightInitializationMuTextBox.ReadOnly = ReadOnly;
96      weightInitializationSigmaTextBox.Enabled = Content != null;
97      weightInitializationSigmaTextBox.ReadOnly = ReadOnly;
98      additiveWeightChangeSigmaTextBox.Enabled = Content != null;
99      additiveWeightChangeSigmaTextBox.ReadOnly = ReadOnly;
100      multiplicativeWeightChangeSigmaTextBox.Enabled = Content != null;
101      multiplicativeWeightChangeSigmaTextBox.ReadOnly = ReadOnly;
102    }
103
104    #region content event handlers
105    private void Content_Changed(object sender, EventArgs e) {
106      UpdateControl();
107    }
108    #endregion
109
110    #region control event handlers
111    private void VariableNames_Changed(object sender, CollectionItemsChangedEventArgs<StringValue> e) {
112      foreach (var newVar in e.Items)
113        newVar.ValueChanged += new EventHandler(Variable_ValueChanged);
114      foreach (var oldVar in e.OldItems)
115        oldVar.ValueChanged -= new EventHandler(Variable_ValueChanged);
116      UpdateContent();
117    }
118
119    private void Variable_ValueChanged(object sender, EventArgs e) {
120      UpdateContent();
121    }
122
123    private void UpdateContent() {
124      if (Content != null) {
125        DeregisterContentEvents();
126        Content.VariableNames = variableNamesView.Content.CheckedItems.Select(x => x.Value).ToList();
127        RegisterContentEvents();
128      }
129    }
130
131    private void WeightMuTextBox_TextChanged(object sender, EventArgs e) {
132      double nu;
133      if (double.TryParse(weightInitializationMuTextBox.Text, out nu)) {
134        Content.WeightMu = nu;
135        errorProvider.SetError(weightInitializationMuTextBox, string.Empty);
136      } else {
137        errorProvider.SetError(weightInitializationMuTextBox, "Invalid value");
138      }
139    }
140    private void WeightSigmaTextBox_TextChanged(object sender, EventArgs e) {
141      double sigma;
142      if (double.TryParse(weightInitializationSigmaTextBox.Text, out sigma) && sigma >= 0.0) {
143        Content.WeightSigma = sigma;
144        errorProvider.SetError(weightInitializationSigmaTextBox, string.Empty);
145      } else {
146        errorProvider.SetError(weightInitializationSigmaTextBox, "Invalid value");
147      }
148    }
149
150    private void AdditiveWeightChangeSigmaTextBox_TextChanged(object sender, EventArgs e) {
151      double sigma;
152      if (double.TryParse(additiveWeightChangeSigmaTextBox.Text, out sigma) && sigma >= 0.0) {
153        Content.WeightManipulatorSigma = sigma;
154        errorProvider.SetError(additiveWeightChangeSigmaTextBox, string.Empty);
155      } else {
156        errorProvider.SetError(additiveWeightChangeSigmaTextBox, "Invalid value");
157      }
158    }
159    private void MultiplicativeWeightChangeSigmaTextBox_TextChanged(object sender, EventArgs e) {
160      double sigma;
161      if (double.TryParse(multiplicativeWeightChangeSigmaTextBox.Text, out sigma) && sigma >= 0.0) {
162        Content.MultiplicativeWeightManipulatorSigma = sigma;
163        errorProvider.SetError(multiplicativeWeightChangeSigmaTextBox, string.Empty);
164      } else {
165        errorProvider.SetError(multiplicativeWeightChangeSigmaTextBox, "Invalid value");
166      }
167    }
168    #endregion
169
170    #region helpers
171    private void UpdateControl() {
172      if (Content == null) {
173        weightInitializationMuTextBox.Text = string.Empty;
174        weightInitializationSigmaTextBox.Text = string.Empty;
175        additiveWeightChangeSigmaTextBox.Text = string.Empty;
176        multiplicativeWeightChangeSigmaTextBox.Text = string.Empty;
177        // temporarily deregister to prevent circular calling of events
178        DeregisterVariableNamesViewContentEvents();
179        variableNamesView.Content.Clear();
180        RegisterVariableNamesViewContentEvents();
181      } else {
182        var existingEntries = variableNamesView.Content.ToList();
183
184        // temporarily deregister to prevent circular calling of events
185        DeregisterVariableNamesViewContentEvents();
186        // add additional entries
187        foreach (var variableName in Content.VariableNames.Except(existingEntries.Select(x => x.Value)))
188          variableNamesView.Content.Add(new StringValue(variableName), true);
189        foreach (var oldEntry in existingEntries.Where(x => !Content.VariableNames.Contains(x.Value)))
190          variableNamesView.Content.Remove(oldEntry);
191        RegisterVariableNamesViewContentEvents();
192
193        weightInitializationMuTextBox.Text = Content.WeightMu.ToString();
194        weightInitializationSigmaTextBox.Text = Content.WeightSigma.ToString();
195        additiveWeightChangeSigmaTextBox.Text = Content.WeightManipulatorSigma.ToString();
196        multiplicativeWeightChangeSigmaTextBox.Text = Content.MultiplicativeWeightManipulatorSigma.ToString();
197      }
198      SetEnabledStateOfControls();
199    }
200    #endregion
201  }
202}
Note: See TracBrowser for help on using the repository browser.