2 // System.ComponentModel.CultureInfoConverter
5 // Martin Willemoes Hansen (mwh@sysrq.dk)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 // Ivan N. Zlatev <contact@i-nz.net>
9 // (C) 2003 Martin Willemoes Hansen
10 // (C) 2003 Andreas Nahr
11 // (C) 2008 Novell, Inc. (http://www.novell.com)
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:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
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.
35 using System.Collections;
36 using System.Globalization;
37 using System.Reflection;
38 using System.ComponentModel.Design.Serialization;
40 namespace System.ComponentModel
42 public class CultureInfoConverter : TypeConverter
44 private class CultureInfoComparer : IComparer
46 public int Compare (object first, object second)
57 return String.Compare (((CultureInfo)first).DisplayName, ((CultureInfo)second).DisplayName,
58 false, CultureInfo.CurrentCulture);
62 private StandardValuesCollection _standardValues;
64 public CultureInfoConverter()
68 public override bool CanConvertFrom (ITypeDescriptorContext context,
71 if (sourceType == typeof (string))
73 return base.CanConvertFrom (context, sourceType);
76 public override bool CanConvertTo (ITypeDescriptorContext context,
79 if (destinationType == typeof (string))
81 if (destinationType == typeof (InstanceDescriptor))
83 return base.CanConvertTo (context, destinationType);
86 public override object ConvertFrom (ITypeDescriptorContext context,
87 CultureInfo culture, object value)
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;
95 // try to create a new CultureInfo if form is RFC 1766
96 return new CultureInfo (culture_string);
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)
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));
108 return base.ConvertFrom (context, culture, value);
111 public override object ConvertTo (ITypeDescriptorContext context,
112 CultureInfo culture, object value, Type destinationType)
114 if (destinationType == typeof (string)) {
115 if (value != null && (value is CultureInfo)) {
116 if (value == CultureInfo.InvariantCulture)
119 return ((CultureInfo) value).DisplayName;
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});
129 return base.ConvertTo (context, culture, value, destinationType);
132 public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
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);
142 return _standardValues;
145 public override bool GetStandardValuesExclusive (ITypeDescriptorContext context)
150 public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
156 protected virtual string GetCultureName (CultureInfo culture)
158 // .Net doesn't throw ArgumentNullException here, ugh.