Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6233 was 6233, checked in by mkommend, 13 years ago

#1532:

  • Enabled renaming of symbols
  • Fixed cloning of grammers
  • Added readonly attribute in grammars to lock grammars during the algorithm run
  • Removed useless clone method in cloner
  • Changed CheckedItemCollectionViews to enable scrolling during the locked state
File size: 7.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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
36  [View("Variable View")]
37  [Content(typeof(Variable), true)]
38  public partial class VariableView : SymbolView {
39    private CheckedItemCollectionView<StringValue> variableNamesView;
40
41    public new Variable Content {
42      get { return (Variable)base.Content; }
43      set { base.Content = value; }
44    }
45
46    public VariableView() {
47      InitializeComponent();
48      variableNamesView = new CheckedItemCollectionView<StringValue>();
49      variableNamesView.Dock = DockStyle.Fill;
50      variableNamesTabPage.Controls.Add(variableNamesView);
51      variableNamesView.Content = new CheckedItemCollection<StringValue>();
52
53      RegisterVariableNamesViewContentEvents();
54    }
55
56    private void RegisterVariableNamesViewContentEvents() {
57      variableNamesView.Content.ItemsAdded += new CollectionItemsChangedEventHandler<StringValue>(variableNames_Changed);
58      variableNamesView.Content.ItemsRemoved += new CollectionItemsChangedEventHandler<StringValue>(variableNames_Changed);
59      variableNamesView.Content.CheckedItemsChanged += new CollectionItemsChangedEventHandler<StringValue>(variableNames_Changed);
60      variableNamesView.Content.CollectionReset += new CollectionItemsChangedEventHandler<StringValue>(variableNames_Changed);
61    }
62
63    private void DeregisterVariableNamesViewContentEvents() {
64      variableNamesView.Content.ItemsAdded -= new CollectionItemsChangedEventHandler<StringValue>(variableNames_Changed);
65      variableNamesView.Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<StringValue>(variableNames_Changed);
66      variableNamesView.Content.CheckedItemsChanged -= new CollectionItemsChangedEventHandler<StringValue>(variableNames_Changed);
67      variableNamesView.Content.CollectionReset -= new CollectionItemsChangedEventHandler<StringValue>(variableNames_Changed);
68    }
69
70
71    protected override void RegisterContentEvents() {
72      base.RegisterContentEvents();
73      Content.Changed += new EventHandler(Content_Changed);
74    }
75
76    protected override void DeregisterContentEvents() {
77      base.DeregisterContentEvents();
78      Content.Changed -= new EventHandler(Content_Changed);
79    }
80
81    protected override void OnContentChanged() {
82      base.OnContentChanged();
83      variableNamesView.Content.Clear();
84      UpdateControl();
85    }
86
87    protected override void SetEnabledStateOfControls() {
88      base.SetEnabledStateOfControls();
89      weightInitializationMuTextBox.Enabled = Content != null;
90      weightInitializationMuTextBox.ReadOnly = ReadOnly;
91      weightInitializationSigmaTextBox.Enabled = Content != null;
92      weightInitializationSigmaTextBox.ReadOnly = ReadOnly;
93      additiveWeightChangeSigmaTextBox.Enabled = Content != null;
94      additiveWeightChangeSigmaTextBox.ReadOnly = ReadOnly;
95      multiplicativeWeightChangeSigmaTextBox.Enabled = Content != null;
96      multiplicativeWeightChangeSigmaTextBox.ReadOnly = ReadOnly;
97    }
98
99    #region content event handlers
100    private void Content_Changed(object sender, EventArgs e) {
101      UpdateControl();
102    }
103    #endregion
104
105    #region control event handlers
106    private void variableNames_Changed(object sender, CollectionItemsChangedEventArgs<StringValue> args) {
107      if (Content != null) {
108        Content.VariableNames = variableNamesView.Content.CheckedItems.Select(x => x.Value).ToList();
109      }
110    }
111
112    private void weightMuTextBox_TextChanged(object sender, EventArgs e) {
113      double nu;
114      if (double.TryParse(weightInitializationMuTextBox.Text, out nu)) {
115        Content.WeightMu = nu;
116        errorProvider.SetError(weightInitializationMuTextBox, string.Empty);
117      } else {
118        errorProvider.SetError(weightInitializationMuTextBox, "Invalid value");
119      }
120    }
121    private void weightSigmaTextBox_TextChanged(object sender, EventArgs e) {
122      double sigma;
123      if (double.TryParse(weightInitializationSigmaTextBox.Text, out sigma) && sigma >= 0.0) {
124        Content.WeightSigma = sigma;
125        errorProvider.SetError(weightInitializationSigmaTextBox, string.Empty);
126      } else {
127        errorProvider.SetError(weightInitializationSigmaTextBox, "Invalid value");
128      }
129    }
130
131    private void additiveWeightChangeSigmaTextBox_TextChanged(object sender, EventArgs e) {
132      double sigma;
133      if (double.TryParse(additiveWeightChangeSigmaTextBox.Text, out sigma) && sigma >= 0.0) {
134        Content.WeightManipulatorSigma = sigma;
135        errorProvider.SetError(additiveWeightChangeSigmaTextBox, string.Empty);
136      } else {
137        errorProvider.SetError(additiveWeightChangeSigmaTextBox, "Invalid value");
138      }
139    }
140
141    private void multiplicativeWeightChangeSigmaTextBox_TextChanged(object sender, EventArgs e) {
142      double sigma;
143      if (double.TryParse(multiplicativeWeightChangeSigmaTextBox.Text, out sigma) && sigma >= 0.0) {
144        Content.MultiplicativeWeightManipulatorSigma = sigma;
145        errorProvider.SetError(multiplicativeWeightChangeSigmaTextBox, string.Empty);
146      } else {
147        errorProvider.SetError(multiplicativeWeightChangeSigmaTextBox, "Invalid value");
148      }
149    }
150    #endregion
151
152    #region helpers
153    private void UpdateControl() {
154      if (Content == null) {
155        weightInitializationMuTextBox.Text = string.Empty;
156        weightInitializationSigmaTextBox.Text = string.Empty;
157        additiveWeightChangeSigmaTextBox.Text = string.Empty;
158        multiplicativeWeightChangeSigmaTextBox.Text = string.Empty;
159        // temporarily deregister to prevent circular calling of events
160        DeregisterVariableNamesViewContentEvents();
161        variableNamesView.Content.Clear();
162        RegisterVariableNamesViewContentEvents();
163      } else {
164        var existingEntries = variableNamesView.Content.Select(x => x.Value);
165
166        // temporarily deregister to prevent circular calling of events
167        DeregisterVariableNamesViewContentEvents();
168        // add additional entries
169        foreach (var variableName in Content.VariableNames.Except(existingEntries)) {
170          variableNamesView.Content.Add(new StringValue(variableName), true);
171        }
172        RegisterVariableNamesViewContentEvents();
173
174        weightInitializationMuTextBox.Text = Content.WeightMu.ToString();
175        weightInitializationSigmaTextBox.Text = Content.WeightSigma.ToString();
176        additiveWeightChangeSigmaTextBox.Text = Content.WeightManipulatorSigma.ToString();
177        multiplicativeWeightChangeSigmaTextBox.Text = Content.MultiplicativeWeightManipulatorSigma.ToString();
178      }
179      SetEnabledStateOfControls();
180    }
181    #endregion
182  }
183}
Note: See TracBrowser for help on using the repository browser.