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 target;
|
---|
14 | public IValueParameter Target {
|
---|
15 | get { return target; }
|
---|
16 | }
|
---|
17 | [Storable]
|
---|
18 | private IValueParameter source;
|
---|
19 | public IValueParameter Source {
|
---|
20 | get { return source; }
|
---|
21 | }
|
---|
22 |
|
---|
23 | [StorableConstructor]
|
---|
24 | protected ParameterValueBinding(bool deserializing) { }
|
---|
25 | protected ParameterValueBinding(ParameterValueBinding original, Cloner cloner) {
|
---|
26 | this.source = cloner.Clone(source);
|
---|
27 | this.target = cloner.Clone(target);
|
---|
28 | RegisterEventHandlers();
|
---|
29 | }
|
---|
30 | public ParameterValueBinding(IValueParameter target, IValueParameter source) {
|
---|
31 | if (target == null) throw new ArgumentNullException("destination");
|
---|
32 | if (source == null) throw new ArgumentNullException("source");
|
---|
33 | this.target = target;
|
---|
34 | this.source = source;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public object Clone() {
|
---|
38 | return Clone(new Cloner());
|
---|
39 | }
|
---|
40 |
|
---|
41 | public virtual IDeepCloneable Clone(Cloner cloner) {
|
---|
42 | return new ParameterValueBinding(this, cloner);
|
---|
43 | }
|
---|
44 |
|
---|
45 | public void Bind() {
|
---|
46 | RegisterEventHandlers();
|
---|
47 | ExecuteBinding();
|
---|
48 | }
|
---|
49 |
|
---|
50 | public void Unbind() {
|
---|
51 | DeregisterEventHandlers();
|
---|
52 | }
|
---|
53 |
|
---|
54 | [StorableHook(HookType.AfterDeserialization)]
|
---|
55 | private void AfterDeserialization() {
|
---|
56 | RegisterEventHandlers();
|
---|
57 | }
|
---|
58 |
|
---|
59 | private void RegisterEventHandlers() {
|
---|
60 | source.ValueChanged += new EventHandler(source_ValueChanged);
|
---|
61 | }
|
---|
62 |
|
---|
63 | private void DeregisterEventHandlers() {
|
---|
64 | source.ValueChanged -= new EventHandler(source_ValueChanged);
|
---|
65 | }
|
---|
66 |
|
---|
67 | private void source_ValueChanged(object sender, EventArgs e) {
|
---|
68 | ExecuteBinding();
|
---|
69 | }
|
---|
70 |
|
---|
71 | private void ExecuteBinding() {
|
---|
72 | target.Value = (IItem)source.Value.Clone();
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|