[runtime] Fix corlib out of date error with disabled COM
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Model / ModelPropertyDescriptor.cs
1 //-----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //-----------------------------------------------------------------------------
4
5 namespace System.Activities.Presentation.Model
6 {
7     using System;
8     using System.ComponentModel;
9
10     // This is a property descriptor that wraps ModelProperty objects.
11     // It is used when someone uses TypeDescriptor to ask for type information
12     // on an editing model item.
13     internal class ModelPropertyDescriptor : PropertyDescriptor
14     {
15         ModelProperty itemProperty;
16         TypeConverter converter;
17
18         internal ModelPropertyDescriptor(ModelProperty itemProperty)
19             : base(itemProperty.Name, null)
20         {
21             this.itemProperty = itemProperty;
22         }
23
24         public override AttributeCollection Attributes
25         {
26             get { return this.itemProperty.Attributes; }
27         }
28
29         // Returns the type converter for this property.  Our property
30         // descriptor wrapper is "complete" in that it always returns
31         // editing model item objects.  Because of that, we must wrap
32         // all type converters.
33         public override TypeConverter Converter
34         {
35             get
36             {
37                 if (this.converter == null)
38                 {
39                     TypeConverter baseConverter = base.Converter;
40                     IModelTreeItem propertyParent = this.itemProperty.Parent as IModelTreeItem;
41                     this.converter = new ModelTypeConverter(propertyParent.ModelTreeManager, baseConverter);
42                 }
43
44                 return converter;
45             }
46         }
47
48
49         public override bool IsBrowsable
50         {
51             get { return this.itemProperty.IsBrowsable; }
52         }
53
54         // Returns the type of object that defined this property.
55         public override Type ComponentType
56         {
57             get { return this.itemProperty.Parent.ItemType; }
58         }
59
60         public override bool IsReadOnly
61         {
62             get { return this.itemProperty.IsReadOnly; }
63         }
64
65         // Returns the data type of the property.  
66         public override Type PropertyType
67         {
68
69             get
70             {
71                 return this.itemProperty.PropertyType;
72             }
73
74         }
75
76         public override PropertyDescriptorCollection GetChildProperties(object instance, Attribute[] filter)
77         {
78             return base.GetChildProperties(instance, filter);
79         }
80
81         public override bool CanResetValue(object component)
82         {
83             return this.itemProperty.IsSet;
84         }
85
86         public override object GetEditor(Type editorBaseType)
87         {
88             // The new PropertyEntry PropertyValue editor model does not use this
89             return null;
90         }
91
92         // Returns the current value of this property.
93         // When the object is not primitive, enum, or string we always return the ModelItem wrapping it.
94         // this enables nested binding in wpf still go through modelItems tree so taht we can intercept
95         // the property sets e.g "{Binding Path=RootModel.ComplexProperty.Blah"}, since we return a ModelItem
96         // for ComplexProperty we can still intercept sets made from Wpf controls to Blah even if ComplexProperty
97         // type does not implement INotifyPropertyChanged.
98         public override object GetValue(object component)
99         {
100             ModelItem value = this.itemProperty.Value;
101             if (value == null)
102             {
103                 return null;
104             }
105             Type itemType = value.ItemType;
106             if (itemType.IsPrimitive || itemType.IsEnum || itemType.Equals(typeof(String)))
107             {
108                 return value.GetCurrentValue();
109             }
110             return value;
111         }
112
113         public override void ResetValue(object component)
114         {
115             this.itemProperty.ClearValue();
116         }
117
118         // Sets the property value to the given value.  For
119         // convenience, the value passed can either be an
120         // item or a raw value.  In the latter case we will
121         // wrap the value into an item for you. 
122         public override void SetValue(object component, object value)
123         {
124             this.itemProperty.SetValue(value);
125         }
126
127         // Returns true if the value should be serialized to code.
128         public override bool ShouldSerializeValue(object component)
129         {
130             // If the local value is set, see if the property supports
131             // a ShouldSerialize on its property descriptor.  If it doesn't,
132             // then we let the IsSet dictate the 'setness'.
133
134             if (this.itemProperty.IsSet)
135             {
136                 ModelPropertyImpl modelProp = this.itemProperty as ModelPropertyImpl;
137                 if (modelProp != null)
138                 {
139                     return modelProp.PropertyDescriptor.ShouldSerializeValue(this.itemProperty.Parent.GetCurrentValue());
140                 }
141                 return true;
142             }
143
144             return false;
145         }
146     }
147 }