Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Parameters.Views/3.3/ConstrainedValueParameterView.cs @ 6451

Last change on this file since 6451 was 4332, checked in by swagner, 14 years ago

Enabled users to choose whether a parameter should be collected in each run or not (#1113)

File size: 7.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Collections.Generic;
24using HeuristicLab.Collections;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29
30namespace HeuristicLab.Parameters.Views {
31  /// <summary>
32  /// The visual representation of a <see cref="Parameter"/>.
33  /// </summary>
34  [View("ConstrainedValueParameter View")]
35  [Content(typeof(OptionalConstrainedValueParameter<>), true)]
36  [Content(typeof(ConstrainedValueParameter<>), true)]
37  public partial class ConstrainedValueParameterView<T> : ParameterView where T : class, IItem {
38    private List<T> valueComboBoxItems;
39
40    /// <summary>
41    /// Gets or sets the variable to represent visually.
42    /// </summary>
43    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
44    /// No own data storage present.</remarks>
45    public new OptionalConstrainedValueParameter<T> Content {
46      get { return (OptionalConstrainedValueParameter<T>)base.Content; }
47      set { base.Content = value; }
48    }
49
50    /// <summary>
51    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
52    /// </summary>
53    public ConstrainedValueParameterView() {
54      InitializeComponent();
55      valueComboBoxItems = new List<T>();
56    }
57
58    /// <summary>
59    /// Removes the eventhandlers from the underlying <see cref="IVariable"/>.
60    /// </summary>
61    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
62    protected override void DeregisterContentEvents() {
63      Content.GetsCollectedChanged -= new EventHandler(Content_GetsCollectedChanged);
64      Content.ValidValues.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
65      Content.ValidValues.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
66      Content.ValidValues.CollectionReset -= new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
67      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
68      base.DeregisterContentEvents();
69    }
70
71    /// <summary>
72    /// Adds eventhandlers to the underlying <see cref="IVariable"/>.
73    /// </summary>
74    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
75    protected override void RegisterContentEvents() {
76      base.RegisterContentEvents();
77      Content.GetsCollectedChanged += new EventHandler(Content_GetsCollectedChanged);
78      Content.ValidValues.ItemsAdded += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
79      Content.ValidValues.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
80      Content.ValidValues.CollectionReset += new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
81      Content.ValueChanged += new EventHandler(Content_ValueChanged);
82    }
83
84    protected override void OnContentChanged() {
85      base.OnContentChanged();
86      if (Content == null) {
87        showInRunCheckBox.Checked = false;
88        viewHost.Content = null;
89        FillValueComboBox();
90      } else {
91        SetDataTypeTextBoxText();
92        FillValueComboBox();
93        showInRunCheckBox.Checked = Content.GetsCollected;
94        viewHost.ViewType = null;
95        viewHost.Content = Content.Value;
96      }
97    }
98
99    protected override void SetEnabledStateOfControls() {
100      base.SetEnabledStateOfControls();
101      valueGroupBox.Enabled = Content != null;
102      valueComboBox.Enabled = (valueComboBox.Items.Count > 0) && !ReadOnly;
103      showInRunCheckBox.Enabled = Content != null && !ReadOnly;
104    }
105
106    protected virtual void FillValueComboBox() {
107      valueComboBox.SelectedIndexChanged -= new EventHandler(valueComboBox_SelectedIndexChanged);
108      valueComboBoxItems.Clear();
109      valueComboBox.Items.Clear();
110      if (!(Content is ConstrainedValueParameter<T>)) {
111        valueComboBoxItems.Add(null);
112        valueComboBox.Items.Add("-");
113      }
114      if (Content != null) {
115        foreach (T item in Content.ValidValues) {
116          valueComboBoxItems.Add(item);
117          valueComboBox.Items.Add(item.ToString());
118        }
119        valueComboBox.Enabled = (valueComboBox.Items.Count > 0) && !ReadOnly;
120        valueComboBox.SelectedIndex = valueComboBoxItems.IndexOf(Content.Value);
121      }
122      valueComboBox.SelectedIndexChanged += new EventHandler(valueComboBox_SelectedIndexChanged);
123    }
124
125    #region Content Events
126    protected virtual void Content_ValueChanged(object sender, EventArgs e) {
127      if (InvokeRequired)
128        Invoke(new EventHandler(Content_ValueChanged), sender, e);
129      else {
130        SetDataTypeTextBoxText();
131        valueComboBox.SelectedIndex = valueComboBoxItems.IndexOf(Content != null ? Content.Value : null);
132        viewHost.ViewType = null;
133        viewHost.Content = Content != null ? Content.Value : null;
134      }
135    }
136    protected virtual void ValidValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
137      if (InvokeRequired)
138        Invoke(new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded), sender, e);
139      else
140        FillValueComboBox();
141    }
142    protected virtual void ValidValues_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
143      if (InvokeRequired)
144        Invoke(new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved), sender, e);
145      else
146        FillValueComboBox();
147    }
148    protected virtual void ValidValues_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
149      if (InvokeRequired)
150        Invoke(new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset), sender, e);
151      else
152        FillValueComboBox();
153    }
154    protected virtual void Content_GetsCollectedChanged(object sender, EventArgs e) {
155      if (InvokeRequired)
156        Invoke(new EventHandler(Content_GetsCollectedChanged), sender, e);
157      else
158        showInRunCheckBox.Checked = Content != null && Content.GetsCollected;
159    }
160    #endregion
161
162    protected virtual void valueComboBox_SelectedIndexChanged(object sender, EventArgs e) {
163      if (valueComboBox.SelectedIndex >= 0)
164        Content.Value = valueComboBoxItems[valueComboBox.SelectedIndex];
165    }
166    protected virtual void showInRunCheckBox_CheckedChanged(object sender, EventArgs e) {
167      if (Content != null) Content.GetsCollected = showInRunCheckBox.Checked;
168    }
169
170    #region Helpers
171    protected void SetDataTypeTextBoxText() {
172      if (Content == null) {
173        dataTypeTextBox.Text = "-";
174      } else {
175        if ((Content.Value != null) && (Content.Value.GetType() != Content.DataType))
176          dataTypeTextBox.Text = Content.DataType.GetPrettyName() + " (" + Content.Value.GetType().GetPrettyName() + ")";
177        else
178          dataTypeTextBox.Text = Content.DataType.GetPrettyName();
179      }
180    }
181    #endregion
182  }
183}
Note: See TracBrowser for help on using the repository browser.