2002-07-29 Gonzalo Paniagua Javier <gonzalo@ximian.com>
authorGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Mon, 29 Jul 2002 20:11:17 +0000 (20:11 -0000)
committerGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Mon, 29 Jul 2002 20:11:17 +0000 (20:11 -0000)
* EnumConverter.cs: new file.

* TypeConverter.cs: implemented a few simple methods.

* TypeDescriptor.cs:
(GetConverter): initial support for converters of well-known types.

svn path=/trunk/mcs/; revision=6254

mcs/class/System/System.ComponentModel/ChangeLog
mcs/class/System/System.ComponentModel/EnumConverter.cs [new file with mode: 0644]
mcs/class/System/System.ComponentModel/TypeConverter.cs
mcs/class/System/System.ComponentModel/TypeDescriptor.cs

index ad2ac706f6d498b6194bb03a5597cff4ae56e6d7..21acaf7a888fd1dbb28891702076f2048655f7dc 100644 (file)
@@ -1,3 +1,12 @@
+2002-07-29  Gonzalo Paniagua Javier <gonzalo@ximian.com>
+
+       * EnumConverter.cs: new file.
+       
+       * TypeConverter.cs: implemented a few simple methods.
+
+       * TypeDescriptor.cs:
+       (GetConverter): initial support for converters of well-known types.
+
 2002-07-28  Gonzalo Paniagua Javier <gonzalo@ximian.com>
 
        * DerivedPropertyDescriptor.cs: New file. Internal class.
diff --git a/mcs/class/System/System.ComponentModel/EnumConverter.cs b/mcs/class/System/System.ComponentModel/EnumConverter.cs
new file mode 100644 (file)
index 0000000..46f43f4
--- /dev/null
@@ -0,0 +1,92 @@
+//
+// System.ComponentModel.EnumConverter
+//
+// Authors:
+//     Gonzalo Paniagua Javier (gonzalo@ximian.com)
+//
+// (C) 2002 Ximian, Inc (http://www.ximian.com)
+//
+using System;
+using System.Globalization;
+
+namespace System.ComponentModel
+{
+       public class EnumConverter : TypeConverter
+       {
+               private Type type;
+               private StandardValuesCollection stdValues;
+
+               public EnumConverter (Type type)
+               {
+                       this.type = type;
+               }
+
+               [MonoTODO]
+               public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
+               {
+                       return base.CanConvertTo (context, destinationType);
+               }
+
+               [MonoTODO]
+               public override object ConvertTo (ITypeDescriptorContext context,
+                                                 CultureInfo culture,
+                                                 object value,
+                                                 Type destinationType)
+               {
+                       if (destinationType == typeof (string))
+                               return value.ToString ();
+                       return base.ConvertTo (context, culture, value, destinationType);
+               }
+
+               public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
+               {
+                       if (sourceType == typeof (string))
+                               return true;
+                       return base.CanConvertFrom (context, sourceType);
+               }
+
+               public override object ConvertFrom (ITypeDescriptorContext context,
+                                                   CultureInfo culture,
+                                                   object value)
+               {
+                       string val = value as string;
+                       if (val == null)
+                               return base.ConvertFrom(context, culture, value);
+
+                       string [] subValues = val.Split (new char [] {','});
+                                       
+                       long longResult = 0;
+                       foreach (string s in subValues)
+                               longResult |= (long) Enum.Parse (type, s, true);
+
+                       return Enum.ToObject (type, longResult);
+               }
+
+               public override bool IsValid (ITypeDescriptorContext context, object value)
+               {
+                       return Enum.IsDefined (type, value);
+               }
+
+               public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
+               {
+                       return true;
+               }
+
+               public override bool GetStandardValuesExclusive (ITypeDescriptorContext context)
+               {
+                       return !(type.IsDefined (typeof (FlagsAttribute), false));
+               }
+
+               public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
+               {
+                       if (stdValues == null) {
+                               Array values = Enum.GetValues (type);
+                               Array.Sort (values);
+                               stdValues = new StandardValuesCollection (values);
+                       }
+                       return stdValues;
+               }
+       }
+
+}
+
index 953a4ab5f1d6985826f18fdeb68d4e298ddca71e..491dc6bd656b72939f98151be77d4347f99dfd64 100755 (executable)
@@ -35,10 +35,9 @@ public class TypeConverter
                return CanConvertTo (null, destinationType);
        }
        
-       [MonoTODO]
        public virtual bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
        {
-               throw new NotImplementedException ();
+               return (destinationType == typeof (string));
        }
 
        public object ConvertFrom (object o)
@@ -79,24 +78,32 @@ public class TypeConverter
                throw new NotImplementedException ();
        }
 
-       [MonoTODO]
        public object ConvertTo (object value, Type destinationType)
        {
-               throw new NotImplementedException ();
+               return ConvertTo (null, null, value, destinationType);
        }
-       [MonoTODO]
+
        public virtual object ConvertTo (ITypeDescriptorContext context,
                                         CultureInfo culture,
                                         object value,
                                         Type destinationType)
        {
-               throw new NotImplementedException ();
+               // context? culture?
+               if (destinationType == null)
+                       throw new ArgumentNullException ("destinationType");
+
+               if (destinationType == typeof (string)) {
+                       if (value != null)
+                               return value.ToString();
+                       return String.Empty;
+               }
+
+               throw new NotSupportedException ("Conversion not supported");
        }
 
-       [MonoTODO]
        public string ConvertToInvariantString (object value)
        {
-               throw new NotImplementedException ();
+               return ConvertToInvariantString (null, value);
        }
 
        [MonoTODO]
