Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9356 was 9356, checked in by jkarder, 11 years ago

#1853:

  • moved ValueConfigurationCheckedItemCollectionView to ParameterConfigurationEncoding
  • renamed ValueConfigurationCheckedItemCollectionView to ValueConfigurationCheckedItemListView
File size: 5.9 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              viewHost.ViewType = typeof(ValueConfigurationCheckedItemListView);
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)) {
80                viewHost.ViewsLabelVisible = true;
81                viewHost.Content = rvc.RangeConstraint;
82              } else if (Content.ActualValue.ValueDataType == typeof(BoolValue)) {
83                viewHost.Content = null; // no configuration required
84              }
85            }
86
87            var pvc = Content as ParameterizedValueConfiguration;
88            if (pvc != null) {
89              viewHost.ViewsLabelVisible = false;
90              viewHost.Content = pvc.ParameterConfigurations;
91            }
92          } else {
93            throw new NotSupportedException("Only IParameterConfiguration and IValueConfiguration types are allowed.");
94          }
95        } else {
96          viewHost.ViewsLabelVisible = false;
97          if (Content.ValuesReadOnly) {
98            viewHost.Content = new MessageItem("Cannot modify value directly. Please modify the value in the list of problems.");
99          } else {
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;
104            if (configuration != null && configuration.ValidValues.Any(x => x.GetType() == Content.ActualValue.ValueDataType)) {
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;
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 {
130        viewHost.Content = null;
131      }
132    }
133
134    protected override void SetEnabledStateOfControls() {
135      base.SetEnabledStateOfControls();
136      if (Content != null) {
137        optimizeCheckBox.Enabled = !ReadOnly && Content.IsOptimizable;
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}
Note: See TracBrowser for help on using the repository browser.