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