Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3044_variableScaling/HeuristicLab.Parameters.Views/3.3/ConstrainedValueParameterView.cs @ 17554

Last change on this file since 17554 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 8.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.ReadOnlyChanged -= new EventHandler(Content_ReadOnlyChanged);
65      Content.ValidValues.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
66      Content.ValidValues.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
67      Content.ValidValues.CollectionReset -= new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
68      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
69      base.DeregisterContentEvents();
70    }
71
72    /// <summary>
73    /// Adds eventhandlers to the underlying <see cref="IVariable"/>.
74    /// </summary>
75    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
76    protected override void RegisterContentEvents() {
77      base.RegisterContentEvents();
78      Content.GetsCollectedChanged += new EventHandler(Content_GetsCollectedChanged);
79      Content.ReadOnlyChanged += new EventHandler(Content_ReadOnlyChanged);
80      Content.ValidValues.ItemsAdded += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
81      Content.ValidValues.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
82      Content.ValidValues.CollectionReset += new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
83      Content.ValueChanged += new EventHandler(Content_ValueChanged);
84    }
85
86    protected override void OnContentChanged() {
87      base.OnContentChanged();
88      if (Content == null) {
89        showInRunCheckBox.Checked = false;
90        viewHost.Content = null;
91        FillValueComboBox();
92      } else {
93        SetDataTypeTextBoxText();
94        FillValueComboBox();
95        showInRunCheckBox.Checked = Content.GetsCollected;
96        viewHost.ViewType = null;
97        viewHost.Content = Content.Value;
98      }
99    }
100
101    protected override void SetEnabledStateOfControls() {
102      base.SetEnabledStateOfControls();
103      valueGroupBox.Enabled = Content != null;
104      valueComboBox.Enabled = Content != null && valueComboBox.Items.Count > 0 && !Content.ReadOnly && !ReadOnly;
105      showInRunCheckBox.Enabled = Content != null && !ReadOnly;
106    }
107
108    protected virtual void FillValueComboBox() {
109      valueComboBox.SelectedIndexChanged -= new EventHandler(valueComboBox_SelectedIndexChanged);
110      valueComboBoxItems.Clear();
111      valueComboBox.Items.Clear();
112      if (!(Content is ConstrainedValueParameter<T>)) {
113        valueComboBoxItems.Add(null);
114        valueComboBox.Items.Add("-");
115      }
116      if (Content != null) {
117        foreach (T item in Content.ValidValues) {
118          valueComboBoxItems.Add(item);
119          valueComboBox.Items.Add(item.ToString());
120        }
121        valueComboBox.Enabled = (valueComboBox.Items.Count > 0) && !ReadOnly;
122        valueComboBox.SelectedIndex = valueComboBoxItems.IndexOf(Content.Value);
123      }
124      valueComboBox.SelectedIndexChanged += new EventHandler(valueComboBox_SelectedIndexChanged);
125    }
126
127    #region Content Events
128    protected virtual void Content_ValueChanged(object sender, EventArgs e) {
129      if (InvokeRequired)
130        Invoke(new EventHandler(Content_ValueChanged), sender, e);
131      else {
132        SetDataTypeTextBoxText();
133        SetComboBoxSelectedIndex();
134        viewHost.ViewType = null;
135        viewHost.Content = Content != null ? Content.Value : null;
136      }
137    }
138
139    protected virtual void ValidValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
140      if (InvokeRequired)
141        Invoke(new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded), sender, e);
142      else
143        FillValueComboBox();
144    }
145    protected virtual void ValidValues_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
146      if (InvokeRequired)
147        Invoke(new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved), sender, e);
148      else
149        FillValueComboBox();
150    }
151    protected virtual void ValidValues_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
152      if (InvokeRequired)
153        Invoke(new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset), sender, e);
154      else
155        FillValueComboBox();
156    }
157    protected virtual void Content_ReadOnlyChanged(object sender, EventArgs e) {
158      if (InvokeRequired)
159        Invoke(new EventHandler(Content_ReadOnlyChanged), sender, e);
160      else {
161        SetEnabledStateOfControls();
162      }
163    }
164    protected virtual void Content_GetsCollectedChanged(object sender, EventArgs e) {
165      if (InvokeRequired)
166        Invoke(new EventHandler(Content_GetsCollectedChanged), sender, e);
167      else
168        showInRunCheckBox.Checked = Content != null && Content.GetsCollected;
169    }
170    #endregion
171
172    protected virtual void valueComboBox_SelectedIndexChanged(object sender, EventArgs e) {
173      if (valueComboBox.SelectedIndex >= 0)
174        Content.Value = valueComboBoxItems[valueComboBox.SelectedIndex];
175    }
176    protected virtual void showInRunCheckBox_CheckedChanged(object sender, EventArgs e) {
177      if (Content != null) Content.GetsCollected = showInRunCheckBox.Checked;
178    }
179
180    #region Helpers
181    protected void SetDataTypeTextBoxText() {
182      if (Content == null) {
183        dataTypeTextBox.Text = "-";
184      } else {
185        if ((Content.Value != null) && (Content.Value.GetType() != Content.DataType))
186          dataTypeTextBox.Text = Content.DataType.GetPrettyName() + " (" + Content.Value.GetType().GetPrettyName() + ")";
187        else
188          dataTypeTextBox.Text = Content.DataType.GetPrettyName();
189      }
190    }
191    protected void SetComboBoxSelectedIndex() {
192      valueComboBox.SelectedIndex = valueComboBoxItems.IndexOf(Content != null ? Content.Value : null);
193    }
194    #endregion
195  }
196}
Note: See TracBrowser for help on using the repository browser.