1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Common;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Parameters {
|
---|
10 | [StorableClass]
|
---|
11 | public class ParameterValueBinding : IParameterBinding {
|
---|
12 | [Storable]
|
---|
13 | private IValueParameter recipient;
|
---|
14 | public IValueParameter Recipient {
|
---|
15 | get { return recipient; }
|
---|
16 | }
|
---|
17 | [Storable]
|
---|
18 | private IValueParameter donor;
|
---|
19 | public IValueParameter Donor {
|
---|
20 | get { return donor; }
|
---|
21 | }
|
---|
22 |
|
---|
23 | [StorableConstructor]
|
---|
24 | protected ParameterValueBinding(bool deserializing) { }
|
---|
25 | protected ParameterValueBinding(ParameterValueBinding original, Cloner cloner) {
|
---|
26 | this.donor = cloner.Clone(donor);
|
---|
27 | this.recipient = cloner.Clone(recipient);
|
---|
28 | RegisterEventHandlers();
|
---|
29 | }
|
---|
30 | public ParameterValueBinding(IValueParameter recipient, IValueParameter donor) {
|
---|
31 | if (recipient == null) throw new ArgumentNullException("destination");
|
---|
32 | if (donor == null) throw new ArgumentNullException("source");
|
---|
33 | this.recipient = recipient;
|
---|
34 | this.donor = donor;
|
---|
35 | RegisterEventHandlers();
|
---|
36 | }
|
---|
37 |
|
---|
38 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
39 | return new ParameterValueBinding(this, cloner);
|
---|
40 | }
|
---|
41 |
|
---|
42 | public void Bind() {
|
---|
43 | RegisterEventHandlers();
|
---|
44 | }
|
---|
45 |
|
---|
46 | public void Unbind() {
|
---|
47 | DeregisterEventHandlers();
|
---|
48 | }
|
---|
49 |
|
---|
50 | [StorableHook(HookType.AfterDeserialization)]
|
---|
51 | private void AfterDeserialization() {
|
---|
52 | RegisterEventHandlers();
|
---|
53 | }
|
---|
54 |
|
---|
55 | private void RegisterEventHandlers() {
|
---|
56 | donor.ValueChanged += new EventHandler(source_ValueChanged);
|
---|
57 | }
|
---|
58 |
|
---|
59 | private void DeregisterEventHandlers() {
|
---|
60 | donor.ValueChanged -= new EventHandler(source_ValueChanged);
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | private void source_ValueChanged(object sender, EventArgs e) {
|
---|
65 | recipient.Value = (IItem)donor.Value.Clone();
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|