Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Common/Auxiliary/ValueStore.cs @ 13808

Last change on this file since 13808 was 12503, checked in by aballeit, 9 years ago

#2283 added GUI and charts; fixed MCTS

File size: 2.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.ComponentModel;
6using System.Diagnostics;
7
8namespace Microsoft.Research.DynamicDataDisplay
9{
10  public sealed class ValueStore : CustomTypeDescriptor, INotifyPropertyChanged
11  {
12    public ValueStore(params string[] propertiesNames)
13    {
14      foreach (var propertyName in propertiesNames)
15      {
16        this[propertyName] = "";
17      }
18    }
19
20    private Dictionary<string, object> cache = new Dictionary<string, object>();
21
22    public object this[string propertyName]
23    {
24      get { return cache[propertyName]; }
25      set { SetValue(propertyName, value); }
26    }
27
28    public ValueStore SetValue(string propertyName, object value)
29    {
30      cache[propertyName] = value;
31      PropertyChanged.Raise(this, propertyName);
32
33      return this;
34    }
35
36    private PropertyDescriptorCollection collection;
37    public override PropertyDescriptorCollection GetProperties()
38    {
39      PropertyDescriptor[] propertyDescriptors = new PropertyDescriptor[cache.Count];
40      var keys = cache.Keys.ToArray();
41      for (int i = 0; i < keys.Length; i++)
42      {
43        propertyDescriptors[i] = new ValueStorePropertyDescriptor(keys[i]);
44      }
45
46      collection = new PropertyDescriptorCollection(propertyDescriptors);
47      return collection;
48    }
49
50    private sealed class ValueStorePropertyDescriptor : PropertyDescriptor
51    {
52      private readonly string name;
53
54      public ValueStorePropertyDescriptor(string name)
55        : base(name, null)
56      {
57        this.name = name;
58      }
59
60      public override bool CanResetValue(object component)
61      {
62        return false;
63      }
64
65      public override Type ComponentType
66      {
67        get { return typeof(ValueStore); }
68      }
69
70      public override object GetValue(object component)
71      {
72        ValueStore store = (ValueStore)component;
73        return store[name];
74      }
75
76      public override bool IsReadOnly
77      {
78        get { return false; }
79      }
80
81      public override Type PropertyType
82      {
83        get { return typeof(string); }
84      }
85
86      public override void ResetValue(object component)
87      {
88      }
89
90      public override void SetValue(object component, object value)
91      {
92      }
93
94      public override bool ShouldSerializeValue(object component)
95      {
96        return false;
97      }
98    }
99
100    #region INotifyPropertyChanged Members
101
102    public event PropertyChangedEventHandler PropertyChanged;
103
104    #endregion
105  }
106}
Note: See TracBrowser for help on using the repository browser.