#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using HeuristicLab.Collections; using HeuristicLab.Core; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; namespace HeuristicLab.Parameters.Views { /// /// The visual representation of a . /// [View("ConstrainedValueParameter View")] [Content(typeof(OptionalConstrainedValueParameter<>), true)] [Content(typeof(ConstrainedValueParameter<>), true)] public partial class ConstrainedValueParameterView : ParameterView where T : class, IItem { private List valueComboBoxItems; /// /// Gets or sets the variable to represent visually. /// /// Uses property of base class . /// No own data storage present. public new OptionalConstrainedValueParameter Content { get { return (OptionalConstrainedValueParameter)base.Content; } set { base.Content = value; } } /// /// Initializes a new instance of with caption "Variable". /// public ConstrainedValueParameterView() { InitializeComponent(); Caption = "ConstrainedValueParameter"; valueComboBoxItems = new List(); } /// /// Initializes a new instance of with the given . /// /// Calls . /// The variable to represent visually. public ConstrainedValueParameterView(OptionalConstrainedValueParameter content) : this() { Content = content; } /// /// Removes the eventhandlers from the underlying . /// /// Calls of base class . protected override void DeregisterContentEvents() { Content.ValidValues.ItemsAdded -= new CollectionItemsChangedEventHandler(ValidValues_ItemsAdded); Content.ValidValues.ItemsRemoved -= new CollectionItemsChangedEventHandler(ValidValues_ItemsRemoved); Content.ValidValues.CollectionReset -= new CollectionItemsChangedEventHandler(ValidValues_CollectionReset); Content.ValueChanged -= new EventHandler(Content_ValueChanged); base.DeregisterContentEvents(); } /// /// Adds eventhandlers to the underlying . /// /// Calls of base class . protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.ValidValues.ItemsAdded += new CollectionItemsChangedEventHandler(ValidValues_ItemsAdded); Content.ValidValues.ItemsRemoved += new CollectionItemsChangedEventHandler(ValidValues_ItemsRemoved); Content.ValidValues.CollectionReset += new CollectionItemsChangedEventHandler(ValidValues_CollectionReset); Content.ValueChanged += new EventHandler(Content_ValueChanged); } protected override void OnContentChanged() { base.OnContentChanged(); if (Content == null) { Caption = "ConstrainedValueParameter"; viewHost.Content = null; valueGroupBox.Enabled = false; FillValueComboBox(); } else { Caption = Content.Name + " (" + Content.GetType().Name + ")"; valueGroupBox.Enabled = true; FillValueComboBox(); viewHost.ViewType = null; viewHost.Content = Content.Value; } } private void FillValueComboBox() { valueComboBox.SelectedIndexChanged -= new EventHandler(valueComboBox_SelectedIndexChanged); valueComboBoxItems.Clear(); valueComboBox.Items.Clear(); if (!(Content is ConstrainedValueParameter)) { valueComboBoxItems.Add(null); valueComboBox.Items.Add("-"); } if (Content != null) { foreach (T item in Content.ValidValues) { valueComboBoxItems.Add(item); valueComboBox.Items.Add(item.ToString()); } valueComboBox.Enabled = valueComboBox.Items.Count > 0; valueComboBox.SelectedIndex = valueComboBoxItems.IndexOf(Content.Value); } valueComboBox.SelectedIndexChanged += new EventHandler(valueComboBox_SelectedIndexChanged); } #region Content Events private void Content_ValueChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_ValueChanged), sender, e); else { valueComboBox.SelectedIndex = valueComboBoxItems.IndexOf(Content.Value); viewHost.ViewType = null; viewHost.Content = Content.Value; } } private void ValidValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs e) { if (InvokeRequired) Invoke(new CollectionItemsChangedEventHandler(ValidValues_ItemsAdded), sender, e); else FillValueComboBox(); } private void ValidValues_ItemsRemoved(object sender, CollectionItemsChangedEventArgs e) { if (InvokeRequired) Invoke(new CollectionItemsChangedEventHandler(ValidValues_ItemsRemoved), sender, e); else FillValueComboBox(); } private void ValidValues_CollectionReset(object sender, CollectionItemsChangedEventArgs e) { if (InvokeRequired) Invoke(new CollectionItemsChangedEventHandler(ValidValues_CollectionReset), sender, e); else FillValueComboBox(); } #endregion private void valueComboBox_SelectedIndexChanged(object sender, EventArgs e) { Content.Value = valueComboBoxItems[valueComboBox.SelectedIndex]; } } }