#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 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.Linq;
using System.Windows.Forms;
using HeuristicLab.Core.Views;
using HeuristicLab.MainForm;
using HeuristicLab.PluginInfrastructure;
namespace HeuristicLab.Encodings.ParameterConfigurationEncoding.Views {
///
/// The visual representation of a .
///
[View("Value View")]
[Content(typeof(ConstrainedValue), true)]
public partial class ValueView : ItemView {
///
/// Gets or sets the variable to represent visually.
///
public new ConstrainedValue Content {
get { return (ConstrainedValue)base.Content; }
set { base.Content = value; }
}
public ValueView() {
InitializeComponent();
}
protected override void OnContentChanged() {
base.OnContentChanged();
if (Content != null) {
valueViewHost.Content = Content.Value;
} else {
valueViewHost.Content = null;
}
SetEnabledStateOfControls();
}
protected override void SetEnabledStateOfControls() {
base.SetEnabledStateOfControls();
if (Content != null) {
clearValueButton.Enabled = Content.Value != null && Content.IsNullable; // in this case IsNullable is not set correctly
setValueButton.Enabled = Content.ValueDataType != null;
}
}
private void setValueButton_Click(object sender, EventArgs e) {
var withoutNullValue = Content.ValidValues.Where(x => x != null && !(x is NullValue));
var typeSelectorDialog = new TypeSelectorDialog();
typeSelectorDialog.Caption = "Select Item";
typeSelectorDialog.TypeSelector.Caption = "Available Items";
typeSelectorDialog.TypeSelector.Configure(withoutNullValue.Select(x => x.GetType()), false, false, false);
if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
try {
Content.Value = withoutNullValue.Single(x => x.GetType() == typeSelectorDialog.TypeSelector.SelectedType);
valueViewHost.Content = Content.Value;
}
catch (Exception ex) {
ErrorHandling.ShowErrorDialog(this, ex);
}
}
SetEnabledStateOfControls();
}
private void clearValueButton_Click(object sender, EventArgs e) {
Content.Value = null;
valueViewHost.Content = null;
SetEnabledStateOfControls();
}
}
}