Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Parameters/3.3/ParameterSecondLevelValueBinding.cs @ 4757

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

#1258

  • worked on parameter binding (wip)
File size: 4.3 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 ParameterSecondLevelActualNameBinding : IParameterBinding {
13    [Storable]
14    private IValueParameter source;
15    public IValueParameter Source {
16      get { return source; }
17    }
18    [Storable]
19    private IValueParameter destination;
20    public IValueParameter Destination {
21      get { return destination; }
22    }
23    [Storable]
24    private string sourcePropertyName;
25    public string SourcePropertyName {
26      get { return sourcePropertyName; }
27    }
28    [Storable]
29    private string destinationPropertyName;
30    public string DestinationPropertyName {
31      get { return destinationPropertyName; }
32    }
33
34    [StorableConstructor]
35    protected ParameterSecondLevelActualNameBinding(bool deserializing) { }
36    protected ParameterSecondLevelActualNameBinding(ParameterSecondLevelActualNameBinding original, Cloner cloner) {
37      this.source = cloner.Clone(source);
38      this.destination = cloner.Clone(destination);
39      RegisterEventHandlers();
40    }
41    public ParameterSecondLevelActualNameBinding(IValueParameter source, IValueParameter destination, string sourcePropertyName, string destinationPropertyName) {
42      if (source == null) throw new ArgumentNullException("source");
43      if (destination == null) throw new ArgumentNullException("destination");
44      this.source = source;
45      this.destination = destination;
46      this.sourcePropertyName = sourcePropertyName;
47      this.destinationPropertyName = destinationPropertyName;
48      RegisterEventHandlers();
49    }
50
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new ParameterSecondLevelActualNameBinding(this, cloner);
53    }
54
55    public void Bind() {
56      RegisterEventHandlers();
57    }
58
59    public void Unbind() {
60      DeregisterEventHandlers();
61    }
62
63    [StorableHook(HookType.AfterDeserialization)]
64    private void AfterDeserialization() {
65      RegisterEventHandlers();
66    }
67
68    private void RegisterEventHandlers() {
69      source.ValueChanged += new EventHandler(source_ValueChanged);
70      ILookupParameter sourceParameter = GetNestedParameter(source, sourcePropertyName);
71      if (sourceParameter != null)
72        sourceParameter.ActualNameChanged += new EventHandler(sourceParameter_ActualNameChanged);
73    }
74
75    private void DeregisterEventHandlers() {
76      source.ValueChanged -= new EventHandler(source_ValueChanged);
77
78      ILookupParameter sourceParameter = GetNestedParameter(source, sourcePropertyName);
79      if (sourceParameter != null)
80        sourceParameter.ActualNameChanged -= new EventHandler(sourceParameter_ActualNameChanged);
81    }
82
83    private ILookupParameter GetNestedParameter(IValueParameter param, string name) {
84      if (param.Value == null) return null;
85      PropertyInfo property = param.Value.GetType().GetProperty(name, BindingFlags.Instance | BindingFlags.NonPublic);
86      if (property == null) return null;
87      if (property.GetGetMethod() == null) throw new InvalidOperationException("Property " + name + " of type " + param.Value.GetType().FullName + " does not have a Get method.");
88      ILookupParameter nestedParam = (property.GetGetMethod().Invoke(param.Value, null) as ILookupParameter);
89      return nestedParam;
90    }
91
92    private void source_ValueChanged(object sender, EventArgs e) {
93      ILookupParameter sourceParameter = GetNestedParameter(source, sourcePropertyName);
94      if (sourceParameter != null) {
95        sourceParameter.ActualNameChanged += new EventHandler(sourceParameter_ActualNameChanged);
96        DoAction();
97      }
98    }
99
100    private void sourceParameter_ActualNameChanged(object sender, EventArgs e) {
101      DoAction();
102    }
103
104    private void DoAction() {
105      ILookupParameter sourceParameter = GetNestedParameter(source, sourcePropertyName);
106      ILookupParameter destinationParameter = GetNestedParameter(destination, destinationPropertyName);
107      if (destinationParameter != null)
108        destinationParameter.ActualName = sourceParameter.ActualName;
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.