Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/TreeEditDialogs/SymbolicExpressionTreeConstantNodeEditDialog.cs @ 18142

Last change on this file since 18142 was 18142, checked in by chaider, 2 years ago

#3140

  • Removed setter from INumericTreeNode
  • Fixed SymbolicExpressionImporter check for constants and number individually
  • Set exponent of power in DerivativeCalculator to number
  • Set number in TreeConstantNodeEditDialog and ExpressionTreeChart
  • Fixed setting of VariableRanges in DataAnalysisProblemData
File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 NumberNodeEditDialog : Form {
31    private NumberTreeNode numberTreeNode;
32    public NumberTreeNode NewNode {
33      get { return numberTreeNode; }
34      set {
35        if (InvokeRequired)
36          Invoke(new Action<SymbolicExpressionTreeNode>(x => numberTreeNode = (NumberTreeNode)x), value);
37        else
38          numberTreeNode = value;
39      }
40    }
41
42    public NumberNodeEditDialog(ISymbolicExpressionTreeNode node) {
43      InitializeComponent();
44      oldValueTextBox.TabStop = false; // cannot receive focus using tab key
45      NewNode = (NumberTreeNode)node;
46      InitializeFields();
47    }
48
49    private void InitializeFields() {
50      if (NewNode == null)
51        throw new ArgumentException("Node is not a number.");
52      else {
53        this.Text = "Edit number";
54        newValueTextBox.Text = oldValueTextBox.Text = Math.Round(numberTreeNode.Value, 4).ToString();
55      }
56    }
57
58    #region text box validation and events
59    private void newValueTextBox_Validating(object sender, CancelEventArgs e) {
60      string errorMessage;
61      if (!ValidateNewValue(newValueTextBox.Text, out errorMessage)) {
62        e.Cancel = true;
63        errorProvider.SetError(newValueTextBox, errorMessage);
64        newValueTextBox.SelectAll();
65      }
66    }
67
68    private void newValueTextBox_Validated(object sender, EventArgs e) {
69      errorProvider.SetError(newValueTextBox, string.Empty);
70    }
71
72    private static bool ValidateNewValue(string value, out string errorMessage) {
73      bool valid = double.TryParse(value, out _);
74      errorMessage = string.Empty;
75      if (!valid) {
76        var sb = new StringBuilder();
77        sb.Append("Invalid Value (Valid Value Format: \"");
78        sb.Append(FormatPatterns.GetDoubleFormatPattern());
79        sb.Append("\")");
80        errorMessage = sb.ToString();
81      }
82      return valid;
83    }
84    #endregion
85
86    // proxy handler passing key strokes to the parent control
87    private void childControl_KeyDown(object sender, KeyEventArgs e) {
88      NumberNodeEditDialog_KeyDown(sender, e);
89    }
90
91    private void NumberNodeEditDialog_KeyDown(object sender, KeyEventArgs e) {
92      if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return)) {
93        if (!ValidateChildren()) return;
94        OnDialogValidated(this, e); // emit validated effect
95        Close();
96      }
97    }
98
99    public event EventHandler DialogValidated;
100    private void OnDialogValidated(object sender, EventArgs e) {
101      if (numberTreeNode == null) return;
102      var value = double.Parse(newValueTextBox.Text);
103      // we impose an extra validation condition: that the new value is different from the original value
104      if (numberTreeNode.Value.Equals(value)) return;
105      numberTreeNode.Value = value;
106      DialogResult = DialogResult.OK;
107      var dialogValidated = DialogValidated;
108      if (dialogValidated != null)
109        dialogValidated(sender, e);
110    }
111
112    private void cancelButton_Click(object sender, EventArgs e) {
113      Close();
114    }
115
116    private void okButton_Click(object sender, EventArgs e) {
117      if (ValidateChildren()) {
118        OnDialogValidated(this, e);
119        Close();
120      }
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.