Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/TreeEditDialogs/SymbolicExpressionTreeVariableNodeEditDialog.cs @ 10679

Last change on this file since 10679 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 5.4 KB
RevLine 
[8409]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 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
22using System;
23using System.ComponentModel;
24using System.Text;
25using System.Windows.Forms;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
[8980]30  public partial class VariableNodeEditDialog : Form {
31    private VariableTreeNode variableTreeNode;
32    public VariableTreeNode NewNode {
33      get { return variableTreeNode; }
[8409]34      set {
35        if (InvokeRequired)
[8980]36          Invoke(new Action<SymbolicExpressionTreeNode>(x => variableTreeNode = (VariableTreeNode)x), value);
[8409]37        else
[8980]38          variableTreeNode = value;
[8409]39      }
40    }
41
[8980]42    public VariableNodeEditDialog(ISymbolicExpressionTreeNode node) {
[8409]43      InitializeComponent();
44      oldValueTextBox.TabStop = false; // cannot receive focus using tab key
[8980]45
46      NewNode = (VariableTreeNode)node; // will throw an invalid cast exception if node is not of the correct type
47      InitializeFields();
[8409]48    }
49
[8980]50    private void InitializeFields() {
51      if (NewNode == null)
52        throw new ArgumentException("Node is not a constant.");
53      else {
54        this.Text = "Edit variable";
55        newValueTextBox.Text = oldValueTextBox.Text = Math.Round(variableTreeNode.Weight, 4).ToString();
[8409]56        // add a dropbox containing all the available variable names
57        variableNameLabel.Visible = true;
58        variableNamesCombo.Visible = true;
[8980]59        foreach (var name in variableTreeNode.Symbol.VariableNames) variableNamesCombo.Items.Add(name);
60        variableNamesCombo.SelectedIndex = variableNamesCombo.Items.IndexOf(variableTreeNode.VariableName);
[8409]61      }
62    }
63
64    #region text box validation and events
65    private void newValueTextBox_Validating(object sender, CancelEventArgs e) {
66      string errorMessage;
67      if (!ValidateNewValue(newValueTextBox.Text, out errorMessage)) {
68        e.Cancel = true;
69        errorProvider.SetError(newValueTextBox, errorMessage);
70        newValueTextBox.SelectAll();
71      }
72    }
73
74    private void newValueTextBox_Validated(object sender, EventArgs e) {
75      errorProvider.SetError(newValueTextBox, string.Empty);
76    }
77
78    private static bool ValidateNewValue(string value, out string errorMessage) {
79      double val;
80      bool valid = double.TryParse(value, out val);
81      errorMessage = string.Empty;
82      if (!valid) {
83        var sb = new StringBuilder();
84        sb.Append("Invalid Value (Valid Value Format: \"");
85        sb.Append(FormatPatterns.GetDoubleFormatPattern());
86        sb.Append("\")");
87        errorMessage = sb.ToString();
88      }
89      return valid;
90    }
91    #endregion
92
93    #region combo box validation and events
94    private void variableNamesCombo_Validating(object sender, CancelEventArgs e) {
95      if (variableNamesCombo.Items.Contains(variableNamesCombo.SelectedItem)) return;
96      e.Cancel = true;
97      errorProvider.SetError(variableNamesCombo, "Invalid variable name");
98      variableNamesCombo.SelectAll();
99    }
100
101    private void variableNamesCombo_Validated(object sender, EventArgs e) {
102      errorProvider.SetError(variableNamesCombo, String.Empty);
103    }
104    #endregion
105    // proxy handler passing key strokes to the parent control
106    private void childControl_KeyDown(object sender, KeyEventArgs e) {
107      ValueChangeDialog_KeyDown(sender, e);
108    }
109
110    private void ValueChangeDialog_KeyDown(object sender, KeyEventArgs e) {
111      if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return)) {
112        if (!ValidateChildren()) return;
[8935]113        OnDialogValidated(this, e); // emit validated effect
[8409]114        Close();
115      }
116    }
117
118    public event EventHandler DialogValidated;
[8935]119    private void OnDialogValidated(object sender, EventArgs e) {
[8980]120      double weight = double.Parse(newValueTextBox.Text);
121      var variableName = (string)variableNamesCombo.SelectedItem;
122      // we impose an extra validation condition: that the weight/value be different than the original ones
123      if (variableTreeNode.Weight.Equals(weight) && variableTreeNode.VariableName.Equals(variableName)) return;
124      variableTreeNode.Weight = weight;
125      variableTreeNode.VariableName = variableName;
126      DialogResult = DialogResult.OK;
[8935]127      var dialogValidated = DialogValidated;
128      if (dialogValidated != null)
129        dialogValidated(sender, e);
130    }
131
132    private void cancelButton_Click(object sender, EventArgs e) {
133      Close();
134    }
135
136    private void okButton_Click(object sender, EventArgs e) {
137      if (ValidateChildren()) {
138        OnDialogValidated(this, e);
139        Close();
140      }
141    }
[8409]142  }
143}
Note: See TracBrowser for help on using the repository browser.