source: branches/ParameterConfigurationEncoding/HeuristicLab.Encodings.ParameterConfigurationEncoding.Views/3.3/OptimizableView.cs @ 8648

Last change on this file since 8648 was 8648, checked in by jkarder, 9 years ago

#1853: fixed loss of valid values caused by setting optimize to true

File size: 5.7 KB
Line 
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
22using System;
23using System.Linq;
24using HeuristicLab.Core.Views;
25using HeuristicLab.Data;
26using HeuristicLab.MainForm;
27
28namespace 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 {
60        optimizeCheckBox.Checked = Content.Optimize;
61        if (Content.Optimize) {
62          viewHost.ViewType = null;
63
64          var pc = Content as IParameterConfiguration;
65          var vc = Content as IValueConfiguration;
66
67          if (pc != null) {
68            if (pc is SingleValuedParameterConfiguration) {
69              viewHost.Content = ((ParameterizedValueConfiguration)pc.ValueConfigurations.Single()).ParameterConfigurations;
70            } else {
71              viewHost.Content = pc.ValueConfigurations;
72            }
73          } else if (vc != null) {
74            var rvc = Content as RangeValueConfiguration;
75            if (rvc != null) {
76              if (Content.ActualValue.ValueDataType == typeof(IntValue) ||
77                Content.ActualValue.ValueDataType == typeof(DoubleValue) ||
78                Content.ActualValue.ValueDataType == typeof(PercentValue)) {
79                viewHost.ViewsLabelVisible = true;
80                viewHost.Content = rvc.RangeConstraint;
81              } else if (Content.ActualValue.ValueDataType == typeof(BoolValue)) {
82                viewHost.Content = null; // no configuration required
83              }
84            }
85
86            var pvc = Content as ParameterizedValueConfiguration;
87            if (pvc != null) {
88              viewHost.ViewsLabelVisible = false;
89              viewHost.Content = pvc.ParameterConfigurations;
90            }
91          } else {
92            throw new NotSupportedException("Only IParameterConfiguration and IValueConfiguration types are allowed.");
93          }
94        } else {
95          viewHost.ViewsLabelVisible = false;
96          if (Content.ValuesReadOnly) {
97            viewHost.Content = new MessageItem("Cannot modify value directly. Please modify the value in the list of problems.");
98          } else {
99            // the content's valid values get lost after optimize is set to true because the constrained value
100            // does not contain the earlier valid values; therefore, the same constrained value with the correct
101            // set of valid values is created before the assignment below
102            var configuration = viewHost.Content as CheckedValueConfigurationList;
103            if (configuration != null) {
104              Content.ActualValue = new ConstrainedValue(Content.ActualValue.Value,
105                Content.ActualValue.ValueDataType,
106                configuration.ValidValues,
107                Content.ActualValue.IsNullable);
108            }
109            viewHost.Content = Content.ActualValue;
110          }
111        }
112      }
113      SetEnabledStateOfControls();
114    }
115    void Content_IsOptimizabeChanged(object sender, EventArgs e) {
116      if (InvokeRequired) {
117        Invoke(new EventHandler(Content_IsOptimizabeChanged), sender, e);
118      } else {
119        SetEnabledStateOfControls();
120      }
121    }
122    #endregion
123
124    protected override void OnContentChanged() {
125      base.OnContentChanged();
126      if (Content != null) {
127        Content_OptimizeChanged(this, EventArgs.Empty);
128      } else {
129        viewHost.Content = null;
130      }
131    }
132
133    protected override void SetEnabledStateOfControls() {
134      base.SetEnabledStateOfControls();
135      if (Content != null) {
136        optimizeCheckBox.Enabled = !ReadOnly && Content.IsOptimizable;
137      }
138    }
139
140    #region Event Handlers (child controls)
141    private void optimizeCheckBox_CheckedChanged(object sender, EventArgs e) {
142      Content.Optimize = optimizeCheckBox.Checked;
143    }
144    #endregion
145  }
146}
Note: See TracBrowser for help on using the repository browser.