Merge pull request #409 from Alkarex/patch-1
[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                         return GetConvertFromException (value);
80                 }
81
82                 public object ConvertFromInvariantString (string text)
83                 {
84                         return ConvertFromInvariantString (null, text); 
85                 }
86
87                 public object ConvertFromInvariantString (ITypeDescriptorContext context, string text)
88                 {
89                         return ConvertFromString (context, CultureInfo.InvariantCulture, text);
90                 }
91
92                 public object ConvertFromString (string text)
93                 {
94                         return ConvertFrom (text);
95                 }
96
97                 public object ConvertFromString (ITypeDescriptorContext context, string text)
98                 {
99                         return ConvertFromString (context, CultureInfo.CurrentCulture, text);
100                 }
101
102                 public object ConvertFromString (ITypeDescriptorContext context, CultureInfo culture, string text)
103                 {
104                         return ConvertFrom (context, culture, text);
105                 }
106
107                 public object ConvertTo (object value, Type destinationType)
108                 {
109                         return ConvertTo (null, null, value, destinationType);
110                 }
111
112                 public virtual object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value,
113                                                  Type destinationType)
114                 {
115                         // context?
116                         if (destinationType == null)
117                                 throw new ArgumentNullException ("destinationType");
118
119                         if (destinationType == typeof (string)) {
120                                 if (value == null)
121                                         return String.Empty;
122
123                                 if (culture != null)
124                                         return Convert.ToString (value, culture);
125
126                                 return value.ToString();
127                         }
128
129                         return GetConvertToException (value, destinationType);
130                 }
131
132                 public string ConvertToInvariantString (object value)
133                 {
134                         return ConvertToInvariantString (null, value);
135                 }
136
137                 public string ConvertToInvariantString (ITypeDescriptorContext context, object value)
138                 {
139                         return (string) ConvertTo (context, CultureInfo.InvariantCulture, value, typeof (string));
140                 }
141
142                 public string ConvertToString (object value)
143                 {
144                         return (string) ConvertTo (null, CultureInfo.CurrentCulture, value, typeof (string));
145                 }
146
147                 public string ConvertToString (ITypeDescriptorContext context, object value)
148                 {
149                         return (string) ConvertTo (context, CultureInfo.CurrentCulture, value, typeof (string));
150                 }
151
152                 public string ConvertToString (ITypeDescriptorContext context, CultureInfo culture, object value)
153                 {
154                         return (string) ConvertTo (context, culture, value, typeof (string));
155                 }
156
157                 protected Exception GetConvertFromException (object value)
158                 {
159                         string destinationType;
160                         if (value == null)
161                                 destinationType = "(null)";
162                         else
163                                 destinationType = value.GetType ().FullName;
164
165                         throw new NotSupportedException (string.Format (CultureInfo.InvariantCulture,
166                                 "{0} cannot convert from {1}.", this.GetType ().Name,
167                                 destinationType));
168                 }
169
170                 protected Exception GetConvertToException (object value, Type destinationType)
171                 {
172                         string sourceType;
173                         if (value == null)
174                                 sourceType = "(null)";
175                         else
176                                 sourceType = value.GetType ().FullName;
177
178                         throw new NotSupportedException (string.Format (CultureInfo.InvariantCulture,
179                                 "'{0}' is unable to convert '{1}' to '{2}'.", this.GetType ().Name,
180                                 sourceType, destinationType.FullName));
181                 }
182
183                 public object CreateInstance (IDictionary propertyValues)
184                 {
185                         return CreateInstance (null, propertyValues);
186                 }
187
188                 public virtual object CreateInstance (ITypeDescriptorContext context, IDictionary propertyValues)
189                 {
190                         return null;
191                 }
192
193                 public bool GetCreateInstanceSupported ()
194                 {
195                         return GetCreateInstanceSupported (null);
196                 }
197
198                 public virtual bool GetCreateInstanceSupported (ITypeDescriptorContext context)
199                 {
200                         return false;
201                 }
202
203                 public PropertyDescriptorCollection GetProperties (object value)
204                 {
205                         return GetProperties (null, value);
206                 }
207
208                 public PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value)
209                 {
210                         return GetProperties (context, value, new Attribute[1] { BrowsableAttribute.Yes });
211                 }
212
213                 public virtual PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context,
214                                                                            object value, Attribute[] attributes)
215                 {
216                         return null;
217                 }
218
219                 public bool GetPropertiesSupported ()
220                 {
221                         return GetPropertiesSupported (null);
222                 }
223
224                 public virtual bool GetPropertiesSupported (ITypeDescriptorContext context)
225                 {
226                         return false;
227                 }
228
229                 public ICollection GetStandardValues ()
230                 {
231                         return GetStandardValues (null);
232                 }
233
234                 public virtual StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
235                 {
236                         return null;
237                 }
238
239                 public bool GetStandardValuesExclusive ()
240                 {
241                         return GetStandardValuesExclusive (null);
242                 }
243
244                 public virtual bool GetStandardValuesExclusive (ITypeDescriptorContext context)
245                 {
246                         return false;
247                 }
248
249                 public bool GetStandardValuesSupported ()
250                 {
251                         return GetStandardValuesSupported (null);
252                 }
253
254                 public virtual bool GetStandardValuesSupported (ITypeDescriptorContext context)
255                 {
256                         return false;
257                 }
258
259                 public bool IsValid (object value)
260                 {
261                         return IsValid (null, value);
262                 }
263
264                 public virtual bool IsValid (ITypeDescriptorContext context, object value)
265                 {
266                         return true;
267                 }
268
269                 protected PropertyDescriptorCollection SortProperties (PropertyDescriptorCollection props, string[] names)
270                 {
271                         props.Sort (names);
272                         return props; 
273                 }
274
275                 public class StandardValuesCollection : ICollection, IEnumerable
276                 {
277                         private ICollection values;
278
279                         public StandardValuesCollection (ICollection values)
280                         {
281                                 this.values = values;
282                         }
283
284                         void ICollection.CopyTo (Array array, int index) {
285                                 CopyTo (array, index);
286                         }
287
288                         public void CopyTo (Array array, int index)
289                         {
290                                 values.CopyTo (array, index);
291                         }
292
293                         IEnumerator IEnumerable.GetEnumerator () {
294                                 return GetEnumerator ();
295                         }
296
297                         public IEnumerator GetEnumerator ()
298                         {
299                                 return values.GetEnumerator ();
300                         }
301
302                         bool ICollection.IsSynchronized {
303                                 get { return false; }
304                         }
305
306                         object ICollection.SyncRoot {
307                                 get { return null; }
308                         }
309
310                         int ICollection.Count {
311                                 get { return this.Count; }
312                         }
313
314                         public int Count {
315                                 get { return values.Count; }
316                         }
317
318                         public object this [int index] {
319                                 get { return ((IList) values) [index]; }
320                         }
321                 }
322
323                 protected abstract class SimplePropertyDescriptor : PropertyDescriptor
324                 {
325                         private Type componentType;
326                         private Type propertyType;
327
328 #if NET_4_0
329                         protected
330 #else
331                         public
332 #endif
333                         SimplePropertyDescriptor (Type componentType,
334                                                          string name,
335                                                          Type propertyType) :
336                                 this (componentType, name, propertyType, null)
337                         {
338                         }
339
340 #if NET_4_0
341                         protected
342 #else
343                         public
344 #endif
345                         SimplePropertyDescriptor (Type componentType,
346                                                          string name,
347                                                          Type propertyType,
348                                                          Attribute [] attributes) : base (name, attributes)
349                         {
350                                 this.componentType = componentType;
351                                 this.propertyType = propertyType;
352                         }
353
354                         public override Type ComponentType {
355                                 get { return componentType; }
356                         }
357
358                         public override Type PropertyType {
359                                 get { return propertyType; }
360                         }
361
362                         public override bool IsReadOnly {
363                                 get { return Attributes.Contains (ReadOnlyAttribute.Yes); }
364                         }
365
366                         public override bool ShouldSerializeValue (object component)
367                         {
368                                         return false; 
369                         }
370
371                         public override bool CanResetValue (object component)
372                         {
373                                 DefaultValueAttribute Attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
374                                 if (Attrib == null) {
375                                         return false; 
376                                 }
377                                 return (Attrib.Value == GetValue (component)); 
378                         }
379
380                         public override void ResetValue (object component)
381                         {
382                                 DefaultValueAttribute Attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
383                                 if (Attrib != null) {
384                                         SetValue (component, Attrib.Value); 
385                                 }
386  
387                         } 
388                 }
389         }
390 }
391