Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2948 was 2948, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • implemented reviewers' comments on version r2917.
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.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(OptionalConstrainedValueParameter<>), true)]
35  [Content(typeof(ConstrainedValueParameter<>), true)]
36  public partial class ConstrainedValueParameterView<T> : ParameterView where T : class, IItem {
37    private List<T> valueComboBoxItems;
38
39    /// <summary>
40    /// Gets or sets the variable to represent visually.
41    /// </summary>
42    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
43    /// No own data storage present.</remarks>
44    public new OptionalConstrainedValueParameter<T> Content {
45      get { return (OptionalConstrainedValueParameter<T>)base.Content; }
46      set { base.Content = value; }
47    }
48
49    /// <summary>
50    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
51    /// </summary>
52    public ConstrainedValueParameterView() {
53      InitializeComponent();
54      Caption = "ConstrainedValueParameter";
55      valueComboBoxItems = new List<T>();
56    }
57    /// <summary>
58    /// Initializes a new instance of <see cref="VariableView"/> with the given <paramref name="variable"/>.
59    /// </summary>
60    /// <remarks>Calls <see cref="VariableView()"/>.</remarks>
61    /// <param name="variable">The variable to represent visually.</param>
62    public ConstrainedValueParameterView(OptionalConstrainedValueParameter<T> content)
63      : this() {
64      Content = content;
65    }
66
67    /// <summary>
68    /// Removes the eventhandlers from the underlying <see cref="IVariable"/>.
69    /// </summary>
70    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
71    protected override void DeregisterContentEvents() {
72      Content.ValidValues.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
73      Content.ValidValues.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
74      Content.ValidValues.CollectionReset -= new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
75      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
76      base.DeregisterContentEvents();
77    }
78
79    /// <summary>
80    /// Adds eventhandlers to the underlying <see cref="IVariable"/>.
81    /// </summary>
82    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
83    protected override void RegisterContentEvents() {
84      base.RegisterContentEvents();
85      Content.ValidValues.ItemsAdded += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
86      Content.ValidValues.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
87      Content.ValidValues.CollectionReset += new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
88      Content.ValueChanged += new EventHandler(Content_ValueChanged);
89    }
90
91    protected override void OnContentChanged() {
92      base.OnContentChanged();
93      if (Content == null) {
94        Caption = "ConstrainedValueParameter";
95        viewHost.Content = null;
96        valueGroupBox.Enabled = false;
97        FillValueComboBox();
98      } else {
99        Caption = Content.Name + " (" + Content.GetType().Name + ")";
100        valueGroupBox.Enabled = true;
101        FillValueComboBox();
102        viewHost.Content = Content.Value;
103      }
104    }
105
106    private 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;
120        valueComboBox.SelectedIndex = valueComboBoxItems.IndexOf(Content.Value);
121      }
122      valueComboBox.SelectedIndexChanged += new EventHandler(valueComboBox_SelectedIndexChanged);
123    }
124
125    #region Content Events
126    private void Content_ValueChanged(object sender, EventArgs e) {
127      if (InvokeRequired)
128        Invoke(new EventHandler(Content_ValueChanged), sender, e);
129      else {
130        valueComboBox.SelectedIndex = valueComboBoxItems.IndexOf(Content.Value);
131        viewHost.Content = Content.Value;
132      }
133    }
134    private void ValidValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
135      if (InvokeRequired)
136        Invoke(new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded), sender, e);
137      else
138        FillValueComboBox();
139    }
140    private void ValidValues_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
141      if (InvokeRequired)
142        Invoke(new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved), sender, e);
143      else
144        FillValueComboBox();
145    }
146    private void ValidValues_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
147      if (InvokeRequired)
148        Invoke(new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset), sender, e);
149      else
150        FillValueComboBox();
151    }
152    #endregion
153
154    private void valueComboBox_SelectedIndexChanged(object sender, EventArgs e) {
155      Content.Value = valueComboBoxItems[valueComboBox.SelectedIndex];
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.