Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/TreeEditDialogs/SymbolicExpressionTreeNodeChangeValueDialog.cs @ 8464

Last change on this file since 8464 was 8409, checked in by bburlacu, 12 years ago

#1763: Fixed bug when pasting subtrees. Moved the InteractiveSymbolicExpressionTreeChart and dialogs in the HeuristicLab.Problems.DataAnalysis.Symbolic.Views namespace and renamed the impact values calculators to a more sensible name.

File size: 5.4 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.ComponentModel;
24using System.Text;
25using System.Windows.Forms;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
30  public partial class ValueChangeDialog : Form {
31    private ISymbolicExpressionTreeNode _content;
32    public ISymbolicExpressionTreeNode Content {
33      get { return _content; }
34      set {
35        if (InvokeRequired)
36          Invoke(new Action<SymbolicExpressionTreeNode>(x => _content = x), value);
37        else
38          _content = value;
39      }
40    }
41
42    public string Caption {
43      get { return this.Text; }
44      set {
45        if (InvokeRequired)
46          Invoke(new Action<string>(x => this.Caption = x), value);
47        else
48          this.Text = value;
49      }
50    }
51
52    public string OldValue {
53      get { return oldValueTextBox.Text; }
54      set {
55        if (InvokeRequired)
56          Invoke(new Action<string>(x => this.NewValue = x), value);
57        else
58          oldValueTextBox.Text = value;
59      }
60    }
61
62    public string NewValue {
63      get { return newValueTextBox.Text; }
64      set {
65        if (InvokeRequired)
66          Invoke(new Action<string>(x => this.NewValue = x), value);
67        else
68          newValueTextBox.Text = value;
69      }
70    }
71
72    public TextBox NewValueTextBox {
73      get { return newValueTextBox; }
74    }
75
76    public ComboBox VariableNameComboBox {
77      get { return variableNamesCombo; }
78    }
79
80    public ValueChangeDialog() {
81      InitializeComponent();
82      oldValueTextBox.TabStop = false; // cannot receive focus using tab key
83    }
84
85    public void SetContent(ISymbolicExpressionTreeNode content) {
86      Content = content;
87      if (Content is VariableTreeNode) {
88        Caption = "Change variable name or weight";
89        var variable = Content as VariableTreeNode;
90        OldValue = Math.Round(variable.Weight, 4).ToString();
91        NewValue = OldValue;
92        // add a dropbox containing all the available variable names
93        variableNameLabel.Visible = true;
94        variableNamesCombo.Visible = true;
95        foreach (var name in variable.Symbol.VariableNames) variableNamesCombo.Items.Add(name);
96        variableNamesCombo.SelectedIndex = 0;
97      } else if (Content is ConstantTreeNode) {
98        Caption = "Change constant value";
99        var constant = Content as ConstantTreeNode;
100        OldValue = Math.Round(constant.Value, 4).ToString();
101        NewValue = OldValue;
102      }
103    }
104
105    #region text box validation and events
106    private void newValueTextBox_Validating(object sender, CancelEventArgs e) {
107      string errorMessage;
108      if (!ValidateNewValue(newValueTextBox.Text, out errorMessage)) {
109        e.Cancel = true;
110        errorProvider.SetError(newValueTextBox, errorMessage);
111        newValueTextBox.SelectAll();
112      }
113    }
114
115    private void newValueTextBox_Validated(object sender, EventArgs e) {
116      errorProvider.SetError(newValueTextBox, string.Empty);
117    }
118
119    private static bool ValidateNewValue(string value, out string errorMessage) {
120      double val;
121      bool valid = double.TryParse(value, out val);
122      errorMessage = string.Empty;
123      if (!valid) {
124        var sb = new StringBuilder();
125        sb.Append("Invalid Value (Valid Value Format: \"");
126        sb.Append(FormatPatterns.GetDoubleFormatPattern());
127        sb.Append("\")");
128        errorMessage = sb.ToString();
129      }
130      return valid;
131    }
132    #endregion
133
134    #region combo box validation and events
135    private void variableNamesCombo_Validating(object sender, CancelEventArgs e) {
136      if (!(Content is VariableTreeNode)) return;
137      if (variableNamesCombo.Items.Contains(variableNamesCombo.SelectedItem)) return;
138      e.Cancel = true;
139      errorProvider.SetError(variableNamesCombo, "Invalid variable name");
140      variableNamesCombo.SelectAll();
141    }
142
143    private void variableNamesCombo_Validated(object sender, EventArgs e) {
144      errorProvider.SetError(variableNamesCombo, String.Empty);
145    }
146    #endregion
147    // proxy handler passing key strokes to the parent control
148    private void childControl_KeyDown(object sender, KeyEventArgs e) {
149      ValueChangeDialog_KeyDown(sender, e);
150    }
151
152    private void ValueChangeDialog_KeyDown(object sender, KeyEventArgs e) {
153      if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return)) {
154        if (!ValidateChildren()) return;
155        DialogValidated(this, e);
156        Close();
157      }
158    }
159
160    public event EventHandler DialogValidated;
161  }
162}
Note: See TracBrowser for help on using the repository browser.