#region License Information
/* HeuristicLab
* Copyright (C) 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 HeuristicLab.Core;
using HeuristicLab.Data;
namespace HeuristicLab.Parameters {
public abstract class ParameterChangeHandler where TItem : class, IItem {
protected Action handler;
protected ParameterChangeHandler(IValueParameter parameter, Action handler) {
if (!(parameter is IFixedValueParameter))
parameter.ValueChanged += ParameterOnValueChanged;
this.handler = handler;
}
protected virtual void ParameterOnValueChanged(object sender, EventArgs e) {
handler();
}
}
public class ValueTypeValueParameterChangeHandler : ParameterChangeHandler
where TValue : struct
where TItem : ValueTypeValue {
private ValueTypeValue last;
protected ValueTypeValueParameterChangeHandler(IValueParameter parameter, Action handler)
: base(parameter, handler) {
last = parameter.Value;
if (last != null && !last.ReadOnly)
last.ValueChanged += ParameterValueOnValueChanged;
}
protected override void ParameterOnValueChanged(object sender, EventArgs e) {
if (last != null) last.ValueChanged -= ParameterValueOnValueChanged;
last = ((IValueParameter)sender).Value;
if (last != null && !last.ReadOnly)
last.ValueChanged += ParameterValueOnValueChanged;
base.ParameterOnValueChanged(sender, e);
}
protected void ParameterValueOnValueChanged(object sender, EventArgs e) {
handler();
}
public static ValueTypeValueParameterChangeHandler Create(IValueParameter parameter, Action handler)
=> new ValueTypeValueParameterChangeHandler(parameter, handler);
}
public class ValueTypeArrayParameterChangeHandler : ParameterChangeHandler
where TValue : struct
where TItem : ValueTypeArray {
private ValueTypeArray last;
protected ValueTypeArrayParameterChangeHandler(IValueParameter parameter, Action handler)
: base(parameter, handler) {
last = parameter.Value;
if (last != null && !last.ReadOnly)
last.ToStringChanged += ParameterValueOnValueChanged;
}
protected override void ParameterOnValueChanged(object sender, EventArgs e) {
if (last != null) last.ToStringChanged -= ParameterValueOnValueChanged;
last = ((IValueParameter)sender).Value;
if (last != null && !last.ReadOnly)
last.ToStringChanged += ParameterValueOnValueChanged;
base.ParameterOnValueChanged(sender, e);
}
protected void ParameterValueOnValueChanged(object sender, EventArgs e) {
handler();
}
public static ValueTypeArrayParameterChangeHandler Create(IValueParameter parameter, Action handler)
=> new ValueTypeArrayParameterChangeHandler(parameter, handler);
}
public class ValueTypeMatrixParameterChangeHandler : ParameterChangeHandler
where TValue : struct
where TItem : ValueTypeMatrix {
private ValueTypeMatrix last;
protected ValueTypeMatrixParameterChangeHandler(IValueParameter parameter, Action handler)
: base(parameter, handler) {
last = parameter.Value;
if (last != null && !last.ReadOnly)
last.ToStringChanged += ParameterValueOnValueChanged;
}
protected override void ParameterOnValueChanged(object sender, EventArgs e) {
if (last != null) last.ToStringChanged -= ParameterValueOnValueChanged;
last = ((IValueParameter)sender).Value;
if (last != null && !last.ReadOnly)
last.ToStringChanged += ParameterValueOnValueChanged;
base.ParameterOnValueChanged(sender, e);
}
protected void ParameterValueOnValueChanged(object sender, EventArgs e) {
handler();
}
public static ValueTypeMatrixParameterChangeHandler Create(IValueParameter parameter, Action handler)
=> new ValueTypeMatrixParameterChangeHandler(parameter, handler);
}
public class BoolValueParameterChangeHandler : ValueTypeValueParameterChangeHandler {
private BoolValueParameterChangeHandler(IValueParameter parameter, Action handler) : base(parameter, handler) { }
public static new BoolValueParameterChangeHandler Create(IValueParameter parameter, Action handler)
=> new BoolValueParameterChangeHandler(parameter, handler);
}
public class IntValueParameterChangeHandler : ValueTypeValueParameterChangeHandler {
private IntValueParameterChangeHandler(IValueParameter parameter, Action handler) : base(parameter, handler) { }
public static new IntValueParameterChangeHandler Create(IValueParameter parameter, Action handler)
=> new IntValueParameterChangeHandler(parameter, handler);
}
public class DoubleValueParameterChangeHandler : ValueTypeValueParameterChangeHandler {
private DoubleValueParameterChangeHandler(IValueParameter parameter, Action handler) : base(parameter, handler) { }
public static new DoubleValueParameterChangeHandler Create(IValueParameter parameter, Action handler)
=> new DoubleValueParameterChangeHandler(parameter, handler);
}
public class PercentValueParameterChangeHandler : ValueTypeValueParameterChangeHandler {
private PercentValueParameterChangeHandler(IValueParameter parameter, Action handler) : base(parameter, handler) { }
public static new PercentValueParameterChangeHandler Create(IValueParameter parameter, Action handler)
=> new PercentValueParameterChangeHandler(parameter, handler);
}
public class DateTimeValueParameterChangeHandler : ValueTypeValueParameterChangeHandler {
private DateTimeValueParameterChangeHandler(IValueParameter parameter, Action handler) : base(parameter, handler) { }
public static new DateTimeValueParameterChangeHandler Create(IValueParameter parameter, Action handler)
=> new DateTimeValueParameterChangeHandler(parameter, handler);
}
public class TimeSpanValueParameterChangeHandler : ValueTypeValueParameterChangeHandler {
private TimeSpanValueParameterChangeHandler(IValueParameter parameter, Action handler) : base(parameter, handler) { }
public static new TimeSpanValueParameterChangeHandler Create(IValueParameter parameter, Action handler)
=> new TimeSpanValueParameterChangeHandler(parameter, handler);
}
public class EnumValueParameterChangeHandler : ValueTypeValueParameterChangeHandler, TEnum> where TEnum : struct, IComparable {
private EnumValueParameterChangeHandler(IValueParameter> parameter, Action handler) : base(parameter, handler) { }
public static new EnumValueParameterChangeHandler Create(IValueParameter> parameter, Action handler)
=> new EnumValueParameterChangeHandler(parameter, handler);
}
public class BoolArrayParameterChangeHandler : ValueTypeArrayParameterChangeHandler {
private BoolArrayParameterChangeHandler(IValueParameter parameter, Action handler) : base(parameter, handler) { }
public static new BoolArrayParameterChangeHandler Create(IValueParameter parameter, Action handler)
=> new BoolArrayParameterChangeHandler(parameter, handler);
}
public class IntArrayParameterChangeHandler : ValueTypeArrayParameterChangeHandler {
private IntArrayParameterChangeHandler(IValueParameter parameter, Action handler) : base(parameter, handler) { }
public static new IntArrayParameterChangeHandler Create(IValueParameter parameter, Action handler)
=> new IntArrayParameterChangeHandler(parameter, handler);
}
public class DoubleArrayParameterChangeHandler : ValueTypeArrayParameterChangeHandler {
private DoubleArrayParameterChangeHandler(IValueParameter parameter, Action handler) : base(parameter, handler) { }
public static new DoubleArrayParameterChangeHandler Create(IValueParameter parameter, Action handler)
=> new DoubleArrayParameterChangeHandler(parameter, handler);
}
public class BoolMatrixParameterChangeHandler : ValueTypeMatrixParameterChangeHandler {
private BoolMatrixParameterChangeHandler(IValueParameter parameter, Action handler) : base(parameter, handler) { }
public static new BoolMatrixParameterChangeHandler Create(IValueParameter parameter, Action handler)
=> new BoolMatrixParameterChangeHandler(parameter, handler);
}
public class IntMatrixParameterChangeHandler : ValueTypeMatrixParameterChangeHandler {
private IntMatrixParameterChangeHandler(IValueParameter parameter, Action handler) : base(parameter, handler) { }
public static new IntMatrixParameterChangeHandler Create(IValueParameter parameter, Action handler)
=> new IntMatrixParameterChangeHandler(parameter, handler);
}
public class DoubleMatrixParameterChangeHandler : ValueTypeMatrixParameterChangeHandler {
private DoubleMatrixParameterChangeHandler(IValueParameter parameter, Action handler) : base(parameter, handler) { }
public static new DoubleMatrixParameterChangeHandler Create(IValueParameter parameter, Action handler)
=> new DoubleMatrixParameterChangeHandler(parameter, handler);
}
public class StringValueParameterChangeHandler : ParameterChangeHandler { // StringValue does not derive from ValueTypeValue
private StringValue last;
private StringValueParameterChangeHandler(IValueParameter parameter, Action handler) : base(parameter, handler) {
last = parameter.Value;
if (last != null && !last.ReadOnly)
last.ValueChanged += ParameterValueOnValueChanged;
}
protected override void ParameterOnValueChanged(object sender, EventArgs e) {
if (last != null) last.ValueChanged -= ParameterValueOnValueChanged;
last = ((IValueParameter)sender).Value;
if (last != null && !last.ReadOnly)
last.ValueChanged += ParameterValueOnValueChanged;
base.ParameterOnValueChanged(sender, e);
}
private void ParameterValueOnValueChanged(object sender, EventArgs e) {
handler();
}
public static StringValueParameterChangeHandler Create(IValueParameter parameter, Action handler)
=> new StringValueParameterChangeHandler(parameter, handler);
}
}