Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Symbols/VariableView.cs @ 8614

Last change on this file since 8614 was 8476, checked in by mkommend, 12 years ago

#1815: Corrected event registration in SymbolicExpressionGrammar and updated the variable view.

File size: 8.7 KB
RevLine 
[3824]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3824]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;
[5750]23using System.Linq;
[3824]24using System.Windows.Forms;
[6233]25using HeuristicLab.Collections;
26using HeuristicLab.Core;
27using HeuristicLab.Core.Views;
28using HeuristicLab.Data;
[4068]29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
[3824]30using HeuristicLab.MainForm;
31using HeuristicLab.MainForm.WindowsForms;
32
[5693]33
34namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
[3824]35  [View("Variable View")]
36  [Content(typeof(Variable), true)]
37  public partial class VariableView : SymbolView {
[5750]38    private CheckedItemCollectionView<StringValue> variableNamesView;
39
[3824]40    public new Variable Content {
41      get { return (Variable)base.Content; }
42      set { base.Content = value; }
43    }
44
45    public VariableView() {
46      InitializeComponent();
[5750]47      variableNamesView = new CheckedItemCollectionView<StringValue>();
48      variableNamesView.Dock = DockStyle.Fill;
49      variableNamesTabPage.Controls.Add(variableNamesView);
50      variableNamesView.Content = new CheckedItemCollection<StringValue>();
51
52      RegisterVariableNamesViewContentEvents();
[3824]53    }
54
[5750]55    private void RegisterVariableNamesViewContentEvents() {
[8103]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);
[6598]60      foreach (var variable in variableNamesView.Content) {
[8103]61        variable.ValueChanged += new EventHandler(Variable_ValueChanged);
[6598]62      }
[5750]63    }
64
[6599]65
[5750]66    private void DeregisterVariableNamesViewContentEvents() {
[8103]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);
[6598]71      foreach (var variable in variableNamesView.Content) {
[8103]72        variable.ValueChanged -= new EventHandler(Variable_ValueChanged);
[6598]73      }
[5750]74    }
75
76
[3824]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
[3904]92    protected override void SetEnabledStateOfControls() {
93      base.SetEnabledStateOfControls();
[8476]94      enabledCheckBox.Enabled = Content != null && Content.VariableNames.Any() && !Locked && !ReadOnly;
[5326]95      weightInitializationMuTextBox.Enabled = Content != null;
96      weightInitializationMuTextBox.ReadOnly = ReadOnly;
97      weightInitializationSigmaTextBox.Enabled = Content != null;
98      weightInitializationSigmaTextBox.ReadOnly = ReadOnly;
99      additiveWeightChangeSigmaTextBox.Enabled = Content != null;
100      additiveWeightChangeSigmaTextBox.ReadOnly = ReadOnly;
101      multiplicativeWeightChangeSigmaTextBox.Enabled = Content != null;
[6599]102      multiplicativeWeightChangeSigmaTextBox.ReadOnly = ReadOnly;
[3824]103    }
104
105    #region content event handlers
106    private void Content_Changed(object sender, EventArgs e) {
107      UpdateControl();
108    }
109    #endregion
110
111    #region control event handlers
[8103]112    private void VariableNames_Changed(object sender, CollectionItemsChangedEventArgs<StringValue> e) {
113      foreach (var newVar in e.Items)
114        newVar.ValueChanged += new EventHandler(Variable_ValueChanged);
115      foreach (var oldVar in e.OldItems)
116        oldVar.ValueChanged -= new EventHandler(Variable_ValueChanged);
[6598]117      UpdateContent();
[6599]118    }
[6598]119
[8103]120    private void Variable_ValueChanged(object sender, EventArgs e) {
[6598]121      UpdateContent();
122    }
123
124    private void UpdateContent() {
[5750]125      if (Content != null) {
[6803]126        DeregisterContentEvents();
[5750]127        Content.VariableNames = variableNamesView.Content.CheckedItems.Select(x => x.Value).ToList();
[6803]128        RegisterContentEvents();
[5750]129      }
130    }
131
[8103]132    private void WeightMuTextBox_TextChanged(object sender, EventArgs e) {
[3824]133      double nu;
[5326]134      if (double.TryParse(weightInitializationMuTextBox.Text, out nu)) {
[4989]135        Content.WeightMu = nu;
[5326]136        errorProvider.SetError(weightInitializationMuTextBox, string.Empty);
[3824]137      } else {
[5326]138        errorProvider.SetError(weightInitializationMuTextBox, "Invalid value");
[3824]139      }
140    }
[8103]141    private void WeightSigmaTextBox_TextChanged(object sender, EventArgs e) {
[3824]142      double sigma;
[5326]143      if (double.TryParse(weightInitializationSigmaTextBox.Text, out sigma) && sigma >= 0.0) {
[3824]144        Content.WeightSigma = sigma;
[5326]145        errorProvider.SetError(weightInitializationSigmaTextBox, string.Empty);
[3824]146      } else {
[5326]147        errorProvider.SetError(weightInitializationSigmaTextBox, "Invalid value");
[3824]148      }
149    }
150
[8103]151    private void AdditiveWeightChangeSigmaTextBox_TextChanged(object sender, EventArgs e) {
[5326]152      double sigma;
153      if (double.TryParse(additiveWeightChangeSigmaTextBox.Text, out sigma) && sigma >= 0.0) {
154        Content.WeightManipulatorSigma = sigma;
155        errorProvider.SetError(additiveWeightChangeSigmaTextBox, string.Empty);
[3824]156      } else {
[5326]157        errorProvider.SetError(additiveWeightChangeSigmaTextBox, "Invalid value");
[3824]158      }
159    }
[8103]160    private void MultiplicativeWeightChangeSigmaTextBox_TextChanged(object sender, EventArgs e) {
[3824]161      double sigma;
[5326]162      if (double.TryParse(multiplicativeWeightChangeSigmaTextBox.Text, out sigma) && sigma >= 0.0) {
163        Content.MultiplicativeWeightManipulatorSigma = sigma;
164        errorProvider.SetError(multiplicativeWeightChangeSigmaTextBox, string.Empty);
[3824]165      } else {
[5326]166        errorProvider.SetError(multiplicativeWeightChangeSigmaTextBox, "Invalid value");
[3824]167      }
168    }
169    #endregion
170
171    #region helpers
172    private void UpdateControl() {
173      if (Content == null) {
[5326]174        weightInitializationMuTextBox.Text = string.Empty;
175        weightInitializationSigmaTextBox.Text = string.Empty;
176        additiveWeightChangeSigmaTextBox.Text = string.Empty;
177        multiplicativeWeightChangeSigmaTextBox.Text = string.Empty;
[5750]178        // temporarily deregister to prevent circular calling of events
179        DeregisterVariableNamesViewContentEvents();
180        variableNamesView.Content.Clear();
181        RegisterVariableNamesViewContentEvents();
[3824]182      } else {
[6235]183        var existingEntries = variableNamesView.Content.ToList();
[6233]184
[5750]185        // temporarily deregister to prevent circular calling of events
186        DeregisterVariableNamesViewContentEvents();
187        // add additional entries
[6235]188        foreach (var variableName in Content.VariableNames.Except(existingEntries.Select(x => x.Value)))
[5750]189          variableNamesView.Content.Add(new StringValue(variableName), true);
[6235]190        foreach (var oldEntry in existingEntries.Where(x => !Content.VariableNames.Contains(x.Value)))
191          variableNamesView.Content.Remove(oldEntry);
[5750]192        RegisterVariableNamesViewContentEvents();
193
[5326]194        weightInitializationMuTextBox.Text = Content.WeightMu.ToString();
195        weightInitializationSigmaTextBox.Text = Content.WeightSigma.ToString();
196        additiveWeightChangeSigmaTextBox.Text = Content.WeightManipulatorSigma.ToString();
197        multiplicativeWeightChangeSigmaTextBox.Text = Content.MultiplicativeWeightManipulatorSigma.ToString();
[3824]198      }
199      SetEnabledStateOfControls();
200    }
201    #endregion
202  }
203}
Note: See TracBrowser for help on using the repository browser.