Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/TreeEditDialogs/SymbolicExpressionTreeNodeInsertDialog.cs

Last change on this file was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 5.6 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.Collections.Generic;
24using System.ComponentModel;
25using System.Linq;
26using System.Text;
27using System.Windows.Forms;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
32  public partial class InsertNodeDialog : Form {
33    public string SelectedVariableName {
34      get {
35        var variable = SelectedSymbol as Variable;
36        if (variable == null)
37          return string.Empty;
38        return variable.VariableNames.Any() ? variableNamesCombo.Text : variableNameTextBox.Text;
39      }
40    }
41
42    public InsertNodeDialog() {
43      InitializeComponent();
44    }
45
46    public void SetAllowedSymbols(IEnumerable<ISymbol> symbols) {
47      allowedSymbolsCombo.Items.Clear();
48      foreach (var s in symbols) allowedSymbolsCombo.Items.Add(s);
49      allowedSymbolsCombo.SelectedIndex = 0;
50    }
51
52    public ISymbol SelectedSymbol {
53      get { return (ISymbol)allowedSymbolsCombo.SelectedItem; }
54    }
55
56    private void allowedSymbolsCombo_SelectedIndexChanged(object sender, EventArgs e) {
57      var combo = (ComboBox)sender;
58      var symbol = combo.Items[combo.SelectedIndex];
59      if (symbol is Constant) {
60        // add controls to the dialog for changing the constant value
61        variableNameLabel.Visible = false;
62        variableNamesCombo.Visible = false;
63        variableWeightLabel.Visible = false;
64        variableWeightTextBox.Visible = false;
65        constantValueLabel.Visible = true;
66        constantValueTextBox.Visible = true;
67      } else if (symbol is VariableBase) {
68        var variableSymbol = (VariableBase)symbol;
69        if (variableSymbol.VariableNames.Any()) {
70          foreach (var name in variableSymbol.VariableNames)
71            variableNamesCombo.Items.Add(name);
72          variableNamesCombo.SelectedIndex = 0;
73          variableNamesCombo.Visible = true;
74          variableNameTextBox.Visible = false;
75        } else {
76          variableNamesCombo.Visible = false;
77          variableNameTextBox.Visible = true;
78        }
79        variableNameLabel.Visible = true;
80        variableWeightLabel.Visible = true;
81        variableWeightTextBox.Visible = true;
82        constantValueLabel.Visible = false;
83        constantValueTextBox.Visible = false;
84        // add controls to the dialog for changing the variable name or weight
85      } else {
86        variableNameLabel.Visible = false;
87        variableNamesCombo.Visible = false;
88        variableWeightLabel.Visible = false;
89        variableWeightTextBox.Visible = false;
90        constantValueLabel.Visible = false;
91        constantValueTextBox.Visible = false;
92      }
93    }
94
95    // validation
96    private void variableWeightTextBox_Validating(object sender, CancelEventArgs e) {
97      string errorMessage;
98      if (ValidateDoubleValue(variableWeightTextBox.Text, out errorMessage)) return;
99      e.Cancel = true;
100      errorProvider.SetError(variableWeightTextBox, errorMessage);
101      variableWeightTextBox.SelectAll();
102    }
103
104    private void constantValueTextBox_Validating(object sender, CancelEventArgs e) {
105      string errorMessage;
106      if (ValidateDoubleValue(constantValueTextBox.Text, out errorMessage)) return;
107      e.Cancel = true;
108      errorProvider.SetError(constantValueTextBox, errorMessage);
109      constantValueTextBox.SelectAll();
110    }
111
112    private static bool ValidateDoubleValue(string value, out string errorMessage) {
113      double val;
114      bool valid = double.TryParse(value, out val);
115      errorMessage = string.Empty;
116      if (!valid) {
117        var sb = new StringBuilder();
118        sb.Append("Invalid Value (Valid Value Format: \"");
119        sb.Append(FormatPatterns.GetDoubleFormatPattern());
120        sb.Append("\")");
121        errorMessage = sb.ToString();
122      }
123      return valid;
124    }
125
126    public event EventHandler DialogValidated;
127    private void OnDialogValidated(object sender, EventArgs e) {
128      DialogResult = DialogResult.OK;
129      var dialogValidated = DialogValidated;
130      if (dialogValidated != null)
131        dialogValidated(sender, e);
132    }
133
134    private void childControl_KeyDown(object sender, KeyEventArgs e) {
135      insertNodeDialog_KeyDown(sender, e);
136    }
137
138    private void insertNodeDialog_KeyDown(object sender, KeyEventArgs e) {
139      if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return) {
140        if (ValidateChildren()) {
141          OnDialogValidated(this, e);
142          Close();
143        }
144      }
145    }
146
147    private void okButton_Click(object sender, EventArgs e) {
148      if (ValidateChildren()) {
149        OnDialogValidated(this, e);
150        Close();
151      }
152    }
153
154    private void cancelButton_Click(object sender, EventArgs e) {
155      Close();
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.