Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/TreeEditDialogs/SymbolicExpressionTreeVariableNodeEditDialog.cs @ 14562

Last change on this file since 14562 was 14432, checked in by bburlacu, 8 years ago

#2710: Offer the user the option to provide the variable name when the symbol does not contain a list of available variable names. In this case the combo box is replaced by a text box.

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