Update EntityFramework. Fixes #10296
[mono.git] / mcs / class / System / System.ComponentModel / CultureInfoConverter.cs
1 //
2 // System.ComponentModel.CultureInfoConverter
3 //
4 // Authors:
5 //  Martin Willemoes Hansen (mwh@sysrq.dk)
6 //  Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //  Ivan N. Zlatev <contact@i-nz.net>
8 //
9 // (C) 2003 Martin Willemoes Hansen
10 // (C) 2003 Andreas Nahr
11 // (C) 2008 Novell, Inc. (http://www.novell.com)
12 //
13
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System.Collections;
36 using System.Globalization;
37 using System.Reflection;
38 using System.ComponentModel.Design.Serialization;
39
40 namespace System.ComponentModel
41 {
42         public class CultureInfoConverter : TypeConverter
43         {
44                 private class CultureInfoComparer : IComparer
45                 {
46                         public int Compare (object first,  object second)
47                         {
48                                 if (first == null) {
49                                         if (second == null)
50                                                 return 0;
51                                         else
52                                                 return -1;
53                                 }
54                                 if (second == null)
55                                         return 1;
56
57                                 return String.Compare (((CultureInfo)first).DisplayName, ((CultureInfo)second).DisplayName, 
58                                                        false, CultureInfo.CurrentCulture);
59                         }
60                 }
61
62                 private StandardValuesCollection _standardValues;
63
64                 public CultureInfoConverter()
65                 {
66                 }
67
68                 public override bool CanConvertFrom (ITypeDescriptorContext context,
69                         Type sourceType)
70                 {
71                         if (sourceType == typeof (string)) 
72                                 return true;
73                         return base.CanConvertFrom (context, sourceType);
74                 }
75
76                 public override bool CanConvertTo (ITypeDescriptorContext context,
77                         Type destinationType)
78                 {
79                         if (destinationType == typeof (string))
80                                 return true;
81                         if (destinationType == typeof (InstanceDescriptor))
82                                 return true;
83                         return base.CanConvertTo (context, destinationType);
84                 }
85
86                 public override object ConvertFrom (ITypeDescriptorContext context,
87                         CultureInfo culture, object value)
88                 {
89                         string culture_string = value as string;
90                         if (culture_string != null) {
91                                 if (String.Compare (culture_string, "(Default)", false) == 0)
92                                         return CultureInfo.InvariantCulture;
93
94                                 try {
95                                         // try to create a new CultureInfo if form is RFC 1766
96                                         return new CultureInfo (culture_string);
97                                 } catch {
98                                         // try to create a new CultureInfo if form is verbose name
99                                         foreach (CultureInfo CI in CultureInfo.GetCultures (CultureTypes.AllCultures))
100                                                 if (string.Compare (CI.DisplayName, 0, culture_string, 0, culture_string.Length, true) == 0)
101                                                         return CI;
102                                 }
103                                 throw new ArgumentException (string.Format (
104                                         "Culture {0} cannot be converted to a " +
105                                         "CultureInfo or is not available in " +
106                                         "this environment.", value));
107                         }
108                         return base.ConvertFrom (context, culture, value);
109                 }
110
111                 public override object ConvertTo (ITypeDescriptorContext context,
112                                                   CultureInfo culture, object value, Type destinationType)
113                 {
114                         if (destinationType == typeof (string)) {
115                                 if (value != null && (value is CultureInfo)) {
116                                         if (value == CultureInfo.InvariantCulture)
117                                                 return "(Default)";
118                                         else
119                                                 return ((CultureInfo) value).DisplayName; 
120                                 } else {
121                                         return "(Default)";
122                                 }
123                         }
124                         if (destinationType == typeof (InstanceDescriptor) && value is CultureInfo) {
125                                 CultureInfo cval = (CultureInfo) value;
126                                 ConstructorInfo ctor = typeof(CultureInfo).GetConstructor (new Type[] {typeof(int)});
127                                 return new InstanceDescriptor (ctor, new object[] {cval.LCID});
128                         }
129                         return base.ConvertTo (context, culture, value, destinationType);
130                 }
131
132                 public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
133                 {
134                         if (_standardValues == null) {
135                                 CultureInfo[] cultures = CultureInfo.GetCultures (CultureTypes.AllCultures);
136                                 Array.Sort (cultures, new CultureInfoComparer ());
137                                 CultureInfo[] stdValues = new CultureInfo[cultures.Length + 1];
138                                 stdValues[0] = CultureInfo.InvariantCulture;
139                                 Array.Copy (cultures, 0, stdValues, 1, cultures.Length);
140                                 _standardValues = new StandardValuesCollection (stdValues);
141                         }
142                         return _standardValues;
143                 }
144
145                 public override bool GetStandardValuesExclusive (ITypeDescriptorContext context)
146                 {
147                         return false;
148                 }
149
150                 public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
151                 {
152                         return true;
153                 }
154
155 #if NET_4_0
156                 protected virtual string GetCultureName (CultureInfo culture)
157                 {
158                         // .Net doesn't throw ArgumentNullException here, ugh.
159                         return culture.Name;
160                 }
161 #endif
162
163         }
164 }