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