2002-08-23 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System / System.ComponentModel / TypeConverter.cs
1 //
2 // System.ComponentModel.TypeConverter
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
8 //
9
10 using System;
11 using System.Collections;
12 using System.Globalization;
13
14 namespace System.ComponentModel {
15
16 public class TypeConverter
17 {
18         public TypeConverter ()
19         {
20         }
21
22         public bool CanConvertFrom (Type sourceType)
23         {
24                 return CanConvertFrom (null, sourceType);
25         }
26         
27         [MonoTODO]
28         public virtual bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
29         {
30                 throw new NotImplementedException ();
31         }
32
33         public bool CanConvertTo (Type destinationType)
34         {
35                 return CanConvertTo (null, destinationType);
36         }
37         
38         public virtual bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
39         {
40                 return (destinationType == typeof (string));
41         }
42
43         public object ConvertFrom (object o)
44         {
45                 return ConvertFrom (null, CultureInfo.CurrentCulture, o);
46         }
47
48         [MonoTODO]
49         public virtual object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
50         {
51                 throw new NotImplementedException ();
52         }
53         
54         public object ConvertFromInvariantString (string text)
55         {
56                 return ConvertFromInvariantString (null, text); 
57         }
58
59         [MonoTODO]
60         public object ConvertFromInvariantString (ITypeDescriptorContext context, string text)
61         {
62                 throw new NotImplementedException ();
63         }
64
65         public object ConvertFromString (string s)
66         {
67                 return ConvertFrom (s);
68         }
69
70         public object ConvertFromString (ITypeDescriptorContext context, string text)
71         {
72                 return ConvertFromString (context, CultureInfo.CurrentCulture, text);
73         }
74
75         [MonoTODO]
76         public object ConvertFromString (ITypeDescriptorContext context, CultureInfo culture, string text)
77         {
78                 throw new NotImplementedException ();
79         }
80
81         public object ConvertTo (object value, Type destinationType)
82         {
83                 return ConvertTo (null, null, value, destinationType);
84         }
85
86         public virtual object ConvertTo (ITypeDescriptorContext context,
87                                          CultureInfo culture,
88                                          object value,
89                                          Type destinationType)
90         {
91                 // context? culture?
92                 if (destinationType == null)
93                         throw new ArgumentNullException ("destinationType");
94
95                 if (destinationType == typeof (string)) {
96                         if (value != null)
97                                 return value.ToString();
98                         return String.Empty;
99                 }
100
101                 throw new NotSupportedException ("Conversion not supported");
102         }
103
104         public string ConvertToInvariantString (object value)
105         {
106                 return ConvertToInvariantString (null, value);
107         }
108
109         [MonoTODO]
110         public string ConvertToInvariantString (ITypeDescriptorContext context, object value)
111         {
112                 throw new NotImplementedException ();
113         }
114
115         public string ConvertToString (object value)
116         {
117                 return (string) ConvertTo (null, CultureInfo.CurrentCulture, value, typeof (string));
118         }
119
120         public string ConvertToString (ITypeDescriptorContext context, object value)
121         {
122                 return (string) ConvertTo (context, CultureInfo.CurrentCulture, value, typeof (string));
123         }
124
125         public string ConvertToString (ITypeDescriptorContext context, CultureInfo culture, object value)
126         {
127                 return (string) ConvertTo (context, culture, value, typeof (string));
128         }
129
130         [MonoTODO]
131         public object CreateInstance (IDictionary propertyValues)
132         {
133                 throw new NotImplementedException ();
134         }
135
136         [MonoTODO]
137         protected Exception GetConvertFromException (object value)
138         {
139                 throw new NotImplementedException ();
140         }
141
142         [MonoTODO]
143         protected Exception GetConvertToException (object value, Type destinationType)
144         {
145                 throw new NotImplementedException ();
146         }
147
148         [MonoTODO]
149         public virtual object CreateInstance (ITypeDescriptorContext context, IDictionary propertyValues)
150         {
151                 throw new NotImplementedException ();
152         }
153
154         [MonoTODO]
155         public bool GetCreateInstanceSupported ()
156         {
157                 throw new NotImplementedException ();
158         }
159
160         [MonoTODO]
161         public virtual bool GetCreateInstanceSupported (ITypeDescriptorContext context)
162         {
163                 throw new NotImplementedException ();
164         }
165
166         public PropertyDescriptorCollection GetProperties (object value)
167         {
168                 return GetProperties (null, value);
169         }
170
171         public PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value)
172         {
173                 return GetProperties (context, value, null);
174         }
175
176         public virtual PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context,
177                                                                    object value,
178                                                                    Attribute[] attributes)
179         {
180                 return null;
181         }
182
183         public bool GetPropertiesSupported ()
184         {
185                 return GetPropertiesSupported (null);
186         }
187
188         public virtual bool GetPropertiesSupported (ITypeDescriptorContext context)
189         {
190                 return false;
191         }
192
193         public ICollection GetStandardValues ()
194         {
195                 return GetStandardValues (null);
196         }
197
198         public virtual StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
199         {
200                 return null;
201         }
202
203         public bool GetStandardValuesExclusive ()
204         {
205                 return GetStandardValuesExclusive (null);
206         }
207
208         public virtual bool GetStandardValuesExclusive (ITypeDescriptorContext context)
209         {
210                 return false;
211         }
212
213         public bool GetStandardValuesSupported ()
214         {
215                 return GetStandardValuesSupported (null);
216         }
217
218         public virtual bool GetStandardValuesSupported (ITypeDescriptorContext context)
219         {
220                 return false;
221         }
222
223         public bool IsValid (object value)
224         {
225                 return IsValid (null, value);
226         }
227
228         public virtual bool IsValid (ITypeDescriptorContext context, object value)
229         {
230                 return true;
231         }
232
233         public class StandardValuesCollection : ICollection, IEnumerable
234         {
235                 private ICollection values;
236                 
237                 public StandardValuesCollection (ICollection values)
238                 {
239                         this.values = values;
240                 }
241
242                 public void CopyTo (Array array, int index)
243                 {
244                         values.CopyTo (array, index);
245                 }
246
247                 public IEnumerator GetEnumerator ()
248                 {
249                         return values.GetEnumerator ();
250                 }
251
252                 bool ICollection.IsSynchronized
253                 {
254                         get { return false; }
255                 }
256
257                 object ICollection.SyncRoot
258                 {
259                         get { return null; }
260                 }
261
262                 int ICollection.Count
263                 {
264                         get { return this.Count; }
265                 }
266
267                 public int Count
268                 {
269                         get { return values.Count; }
270                 }
271
272                 public object this [int index]
273                 {
274                         get { return ((IList) values) [index]; }
275                 }
276         }
277
278         protected abstract class SimplePropertyDescriptor : PropertyDescriptor
279         {
280                 private Type componentType;
281                 private Type propertyType;
282                 
283                 public SimplePropertyDescriptor (Type componentType,
284                                                  string name,
285                                                  Type propertyType) :
286                         this (componentType, name, propertyType, new Attribute [0])
287                 {
288                 }
289
290                 public SimplePropertyDescriptor (Type componentType,
291                                                  string name,
292                                                  Type propertyType,
293                                                  Attribute [] attributes) : base (name, attributes)
294                 {
295                         this.componentType = componentType;
296                         this.propertyType = propertyType;
297                 }
298
299                 public override Type ComponentType
300                 {
301                         get { return componentType; }
302                 }
303
304                 public override Type PropertyType
305                 {
306                         get { return propertyType; }
307                 }
308
309                 public override bool IsReadOnly
310                 {
311                         get {
312                                 return Attributes.Contains (ReadOnlyAttribute.Yes);
313                         }
314                 }
315         }
316 }
317 }
318