for TARGET_J2EE only:
[mono.git] / mcs / class / System / System.ComponentModel / TypeConverter.cs
old mode 100755 (executable)
new mode 100644 (file)
index fd91454..419764a
@@ -9,8 +9,30 @@
 // (C) 2003 Andreas Nahr
 //
 
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System;
 using System.Collections;
+using System.ComponentModel.Design.Serialization;
 using System.Globalization;
 using System.Runtime.InteropServices;
 
@@ -26,6 +48,10 @@ namespace System.ComponentModel
 
                public virtual bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
                {
+                       if (sourceType == typeof (InstanceDescriptor)) {
+                               return true;
+                       }
+
                        return false;
                }
 
@@ -46,8 +72,11 @@ namespace System.ComponentModel
 
                public virtual object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
                {
-                       throw new NotSupportedException (this.ToString() + " cannot be created from '" +
-                                                        value.GetType().ToString() + "'");
+                       if (value is InstanceDescriptor) {
+                               return ((InstanceDescriptor) value).Invoke ();
+                       }
+
+                       return GetConvertFromException (value);
                }
 
                public object ConvertFromInvariantString (string text)
@@ -93,7 +122,7 @@ namespace System.ComponentModel
                                return String.Empty;
                        }
 
-                       throw new NotSupportedException ("Conversion not supported");
+                       return GetConvertToException (value, destinationType);
                }
 
                public string ConvertToInvariantString (object value)
@@ -123,14 +152,28 @@ namespace System.ComponentModel
 
                protected Exception GetConvertFromException (object value)
                {
-                       throw new NotSupportedException (this.ToString() + " cannot convert from '" + 
-                                                       value.GetType().ToString() + "'");
+                       string destinationType;
+                       if (value == null)
+                               destinationType = "(null)";
+                       else
+                               destinationType = value.GetType ().FullName;
+
+                       throw new NotSupportedException (string.Format (CultureInfo.InvariantCulture,
+                               "{0} cannot convert from {1}.", this.GetType ().Name,
+                               destinationType));
                }
 
                protected Exception GetConvertToException (object value, Type destinationType)
                {
-                       throw new NotSupportedException (this.ToString() + " cannot convert from '" + 
-                                                       value.GetType().ToString() + "' to '" + destinationType.ToString() + "'");
+                       string sourceType;
+                       if (value == null)
+                               sourceType = "(null)";
+                       else
+                               sourceType = value.GetType ().FullName;
+
+                       throw new NotSupportedException (string.Format (CultureInfo.InvariantCulture,
+                               "'{0}' is unable to convert '{1}' to '{2}'.", this.GetType ().Name,
+                               sourceType, destinationType.FullName));
                }
 
                public object CreateInstance (IDictionary propertyValues)
@@ -160,7 +203,7 @@ namespace System.ComponentModel
 
                public PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value)
                {
-                       return GetProperties (context, value, null);
+                       return GetProperties (context, value, new Attribute[1] { BrowsableAttribute.Yes });
                }
 
                public virtual PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context,
@@ -234,11 +277,19 @@ namespace System.ComponentModel
                                this.values = values;
                        }
 
+                       void ICollection.CopyTo (Array array, int index) {
+                               CopyTo (array, index);
+                       }
+
                        public void CopyTo (Array array, int index)
                        {
                                values.CopyTo (array, index);
                        }
 
+                       IEnumerator IEnumerable.GetEnumerator () {
+                               return GetEnumerator ();
+                       }
+
                        public IEnumerator GetEnumerator ()
                        {
                                return values.GetEnumerator ();
@@ -273,7 +324,7 @@ namespace System.ComponentModel
                        public SimplePropertyDescriptor (Type componentType,
                                                         string name,
                                                         Type propertyType) :
-                               this (componentType, name, propertyType, new Attribute [0])
+                               this (componentType, name, propertyType, null)
                        {
                        }