Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Scheduling/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Symbols/VariableView.cs @ 6307

Last change on this file since 6307 was 5809, checked in by mkommend, 14 years ago

#1418: Reintegrated branch into trunk.

File size: 7.7 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.Encodings.SymbolicExpressionTreeEncoding.Views;
26using HeuristicLab.MainForm;
27using HeuristicLab.MainForm.WindowsForms;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Core.Views;
31using HeuristicLab.Collections;
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      UpdateControl();
84    }
85
86    protected override void SetEnabledStateOfControls() {
87      base.SetEnabledStateOfControls();
88      weightInitializationMuTextBox.Enabled = Content != null;
89      weightInitializationMuTextBox.ReadOnly = ReadOnly;
90      weightInitializationSigmaTextBox.Enabled = Content != null;
91      weightInitializationSigmaTextBox.ReadOnly = ReadOnly;
92      additiveWeightChangeSigmaTextBox.Enabled = Content != null;
93      additiveWeightChangeSigmaTextBox.ReadOnly = ReadOnly;
94      multiplicativeWeightChangeSigmaTextBox.Enabled = Content != null;
95      multiplicativeWeightChangeSigmaTextBox.ReadOnly = ReadOnly;
96    }
97
98    #region content event handlers
99    private void Content_Changed(object sender, EventArgs e) {
100      UpdateControl();
101    }
102    #endregion
103
104    #region control event handlers
105    private void variableNames_Changed(object sender, CollectionItemsChangedEventArgs<StringValue> args) {
106      if (Content != null) {
107        Content.VariableNames = variableNamesView.Content.CheckedItems.Select(x => x.Value).ToList();
108      }
109    }
110
111    private void weightMuTextBox_TextChanged(object sender, EventArgs e) {
112      double nu;
113      if (double.TryParse(weightInitializationMuTextBox.Text, out nu)) {
114        Content.WeightMu = nu;
115        errorProvider.SetError(weightInitializationMuTextBox, string.Empty);
116      } else {
117        errorProvider.SetError(weightInitializationMuTextBox, "Invalid value");
118      }
119    }
120    private void weightSigmaTextBox_TextChanged(object sender, EventArgs e) {
121      double sigma;
122      if (double.TryParse(weightInitializationSigmaTextBox.Text, out sigma) && sigma >= 0.0) {
123        Content.WeightSigma = sigma;
124        errorProvider.SetError(weightInitializationSigmaTextBox, string.Empty);
125      } else {
126        errorProvider.SetError(weightInitializationSigmaTextBox, "Invalid value");
127      }
128    }
129
130    private void additiveWeightChangeSigmaTextBox_TextChanged(object sender, EventArgs e) {
131      double sigma;
132      if (double.TryParse(additiveWeightChangeSigmaTextBox.Text, out sigma) && sigma >= 0.0) {
133        Content.WeightManipulatorSigma = sigma;
134        errorProvider.SetError(additiveWeightChangeSigmaTextBox, string.Empty);
135      } else {
136        errorProvider.SetError(additiveWeightChangeSigmaTextBox, "Invalid value");
137      }
138    }
139
140    private void multiplicativeWeightChangeSigmaTextBox_TextChanged(object sender, EventArgs e) {
141      double sigma;
142      if (double.TryParse(multiplicativeWeightChangeSigmaTextBox.Text, out sigma) && sigma >= 0.0) {
143        Content.MultiplicativeWeightManipulatorSigma = sigma;
144        errorProvider.SetError(multiplicativeWeightChangeSigmaTextBox, string.Empty);
145      } else {
146        errorProvider.SetError(multiplicativeWeightChangeSigmaTextBox, "Invalid value");
147      }
148    }
149    #endregion
150
151    #region helpers
152    private void UpdateControl() {
153      if (Content == null) {
154        weightInitializationMuTextBox.Text = string.Empty;
155        weightInitializationSigmaTextBox.Text = string.Empty;
156        additiveWeightChangeSigmaTextBox.Text = string.Empty;
157        multiplicativeWeightChangeSigmaTextBox.Text = string.Empty;
158        // temporarily deregister to prevent circular calling of events
159        DeregisterVariableNamesViewContentEvents();
160        variableNamesView.Content.Clear();
161        RegisterVariableNamesViewContentEvents();
162      } else {
163        var existingEntries = variableNamesView.Content.Select(x => x.Value);
164       
165        // temporarily deregister to prevent circular calling of events
166        DeregisterVariableNamesViewContentEvents();
167        // add additional entries
168        foreach (var variableName in Content.VariableNames.Except(existingEntries)) {
169          variableNamesView.Content.Add(new StringValue(variableName), true);
170        }
171        RegisterVariableNamesViewContentEvents();
172
173        weightInitializationMuTextBox.Text = Content.WeightMu.ToString();
174        weightInitializationSigmaTextBox.Text = Content.WeightSigma.ToString();
175        additiveWeightChangeSigmaTextBox.Text = Content.WeightManipulatorSigma.ToString();
176        multiplicativeWeightChangeSigmaTextBox.Text = Content.MultiplicativeWeightManipulatorSigma.ToString();
177      }
178      SetEnabledStateOfControls();
179    }
180    #endregion
181  }
182}
Note: See TracBrowser for help on using the repository browser.