Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8935 was 8935, checked in by bburlacu, 11 years ago

#1763: Bugfixes and refactoring as suggested in the comments above.

File size: 5.0 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 ValueChangeDialog() {
43      InitializeComponent();
44      oldValueTextBox.TabStop = false; // cannot receive focus using tab key
45    }
46
47    public void SetContent(ISymbolicExpressionTreeNode content) {
48      Content = content;
49      if (Content is VariableTreeNode) {
50        this.Text = "Change variable name or weight";
51        var variable = Content as VariableTreeNode;
52        newValueTextBox.Text = oldValueTextBox.Text = Math.Round(variable.Weight, 4).ToString();
53        // add a dropbox containing all the available variable names
54        variableNameLabel.Visible = true;
55        variableNamesCombo.Visible = true;
56        foreach (var name in variable.Symbol.VariableNames) variableNamesCombo.Items.Add(name);
57        variableNamesCombo.SelectedIndex = variableNamesCombo.Items.IndexOf(variable.VariableName);
58      } else if (Content is ConstantTreeNode) {
59        this.Text = "Change constant value";
60        var constant = Content as ConstantTreeNode;
61        newValueTextBox.Text = oldValueTextBox.Text = Math.Round(constant.Value, 4).ToString();
62      }
63    }
64
65    #region text box validation and events
66    private void newValueTextBox_Validating(object sender, CancelEventArgs e) {
67      string errorMessage;
68      if (!ValidateNewValue(newValueTextBox.Text, out errorMessage)) {
69        e.Cancel = true;
70        errorProvider.SetError(newValueTextBox, errorMessage);
71        newValueTextBox.SelectAll();
72      }
73    }
74
75    private void newValueTextBox_Validated(object sender, EventArgs e) {
76      errorProvider.SetError(newValueTextBox, string.Empty);
77    }
78
79    private static bool ValidateNewValue(string value, out string errorMessage) {
80      double val;
81      bool valid = double.TryParse(value, out val);
82      errorMessage = string.Empty;
83      if (!valid) {
84        var sb = new StringBuilder();
85        sb.Append("Invalid Value (Valid Value Format: \"");
86        sb.Append(FormatPatterns.GetDoubleFormatPattern());
87        sb.Append("\")");
88        errorMessage = sb.ToString();
89      }
90      return valid;
91    }
92    #endregion
93
94    #region combo box validation and events
95    private void variableNamesCombo_Validating(object sender, CancelEventArgs e) {
96      if (!(Content is VariableTreeNode)) return;
97      if (variableNamesCombo.Items.Contains(variableNamesCombo.SelectedItem)) return;
98      e.Cancel = true;
99      errorProvider.SetError(variableNamesCombo, "Invalid variable name");
100      variableNamesCombo.SelectAll();
101    }
102
103    private void variableNamesCombo_Validated(object sender, EventArgs e) {
104      errorProvider.SetError(variableNamesCombo, String.Empty);
105    }
106    #endregion
107    // proxy handler passing key strokes to the parent control
108    private void childControl_KeyDown(object sender, KeyEventArgs e) {
109      ValueChangeDialog_KeyDown(sender, e);
110    }
111
112    private void ValueChangeDialog_KeyDown(object sender, KeyEventArgs e) {
113      if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return)) {
114        if (!ValidateChildren()) return;
115        OnDialogValidated(this, e); // emit validated effect
116        Close();
117      }
118    }
119
120    public event EventHandler DialogValidated;
121    private void OnDialogValidated(object sender, EventArgs e) {
122      var dialogValidated = DialogValidated;
123      if (dialogValidated != null)
124        dialogValidated(sender, e);
125    }
126
127    private void cancelButton_Click(object sender, EventArgs e) {
128      Close();
129    }
130
131    private void okButton_Click(object sender, EventArgs e) {
132      if (ValidateChildren()) {
133        OnDialogValidated(this, e);
134        Close();
135      }
136    }
137
138  }
139}
Note: See TracBrowser for help on using the repository browser.