#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.Drawing;
using HeuristicLab.Collections;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Parameters {
///
/// A parameter whose value has to be chosen from a set of valid values or is null.
///
[Item("OptionalConstrainedValueParameter", "A parameter whose value has to be chosen from a set of valid values or is null.")]
[StorableClass]
public class OptionalConstrainedValueParameter : Parameter, IValueParameter where T : class, IItem {
public override Image ItemImage {
get {
if (value != null) return value.ItemImage;
else return base.ItemImage;
}
}
[Storable]
private ItemSet validValues;
public ItemSet ValidValues {
get { return validValues; }
}
[Storable]
private T value;
public virtual T Value {
get { return this.value; }
set {
if (value != this.value) {
if ((value != null) && !validValues.Contains(value)) throw new ArgumentException("Invalid value.");
DeregisterValueEvents();
this.value = value;
RegisterValueEvents();
OnValueChanged();
}
}
}
IItem IValueParameter.Value {
get { return Value; }
set {
T val = value as T;
if ((value != null) && (val == null))
throw new InvalidOperationException(
string.Format("Type mismatch. Value is not a \"{0}\".",
typeof(T).GetPrettyName())
);
Value = val;
}
}
public OptionalConstrainedValueParameter()
: base("Anonymous", typeof(T)) {
this.validValues = new ItemSet();
Initialize();
}
public OptionalConstrainedValueParameter(string name)
: base(name, typeof(T)) {
this.validValues = new ItemSet();
Initialize();
}
public OptionalConstrainedValueParameter(string name, ItemSet validValues)
: base(name, typeof(T)) {
this.validValues = validValues;
Initialize();
}
public OptionalConstrainedValueParameter(string name, ItemSet validValues, T value)
: base(name, typeof(T)) {
this.validValues = validValues;
this.value = value;
Initialize();
}
public OptionalConstrainedValueParameter(string name, string description)
: base(name, description, typeof(T)) {
this.validValues = new ItemSet();
Initialize();
}
public OptionalConstrainedValueParameter(string name, string description, ItemSet validValues)
: base(name, description, typeof(T)) {
this.validValues = validValues;
Initialize();
}
public OptionalConstrainedValueParameter(string name, string description, ItemSet validValues, T value)
: base(name, description, typeof(T)) {
this.validValues = validValues;
this.value = value;
Initialize();
}
[StorableConstructor]
protected OptionalConstrainedValueParameter(bool deserializing) : base(deserializing) { }
[StorableHook(HookType.AfterDeserialization)]
private void Initialize() {
RegisterValidValuesEvents();
RegisterValueEvents();
}
public override IDeepCloneable Clone(Cloner cloner) {
OptionalConstrainedValueParameter clone = (OptionalConstrainedValueParameter)base.Clone(cloner);
clone.validValues = (ItemSet)cloner.Clone(validValues);
clone.value = (T)cloner.Clone(value);
clone.Initialize();
return clone;
}
public override string ToString() {
return Name + ": " + (Value != null ? Value.ToString() : "null");
}
protected override IItem GetActualValue() {
return Value;
}
protected override void SetActualValue(IItem value) {
((IValueParameter)this).Value = value;
}
public event EventHandler ValueChanged;
protected virtual void OnValueChanged() {
if (ValueChanged != null)
ValueChanged(this, EventArgs.Empty);
OnItemImageChanged();
OnToStringChanged();
}
private void RegisterValidValuesEvents() {
if (validValues != null) {
validValues.ItemsAdded += new CollectionItemsChangedEventHandler(ValidValues_ItemsAdded);
validValues.ItemsRemoved += new CollectionItemsChangedEventHandler(ValidValues_ItemsRemoved);
validValues.CollectionReset += new CollectionItemsChangedEventHandler(ValidValues_CollectionReset);
}
}
private void DeregisterValidValuesEvents() {
if (validValues != null) {
validValues.ItemsAdded -= new CollectionItemsChangedEventHandler(ValidValues_ItemsAdded);
validValues.ItemsRemoved -= new CollectionItemsChangedEventHandler(ValidValues_ItemsRemoved);
validValues.CollectionReset -= new CollectionItemsChangedEventHandler(ValidValues_CollectionReset);
}
}
protected virtual void ValidValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs e) { }
protected virtual void ValidValues_ItemsRemoved(object sender, CollectionItemsChangedEventArgs e) {
if ((Value != null) && !validValues.Contains(Value)) Value = null;
}
protected virtual void ValidValues_CollectionReset(object sender, CollectionItemsChangedEventArgs e) {
if ((Value != null) && !validValues.Contains(Value)) Value = null;
}
private void RegisterValueEvents() {
if (value != null) {
value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
value.ToStringChanged += new EventHandler(Value_ToStringChanged);
}
}
private void DeregisterValueEvents() {
if (value != null) {
value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
}
}
private void Value_ItemImageChanged(object sender, EventArgs e) {
OnItemImageChanged();
}
private void Value_ToStringChanged(object sender, EventArgs e) {
OnToStringChanged();
}
}
}