2007-07-11 Igor Zelmanovich <igorz@mainsoft.com>
[mono.git] / mcs / class / System / System.ComponentModel / ReflectionPropertyDescriptor.cs
1 //
2 // System.ComponentModel.PropertyDescriptor.cs
3 //
4 // Author:
5 //  Lluis Sanchez Gual (lluis@ximian.com)
6 //  Ivan N. Zlatev (contact i-nZ.net)
7 // (C) Novell, Inc.
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.Reflection;
34 using System.Runtime.InteropServices;
35 using System.ComponentModel.Design;
36
37 namespace System.ComponentModel
38 {
39         internal class ReflectionPropertyDescriptor : PropertyDescriptor
40         {
41                 PropertyInfo _member;
42                 Type _componentType;
43
44                 public ReflectionPropertyDescriptor (Type componentType, PropertyDescriptor oldPropertyDescriptor, Attribute [] attributes)
45                 : base (oldPropertyDescriptor, attributes)
46                 {
47                         _componentType = componentType;
48                 }
49
50                 public ReflectionPropertyDescriptor (Type componentType, string name, Type type, Attribute [] attributes)
51                 : base (name, attributes)
52                 {
53                         _componentType = componentType;
54                 }
55
56                 public ReflectionPropertyDescriptor (PropertyInfo info)
57                 : base (info.Name, (Attribute[])info.GetCustomAttributes (true))
58                 {
59                         _member = info;
60                         _componentType = _member.DeclaringType;
61                 }
62
63                 PropertyInfo GetPropertyInfo ()
64                 {
65                         if (_member == null) {
66                                 _member = _componentType.GetProperty (Name, BindingFlags.GetProperty |  BindingFlags.NonPublic |
67                                                                         BindingFlags.Public | BindingFlags.Instance);
68                                 if (_member == null)
69                                         throw new ArgumentException ("Accessor methods for the " + Name + " property are missing");
70                         }
71                         return _member;
72                 }
73
74                 public override Type ComponentType
75                 {
76                         get { return _componentType; }
77                 }
78
79                 public override bool IsReadOnly
80                 {
81                         get
82                         {
83                                 bool attr_ro = false;
84
85                                 ReadOnlyAttribute attrib = ((ReadOnlyAttribute) Attributes[typeof (ReadOnlyAttribute)]);
86                                 if (attrib != null)
87                                         attr_ro = attrib.IsReadOnly;
88
89                                 return !GetPropertyInfo ().CanWrite || attrib.IsReadOnly;
90                         }
91                 }
92
93                 public override Type PropertyType
94                 {
95                         get
96                         {
97                                 return GetPropertyInfo ().PropertyType;
98                         }
99                 }
100
101                 public override object GetValue (object component)
102                 {
103                         component = MemberDescriptor.GetInvokee (_componentType, component);                    
104                         return GetPropertyInfo ().GetValue (component, null);
105                 }
106
107                 DesignerTransaction CreateTransaction (object obj)
108                 {
109                         IComponent com = obj as IComponent;
110                         if (com == null || com.Site == null)
111                                 return null;
112
113                         IDesignerHost dh = (IDesignerHost) com.Site.GetService (typeof(IDesignerHost));
114                         if (dh == null)
115                                 return null;
116
117                         DesignerTransaction tran = dh.CreateTransaction ();
118                         IComponentChangeService ccs = (IComponentChangeService) com.Site.GetService (typeof(IComponentChangeService));
119                         if (ccs != null)
120                                 ccs.OnComponentChanging (com, this);
121                         return tran;
122                 }
123
124                 void EndTransaction (object obj, DesignerTransaction tran, object oldValue, object newValue, bool commit)
125                 {
126                         if (tran == null) {
127                                 // FIXME: EventArgs might be differen type.
128                                 OnValueChanged (obj, new PropertyChangedEventArgs (Name));
129                                 return;
130                         }
131
132                         if (commit) {
133                                 IComponent com = obj as IComponent;
134                                 IComponentChangeService ccs = (IComponentChangeService) com.Site.GetService (typeof(IComponentChangeService));
135                                 if (ccs != null)
136                                         ccs.OnComponentChanged (com, this, oldValue, newValue);
137                                 tran.Commit ();
138                                 // FIXME: EventArgs might be differen type.
139                                 OnValueChanged (obj, new PropertyChangedEventArgs (Name));
140                         } else
141                                 tran.Cancel ();
142                 }
143
144                 public override void SetValue (object component, object value)
145                 {
146                         DesignerTransaction tran = CreateTransaction (component);
147                         
148                         object propertyHolder = MemberDescriptor.GetInvokee (_componentType, component);
149                         object old = GetValue (propertyHolder);
150
151                         try {
152                                 GetPropertyInfo ().SetValue (propertyHolder, value, null);
153                                 EndTransaction (component, tran, old, value, true);
154                         } catch {
155                                 EndTransaction (component, tran, old, value, false);
156                                 throw;
157                         }
158                 }
159
160                 MethodInfo FindPropertyMethod (object o, string method_name)
161                 {
162                         MethodInfo mi = null;
163                         string name = method_name + Name;
164
165                         foreach (MethodInfo m in o.GetType().GetMethods (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) {
166                                 // XXX should we really not check the return type of the method?
167                                 if (m.Name == name && m.GetParameters().Length == 0) {
168                                         mi = m;
169                                         break;
170                                 }
171                         }
172
173                         return mi;
174                 }
175
176                 public override void ResetValue (object component)
177                 {
178                         object propertyHolder = MemberDescriptor.GetInvokee (_componentType, component);
179                         
180                         DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
181                         if (attrib != null)
182                                 SetValue (propertyHolder, attrib.Value);
183
184                         DesignerTransaction tran = CreateTransaction (component);
185                         object old = GetValue (propertyHolder);
186
187                         try {
188                                 MethodInfo mi = FindPropertyMethod (propertyHolder, "Reset");
189                                 if (mi != null)
190                                         mi.Invoke (propertyHolder, null);
191                                 EndTransaction (component, tran, old, GetValue (propertyHolder), true);
192                         } catch {
193                                 EndTransaction (component, tran, old, GetValue (propertyHolder), false);
194                                 throw;
195                         }
196                 }
197
198                 public override bool CanResetValue (object component)
199                 {
200                         component = MemberDescriptor.GetInvokee (_componentType, component);
201                         
202                         DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
203                         if (attrib != null) {
204                                 object current = GetValue (component);
205                                 if (attrib.Value == null || current == null){
206                                         if (attrib.Value != current)
207                                                 return true;
208                                         if (attrib.Value == null && current == null)
209                                                 return false;
210                                 }
211
212                                 return !attrib.Value.Equals (current);
213                         } else {
214 #if NET_2_0
215                                 if (!_member.CanWrite)
216                                         return false;
217 #endif
218                                 MethodInfo mi = FindPropertyMethod (component, "ShouldPersist");
219                                 if (mi != null)
220                                         return (bool) mi.Invoke (component, null);
221
222                                 mi = FindPropertyMethod (component, "ShouldSerialize");
223                                 if (mi != null && !((bool) mi.Invoke (component, null)))
224                                         return false;
225
226                                 mi = FindPropertyMethod (component, "Reset");
227                                 return mi != null;
228                         }
229                 }
230
231                 public override bool ShouldSerializeValue (object component)
232                 {
233                         component = MemberDescriptor.GetInvokee (_componentType, component);
234                         
235                         DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
236                         if (attrib != null) {
237                                 object current = GetValue (component);
238                                 if (attrib.Value == null || current == null)
239                                         return attrib.Value != current;
240                                 return !attrib.Value.Equals (current);
241                         }
242                         else {
243                                 MethodInfo mi = FindPropertyMethod (component, "ShouldSerialize");
244                                 if (mi != null)
245                                         return (bool) mi.Invoke (component, null);
246                                 // MSDN: If this method cannot find a DefaultValueAttribute or a ShouldSerializeMyProperty method, 
247                                 // it cannot create optimizations and it returns true. 
248                                 return true;
249                         }
250                 }
251         }
252 }
253