Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TreeSimplifierView/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeNodeChangeValueDialog.cs @ 7884

Last change on this file since 7884 was 7784, checked in by bburlacu, 13 years ago

#1832: Moved replacement and impact values calculation code from view to separate files. Implemented simplifier actions: copy, cut, delete, insert node/subtree.

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.Problems.DataAnalysis.Symbolic;
28
29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.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        var names = variable.Symbol.VariableNames;
94        variableNameLabel.Visible = true;
95        variableNamesCombo.Visible = true;
96        foreach (var name in names) variableNamesCombo.Items.Add(name);
97        variableNamesCombo.SelectedIndex = 0;
98      } else if (Content is ConstantTreeNode) {
99        Caption = "Change constant value";
100        var constant = Content as ConstantTreeNode;
101        OldValue = Math.Round(constant.Value, 4).ToString();
102        NewValue = OldValue;
103      }
104    }
105
106    #region text box validation and events
107    private void newValueTextBox_Validating(object sender, CancelEventArgs e) {
108      string errorMessage;
109      if (!ValidateNewValue(newValueTextBox.Text, out errorMessage)) {
110        e.Cancel = true;
111        errorProvider.SetError(newValueTextBox, errorMessage);
112        newValueTextBox.SelectAll();
113      }
114    }
115
116    private void newValueTextBox_Validated(object sender, EventArgs e) {
117      errorProvider.SetError(newValueTextBox, string.Empty);
118    }
119
120    private static bool ValidateNewValue(string value, out string errorMessage) {
121      double val;
122      bool valid = double.TryParse(value, out val);
123      errorMessage = string.Empty;
124      if (!valid) {
125        var sb = new StringBuilder();
126        sb.Append("Invalid Value (Valid Value Format: \"");
127        sb.Append(FormatPatterns.GetDoubleFormatPattern());
128        sb.Append("\")");
129        errorMessage = sb.ToString();
130      }
131      return valid;
132    }
133    #endregion
134
135    #region combo box validation and events
136    private void variableNamesCombo_Validating(object sender, CancelEventArgs e) {
137      if (!(Content is VariableTreeNode)) return;
138      if (variableNamesCombo.Items.Contains(variableNamesCombo.SelectedItem)) return;
139      e.Cancel = true;
140      errorProvider.SetError(variableNamesCombo, "Invalid variable name");
141      variableNamesCombo.SelectAll();
142    }
143
144    private void variableNamesCombo_Validated(object sender, EventArgs e) {
145      errorProvider.SetError(variableNamesCombo, String.Empty);
146    }
147    #endregion
148    // proxy handler passing key strokes to the parent control
149    private void childControl_KeyDown(object sender, KeyEventArgs e) {
150      ValueChangeDialog_KeyDown(sender, e);
151    }
152
153    private void ValueChangeDialog_KeyDown(object sender, KeyEventArgs e) {
154      if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return)) {
155        if (!ValidateChildren()) return;
156        DialogValidated(this, e);
157        Close();
158      }
159    }
160
161    public event EventHandler DialogValidated;
162  }
163}
Note: See TracBrowser for help on using the repository browser.