[corlib] Avoid unnecessary ephemeron array resizes
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Model / ModelTypeDescriptorContextWrapper.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation.Model
6 {
7
8     using System;
9     using System.ComponentModel;
10     using System.Diagnostics;
11
12     //
13     // We need to provide a type descriptor context that can get to
14     // services in the editing context.  This wrapper does that by
15     // first checking the original set of services and then routing
16     // to the given service provider (which is usually an editing context's
17     // service manager).
18     //
19     internal class ModelTypeDescriptorContextWrapper : ITypeDescriptorContext
20     {
21
22         ITypeDescriptorContext context;
23         ModelTreeManager modelTreeManager;
24
25         internal ModelTypeDescriptorContextWrapper(ITypeDescriptorContext context, ModelTreeManager modelTreeManager)
26         {
27
28             this.context = context;
29             this.modelTreeManager = modelTreeManager;
30         }
31
32
33         public IContainer Container
34         {
35             get { return this.context == null ? null : this.context.Container; }
36         }
37
38         public object Instance
39         {
40             get
41             {
42                 object instance = null;
43
44                 if (this.context != null)
45                 {
46                     instance = this.context.Instance;
47                     ModelItem item = instance as ModelItem;
48                     if (item != null)
49                     {
50                         instance = item.GetCurrentValue();
51                     }
52                 }
53
54                 return instance;
55             }
56         }
57
58         public PropertyDescriptor PropertyDescriptor
59         {
60             get
61             {
62                 PropertyDescriptor desc = null;
63
64                 if (this.context != null)
65                 {
66                     desc = this.context.PropertyDescriptor;
67                     if (desc != null)
68                     {
69                         ModelItem item = this.context.Instance as ModelItem;
70                         if (item != null)
71                         {
72                             desc = new WrappedPropertyDescriptor(desc, item);
73                         }
74                     }
75                 }
76
77                 return desc;
78             }
79         }
80
81
82         public void OnComponentChanged()
83         {
84             if (this.context != null)
85             {
86                 this.context.OnComponentChanged();
87             }
88         }
89
90         public bool OnComponentChanging()
91         {
92             return this.context == null ? false : this.context.OnComponentChanging();
93         }
94
95         public object GetService(Type serviceType)
96         {
97             object service = null;
98
99             if (this.context != null)
100             {
101                 service = this.context.GetService(serviceType);
102             }
103
104             if (service == null)
105             {
106                 service = this.modelTreeManager.Context.Services.GetService(serviceType);
107             }
108
109
110             return service;
111         }
112
113
114         // This property descriptor dynamically converts calls to
115         // the original property descriptor, converting values on
116         // the fly.
117         class WrappedPropertyDescriptor : PropertyDescriptor
118         {
119             private PropertyDescriptor _parent;
120             private ModelItem _item;
121
122             internal WrappedPropertyDescriptor(PropertyDescriptor parent, ModelItem item)
123                 : base(parent)
124             {
125                 _parent = parent;
126                 _item = item;
127             }
128             public override Type ComponentType
129             { get { return _parent.ComponentType; } }
130             public override bool IsReadOnly
131             { get { return _parent.IsReadOnly; } }
132             public override Type PropertyType
133             { get { return _parent.PropertyType; } }
134
135             public override bool CanResetValue(object component)
136             {
137                 return _parent.CanResetValue(_item);
138             }
139             public override object GetValue(object component)
140             {
141                 return _parent.GetValue(_item);
142             }
143             public override void ResetValue(object component)
144             {
145                 _parent.ResetValue(_item);
146             }
147             public override void SetValue(object component, object value)
148             {
149                 _parent.SetValue(_item, value);
150             }
151             public override bool ShouldSerializeValue(object component)
152             {
153                 return _parent.ShouldSerializeValue(_item);
154             }
155         }
156     }
157 }