Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/TreeEditDialogs/SymbolicExpressionTreeVariableNodeEditDialog.cs @ 17207

Last change on this file since 17207 was 17207, checked in by gkronber, 5 years ago

#2971: merged r17180:17184 from trunk to branch

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