@@ -105,22 +112,19 @@ public class TypeConverter
                throw new NotImplementedException ();
        }
 
-       [MonoTODO]
        public string ConvertToString (object value)
        {
-               throw new NotImplementedException ();
+               return (string) ConvertTo (null, CultureInfo.CurrentCulture, value, typeof (string));
        }
 
-       [MonoTODO]
        public string ConvertToString (ITypeDescriptorContext context, object value)
        {
-               throw new NotImplementedException ();
+               return (string) ConvertTo (context, CultureInfo.CurrentCulture, value, typeof (string));
        }
 
-       [MonoTODO]
        public string ConvertToString (ITypeDescriptorContext context, CultureInfo culture, object value)
        {
-               throw new NotImplementedException ();
+               return (string) ConvertTo (context, culture, value, typeof (string));
        }
 
        [MonoTODO]
@@ -159,84 +163,71 @@ public class TypeConverter
                throw new NotImplementedException ();
        }
 
-       [MonoTODO]
        public PropertyDescriptorCollection GetProperties (object value)
        {
-               throw new NotImplementedException ();
+               return GetProperties (null, value);
        }
 
-       [MonoTODO]
        public PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value)
        {
-               throw new NotImplementedException ();
+               return GetProperties (context, value, null);
        }
 
-       [MonoTODO]
        public virtual PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context,
                                                                   object value,
                                                                   Attribute[] attributes)
        {
-               throw new NotImplementedException ();
+               return null;
        }
 
-       [MonoTODO]
        public bool GetPropertiesSupported ()
        {
-               throw new NotImplementedException ();
+               return GetPropertiesSupported (null);
        }
 
-       [MonoTODO]
        public virtual bool GetPropertiesSupported (ITypeDescriptorContext context)
        {
-               throw new NotImplementedException ();
+               return false;
        }
 
-       [MonoTODO]
        public ICollection GetStandardValues ()
        {
-               throw new NotImplementedException ();
+               return GetStandardValues (null);
        }
 
-       [MonoTODO]
        public virtual StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
        {
-               throw new NotImplementedException ();
+               return null;
        }
 
-       [MonoTODO]
        public bool GetStandardValuesExclusive ()
        {
-               throw new NotImplementedException ();
+               return GetStandardValuesExclusive (null);
        }
 
-       [MonoTODO]
        public virtual bool GetStandardValuesExclusive (ITypeDescriptorContext context)
        {
-               throw new NotImplementedException ();
+               return false;
        }
 
-       [MonoTODO]
        public bool GetStandardValuesSupported ()
        {
-               throw new NotImplementedException ();
+               return GetStandardValuesSupported (null);
        }
 
-       [MonoTODO]
        public virtual bool GetStandardValuesSupported (ITypeDescriptorContext context)
        {
-               throw new NotImplementedException ();
+               return false;
        }
 
-       [MonoTODO]
        public bool IsValid (object value)
        {
-               throw new NotImplementedException ();
+               return IsValid (null, value);
        }
 
-       [MonoTODO]
        public virtual bool IsValid (ITypeDescriptorContext context, object value)
        {
-               throw new NotImplementedException ();
+               return true;
        }
 
        public class StandardValuesCollection : ICollection, IEnumerable
index 7841cf2f44e4f6e3955ed9528033682d68e6fa14..6336aa6bf622f69af95e4da359f02966ab788118 100644 (file)
@@ -16,6 +16,9 @@ namespace System.ComponentModel
 
 public sealed class TypeDescriptor
 {
+       private static readonly string creatingDefaultConverters = "creatingDefaultConverters";
+       private static Hashtable defaultConverters;
+
        [MonoTODO]
        public static void AddEditorTable (Type editorBaseType, Hashtable table)
        {
@@ -111,17 +114,45 @@ public sealed class TypeDescriptor
                throw new NotImplementedException ();
        }
 
+       private static Hashtable DefaultConverters
+       {
+               get {
+                       if (defaultConverters != null)
+                               return defaultConverters;
+
+                       lock (creatingDefaultConverters) {
+                               if (defaultConverters != null)
+                                       return defaultConverters;
+                               
+                               //FIXME: add more converters as we implement them
+                               defaultConverters = new Hashtable ();
+                               defaultConverters.Add (typeof (Enum), typeof (EnumConverter));
+                               defaultConverters.Add (typeof (string), typeof (StringConverter));
+                       }
+                       return defaultConverters;
+               }
+       }
+       
        public static TypeConverter GetConverter (Type type)
        {
-               object [] attrs = type.GetCustomAttributes (false);
+               Type t = DefaultConverters [type] as Type;
+               if (t == null && type.IsEnum)
+                       t = (Type) DefaultConverters [typeof (Enum)];
+
                string converter_name = null;
-               foreach (object o in attrs){
-                       if (o is TypeConverterAttribute){
-                               TypeConverterAttribute tc = (TypeConverterAttribute) o;
-                               converter_name = tc.ConverterTypeName;
-                               break;
+               if (t == null) {
+                       object [] attrs = type.GetCustomAttributes (false);
+                       foreach (object o in attrs){
+                               if (o is TypeConverterAttribute){
+                                       TypeConverterAttribute tc = (TypeConverterAttribute) o;
+                                       converter_name = tc.ConverterTypeName;
+                                       break;
+                               }
                        }
+               } else {
+                       converter_name = t.FullName;
                }
+               
 
                if (converter_name == null)
                        return null;