using System; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.MetaOptimization { [StorableClass] public class ValueConfiguration : DeepCloneable, IValueConfiguration { [Storable] protected IItemList parameterConfigurations = new ItemList(); public IItemList ParameterConfigurations { get { return this.parameterConfigurations; } protected set { this.parameterConfigurations = value; } } protected IItem value; public IItem Value { get { return value; } set { if (this.value != value) { ClearChildParameterConfigurations(); this.value = value; if (this.value is IParameterizedNamedItem) AddChildParameterConfigurations(this.value as IParameterizedNamedItem); OnValueChanged(); } } } protected Type valueDataType; public Type ValueDataType { get { return this.valueDataType; } protected set { this.valueDataType = value; } } public ValueConfiguration(IItem value, Type valueDataType) { this.Value = value; this.ValueDataType = valueDataType; } public ValueConfiguration() { } [StorableConstructor] protected ValueConfiguration(bool deserializing) { } protected ValueConfiguration(ValueConfiguration original, Cloner cloner) : base(original, cloner) { this.ParameterConfigurations = cloner.Clone(original.ParameterConfigurations); this.Value = cloner.Clone(original.Value); this.ValueDataType = original.ValueDataType; } public override IDeepCloneable Clone(Cloner cloner) { return new ValueConfiguration(this, cloner); } protected virtual void AddChildParameterConfigurations(IParameterizedNamedItem parameterizedItem) { foreach (var childParameter in parameterizedItem.Parameters) { var pc = ParameterConfiguration.Create(parameterizedItem, childParameter); if (pc != null) this.ParameterConfigurations.Add(pc); } } protected virtual void ClearChildParameterConfigurations() { ParameterConfigurations.Clear(); } #region IItem Members public string ItemDescription { get { return value.ItemDescription; } } public System.Drawing.Image ItemImage { get { return value.ItemImage; } } public string ItemName { get { return value.ItemDescription; } } public Version ItemVersion { get { return value.ItemVersion; } } #endregion #region Events public virtual event EventHandler ItemImageChanged; protected virtual void OnItemImageChanged(object sender, EventArgs e) { var handler = ItemImageChanged; if (handler != null) handler(sender, e); } public virtual event EventHandler ToStringChanged; protected virtual void OnStringChanged(object sender, EventArgs e) { var handler = ToStringChanged; if (handler != null) handler(this, e); // important to set 'this' as sender } public virtual event EventHandler ValueChanged; protected virtual void OnValueChanged() { var handler = ValueChanged; if (handler != null) handler(this, EventArgs.Empty); } #endregion public override string ToString() { return value.ItemName; } } }