Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Parameters.Views/3.3/ConstrainedValueParameterView.cs @ 3566

Last change on this file since 3566 was 3566, checked in by mkommend, 14 years ago

removed ctors with contents in all views (ticket #972)

File size: 6.6 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      Caption = "ConstrainedValueParameter";
56      valueComboBoxItems = new List<T>();
57    }
58
59    /// <summary>
60    /// Removes the eventhandlers from the underlying <see cref="IVariable"/>.
61    /// </summary>
62    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
63    protected override void DeregisterContentEvents() {
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.ValidValues.ItemsAdded += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
78      Content.ValidValues.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
79      Content.ValidValues.CollectionReset += new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
80      Content.ValueChanged += new EventHandler(Content_ValueChanged);
81    }
82
83    protected override void OnContentChanged() {
84      base.OnContentChanged();
85      if (Content == null) {
86        Caption = "ConstrainedValueParameter";
87        viewHost.Content = null;
88        FillValueComboBox();
89      } else {
90        Caption = Content.Name + " (" + Content.GetType().Name + ")";
91        FillValueComboBox();
92        viewHost.ViewType = null;
93        viewHost.Content = Content.Value;
94      }
95      SetEnabledStateOfControls();
96    }
97
98    protected override void OnReadOnlyChanged() {
99      base.OnReadOnlyChanged();
100      SetEnabledStateOfControls();
101    }
102
103    private void SetEnabledStateOfControls() {
104      valueGroupBox.Enabled = Content != null;
105      valueComboBox.Enabled = (valueComboBox.Items.Count > 0) && !ReadOnly;
106    }
107
108    private 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    private void Content_ValueChanged(object sender, EventArgs e) {
129      if (InvokeRequired)
130        Invoke(new EventHandler(Content_ValueChanged), sender, e);
131      else {
132        valueComboBox.SelectedIndex = valueComboBoxItems.IndexOf(Content.Value);
133        viewHost.ViewType = null;
134        viewHost.Content = Content.Value;
135      }
136    }
137    private void ValidValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
138      if (InvokeRequired)
139        Invoke(new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded), sender, e);
140      else
141        FillValueComboBox();
142    }
143    private void ValidValues_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
144      if (InvokeRequired)
145        Invoke(new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved), sender, e);
146      else
147        FillValueComboBox();
148    }
149    private void ValidValues_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
150      if (InvokeRequired)
151        Invoke(new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset), sender, e);
152      else
153        FillValueComboBox();
154    }
155    #endregion
156
157    private void valueComboBox_SelectedIndexChanged(object sender, EventArgs e) {
158      if (valueComboBox.SelectedIndex >= 0)
159        Content.Value = valueComboBoxItems[valueComboBox.SelectedIndex];
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.