Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Parameters/3.3/ParameterSecondLevelNameBinding.cs @ 4770

Last change on this file since 4770 was 4770, checked in by abeham, 13 years ago

#1258

  • worked on parameter binding
File size: 3.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
6using HeuristicLab.Core;
7using HeuristicLab.Common;
8using System.Reflection;
9
10namespace HeuristicLab.Parameters {
11  [StorableClass]
12  public class ParameterSecondLevelNameBinding : IParameterBinding {
13    [Storable]
14    private IValueParameter source;
15    public IValueParameter Source {
16      get { return source; }
17    }
18    [Storable]
19    private ILookupParameter target;
20    public ILookupParameter Target {
21      get { return target; }
22    }
23    [Storable]
24    private string sourcePropertyName;
25    public string SourcePropertyName {
26      get { return sourcePropertyName; }
27    }
28    [Storable]
29    private ParameterNameBinding nameBinding;
30
31    [StorableConstructor]
32    protected ParameterSecondLevelNameBinding(bool deserializing) { }
33    protected ParameterSecondLevelNameBinding(ParameterSecondLevelNameBinding original, Cloner cloner) {
34      this.source = cloner.Clone(source);
35      this.target = cloner.Clone(target);
36      this.sourcePropertyName = original.sourcePropertyName;
37      RegisterEventHandlers(); // nameBinding does not need to be cloned, is recreated here
38    }
39    public ParameterSecondLevelNameBinding(ILookupParameter target, IValueParameter source, string sourcePropertyName) {
40      if (source == null) throw new ArgumentNullException("source");
41      if (target == null) throw new ArgumentNullException("target");
42      this.source = source;
43      this.target = target;
44      this.sourcePropertyName = sourcePropertyName;
45      this.nameBinding = null;
46    }
47
48    public object Clone() {
49      return Clone(new Cloner());
50    }
51
52    public virtual IDeepCloneable Clone(Cloner cloner) {
53      return new ParameterSecondLevelNameBinding(this, cloner);
54    }
55
56    public void Bind() {
57      RegisterEventHandlers();
58    }
59
60    public void Unbind() {
61      DeregisterEventHandlers();
62    }
63
64    [StorableHook(HookType.AfterDeserialization)]
65    private void AfterDeserialization() {
66      RegisterEventHandlers();
67    }
68
69    private void RegisterEventHandlers() {
70      source.ValueChanged += new EventHandler(source_ValueChanged);
71      IParameter sourceParameter = GetNestedParameter(source, sourcePropertyName);
72      if (sourceParameter != null) {
73        nameBinding = new ParameterNameBinding(target, sourceParameter);
74        nameBinding.Bind();
75      }
76    }
77
78    private void DeregisterEventHandlers() {
79      source.ValueChanged -= new EventHandler(source_ValueChanged);
80
81      if (nameBinding != null) nameBinding.Unbind();
82      nameBinding = null;
83    }
84
85    private void source_ValueChanged(object sender, EventArgs e) {
86      IParameter sourceParameter = GetNestedParameter(source, sourcePropertyName);
87      if (sourceParameter != null) {
88        if (nameBinding != null) nameBinding.Unbind();
89        nameBinding = new ParameterNameBinding(target, sourceParameter);
90        nameBinding.Bind();
91      }
92    }
93
94    private IParameter GetNestedParameter(IValueParameter param, string name) {
95      if (param.Value == null) return null;
96      PropertyInfo property = param.Value.GetType().GetProperty(name, BindingFlags.Instance | BindingFlags.NonPublic);
97      if (property == null) return null;
98      if (property.GetGetMethod() == null) throw new InvalidOperationException("Property " + name + " of type " + param.Value.GetType().FullName + " does not have a Get method.");
99      IParameter nestedParam = (property.GetGetMethod().Invoke(param.Value, null) as IParameter);
100      return nestedParam;
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.