Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

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