#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 HeuristicLab.Core.Views; using HeuristicLab.Data; using HeuristicLab.MainForm; namespace HeuristicLab.Encodings.ParameterConfigurationEncoding.Views { [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 { optimizeCheckBox.Checked = Content.Optimize; if (Content.Optimize) { viewHost.ViewType = null; var pc = Content as IParameterConfiguration; var vc = Content as IValueConfiguration; if (pc != null) { if (pc is SingleValuedParameterConfiguration) { viewHost.Content = ((ParameterizedValueConfiguration)pc.ValueConfigurations.Single()).ParameterConfigurations; } else { viewHost.Content = pc.ValueConfigurations; viewHost.ViewType = typeof(CheckedValueConfigurationListView); } } 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)) { viewHost.ViewsLabelVisible = true; viewHost.Content = rvc.RangeConstraint; } else if (Content.ActualValue.ValueDataType == typeof(BoolValue)) { viewHost.Content = null; // no configuration required } } var pvc = Content as ParameterizedValueConfiguration; if (pvc != null) { viewHost.ViewsLabelVisible = false; viewHost.Content = pvc.ParameterConfigurations; } } else { throw new NotSupportedException("Only IParameterConfiguration and IValueConfiguration types are allowed."); } } else { viewHost.ViewsLabelVisible = false; if (Content.ValuesReadOnly) { viewHost.Content = new MessageItem("Cannot modify value directly. Please modify the value in the list of problems."); } else { // the content's valid values get lost after optimize is set to true because the constrained value // does not contain the earlier valid values; therefore, the same constrained value with the correct // set of valid values is created before the assignment below var configuration = viewHost.Content as CheckedValueConfigurationList; if (configuration != null) { Content.ActualValue = new ConstrainedValue(Content.ActualValue.Value, Content.ActualValue.ValueDataType, configuration.ValidValues, Content.ActualValue.IsNullable); } 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 { viewHost.Content = null; } } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); if (Content != null) { optimizeCheckBox.Enabled = !ReadOnly && Content.IsOptimizable; } } #region Event Handlers (child controls) private void optimizeCheckBox_CheckedChanged(object sender, EventArgs e) { Content.Optimize = optimizeCheckBox.Checked; } #endregion } }