Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ExternalEvaluation.GP.Views/3.3/VariableSymbolView.cs @ 5594

Last change on this file since 5594 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 5.6 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.ComponentModel;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Collections;
27using HeuristicLab.Core;
28using HeuristicLab.Core.Views;
29using HeuristicLab.Data;
30using HeuristicLab.MainForm;
31
32namespace HeuristicLab.Problems.ExternalEvaluation.GP.Views {
33  [View("Variable View")]
34  [Content(typeof(Variable), IsDefaultView = true)]
35  public sealed partial class VariableSymbolView : NamedItemView {
36    private CheckedItemCollectionView<StringValue> variableNamesView;
37    private CheckedItemCollection<StringValue> variableNames;
38    private bool suppressDownstreamSynchronization;
39
40    public new Variable Content {
41      get { return (Variable)base.Content; }
42      set { base.Content = value; }
43    }
44
45    public VariableSymbolView() {
46      InitializeComponent();
47      variableNamesView = new CheckedItemCollectionView<StringValue>();
48      variableNamesView.Dock = DockStyle.Fill;
49      variableNamesTabPage.Controls.Add(variableNamesView);
50    }
51
52    protected override void DeregisterContentEvents() {
53      Content.Changed += new EventHandler(Content_Changed);
54      base.DeregisterContentEvents();
55    }
56
57    protected override void RegisterContentEvents() {
58      base.RegisterContentEvents();
59      Content.Changed -= new EventHandler(Content_Changed);
60    }
61
62    #region Event Handlers (Content)
63    private void Content_Changed(object sender, EventArgs e) {
64      SynchronizeVariableNames();
65      SynchronizeParameters();
66    }
67    #endregion
68
69    protected override void OnContentChanged() {
70      base.OnContentChanged();
71      if (Content == null) {
72        variableNamesView.Content = null;
73      } else {
74        SynchronizeVariableNames();
75        SynchronizeParameters();
76      }
77    }
78
79    private void SynchronizeVariableNames() {
80      suppressDownstreamSynchronization = true;
81      variableNames = new CheckedItemCollection<StringValue>();
82      variableNamesView.Content = variableNames;
83      variableNames.AddRange(Content.VariableNames.Select(x => new StringValue(x)).ToList());
84      variableNames.ItemsAdded += new CollectionItemsChangedEventHandler<StringValue>(variableNames_Changed);
85      variableNames.ItemsRemoved += new CollectionItemsChangedEventHandler<StringValue>(variableNames_Changed);
86      variableNames.CheckedItemsChanged += new CollectionItemsChangedEventHandler<StringValue>(variableNames_Changed);
87      variableNames.CollectionReset += new CollectionItemsChangedEventHandler<StringValue>(variableNames_Changed);
88      suppressDownstreamSynchronization = false;
89    }
90
91    private void SynchronizeParameters() {
92      suppressDownstreamSynchronization = true;
93      weightNuTextBox.Text = Content.WeightNu.ToString();
94      weightSigmaTextBox.Text = Content.WeightSigma.ToString();
95      weightManipulatorNuTextBox.Text = Content.WeightManipulatorNu.ToString();
96      weightManipulatorSigmaTextBox.Text = Content.WeightManipulatorSigma.ToString();
97      suppressDownstreamSynchronization = false;
98    }
99
100    protected override void SetEnabledStateOfControls() {
101      base.SetEnabledStateOfControls();
102      weightNuTextBox.Enabled = !ReadOnly && Content != null;
103      weightSigmaTextBox.Enabled = !ReadOnly && Content != null;
104      weightManipulatorNuTextBox.Enabled = !ReadOnly && Content != null;
105      weightManipulatorSigmaTextBox.Enabled = !ReadOnly && Content != null;
106    }
107
108    #region Event Handlers (child controls)
109    private void variableNames_Changed(object sender, CollectionItemsChangedEventArgs<StringValue> args) {
110      if (!suppressDownstreamSynchronization && Content != null) {
111        Content.VariableNames = variableNames.CheckedItems.Select(x => x.Value).ToList();
112      }
113    }
114
115    private void weightTextBox_Validating(object sender, CancelEventArgs e) {
116      e.Cancel = false;
117      if (!suppressDownstreamSynchronization && Content != null) {
118        TextBox textBox = (sender as TextBox);
119        if (textBox == null) throw new ArgumentException("Event handler expected a TextBox as sender", "sender");
120        double value;
121        if (!double.TryParse(textBox.Text, out value))
122          e.Cancel = true;
123        else {
124          if (textBox == weightNuTextBox) {
125            Content.WeightNu = value;
126          } else if (textBox == weightSigmaTextBox) {
127            Content.WeightSigma = value;
128          } else if (textBox == weightManipulatorNuTextBox) {
129            Content.WeightManipulatorNu = value;
130          } else if (textBox == weightManipulatorSigmaTextBox) {
131            Content.WeightManipulatorSigma = value;
132          } else throw new ArgumentException("Unknown sender when trying to set the weights of the variable symbol.");
133        }
134      }
135    }
136    #endregion
137  }
138}
Note: See TracBrowser for help on using the repository browser.