using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; using HeuristicLab.Parameters; using HeuristicLab.Core; using HeuristicLab.Data; namespace HeuristicLab.Problems.MetaOptimization.Views.ValueConfigurationViews { [View("IOptimizable View")] [Content(typeof(IOptimizable), IsDefaultView = false)] [Content(typeof(ParameterConfiguration), IsDefaultView = true)] [Content(typeof(ValueConfiguration), IsDefaultView = true)] public sealed partial class OptimizableView : ItemView { public new IOptimizable Content { get { return (IOptimizable)base.Content; } set { base.Content = value; } } public OptimizableView() { InitializeComponent(); } protected override void DeregisterContentEvents() { Content.OptimizeChanged -= new EventHandler(Content_OptimizeChanged); Content.IsOptimizableChanged -= new EventHandler(Content_IsOptimizabeChanged); base.DeregisterContentEvents(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.IsOptimizableChanged += new EventHandler(Content_IsOptimizabeChanged); Content.OptimizeChanged += new EventHandler(Content_OptimizeChanged); } #region Event Handlers (Content) void Content_OptimizeChanged(object sender, EventArgs e) { if (InvokeRequired) { Invoke(new EventHandler(Content_OptimizeChanged), sender, e); } else { this.optimizeCheckBox.Checked = Content.Optimize; if (Content.Optimize) { this.viewHost.ViewType = null; if (Content is IParameterConfiguration) { this.viewHost.Content = ((IParameterConfiguration)Content).ValueConfigurations; } else if (Content is IValueConfiguration) { if (Content.ActualValue.ValueDataType == typeof(IntValue) || Content.ActualValue.ValueDataType == typeof(DoubleValue) || Content.ActualValue.ValueDataType == typeof(PercentValue)) { this.viewHost.ViewsLabelVisible = true; this.viewHost.Content = ((IValueConfiguration)Content).RangeConstraint; } else if(Content.ActualValue.ValueDataType == typeof(BoolValue)) { this.viewHost.Content = null; // no configuration required } else { this.viewHost.ViewsLabelVisible = false; this.viewHost.Content = ((IValueConfiguration)Content).ParameterConfigurations; } } else { throw new NotSupportedException("Only IParameterConfiguration and IValueConfiguration types are allowed."); } } else { this.viewHost.ViewsLabelVisible = false; this.viewHost.Content = Content.ActualValue; } } SetEnabledStateOfControls(); } void Content_IsOptimizabeChanged(object sender, EventArgs e) { if (InvokeRequired) { Invoke(new EventHandler(Content_IsOptimizabeChanged), sender, e); } else { SetEnabledStateOfControls(); } } #endregion protected override void OnContentChanged() { base.OnContentChanged(); if (Content != null) { Content_OptimizeChanged(this, EventArgs.Empty); } else { this.viewHost.Content = null; } } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); if (Content != null) { this.optimizeCheckBox.Enabled = !this.ReadOnly && Content.IsOptimizable; } } #region Event Handlers (child controls) private void optimizeCheckBox_CheckedChanged(object sender, EventArgs e) { Content.Optimize = optimizeCheckBox.Checked; } #endregion } }