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