2006-04-25 Miguel de Icaza <miguel@novell.com>
[mono.git] / mcs / class / System / System.ComponentModel / TypeConverter.cs
1 //
2 // System.ComponentModel.TypeConverter.cs
3 //
4 // Authors:
5 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) 2002/2003 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.ComponentModel.Design.Serialization;
36 using System.Globalization;
37 using System.Runtime.InteropServices;
38
39 namespace System.ComponentModel
40 {
41         [ComVisible (true)]
42         public class TypeConverter
43         {
44                 public bool CanConvertFrom (Type sourceType)
45                 {
46                         return CanConvertFrom (null, sourceType);
47                 }
48
49                 public virtual bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
50                 {
51                         if (sourceType == typeof (InstanceDescriptor)) {
52                                 return true;
53                         }
54
55                         return false;
56                 }
57
58                 public bool CanConvertTo (Type destinationType)
59                 {
60                         return CanConvertTo (null, destinationType);
61                 }
62
63                 public virtual bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
64                 {
65                         return (destinationType == typeof (string));
66                 }
67
68                 public object ConvertFrom (object o)
69                 {
70                         return ConvertFrom (null, CultureInfo.CurrentCulture, o);
71                 }
72
73                 public virtual object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
74                 {
75                         if (value is InstanceDescriptor) {
76                                 return ((InstanceDescriptor) value).Invoke ();
77                         }
78
79                         throw new NotSupportedException (this.ToString() + " cannot be created from '" +
80                                                          value == null ? "null" : value.GetType().ToString() + "'");
81                 }
82
83                 public object ConvertFromInvariantString (string text)
84                 {
85                         return ConvertFromInvariantString (null, text); 
86                 }
87
88                 public object ConvertFromInvariantString (ITypeDescriptorContext context, string text)
89                 {
90                         return ConvertFromString (context, CultureInfo.InvariantCulture, text);
91                 }
92
93                 public object ConvertFromString (string text)
94                 {
95                         return ConvertFrom (text);
96                 }
97
98                 public object ConvertFromString (ITypeDescriptorContext context, string text)
99                 {
100                         return ConvertFromString (context, CultureInfo.CurrentCulture, text);
101                 }
102
103                 public object ConvertFromString (ITypeDescriptorContext context, CultureInfo culture, string text)
104                 {
105                         return ConvertFrom (context, culture, text);
106                 }
107
108                 public object ConvertTo (object value, Type destinationType)
109                 {
110                         return ConvertTo (null, null, value, destinationType);
111                 }
112
113                 public virtual object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value,
114                                                  Type destinationType)
115                 {
116                         // context? culture?
117                         if (destinationType == null)
118                                 throw new ArgumentNullException ("destinationType");
119
120                         if (destinationType == typeof (string)) {
121                                 if (value != null)
122                                         return value.ToString();
123                                 return String.Empty;
124                         }
125
126                         throw new NotSupportedException ("Conversion not supported");
127                 }
128
129                 public string ConvertToInvariantString (object value)
130                 {
131                         return ConvertToInvariantString (null, value);
132                 }
133
134                 public string ConvertToInvariantString (ITypeDescriptorContext context, object value)
135                 {
136                         return (string) ConvertTo (context, CultureInfo.InvariantCulture, value, typeof (string));
137                 }
138
139                 public string ConvertToString (object value)
140                 {
141                         return (string) ConvertTo (null, CultureInfo.CurrentCulture, value, typeof (string));
142                 }
143
144                 public string ConvertToString (ITypeDescriptorContext context, object value)
145                 {
146                         return (string) ConvertTo (context, CultureInfo.CurrentCulture, value, typeof (string));
147                 }
148
149                 public string ConvertToString (ITypeDescriptorContext context, CultureInfo culture, object value)
150                 {
151                         return (string) ConvertTo (context, culture, value, typeof (string));
152                 }
153
154                 protected Exception GetConvertFromException (object value)
155                 {
156                         throw new NotSupportedException (this.ToString() + " cannot convert from '" + 
157                                                         value.GetType().ToString() + "'");
158                 }
159
160                 protected Exception GetConvertToException (object value, Type destinationType)
161                 {
162                         throw new NotSupportedException (this.ToString() + " cannot convert from '" + 
163                                                         value.GetType().ToString() + "' to '" + destinationType.ToString() + "'");
164                 }
165
166                 public object CreateInstance (IDictionary propertyValues)
167                 {
168                         return CreateInstance (null, propertyValues);
169                 }
170
171                 public virtual object CreateInstance (ITypeDescriptorContext context, IDictionary propertyValues)
172                 {
173                         return null;
174                 }
175
176                 public bool GetCreateInstanceSupported ()
177                 {
178                         return GetCreateInstanceSupported (null);
179                 }
180
181                 public virtual bool GetCreateInstanceSupported (ITypeDescriptorContext context)
182                 {
183                         return false;
184                 }
185
186                 public PropertyDescriptorCollection GetProperties (object value)
187                 {
188                         return GetProperties (null, value);
189                 }
190
191                 public PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value)
192                 {
193                         return GetProperties (context, value, new Attribute[1] { BrowsableAttribute.Yes });
194                 }
195
196                 public virtual PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context,
197                                                                            object value, Attribute[] attributes)
198                 {
199                         return null;
200                 }
201
202                 public bool GetPropertiesSupported ()
203                 {
204                         return GetPropertiesSupported (null);
205                 }
206
207                 public virtual bool GetPropertiesSupported (ITypeDescriptorContext context)
208                 {
209                         return false;
210                 }
211
212                 public ICollection GetStandardValues ()
213                 {
214                         return GetStandardValues (null);
215                 }
216
217                 public virtual StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
218                 {
219                         return null;
220                 }
221
222                 public bool GetStandardValuesExclusive ()
223                 {
224                         return GetStandardValuesExclusive (null);
225                 }
226
227                 public virtual bool GetStandardValuesExclusive (ITypeDescriptorContext context)
228                 {
229                         return false;
230                 }
231
232                 public bool GetStandardValuesSupported ()
233                 {
234                         return GetStandardValuesSupported (null);
235                 }
236
237                 public virtual bool GetStandardValuesSupported (ITypeDescriptorContext context)
238                 {
239                         return false;
240                 }
241
242                 public bool IsValid (object value)
243                 {
244                         return IsValid (null, value);
245                 }
246
247                 public virtual bool IsValid (ITypeDescriptorContext context, object value)
248                 {
249                         return true;
250                 }
251
252                 protected PropertyDescriptorCollection SortProperties (PropertyDescriptorCollection props, string[] names)
253                 {
254                         props.Sort (names);
255                         return props; 
256                 }
257
258                 public class StandardValuesCollection : ICollection, IEnumerable
259                 {
260                         private ICollection values;
261
262                         public StandardValuesCollection (ICollection values)
263                         {
264                                 this.values = values;
265                         }
266
267                         public void CopyTo (Array array, int index)
268                         {
269                                 values.CopyTo (array, index);
270                         }
271
272                         public IEnumerator GetEnumerator ()
273                         {
274                                 return values.GetEnumerator ();
275                         }
276
277                         bool ICollection.IsSynchronized {
278                                 get { return false; }
279                         }
280
281                         object ICollection.SyncRoot {
282                                 get { return null; }
283                         }
284
285                         int ICollection.Count {
286                                 get { return this.Count; }
287                         }
288
289                         public int Count {
290                                 get { return values.Count; }
291                         }
292
293                         public object this [int index] {
294                                 get { return ((IList) values) [index]; }
295                         }
296                 }
297
298                 protected abstract class SimplePropertyDescriptor : PropertyDescriptor
299                 {
300                         private Type componentType;
301                         private Type propertyType;
302
303                         public SimplePropertyDescriptor (Type componentType,
304                                                          string name,
305                                                          Type propertyType) :
306                                 this (componentType, name, propertyType, null)
307                         {
308                         }
309
310                         public SimplePropertyDescriptor (Type componentType,
311                                                          string name,
312                                                          Type propertyType,
313                                                          Attribute [] attributes) : base (name, attributes)
314                         {
315                                 this.componentType = componentType;
316                                 this.propertyType = propertyType;
317                         }
318
319                         public override Type ComponentType {
320                                 get { return componentType; }
321                         }
322
323                         public override Type PropertyType {
324                                 get { return propertyType; }
325                         }
326
327                         public override bool IsReadOnly {
328                                 get { return Attributes.Contains (ReadOnlyAttribute.Yes); }
329                         }
330
331                         public override bool ShouldSerializeValue (object component)
332                         {
333                                         return false; 
334                         }
335
336                         public override bool CanResetValue (object component)
337                         {
338                                 DefaultValueAttribute Attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
339                                 if (Attrib == null) {
340                                         return false; 
341                                 }
342                                 return (Attrib.Value == GetValue (component)); 
343                         }
344
345                         public override void ResetValue (object component)
346                         {
347                                 DefaultValueAttribute Attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
348                                 if (Attrib != null) {
349                                         SetValue (component, Attrib.Value); 
350                                 }
351  
352                         } 
353                 }
354         }
355 }
356