using System; using System.Linq; using HeuristicLab.Core.Views; using HeuristicLab.Data; using HeuristicLab.MainForm; 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; var pc = Content as IParameterConfiguration; var vc = Content as IValueConfiguration; if (pc != null) { if (pc is SingleValuedParameterConfiguration) { this.viewHost.Content = ((ParameterizedValueConfiguration)pc.ValueConfigurations.Single()).ParameterConfigurations; } else { this.viewHost.Content = pc.ValueConfigurations; } } else if (vc != null) { var rvc = Content as RangeValueConfiguration; if (rvc != null) { if (Content.ActualValue.ValueDataType == typeof(IntValue) || Content.ActualValue.ValueDataType == typeof(DoubleValue) || Content.ActualValue.ValueDataType == typeof(PercentValue)) { this.viewHost.ViewsLabelVisible = true; this.viewHost.Content = rvc.RangeConstraint; } else if (Content.ActualValue.ValueDataType == typeof(BoolValue)) { this.viewHost.Content = null; // no configuration required } } var pvc = Content as ParameterizedValueConfiguration; if (pvc != null) { this.viewHost.ViewsLabelVisible = false; this.viewHost.Content = pvc.ParameterConfigurations; } //var symbolicVc = Content as SymbolicExpressionGrammarValueConfiguration; //if (symbolicVc != null) { // this.viewHost.ViewsLabelVisible = false; // this.viewHost.Content = symbolicVc.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 } }