#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.Common;
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();
valueComboBoxItems = new List();
}
///
/// 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) {
viewHost.Content = null;
FillValueComboBox();
} else {
FillValueComboBox();
viewHost.ViewType = null;
viewHost.Content = Content.Value;
}
SetEnabledStateOfControls();
}
protected override void OnReadOnlyChanged() {
base.OnReadOnlyChanged();
SetEnabledStateOfControls();
}
private void SetEnabledStateOfControls() {
valueGroupBox.Enabled = Content != null;
valueComboBox.Enabled = (valueComboBox.Items.Count > 0) && !ReadOnly;
}
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) && !ReadOnly;
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) {
if (valueComboBox.SelectedIndex >= 0)
Content.Value = valueComboBoxItems[valueComboBox.SelectedIndex];
}
}
}