New test.
[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 //
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);
67                                 if (_member == null)
68                                         throw new ArgumentException ("Accessor methods for the " + Name + " property are missing");
69                         }
70                         return _member;
71                 }               
72
73                 public override Type ComponentType 
74                 { 
75                         get { return _componentType; }
76                 }
77
78                 public override bool IsReadOnly 
79                 {
80                         get
81                         {
82                                 return !GetPropertyInfo ().CanWrite;
83                         }
84                 }
85
86                 public override Type PropertyType 
87                 {
88                         get
89                         {
90                                 return GetPropertyInfo ().PropertyType;
91                         }
92                 }
93                 
94                 public override object GetValue (object component)
95                 {
96                         return GetPropertyInfo ().GetValue (component, null);
97                 }
98                 
99                 DesignerTransaction CreateTransaction (object obj)
100                 {
101                         IComponent com = obj as IComponent;
102                         if (com == null || com.Site == null)
103                                 return null;
104                         
105                         IDesignerHost dh = (IDesignerHost) com.Site.GetService (typeof(IDesignerHost));
106                         if (dh == null)
107                                 return null;
108                         
109                         DesignerTransaction tran = dh.CreateTransaction ();
110                         IComponentChangeService ccs = (IComponentChangeService) com.Site.GetService (typeof(IComponentChangeService));
111                         if (ccs != null)
112                                 ccs.OnComponentChanging (com, this);
113                         return tran;
114                 }
115                 
116                 void EndTransaction (object obj, DesignerTransaction tran, object oldValue, object newValue, bool commit)
117                 {
118                         if (tran == null) {
119                                 // FIXME: EventArgs might be differen type.
120                                 OnValueChanged (obj, new PropertyChangedEventArgs (Name));
121                                 return;
122                         }
123                         
124                         if (commit) {
125                                 IComponent com = obj as IComponent;
126                                 IComponentChangeService ccs = (IComponentChangeService) com.Site.GetService (typeof(IComponentChangeService));
127                                 if (ccs != null)
128                                         ccs.OnComponentChanged (com, this, oldValue, newValue);
129                                 tran.Commit ();
130                                 // FIXME: EventArgs might be differen type.
131                                 OnValueChanged (obj, new PropertyChangedEventArgs (Name));
132                         } else
133                                 tran.Cancel ();
134                 }
135                 
136                 public override void SetValue (object component, object value)
137                 {
138                         DesignerTransaction tran = CreateTransaction (component);
139                         object old = GetValue (component);
140                         
141                         try {
142                                 GetPropertyInfo ().SetValue (component, value, null);
143                                 EndTransaction (component, tran, old, value, true);
144                         } catch {
145                                 EndTransaction (component, tran, old, value, false);
146                                 throw;
147                         }
148                 }
149
150                 public override void ResetValue (object component)
151                 {
152                         DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
153                         if (attrib != null) 
154                                 SetValue (component, attrib.Value); 
155                         
156                         DesignerTransaction tran = CreateTransaction (component);
157                         object old = GetValue (component);
158                         
159                         try {
160                                 MethodInfo mi = component.GetType().GetMethod ("Reset" + Name, Type.EmptyTypes);
161                                 if (mi != null)
162                                         mi.Invoke (component, null);
163                                 EndTransaction (component, tran, old, GetValue (component), true);
164                         } catch {
165                                 EndTransaction (component, tran, old, GetValue (component), false);
166                                 throw;
167                         }
168                 }
169
170                 public override bool CanResetValue (object component)
171                 {
172                         DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
173                         if (attrib != null) {
174                                 object current = GetValue (component);
175                                 if (attrib.Value == null || current == null){
176                                         if (attrib.Value != current)
177                                                 return true;
178                                         if (attrib.Value == null && current == null)
179                                                 return false;
180                                 }
181
182                                 return !attrib.Value.Equals (current);
183                         } else {
184                                 MethodInfo mi = component.GetType().GetMethod ("ShouldPersist" + Name, Type.EmptyTypes);
185                                 if (mi != null)
186                                         return (bool) mi.Invoke (component, null);
187                                 mi = component.GetType().GetMethod ("Reset" + Name, Type.EmptyTypes);
188                                 return mi != null;
189                         }
190                 }
191
192                 public override bool ShouldSerializeValue (object component)
193                 {
194                         DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
195                         if (attrib != null) {
196                                 object current = GetValue (component);
197                                 if ((attrib.Value == null || current == null) && attrib.Value != current)
198                                         return true;
199                                 return !attrib.Value.Equals (current);
200                         }
201                         else {
202                                 MethodInfo mi = component.GetType().GetMethod ("ShouldSerialize" + Name, Type.EmptyTypes);
203                                 if (mi != null)
204                                         return (bool) mi.Invoke (component, null);
205                                 return true;
206                         }
207                 }
208         }
209 }
210