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

Last change on this file since 2924 was 2924, checked in by swagner, 12 years ago

Operator architecture refactoring (#95)

  • implemented reviewers' comments on version r2917.
File size: 6.1 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.Linq;
24using HeuristicLab.Collections;
25using HeuristicLab.Core;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28
29namespace HeuristicLab.Parameters.Views {
30  /// <summary>
31  /// The visual representation of a <see cref="Parameter"/>.
32  /// </summary>
33  [View("ConstrainedValueParameter View")]
34  [Content(typeof(ConstrainedValueParameter<>), true)]
35  public partial class ConstrainedValueParameterView<T> : ParameterView where T : class, IItem {
36    /// <summary>
37    /// Gets or sets the variable to represent visually.
38    /// </summary>
39    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
40    /// No own data storage present.</remarks>
41    public new ConstrainedValueParameter<T> Content {
42      get { return (ConstrainedValueParameter<T>)base.Content; }
43      set { base.Content = value; }
44    }
45
46    /// <summary>
47    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
48    /// </summary>
49    public ConstrainedValueParameterView() {
50      InitializeComponent();
51      Caption = "ConstrainedValueParameter";
52    }
53    /// <summary>
54    /// Initializes a new instance of <see cref="VariableView"/> with the given <paramref name="variable"/>.
55    /// </summary>
56    /// <remarks>Calls <see cref="VariableView()"/>.</remarks>
57    /// <param name="variable">The variable to represent visually.</param>
58    public ConstrainedValueParameterView(ConstrainedValueParameter<T> content)
59      : this() {
60      Content = content;
61    }
62
63    /// <summary>
64    /// Removes the eventhandlers from the underlying <see cref="IVariable"/>.
65    /// </summary>
66    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
67    protected override void DeregisterContentEvents() {
68      Content.ValidValues.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
69      Content.ValidValues.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
70      Content.ValidValues.CollectionReset -= new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
71      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
72      base.DeregisterContentEvents();
73    }
74
75    /// <summary>
76    /// Adds eventhandlers to the underlying <see cref="IVariable"/>.
77    /// </summary>
78    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
79    protected override void RegisterContentEvents() {
80      base.RegisterContentEvents();
81      Content.ValidValues.ItemsAdded += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
82      Content.ValidValues.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
83      Content.ValidValues.CollectionReset += new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
84      Content.ValueChanged += new EventHandler(Content_ValueChanged);
85    }
86
87    protected override void OnContentChanged() {
88      base.OnContentChanged();
89      if (Content == null) {
90        Caption = "ConstrainedValueParameter";
91        viewHost.Content = null;
92        valueGroupBox.Enabled = false;
93        FillValueComboBox();
94      } else {
95        Caption = Content.Name + " (" + Content.GetType().Name + ")";
96        valueGroupBox.Enabled = true;
97        FillValueComboBox();
98        viewHost.Content = Content.Value;
99      }
100    }
101
102    private void FillValueComboBox() {
103      valueComboBox.SelectedIndexChanged -= new EventHandler(valueComboBox_SelectedIndexChanged);
104      valueComboBox.DataSource = null;
105      if (Content != null) {
106        valueComboBox.DataSource = Content.ValidValues.ToList();
107        valueComboBox.Enabled = valueComboBox.Items.Count > 0;
108        valueComboBox.SelectedItem = Content.Value;
109      }
110      valueComboBox.SelectedIndexChanged += new EventHandler(valueComboBox_SelectedIndexChanged);
111    }
112
113    #region Content Events
114    private void Content_ValueChanged(object sender, EventArgs e) {
115      if (InvokeRequired)
116        Invoke(new EventHandler(Content_ValueChanged), sender, e);
117      else {
118        valueComboBox.SelectedItem = Content.Value;
119        viewHost.Content = Content.Value;
120      }
121    }
122    private void ValidValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
123      if (InvokeRequired)
124        Invoke(new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded), sender, e);
125      else
126        FillValueComboBox();
127    }
128    private void ValidValues_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
129      if (InvokeRequired)
130        Invoke(new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved), sender, e);
131      else
132        FillValueComboBox();
133    }
134    private void ValidValues_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
135      if (InvokeRequired)
136        Invoke(new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset), sender, e);
137      else
138        FillValueComboBox();
139    }
140    #endregion
141
142    private void valueComboBox_SelectedIndexChanged(object sender, EventArgs e) {
143      Content.Value = (T)valueComboBox.SelectedItem;
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.