1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.ComponentModel;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Parameters {
|
---|
28 | public class ParameterChangeHandler<TItem> where TItem : class, IItem {
|
---|
29 | protected Action handler;
|
---|
30 |
|
---|
31 | protected ParameterChangeHandler(IValueParameter<TItem> parameter, Action handler) {
|
---|
32 | if (!(parameter is IFixedValueParameter<TItem>))
|
---|
33 | parameter.ValueChanged += ParameterOnValueChanged;
|
---|
34 | this.handler = handler;
|
---|
35 | }
|
---|
36 |
|
---|
37 | protected virtual void ParameterOnValueChanged(object sender, EventArgs e) {
|
---|
38 | handler();
|
---|
39 | }
|
---|
40 |
|
---|
41 | public static ParameterChangeHandler<TItem> Create(IValueParameter<TItem> parameter, Action handler) {
|
---|
42 | return new ParameterChangeHandler<TItem>(parameter, handler);
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public class ValueTypeValueParameterChangeHandler<TItem, TValue> : ParameterChangeHandler<TItem>
|
---|
47 | where TValue : struct
|
---|
48 | where TItem : ValueTypeValue<TValue> {
|
---|
49 | private ValueTypeValue<TValue> last;
|
---|
50 |
|
---|
51 | protected ValueTypeValueParameterChangeHandler(IValueParameter<TItem> parameter, Action handler)
|
---|
52 | : base(parameter, handler) {
|
---|
53 | last = parameter.Value;
|
---|
54 | if (last != null && !last.ReadOnly)
|
---|
55 | last.ValueChanged += ParameterValueOnValueChanged;
|
---|
56 | }
|
---|
57 |
|
---|
58 | protected override void ParameterOnValueChanged(object sender, EventArgs e) {
|
---|
59 | if (last != null) last.ValueChanged -= ParameterValueOnValueChanged;
|
---|
60 | last = ((IValueParameter<TItem>)sender).Value;
|
---|
61 | if (last != null && !last.ReadOnly)
|
---|
62 | last.ValueChanged += ParameterValueOnValueChanged;
|
---|
63 | base.ParameterOnValueChanged(sender, e);
|
---|
64 | }
|
---|
65 |
|
---|
66 | protected void ParameterValueOnValueChanged(object sender, EventArgs e) {
|
---|
67 | handler();
|
---|
68 | }
|
---|
69 |
|
---|
70 | public static ValueTypeValueParameterChangeHandler<TItem, TValue> Create(IValueParameter<TItem> parameter, Action handler)
|
---|
71 | => new ValueTypeValueParameterChangeHandler<TItem, TValue>(parameter, handler);
|
---|
72 | }
|
---|
73 | public class ValueTypeArrayParameterChangeHandler<TItem, TValue> : ParameterChangeHandler<TItem>
|
---|
74 | where TValue : struct
|
---|
75 | where TItem : ValueTypeArray<TValue> {
|
---|
76 | private ValueTypeArray<TValue> last;
|
---|
77 |
|
---|
78 | protected ValueTypeArrayParameterChangeHandler(IValueParameter<TItem> parameter, Action handler)
|
---|
79 | : base(parameter, handler) {
|
---|
80 | last = parameter.Value;
|
---|
81 | if (last != null && !last.ReadOnly)
|
---|
82 | last.ToStringChanged += ParameterValueOnValueChanged;
|
---|
83 | }
|
---|
84 |
|
---|
85 | protected override void ParameterOnValueChanged(object sender, EventArgs e) {
|
---|
86 | if (last != null) last.ToStringChanged -= ParameterValueOnValueChanged;
|
---|
87 | last = ((IValueParameter<TItem>)sender).Value;
|
---|
88 | if (last != null && !last.ReadOnly)
|
---|
89 | last.ToStringChanged += ParameterValueOnValueChanged;
|
---|
90 | base.ParameterOnValueChanged(sender, e);
|
---|
91 | }
|
---|
92 |
|
---|
93 | protected void ParameterValueOnValueChanged(object sender, EventArgs e) {
|
---|
94 | handler();
|
---|
95 | }
|
---|
96 |
|
---|
97 | public static ValueTypeArrayParameterChangeHandler<TItem, TValue> Create(IValueParameter<TItem> parameter, Action handler)
|
---|
98 | => new ValueTypeArrayParameterChangeHandler<TItem, TValue>(parameter, handler);
|
---|
99 | }
|
---|
100 |
|
---|
101 | public class ValueTypeMatrixParameterChangeHandler<TItem, TValue> : ParameterChangeHandler<TItem>
|
---|
102 | where TValue : struct
|
---|
103 | where TItem : ValueTypeMatrix<TValue> {
|
---|
104 | private ValueTypeMatrix<TValue> last;
|
---|
105 |
|
---|
106 | protected ValueTypeMatrixParameterChangeHandler(IValueParameter<TItem> parameter, Action handler)
|
---|
107 | : base(parameter, handler) {
|
---|
108 | last = parameter.Value;
|
---|
109 | if (last != null && !last.ReadOnly)
|
---|
110 | last.ToStringChanged += ParameterValueOnValueChanged;
|
---|
111 | }
|
---|
112 |
|
---|
113 | protected override void ParameterOnValueChanged(object sender, EventArgs e) {
|
---|
114 | if (last != null) last.ToStringChanged -= ParameterValueOnValueChanged;
|
---|
115 | last = ((IValueParameter<TItem>)sender).Value;
|
---|
116 | if (last != null && !last.ReadOnly)
|
---|
117 | last.ToStringChanged += ParameterValueOnValueChanged;
|
---|
118 | base.ParameterOnValueChanged(sender, e);
|
---|
119 | }
|
---|
120 |
|
---|
121 | protected void ParameterValueOnValueChanged(object sender, EventArgs e) {
|
---|
122 | handler();
|
---|
123 | }
|
---|
124 |
|
---|
125 | public static ValueTypeMatrixParameterChangeHandler<TItem, TValue> Create(IValueParameter<TItem> parameter, Action handler)
|
---|
126 | => new ValueTypeMatrixParameterChangeHandler<TItem, TValue>(parameter, handler);
|
---|
127 | }
|
---|
128 |
|
---|
129 | public class BoolValueParameterChangeHandler : ValueTypeValueParameterChangeHandler<BoolValue, bool> {
|
---|
130 | private BoolValueParameterChangeHandler(IValueParameter<BoolValue> parameter, Action handler) : base(parameter, handler) { }
|
---|
131 | public static new BoolValueParameterChangeHandler Create(IValueParameter<BoolValue> parameter, Action handler)
|
---|
132 | => new BoolValueParameterChangeHandler(parameter, handler);
|
---|
133 | }
|
---|
134 | public class IntValueParameterChangeHandler : ValueTypeValueParameterChangeHandler<IntValue, int> {
|
---|
135 | private IntValueParameterChangeHandler(IValueParameter<IntValue> parameter, Action handler) : base(parameter, handler) { }
|
---|
136 | public static new IntValueParameterChangeHandler Create(IValueParameter<IntValue> parameter, Action handler)
|
---|
137 | => new IntValueParameterChangeHandler(parameter, handler);
|
---|
138 | }
|
---|
139 | public class DoubleValueParameterChangeHandler : ValueTypeValueParameterChangeHandler<DoubleValue, double> {
|
---|
140 | private DoubleValueParameterChangeHandler(IValueParameter<DoubleValue> parameter, Action handler) : base(parameter, handler) { }
|
---|
141 | public static new DoubleValueParameterChangeHandler Create(IValueParameter<DoubleValue> parameter, Action handler)
|
---|
142 | => new DoubleValueParameterChangeHandler(parameter, handler);
|
---|
143 | }
|
---|
144 | public class PercentValueParameterChangeHandler : ValueTypeValueParameterChangeHandler<PercentValue, double> {
|
---|
145 | private PercentValueParameterChangeHandler(IValueParameter<PercentValue> parameter, Action handler) : base(parameter, handler) { }
|
---|
146 | public static new PercentValueParameterChangeHandler Create(IValueParameter<PercentValue> parameter, Action handler)
|
---|
147 | => new PercentValueParameterChangeHandler(parameter, handler);
|
---|
148 | }
|
---|
149 | public class DateTimeValueParameterChangeHandler : ValueTypeValueParameterChangeHandler<DateTimeValue, DateTime> {
|
---|
150 | private DateTimeValueParameterChangeHandler(IValueParameter<DateTimeValue> parameter, Action handler) : base(parameter, handler) { }
|
---|
151 | public static new DateTimeValueParameterChangeHandler Create(IValueParameter<DateTimeValue> parameter, Action handler)
|
---|
152 | => new DateTimeValueParameterChangeHandler(parameter, handler);
|
---|
153 | }
|
---|
154 | public class TimeSpanValueParameterChangeHandler : ValueTypeValueParameterChangeHandler<TimeSpanValue, TimeSpan> {
|
---|
155 | private TimeSpanValueParameterChangeHandler(IValueParameter<TimeSpanValue> parameter, Action handler) : base(parameter, handler) { }
|
---|
156 | public static new TimeSpanValueParameterChangeHandler Create(IValueParameter<TimeSpanValue> parameter, Action handler)
|
---|
157 | => new TimeSpanValueParameterChangeHandler(parameter, handler);
|
---|
158 | }
|
---|
159 | public class EnumValueParameterChangeHandler<TEnum> : ValueTypeValueParameterChangeHandler<EnumValue<TEnum>, TEnum> where TEnum : struct, IComparable {
|
---|
160 | private EnumValueParameterChangeHandler(IValueParameter<EnumValue<TEnum>> parameter, Action handler) : base(parameter, handler) { }
|
---|
161 | public static new EnumValueParameterChangeHandler<TEnum> Create(IValueParameter<EnumValue<TEnum>> parameter, Action handler)
|
---|
162 | => new EnumValueParameterChangeHandler<TEnum>(parameter, handler);
|
---|
163 | }
|
---|
164 | public class BoolArrayParameterChangeHandler : ValueTypeArrayParameterChangeHandler<BoolArray, bool> {
|
---|
165 | private BoolArrayParameterChangeHandler(IValueParameter<BoolArray> parameter, Action handler) : base(parameter, handler) { }
|
---|
166 | public static new BoolArrayParameterChangeHandler Create(IValueParameter<BoolArray> parameter, Action handler)
|
---|
167 | => new BoolArrayParameterChangeHandler(parameter, handler);
|
---|
168 | }
|
---|
169 | public class IntArrayParameterChangeHandler : ValueTypeArrayParameterChangeHandler<IntArray, int> {
|
---|
170 | private IntArrayParameterChangeHandler(IValueParameter<IntArray> parameter, Action handler) : base(parameter, handler) { }
|
---|
171 | public static new IntArrayParameterChangeHandler Create(IValueParameter<IntArray> parameter, Action handler)
|
---|
172 | => new IntArrayParameterChangeHandler(parameter, handler);
|
---|
173 | }
|
---|
174 | public class DoubleArrayParameterChangeHandler : ValueTypeArrayParameterChangeHandler<DoubleArray, double> {
|
---|
175 | private DoubleArrayParameterChangeHandler(IValueParameter<DoubleArray> parameter, Action handler) : base(parameter, handler) { }
|
---|
176 | public static new DoubleArrayParameterChangeHandler Create(IValueParameter<DoubleArray> parameter, Action handler)
|
---|
177 | => new DoubleArrayParameterChangeHandler(parameter, handler);
|
---|
178 | }
|
---|
179 | public class BoolMatrixParameterChangeHandler : ValueTypeMatrixParameterChangeHandler<BoolMatrix, bool> {
|
---|
180 | private BoolMatrixParameterChangeHandler(IValueParameter<BoolMatrix> parameter, Action handler) : base(parameter, handler) { }
|
---|
181 | public static new BoolMatrixParameterChangeHandler Create(IValueParameter<BoolMatrix> parameter, Action handler)
|
---|
182 | => new BoolMatrixParameterChangeHandler(parameter, handler);
|
---|
183 | }
|
---|
184 | public class IntMatrixParameterChangeHandler : ValueTypeMatrixParameterChangeHandler<IntMatrix, int> {
|
---|
185 | private IntMatrixParameterChangeHandler(IValueParameter<IntMatrix> parameter, Action handler) : base(parameter, handler) { }
|
---|
186 | public static new IntMatrixParameterChangeHandler Create(IValueParameter<IntMatrix> parameter, Action handler)
|
---|
187 | => new IntMatrixParameterChangeHandler(parameter, handler);
|
---|
188 | }
|
---|
189 | public class DoubleMatrixParameterChangeHandler : ValueTypeMatrixParameterChangeHandler<DoubleMatrix, double> {
|
---|
190 | private DoubleMatrixParameterChangeHandler(IValueParameter<DoubleMatrix> parameter, Action handler) : base(parameter, handler) { }
|
---|
191 | public static new DoubleMatrixParameterChangeHandler Create(IValueParameter<DoubleMatrix> parameter, Action handler)
|
---|
192 | => new DoubleMatrixParameterChangeHandler(parameter, handler);
|
---|
193 | }
|
---|
194 | public class StringValueParameterChangeHandler : ParameterChangeHandler<StringValue> { // StringValue does not derive from ValueTypeValue
|
---|
195 | private StringValue last;
|
---|
196 |
|
---|
197 | private StringValueParameterChangeHandler(IValueParameter<StringValue> parameter, Action handler) : base(parameter, handler) {
|
---|
198 | last = parameter.Value;
|
---|
199 | if (last != null && !last.ReadOnly)
|
---|
200 | last.ValueChanged += ParameterValueOnValueChanged;
|
---|
201 | }
|
---|
202 |
|
---|
203 | protected override void ParameterOnValueChanged(object sender, EventArgs e) {
|
---|
204 | if (last != null) last.ValueChanged -= ParameterValueOnValueChanged;
|
---|
205 | last = ((IValueParameter<StringValue>)sender).Value;
|
---|
206 | if (last != null && !last.ReadOnly)
|
---|
207 | last.ValueChanged += ParameterValueOnValueChanged;
|
---|
208 | base.ParameterOnValueChanged(sender, e);
|
---|
209 | }
|
---|
210 |
|
---|
211 | private void ParameterValueOnValueChanged(object sender, EventArgs e) {
|
---|
212 | handler();
|
---|
213 | }
|
---|
214 | public static StringValueParameterChangeHandler Create(IValueParameter<StringValue> parameter, Action handler)
|
---|
215 | => new StringValueParameterChangeHandler(parameter, handler);
|
---|
216 | }
|
---|
217 |
|
---|
218 | public class ItemListParameterChangeHandler<T> : ParameterChangeHandler<ItemList<T>> where T : class,IItem {
|
---|
219 | private ItemList<T> last;
|
---|
220 |
|
---|
221 | private ItemListParameterChangeHandler(IValueParameter<ItemList<T>> parameter, Action handler)
|
---|
222 | : base(parameter, handler) {
|
---|
223 | last = parameter.Value;
|
---|
224 | if (last != null && !(last is ReadOnlyItemList<T>)) {
|
---|
225 | last.PropertyChanged += ParameterValueOnListChanged;
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | protected override void ParameterOnValueChanged(object sender, EventArgs e) {
|
---|
230 | if (last != null && !(last is ReadOnlyItemList<T>))
|
---|
231 | last.PropertyChanged -= ParameterValueOnListChanged;
|
---|
232 | last = ((IValueParameter<ItemList<T>>)sender).Value;
|
---|
233 | if (last != null && !(last is ReadOnlyItemList<T>))
|
---|
234 | last.PropertyChanged += ParameterValueOnListChanged;
|
---|
235 | base.ParameterOnValueChanged(sender, e);
|
---|
236 | }
|
---|
237 |
|
---|
238 | private void ParameterValueOnListChanged(object sender, PropertyChangedEventArgs e) {
|
---|
239 | if (e.PropertyName == "Item[]") handler();
|
---|
240 | }
|
---|
241 | public static ItemListParameterChangeHandler<T> Create(IValueParameter<ItemList<T>> parameter, Action handler)
|
---|
242 | => new ItemListParameterChangeHandler<T>(parameter, handler);
|
---|
243 | }
|
---|
244 | }
|
---|