Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8614 was 8535, checked in by jkarder, 12 years ago

#1853:

  • enhanced combinations count calculation
  • restructured code
  • minor code improvements
  • added license information
File size: 5.1 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        this.optimizeCheckBox.Checked = Content.Optimize;
61        if (Content.Optimize) {
62          this.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              this.viewHost.Content = ((ParameterizedValueConfiguration)pc.ValueConfigurations.Single()).ParameterConfigurations;
70            } else {
71              this.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                this.viewHost.ViewsLabelVisible = true;
80                this.viewHost.Content = rvc.RangeConstraint;
81              } else if (Content.ActualValue.ValueDataType == typeof(BoolValue)) {
82                this.viewHost.Content = null; // no configuration required
83              }
84            }
85
86            var pvc = Content as ParameterizedValueConfiguration;
87            if (pvc != null) {
88              this.viewHost.ViewsLabelVisible = false;
89              this.viewHost.Content = pvc.ParameterConfigurations;
90            }
91          } else {
92            throw new NotSupportedException("Only IParameterConfiguration and IValueConfiguration types are allowed.");
93          }
94        } else {
95          this.viewHost.ViewsLabelVisible = false;
96          if (Content.ValuesReadOnly) {
97            this.viewHost.Content = new MessageItem("Cannot modify value directly. Please modify the value in the list of problems.");
98          } else {
99            this.viewHost.Content = Content.ActualValue;
100          }
101        }
102      }
103      SetEnabledStateOfControls();
104    }
105    void Content_IsOptimizabeChanged(object sender, EventArgs e) {
106      if (InvokeRequired) {
107        Invoke(new EventHandler(Content_IsOptimizabeChanged), sender, e);
108      } else {
109        SetEnabledStateOfControls();
110      }
111    }
112    #endregion
113
114    protected override void OnContentChanged() {
115      base.OnContentChanged();
116      if (Content != null) {
117        Content_OptimizeChanged(this, EventArgs.Empty);
118      } else {
119        this.viewHost.Content = null;
120      }
121    }
122
123    protected override void SetEnabledStateOfControls() {
124      base.SetEnabledStateOfControls();
125      if (Content != null) {
126        this.optimizeCheckBox.Enabled = !this.ReadOnly && Content.IsOptimizable;
127      }
128    }
129
130    #region Event Handlers (child controls)
131    private void optimizeCheckBox_CheckedChanged(object sender, EventArgs e) {
132      Content.Optimize = optimizeCheckBox.Checked;
133    }
134    #endregion
135  }
136}
Note: See TracBrowser for help on using the repository browser.