1 //------------------------------------------------------------------------------
2 // <copyright file="FontNamesConverter.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6 
7 namespace System.Web.UI.WebControls {
8 
9     using System.ComponentModel.Design;
10     using System;
11     using System.ComponentModel;
12     using System.Collections;
13     using System.Globalization;
14 
15     /// <devdoc>
16     ///   Converts a string with font names separated by commas to and from
17     ///   an array of strings containing individual names.
18     /// </devdoc>
19     public class FontNamesConverter : TypeConverter {
20 
21 
22         /// <devdoc>
23         ///   Determines if the specified data type can be converted to an array of strings
24         ///   containing individual font names.
25         /// </devdoc>
CanConvertFrom(ITypeDescriptorContext context, Type sourceType)26         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
27             if (sourceType == typeof(string)) {
28                 return true;
29             }
30             return false;
31         }
32 
33 
34         /// <devdoc>
35         ///   Parses a string that represents a list of font names separated by
36         ///   commas into an array of strings containing individual font names.
37         /// </devdoc>
ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)38         public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
39             if (value is string) {
40                 if (((string)value).Length == 0) {
41                     return new string[0];
42                 }
43 
44                 string[] names = ((string)value).Split(new char[] { culture.TextInfo.ListSeparator[0] });
45                 for (int i = 0; i < names.Length; i++) {
46                     names[i] = names[i].Trim();
47                 }
48                 return names;
49             }
50             throw GetConvertFromException(value);
51         }
52 
53 
54         /// <devdoc>
55         ///   Creates a string that represents a list of font names separated
56         ///   by commas from an array of strings containing individual font names.
57         /// </devdoc>
ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)58         public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
59             if (destinationType == typeof(string)) {
60 
61                 if (value == null) {
62                     return String.Empty;
63                 }
64                 return string.Join(culture.TextInfo.ListSeparator, ((string[])value));
65             }
66             throw GetConvertToException(value, destinationType);
67         }
68     }
69 }
70