[8535] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[8517] | 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Core.Views;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.MainForm;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.Encodings.ParameterConfigurationEncoding.Views {
|
---|
| 29 | [View("IOptimizable View")]
|
---|
| 30 | [Content(typeof(IOptimizable), IsDefaultView = false)]
|
---|
| 31 | [Content(typeof(ParameterConfiguration), IsDefaultView = true)]
|
---|
| 32 | [Content(typeof(ValueConfiguration), IsDefaultView = true)]
|
---|
| 33 | public sealed partial class OptimizableView : ItemView {
|
---|
| 34 | public new IOptimizable Content {
|
---|
| 35 | get { return (IOptimizable)base.Content; }
|
---|
| 36 | set { base.Content = value; }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | public OptimizableView() {
|
---|
| 40 | InitializeComponent();
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | protected override void DeregisterContentEvents() {
|
---|
| 44 | Content.OptimizeChanged -= new EventHandler(Content_OptimizeChanged);
|
---|
| 45 | Content.IsOptimizableChanged -= new EventHandler(Content_IsOptimizabeChanged);
|
---|
| 46 | base.DeregisterContentEvents();
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | protected override void RegisterContentEvents() {
|
---|
| 50 | base.RegisterContentEvents();
|
---|
| 51 | Content.IsOptimizableChanged += new EventHandler(Content_IsOptimizabeChanged);
|
---|
| 52 | Content.OptimizeChanged += new EventHandler(Content_OptimizeChanged);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | #region Event Handlers (Content)
|
---|
| 56 | void Content_OptimizeChanged(object sender, EventArgs e) {
|
---|
| 57 | if (InvokeRequired) {
|
---|
| 58 | Invoke(new EventHandler(Content_OptimizeChanged), sender, e);
|
---|
| 59 | } else {
|
---|
[8648] | 60 | optimizeCheckBox.Checked = Content.Optimize;
|
---|
[8517] | 61 | if (Content.Optimize) {
|
---|
[8648] | 62 | viewHost.ViewType = null;
|
---|
[8517] | 63 |
|
---|
| 64 | var pc = Content as IParameterConfiguration;
|
---|
| 65 | var vc = Content as IValueConfiguration;
|
---|
| 66 |
|
---|
| 67 | if (pc != null) {
|
---|
| 68 | if (pc is SingleValuedParameterConfiguration) {
|
---|
[8648] | 69 | viewHost.Content = ((ParameterizedValueConfiguration)pc.ValueConfigurations.Single()).ParameterConfigurations;
|
---|
[8517] | 70 | } else {
|
---|
[8648] | 71 | viewHost.Content = pc.ValueConfigurations;
|
---|
[9356] | 72 | viewHost.ViewType = typeof(ValueConfigurationCheckedItemListView);
|
---|
[8517] | 73 | }
|
---|
| 74 | } else if (vc != null) {
|
---|
| 75 | var rvc = Content as RangeValueConfiguration;
|
---|
| 76 | if (rvc != null) {
|
---|
| 77 | if (Content.ActualValue.ValueDataType == typeof(IntValue) ||
|
---|
| 78 | Content.ActualValue.ValueDataType == typeof(DoubleValue) ||
|
---|
| 79 | Content.ActualValue.ValueDataType == typeof(PercentValue)) {
|
---|
[8648] | 80 | viewHost.ViewsLabelVisible = true;
|
---|
| 81 | viewHost.Content = rvc.RangeConstraint;
|
---|
[8517] | 82 | } else if (Content.ActualValue.ValueDataType == typeof(BoolValue)) {
|
---|
[8648] | 83 | viewHost.Content = null; // no configuration required
|
---|
[8517] | 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | var pvc = Content as ParameterizedValueConfiguration;
|
---|
| 88 | if (pvc != null) {
|
---|
[8648] | 89 | viewHost.ViewsLabelVisible = false;
|
---|
| 90 | viewHost.Content = pvc.ParameterConfigurations;
|
---|
[8517] | 91 | }
|
---|
| 92 | } else {
|
---|
| 93 | throw new NotSupportedException("Only IParameterConfiguration and IValueConfiguration types are allowed.");
|
---|
| 94 | }
|
---|
| 95 | } else {
|
---|
[8648] | 96 | viewHost.ViewsLabelVisible = false;
|
---|
[8517] | 97 | if (Content.ValuesReadOnly) {
|
---|
[8648] | 98 | viewHost.Content = new MessageItem("Cannot modify value directly. Please modify the value in the list of problems.");
|
---|
[8517] | 99 | } else {
|
---|
[8648] | 100 | // the content's valid values get lost after optimize is set to true because the constrained value
|
---|
| 101 | // does not contain the earlier valid values; therefore, the same constrained value with the correct
|
---|
| 102 | // set of valid values is created before the assignment below
|
---|
| 103 | var configuration = viewHost.Content as CheckedValueConfigurationList;
|
---|
[8657] | 104 | if (configuration != null && configuration.ValidValues.Any(x => x.GetType() == Content.ActualValue.ValueDataType)) {
|
---|
[8648] | 105 | Content.ActualValue = new ConstrainedValue(Content.ActualValue.Value,
|
---|
| 106 | Content.ActualValue.ValueDataType,
|
---|
| 107 | configuration.ValidValues,
|
---|
| 108 | Content.ActualValue.IsNullable);
|
---|
| 109 | }
|
---|
| 110 | viewHost.Content = Content.ActualValue;
|
---|
[8517] | 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | SetEnabledStateOfControls();
|
---|
| 115 | }
|
---|
| 116 | void Content_IsOptimizabeChanged(object sender, EventArgs e) {
|
---|
| 117 | if (InvokeRequired) {
|
---|
| 118 | Invoke(new EventHandler(Content_IsOptimizabeChanged), sender, e);
|
---|
| 119 | } else {
|
---|
| 120 | SetEnabledStateOfControls();
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | #endregion
|
---|
| 124 |
|
---|
| 125 | protected override void OnContentChanged() {
|
---|
| 126 | base.OnContentChanged();
|
---|
| 127 | if (Content != null) {
|
---|
| 128 | Content_OptimizeChanged(this, EventArgs.Empty);
|
---|
| 129 | } else {
|
---|
[8648] | 130 | viewHost.Content = null;
|
---|
[8517] | 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | protected override void SetEnabledStateOfControls() {
|
---|
| 135 | base.SetEnabledStateOfControls();
|
---|
| 136 | if (Content != null) {
|
---|
[8648] | 137 | optimizeCheckBox.Enabled = !ReadOnly && Content.IsOptimizable;
|
---|
[8517] | 138 | }
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | #region Event Handlers (child controls)
|
---|
| 142 | private void optimizeCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
| 143 | Content.Optimize = optimizeCheckBox.Checked;
|
---|
| 144 | }
|
---|
| 145 | #endregion
|
---|
| 146 | }
|
---|
| 147 | }
|
---|