| | 1 | | using PropertyGridHelpers.Attributes; |
| | 2 | | using PropertyGridHelpers.UIEditors; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.ComponentModel; |
| | 6 | | using System.Globalization; |
| | 7 | | using System.Linq; |
| | 8 | |
|
| | 9 | | namespace PropertyGridHelpers.Converters |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Provides a type converter that restricts input to selectable values only. |
| | 13 | | /// </summary> |
| | 14 | | /// <remarks> |
| | 15 | | /// This converter is used with the <see cref="ResourcePathEditor" /> to |
| | 16 | | /// prevent users from manually editing text in the PropertyGrid, while still |
| | 17 | | /// allowing selection from a predefined dropdown list. |
| | 18 | | /// </remarks> |
| | 19 | | /// <example> |
| | 20 | | /// <code language="csharp"> |
| | 21 | | /// [AllowBlank(includeItem: true, resourceItem: "Blank_ResourcePath")] |
| | 22 | | /// [LocalizedCategory("Category_TestItems")] |
| | 23 | | /// [LocalizedDescription("Description_ResourcePath")] |
| | 24 | | /// [LocalizedDisplayName("DisplayName_ResourcePath")] |
| | 25 | | /// [Editor(typeof(ResourcePathEditor), typeof(UITypeEditor))] |
| | 26 | | /// [TypeConverter(typeof(OnlySelectableTypeConverter))] |
| | 27 | | /// [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] |
| | 28 | | /// public string ResourcePath { get; set; } |
| | 29 | | /// </code> |
| | 30 | | /// </example> |
| | 31 | | /// <seealso cref="StringConverter" /> |
| | 32 | | /// <seealso cref="AllowBlankAttribute"/> |
| | 33 | | /// <seealso cref="ResourcePathEditor"/> |
| | 34 | | public class OnlySelectableTypeConverter : StringConverter |
| | 35 | | { |
| | 36 | | /// <summary> |
| | 37 | | /// Converts from a string to a element in an Enum. |
| | 38 | | /// </summary> |
| | 39 | | /// <param name="context">Context information from the property grid.</param> |
| | 40 | | /// <param name="culture">Culture info to use for parsing.</param> |
| | 41 | | /// <param name="value">The string to convert from.</param> |
| | 42 | | /// <returns></returns> |
| | 43 | | /// <exception cref="ArgumentException">Value cannot be blank and no valid options are available.</exception> |
| | 44 | | public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) |
| 28 | 45 | | { |
| | 46 | | // If the incoming value is an empty string, or just white space |
| | 47 | | #if NET35 |
| | 48 | | if (value is string str && string.IsNullOrEmpty(str)) |
| | 49 | | #else |
| 36 | 50 | | if (value is string str && string.IsNullOrWhiteSpace(str)) |
| | 51 | | #endif |
| 20 | 52 | | { |
| | 53 | | // check to see if blanks are allowed or not |
| 28 | 54 | | if (!AllowBlankAttribute.IsBlankAllowed(context)) |
| 16 | 55 | | { |
| | 56 | | // If blank is not allowed, then we need to return the name of the first resource path |
| 24 | 57 | | var first = GetStandardValues(context).Cast<string>().FirstOrDefault(); |
| | 58 | | // If we have a resource path, return it, or throw an error if there are no resource paths available |
| 24 | 59 | | return first != null ? (object)first : throw new ArgumentException("Value cannot be blank and no val |
| | 60 | | } |
| 12 | 61 | | } |
| | 62 | |
|
| 28 | 63 | | return base.ConvertFrom(context, culture, value); |
| 20 | 64 | | } |
| | 65 | |
|
| | 66 | | /// <inheritdoc/> |
| 20 | 67 | | public override bool GetStandardValuesSupported(ITypeDescriptorContext context) => true; |
| | 68 | |
|
| | 69 | | /// <inheritdoc/> |
| 20 | 70 | | public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) => true; |
| | 71 | |
|
| | 72 | | /// <summary> |
| | 73 | | /// Returns a list of resource paths found in the current instance’s assembly, |
| | 74 | | /// suitable for selection in a property grid editor or a dropdown. |
| | 75 | | /// </summary> |
| | 76 | | /// <param name="context"> |
| | 77 | | /// Provides contextual information, including the instance whose assembly will be inspected. |
| | 78 | | /// </param> |
| | 79 | | /// <returns> |
| | 80 | | /// A <see cref="TypeConverter.StandardValuesCollection"/> containing the names of resources |
| | 81 | | /// (without the assembly prefix and without the <c>.resources</c> extension) that are embedded |
| | 82 | | /// in the same assembly as the edited object. If no assembly is available in the context, an |
| | 83 | | /// empty collection is returned. |
| | 84 | | /// </returns> |
| | 85 | | /// <remarks> |
| | 86 | | /// This is used to present a list of available resource names (for example, localization resources or |
| | 87 | | /// embedded files) that a user might select from in the property grid. |
| | 88 | | /// </remarks> |
| | 89 | |
|
| | 90 | | public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) |
| 24 | 91 | | { |
| 32 | 92 | | var assembly = context?.Instance?.GetType().Assembly; |
| 32 | 93 | | if (assembly == null) |
| | 94 | | #if NET8_0_OR_GREATER || NET462_OR_GREATER |
| 20 | 95 | | return new StandardValuesCollection(Array.Empty<string>()); |
| | 96 | | #else |
| 4 | 97 | | return new StandardValuesCollection(new string[0]); |
| | 98 | | #endif |
| | 99 | |
|
| 24 | 100 | | var resources = assembly.GetManifestResourceNames(); |
| 24 | 101 | | var list = new List<string>(); |
| 24 | 102 | | var prefix = $"{assembly.GetName().Name}."; |
| | 103 | |
|
| 104 | 104 | | foreach (var res in resources) |
| 40 | 105 | | { |
| 48 | 106 | | if (res.EndsWith(".resources", StringComparison.InvariantCultureIgnoreCase)) |
| 32 | 107 | | { |
| | 108 | | #if NET8_0_OR_GREATER |
| 24 | 109 | | var name = res[..^".resources".Length]; |
| | 110 | | #else |
| 16 | 111 | | var name = res.Substring(0, res.Length - ".resources".Length); |
| | 112 | | #endif |
| 40 | 113 | | if (name.StartsWith(prefix, StringComparison.InvariantCultureIgnoreCase)) |
| | 114 | | #if NET8_0_OR_GREATER |
| 24 | 115 | | name = name[prefix.Length..]; |
| | 116 | | #else |
| 16 | 117 | | name = name.Substring(prefix.Length); |
| | 118 | | #endif |
| 40 | 119 | | list.Add(name); |
| 32 | 120 | | } |
| 40 | 121 | | } |
| | 122 | |
|
| 48 | 123 | | return new StandardValuesCollection(list.OrderBy(s => s).ToArray()); |
| 24 | 124 | | } |
| | 125 | | } |
| | 126 | | } |