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