Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/TreeEditDialogs/SymbolicExpressionTreeNodeInsertDialog.cs @ 8409

Last change on this file since 8409 was 8409, checked in by bburlacu, 12 years ago

#1763: Fixed bug when pasting subtrees. Moved the InteractiveSymbolicExpressionTreeChart and dialogs in the HeuristicLab.Problems.DataAnalysis.Symbolic.Views namespace and renamed the impact values calculators to a more sensible name.

File size: 3.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Text;
5using System.Windows.Forms;
6using HeuristicLab.Data;
7using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
8
9namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
10  public partial class InsertNodeDialog : Form {
11    public string Caption {
12      get { return this.Text; }
13      set {
14        if (InvokeRequired)
15          Invoke(new Action<string>(x => this.Caption = x), value);
16        else
17          this.Text = value;
18      }
19    }
20
21    public InsertNodeDialog() {
22      InitializeComponent();
23    }
24
25    public void SetAllowedSymbols(IEnumerable<ISymbol> symbols) {
26      allowedSymbolsCombo.Items.Clear();
27      foreach (var s in symbols) allowedSymbolsCombo.Items.Add(s);
28      allowedSymbolsCombo.SelectedIndex = 0;
29    }
30
31    public ISymbol SelectedSymbol() {
32      return (ISymbol)allowedSymbolsCombo.SelectedItem;
33    }
34
35    private void allowedSymbolsCombo_SelectedIndexChanged(object sender, EventArgs e) {
36      var combo = (ComboBox)sender;
37      var symbol = combo.Items[combo.SelectedIndex];
38      if (symbol is Constant) {
39        // add controls to the dialog for changing the constant value
40        variableNameLabel.Visible = false;
41        variableNamesCombo.Visible = false;
42        variableWeightLabel.Visible = false;
43        variableWeightTextBox.Visible = false;
44        constantValueLabel.Visible = true;
45        constantValueTextBox.Visible = true;
46      } else if (symbol is Variable) {
47        var variable = (Variable)symbol;
48        foreach (var name in variable.VariableNames) variableNamesCombo.Items.Add(name);
49        variableNamesCombo.SelectedIndex = 0;
50        variableNameLabel.Visible = true;
51        variableNamesCombo.Visible = true;
52        variableWeightLabel.Visible = true;
53        variableWeightTextBox.Visible = true;
54        constantValueLabel.Visible = false;
55        constantValueTextBox.Visible = false;
56        // add controls to the dialog for changing the variable name or weight
57      }
58    }
59
60    // validation
61    private void variableWeightTextBox_Validating(object sender, CancelEventArgs e) {
62      string errorMessage;
63      if (ValidateDoubleValue(variableWeightTextBox.Text, out errorMessage)) return;
64      e.Cancel = true;
65      errorProvider.SetError(variableWeightTextBox, errorMessage);
66      variableWeightTextBox.SelectAll();
67    }
68
69    private void constantValueTextBox_Validating(object sender, CancelEventArgs e) {
70      string errorMessage;
71      if (ValidateDoubleValue(constantValueTextBox.Text, out errorMessage)) return;
72      e.Cancel = true;
73      errorProvider.SetError(constantValueTextBox, errorMessage);
74      constantValueTextBox.SelectAll();
75    }
76
77    private static bool ValidateDoubleValue(string value, out string errorMessage) {
78      double val;
79      bool valid = double.TryParse(value, out val);
80      errorMessage = string.Empty;
81      if (!valid) {
82        var sb = new StringBuilder();
83        sb.Append("Invalid Value (Valid Value Format: \"");
84        sb.Append(FormatPatterns.GetDoubleFormatPattern());
85        sb.Append("\")");
86        errorMessage = sb.ToString();
87      }
88      return valid;
89    }
90
91    public event EventHandler DialogValidated;
92
93    private void childControl_KeyDown(object sender, KeyEventArgs e) {
94      insertNodeDialog_KeyDown(sender, e);
95    }
96
97    private void insertNodeDialog_KeyDown(object sender, KeyEventArgs e) {
98      if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return) {
99        if (ValidateChildren()) {
100          DialogValidated(this, e);
101          Close();
102        }
103      }
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.