2006-11-28 Miguel de Icaza <miguel@novell.com>
[mono.git] / mcs / class / System / System.ComponentModel / EnumConverter.cs
1 //
2 // System.ComponentModel.EnumConverter.cs
3 //
4 // Authors:
5 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) 2002 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 using System;
33 using System.Collections;
34 using System.Globalization;
35 using System.Reflection;
36 using System.ComponentModel.Design.Serialization;
37
38 namespace System.ComponentModel
39 {
40         public class EnumConverter : TypeConverter
41         {
42                 private Type type;
43                 private StandardValuesCollection stdValues;
44
45                 public EnumConverter (Type type)
46                 {
47                         this.type = type;
48                 }
49
50                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
51                 {
52                         if (destinationType == typeof (InstanceDescriptor))
53                                 return true;
54
55                         return base.CanConvertTo (context, destinationType);
56                 }
57
58                 public override object ConvertTo (ITypeDescriptorContext context,
59                                                   CultureInfo culture,
60                                                   object value,
61                                                   Type destinationType)
62                 {
63                         if (destinationType == typeof (string))
64                                 if (value != null)
65                                         return Enum.Format (type, value, "G");
66                                         
67                         if (destinationType == typeof (InstanceDescriptor) && type.IsInstanceOfType (value)) {
68                                 FieldInfo f = type.GetField (value.ToString ());
69                                 if (f == null) throw new ArgumentException (string.Format ("The value '{0}' is not a valid value for the enum '{1}'", value, type));
70                                 return new InstanceDescriptor (f, null);
71                         }
72                         
73                         return base.ConvertTo (context, culture, value, destinationType);
74                 }
75
76                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
77                 {
78                         if (sourceType == typeof (string))
79                                 return true;
80                         return base.CanConvertFrom (context, sourceType);
81                 }
82
83                 public override object ConvertFrom (ITypeDescriptorContext context,
84                                                     CultureInfo culture,
85                                                     object value)
86                 {
87                         string val = value as string;
88                         if (val == null)
89                                 return base.ConvertFrom(context, culture, value);
90
91                         return Enum.Parse (type, val, true);
92                 }
93
94                 public override bool IsValid (ITypeDescriptorContext context, object value)
95                 {
96                         return Enum.IsDefined (type, value);
97                 }
98
99                 public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
100                 {
101                         return true;
102                 }
103
104                 public override bool GetStandardValuesExclusive (ITypeDescriptorContext context)
105                 {
106                         return !(type.IsDefined (typeof (FlagsAttribute), false));
107                 }
108
109                 public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
110                 {
111                         if (stdValues == null) {
112                                 Array values = Enum.GetValues (type);
113                                 Array.Sort (values);
114                                 stdValues = new StandardValuesCollection (values);
115                         }
116                         return stdValues;
117                 }
118
119                 protected virtual IComparer Comparer {
120                         get { return new EnumConverter.EnumComparer (); }
121                 }
122
123                 protected Type EnumType {
124                         get { return type; }
125                 }
126
127                 protected TypeConverter.StandardValuesCollection Values {
128                         get { return stdValues;  }
129                         set { stdValues = value; }
130                 }
131
132                 private class EnumComparer : IComparer
133                 {
134                         int IComparer.Compare (object compareObject1, object compareObject2) 
135                         {
136                                 string CompareString1 = (compareObject1 as string);
137                                 string CompareString2 = (compareObject2 as string);
138                                 if ((CompareString1 == null) || (CompareString2 == null))
139                                         return Collections.Comparer.Default.Compare (compareObject1, compareObject2);
140                                 return CultureInfo.InvariantCulture.CompareInfo.Compare (CompareString1, CompareString2);
141                         }
142                 }
143         }
144
145 }
146