Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/TreeEditDialogs/SymbolicExpressionTreeConstantNodeEditDialog.cs @ 14432

Last change on this file since 14432 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 ConstantNodeEditDialog : Form {
31    private ConstantTreeNode constantTreeNode;
32    public ConstantTreeNode NewNode {
33      get { return constantTreeNode; }
34      set {
35        if (InvokeRequired)
36          Invoke(new Action<SymbolicExpressionTreeNode>(x => constantTreeNode = (ConstantTreeNode)x), value);
37        else
38          constantTreeNode = value;
39      }
40    }
41
42    public ConstantNodeEditDialog(ISymbolicExpressionTreeNode node) {
43      InitializeComponent();
44      oldValueTextBox.TabStop = false; // cannot receive focus using tab key
45      NewNode = (ConstantTreeNode)node;
46      InitializeFields();
47    }
48
49    private void InitializeFields() {
50      if (NewNode == null)
51        throw new ArgumentException("Node is not a constant.");
52      else {
53        this.Text = "Edit constant";
54        newValueTextBox.Text = oldValueTextBox.Text = Math.Round(constantTreeNode.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      double val;
74      bool valid = double.TryParse(value, out val);
75      errorMessage = string.Empty;
76      if (!valid) {
77        var sb = new StringBuilder();
78        sb.Append("Invalid Value (Valid Value Format: \"");
79        sb.Append(FormatPatterns.GetDoubleFormatPattern());
80        sb.Append("\")");
81        errorMessage = sb.ToString();
82      }
83      return valid;
84    }
85    #endregion
86
87    // proxy handler passing key strokes to the parent control
88    private void childControl_KeyDown(object sender, KeyEventArgs e) {
89      ConstantNodeEditDialog_KeyDown(sender, e);
90    }
91
92    private void ConstantNodeEditDialog_KeyDown(object sender, KeyEventArgs e) {
93      if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return)) {
94        if (!ValidateChildren()) return;
95        OnDialogValidated(this, e); // emit validated effect
96        Close();
97      }
98    }
99
100    public event EventHandler DialogValidated;
101    private void OnDialogValidated(object sender, EventArgs e) {
102      if (constantTreeNode == null) return;
103      var value = double.Parse(newValueTextBox.Text);
104      // we impose an extra validation condition: that the new value is different from the original value
105      if (constantTreeNode.Value.Equals(value)) return;
106
107      constantTreeNode.Value = value;
108      DialogResult = DialogResult.OK;
109      var dialogValidated = DialogValidated;
110      if (dialogValidated != null)
111        dialogValidated(sender, e);
112    }
113
114    private void cancelButton_Click(object sender, EventArgs e) {
115      Close();
116    }
117
118    private void okButton_Click(object sender, EventArgs e) {
119      if (ValidateChildren()) {
120        OnDialogValidated(this, e);
121        Close();
122      }
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.