1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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.ComponentModel;
|
---|
24 | using System.Linq;
|
---|
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 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 => variableTreeNode = (VariableTreeNode)x), value);
|
---|
38 | else
|
---|
39 | variableTreeNode = value;
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public string SelectedVariableName {
|
---|
44 | get { return variableNamesCombo.Visible ? variableNamesCombo.Text : variableNameTextBox.Text; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public VariableNodeEditDialog(ISymbolicExpressionTreeNode node) {
|
---|
48 | InitializeComponent();
|
---|
49 | oldValueTextBox.TabStop = false; // cannot receive focus using tab key
|
---|
50 | NewNode = (VariableTreeNode)node; // will throw an invalid cast exception if node is not of the correct type
|
---|
51 | InitializeFields();
|
---|
52 | }
|
---|
53 |
|
---|
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();
|
---|
60 | // add a dropbox containing all the available variable names
|
---|
61 | variableNameLabel.Visible = true;
|
---|
62 | variableNamesCombo.Visible = true;
|
---|
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 | }
|
---|
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) {
|
---|
107 | if (variableNamesCombo.Items.Count == 0) return;
|
---|
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;
|
---|
126 | OnDialogValidated(this, e); // emit validated effect
|
---|
127 | Close();
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | public event EventHandler DialogValidated;
|
---|
132 | private void OnDialogValidated(object sender, EventArgs e) {
|
---|
133 | double weight = double.Parse(newValueTextBox.Text);
|
---|
134 | // we impose an extra validation condition: that the weight/value be different than the original ones
|
---|
135 | var variableName = SelectedVariableName;
|
---|
136 | if (variableTreeNode.Weight.Equals(weight) && variableTreeNode.VariableName.Equals(variableName)) return;
|
---|
137 | variableTreeNode.Weight = weight;
|
---|
138 | variableTreeNode.VariableName = variableName;
|
---|
139 | DialogResult = DialogResult.OK;
|
---|
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 | }
|
---|
155 | }
|
---|
156 | }
|
---|