[8409] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11171] | 3 | * Copyright (C) 2002-2014 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.ComponentModel;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
|
---|
[8980] | 30 | public partial class ConstantNodeEditDialog : Form {
|
---|
| 31 | private ConstantTreeNode constantTreeNode;
|
---|
| 32 | public ConstantTreeNode NewNode {
|
---|
| 33 | get { return constantTreeNode; }
|
---|
[8409] | 34 | set {
|
---|
| 35 | if (InvokeRequired)
|
---|
[8980] | 36 | Invoke(new Action<SymbolicExpressionTreeNode>(x => constantTreeNode = (ConstantTreeNode)x), value);
|
---|
[8409] | 37 | else
|
---|
[8980] | 38 | constantTreeNode = value;
|
---|
[8409] | 39 | }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[8980] | 42 | public ConstantNodeEditDialog(ISymbolicExpressionTreeNode node) {
|
---|
[8409] | 43 | InitializeComponent();
|
---|
| 44 | oldValueTextBox.TabStop = false; // cannot receive focus using tab key
|
---|
[8980] | 45 | NewNode = (ConstantTreeNode)node;
|
---|
| 46 | InitializeFields();
|
---|
[8409] | 47 | }
|
---|
| 48 |
|
---|
[8980] | 49 | private void InitializeFields() {
|
---|
| 50 | if (NewNode == null)
|
---|
| 51 | throw new ArgumentException("Node is not a constant.");
|
---|
| 52 | else {
|
---|
| 53 | this.Text = "Edit constant";
|
---|
| 54 | newValueTextBox.Text = oldValueTextBox.Text = Math.Round(constantTreeNode.Value, 4).ToString();
|
---|
[8409] | 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | #region text box validation and events
|
---|
| 59 | private void newValueTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
| 60 | string errorMessage;
|
---|
| 61 | if (!ValidateNewValue(newValueTextBox.Text, out errorMessage)) {
|
---|
| 62 | e.Cancel = true;
|
---|
| 63 | errorProvider.SetError(newValueTextBox, errorMessage);
|
---|
| 64 | newValueTextBox.SelectAll();
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | private void newValueTextBox_Validated(object sender, EventArgs e) {
|
---|
| 69 | errorProvider.SetError(newValueTextBox, string.Empty);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | private static bool ValidateNewValue(string value, out string errorMessage) {
|
---|
| 73 | double val;
|
---|
| 74 | bool valid = double.TryParse(value, out val);
|
---|
| 75 | errorMessage = string.Empty;
|
---|
| 76 | if (!valid) {
|
---|
| 77 | var sb = new StringBuilder();
|
---|
| 78 | sb.Append("Invalid Value (Valid Value Format: \"");
|
---|
| 79 | sb.Append(FormatPatterns.GetDoubleFormatPattern());
|
---|
| 80 | sb.Append("\")");
|
---|
| 81 | errorMessage = sb.ToString();
|
---|
| 82 | }
|
---|
| 83 | return valid;
|
---|
| 84 | }
|
---|
| 85 | #endregion
|
---|
| 86 |
|
---|
| 87 | // proxy handler passing key strokes to the parent control
|
---|
| 88 | private void childControl_KeyDown(object sender, KeyEventArgs e) {
|
---|
[8980] | 89 | ConstantNodeEditDialog_KeyDown(sender, e);
|
---|
[8409] | 90 | }
|
---|
| 91 |
|
---|
[8980] | 92 | private void ConstantNodeEditDialog_KeyDown(object sender, KeyEventArgs e) {
|
---|
[8409] | 93 | if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return)) {
|
---|
| 94 | if (!ValidateChildren()) return;
|
---|
[8935] | 95 | OnDialogValidated(this, e); // emit validated effect
|
---|
[8409] | 96 | Close();
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | public event EventHandler DialogValidated;
|
---|
[8935] | 101 | private void OnDialogValidated(object sender, EventArgs e) {
|
---|
[8980] | 102 | if (constantTreeNode == null) return;
|
---|
| 103 | var value = double.Parse(newValueTextBox.Text);
|
---|
| 104 | // we impose an extra validation condition: that the new value is different from the original value
|
---|
| 105 | if (constantTreeNode.Value.Equals(value)) return;
|
---|
| 106 |
|
---|
| 107 | constantTreeNode.Value = value;
|
---|
| 108 | DialogResult = DialogResult.OK;
|
---|
[8935] | 109 | var dialogValidated = DialogValidated;
|
---|
| 110 | if (dialogValidated != null)
|
---|
| 111 | dialogValidated(sender, e);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | private void cancelButton_Click(object sender, EventArgs e) {
|
---|
| 115 | Close();
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | private void okButton_Click(object sender, EventArgs e) {
|
---|
| 119 | if (ValidateChildren()) {
|
---|
| 120 | OnDialogValidated(this, e);
|
---|
| 121 | Close();
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
[8409] | 124 | }
|
---|
| 125 | }
|
---|