#region License Information
/* HeuristicLab
* Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HeuristicLab.Data;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
public partial class InsertNodeDialog : Form {
public string SelectedVariableName {
get {
var variable = SelectedSymbol as Variable;
if (variable == null)
return string.Empty;
return variable.VariableNames.Any() ? variableNamesCombo.Text : variableNameTextBox.Text;
}
}
public InsertNodeDialog() {
InitializeComponent();
}
public void SetAllowedSymbols(IEnumerable symbols) {
allowedSymbolsCombo.Items.Clear();
foreach (var s in symbols) allowedSymbolsCombo.Items.Add(s);
allowedSymbolsCombo.SelectedIndex = 0;
}
public ISymbol SelectedSymbol {
get { return (ISymbol)allowedSymbolsCombo.SelectedItem; }
}
private void allowedSymbolsCombo_SelectedIndexChanged(object sender, EventArgs e) {
var combo = (ComboBox)sender;
var symbol = combo.Items[combo.SelectedIndex];
if (symbol is Number) {
// add controls to the dialog for changing the number value
variableNameLabel.Visible = false;
variableNamesCombo.Visible = false;
variableWeightLabel.Visible = false;
variableWeightTextBox.Visible = false;
numberValueLabel.Visible = true;
numberValueTextBox.Visible = true;
} else if (symbol is VariableBase) {
var variableSymbol = (VariableBase)symbol;
if (variableSymbol.VariableNames.Any()) {
foreach (var name in variableSymbol.VariableNames)
variableNamesCombo.Items.Add(name);
variableNamesCombo.SelectedIndex = 0;
variableNamesCombo.Visible = true;
variableNameTextBox.Visible = false;
} else {
variableNamesCombo.Visible = false;
variableNameTextBox.Visible = true;
}
variableNameLabel.Visible = true;
variableWeightLabel.Visible = true;
variableWeightTextBox.Visible = true;
numberValueLabel.Visible = false;
numberValueTextBox.Visible = false;
// add controls to the dialog for changing the variable name or weight
} else {
variableNameLabel.Visible = false;
variableNamesCombo.Visible = false;
variableWeightLabel.Visible = false;
variableWeightTextBox.Visible = false;
numberValueLabel.Visible = false;
numberValueTextBox.Visible = false;
}
}
// validation
private void variableWeightTextBox_Validating(object sender, CancelEventArgs e) {
string errorMessage;
if (ValidateDoubleValue(variableWeightTextBox.Text, out errorMessage)) return;
e.Cancel = true;
errorProvider.SetError(variableWeightTextBox, errorMessage);
variableWeightTextBox.SelectAll();
}
private void numberValueTextBox_Validating(object sender, CancelEventArgs e) {
string errorMessage;
if (ValidateDoubleValue(numberValueTextBox.Text, out errorMessage)) return;
e.Cancel = true;
errorProvider.SetError(numberValueTextBox, errorMessage);
numberValueTextBox.SelectAll();
}
private static bool ValidateDoubleValue(string value, out string errorMessage) {
double val;
bool valid = double.TryParse(value, out val);
errorMessage = string.Empty;
if (!valid) {
var sb = new StringBuilder();
sb.Append("Invalid Value (Valid Value Format: \"");
sb.Append(FormatPatterns.GetDoubleFormatPattern());
sb.Append("\")");
errorMessage = sb.ToString();
}
return valid;
}
public event EventHandler DialogValidated;
private void OnDialogValidated(object sender, EventArgs e) {
DialogResult = DialogResult.OK;
var dialogValidated = DialogValidated;
if (dialogValidated != null)
dialogValidated(sender, e);
}
private void childControl_KeyDown(object sender, KeyEventArgs e) {
insertNodeDialog_KeyDown(sender, e);
}
private void insertNodeDialog_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return) {
if (ValidateChildren()) {
OnDialogValidated(this, e);
Close();
}
}
}
private void okButton_Click(object sender, EventArgs e) {
if (ValidateChildren()) {
OnDialogValidated(this, e);
Close();
}
}
private void cancelButton_Click(object sender, EventArgs e) {
Close();
}
}
}