2006-11-27 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System / System.ComponentModel / PropertyDescriptor.cs
1 //
2 // System.ComponentModel.PropertyDescriptor.cs
3 //
4 // Author:
5 //  Miguel de Icaza (miguel@ximian.com)
6 //  Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 // (C) 2003 Andreas Nahr
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Collections;
35 using System.Reflection;
36 using System.Runtime.InteropServices;
37
38 namespace System.ComponentModel
39 {
40         [ComVisible (true)]
41         public abstract class PropertyDescriptor : MemberDescriptor
42         {
43                 TypeConverter converter;
44
45                 protected PropertyDescriptor (MemberDescriptor reference)
46                 : base (reference)
47                 {
48                 }
49
50                 protected PropertyDescriptor (MemberDescriptor reference, Attribute [] attrs)
51                 : base (reference, attrs)
52                 {
53                 }
54
55                 protected PropertyDescriptor (string name, Attribute [] attrs)
56                 : base (name, attrs)
57                 {
58                 }
59
60                 public abstract Type ComponentType { get; }
61
62                 public virtual TypeConverter Converter {
63                         get {
64                                 if (converter == null) {
65                                         TypeConverterAttribute at = (TypeConverterAttribute) Attributes [typeof(TypeConverterAttribute)];
66                                         if (at == null || at == TypeConverterAttribute.Default)
67                                                 converter = TypeDescriptor.GetConverter (PropertyType);
68                                         else {
69                                                 Type t = Type.GetType (at.ConverterTypeName);
70                                                 ConstructorInfo ci = t.GetConstructor (new Type[] { typeof(Type) });
71                                                 if (ci != null)
72                                                         converter = (TypeConverter) ci.Invoke (new object[] { PropertyType });
73                                                 else
74                                                         converter = (TypeConverter) Activator.CreateInstance (t);
75                                         }
76                                 }
77                                 return converter;
78                         }
79                 }
80
81                 public virtual bool IsLocalizable {
82                         get {
83                                 foreach (Attribute attr in AttributeArray){
84                                         if (attr is LocalizableAttribute)
85                                                 return ((LocalizableAttribute) attr).IsLocalizable;
86                                 }
87
88                                 return false;
89                         }
90                 }
91
92                 public abstract bool IsReadOnly { get; }
93
94                 public abstract Type PropertyType { get; }
95
96                 public DesignerSerializationVisibility SerializationVisibility {
97                         get {
98                                 foreach (Attribute attr in AttributeArray) {
99                                         if (attr is DesignerSerializationVisibilityAttribute){
100                                                 DesignerSerializationVisibilityAttribute a;
101
102                                                 a = (DesignerSerializationVisibilityAttribute) attr;
103
104                                                 return a.Visibility;
105                                         }
106                                 }
107
108                                 return DesignerSerializationVisibility.Visible;
109                         }
110                 }
111
112                 Hashtable notifiers;
113
114                 public virtual void AddValueChanged (object component, EventHandler handler)
115                 {
116                         EventHandler component_notifiers;
117
118                         if (component == null)
119                                 throw new ArgumentNullException ("component");
120
121                         if (handler == null)
122                                 throw new ArgumentNullException ("handler");
123
124                         if (notifiers == null)
125                                 notifiers = new Hashtable ();
126
127                         component_notifiers = (EventHandler) notifiers [component];
128
129                         if (component_notifiers != null)
130                                 component_notifiers += handler;
131                         else
132                                 notifiers [component] = handler;
133                 }
134
135                 public virtual void RemoveValueChanged (object component, System.EventHandler handler)
136                 {
137                         EventHandler component_notifiers;
138
139                         if (component == null)
140                                 throw new ArgumentNullException ("component");
141
142                         if (handler == null)
143                                 throw new ArgumentNullException ("handler");
144
145                         if (notifiers == null) return;
146
147                         component_notifiers = (EventHandler) notifiers [component];
148                         component_notifiers -= handler;
149                         
150                         if (component_notifiers == null)
151                                 notifiers.Remove (component);
152                         else
153                                 notifiers [component] = handler;
154                 }
155
156                 protected virtual void OnValueChanged (object component, EventArgs e)
157                 {
158                         if (notifiers == null)
159                                 return;
160
161                         EventHandler component_notifiers = (EventHandler) notifiers [component];
162
163                         if (component_notifiers == null)
164                                 return;
165
166                         component_notifiers (component, e);
167                 }
168
169                 public abstract object GetValue (object component);
170
171                 public abstract void SetValue (object component, object value);
172
173                 public abstract void ResetValue (object component);
174
175                 public abstract bool CanResetValue (object component);
176
177                 public abstract bool ShouldSerializeValue (object component);
178
179                 protected object CreateInstance(System.Type type)
180                 {
181                         return Assembly.GetExecutingAssembly ().CreateInstance (type.Name);
182                 }
183
184                 public override bool Equals(object obj)
185                 {
186                         if (!base.Equals (obj)) return false;
187                         PropertyDescriptor other = obj as PropertyDescriptor;
188                         if (other == null) return false;
189                         return other.PropertyType == PropertyType;
190                 }
191
192                 public PropertyDescriptorCollection GetChildProperties()
193                 {
194                         return GetChildProperties (null, null);
195                 }
196
197                 public PropertyDescriptorCollection GetChildProperties(object instance)
198                 {
199                         return GetChildProperties (instance, null);
200                 }
201
202                 public PropertyDescriptorCollection GetChildProperties(Attribute[] filter)
203                 {
204                         return GetChildProperties (null, filter);
205                 }
206
207                 public override int GetHashCode() 
208                 {
209                         return base.GetHashCode ();
210                 }
211
212                 public virtual PropertyDescriptorCollection GetChildProperties (object instance, Attribute[] filter)
213                 {
214                         return TypeDescriptor.GetProperties (instance, filter);
215                 }
216
217                 public virtual object GetEditor(Type editorBaseType)
218                 {
219                         return TypeDescriptor.GetEditor (PropertyType, editorBaseType);
220                 }
221
222                 protected Type GetTypeFromName(string typeName)
223                 {
224                         return Type.GetType (typeName);
225                 }
226         }
227 }
228