| | 1 | | using PropertyGridHelpers.Converters; |
| | 2 | | using PropertyGridHelpers.UIEditors; |
| | 3 | | using System; |
| | 4 | | using System.ComponentModel; |
| | 5 | | using System.Globalization; |
| | 6 | | using System.Linq; |
| | 7 | |
|
| | 8 | | namespace PropertyGridHelpers.Attributes |
| | 9 | | { |
| | 10 | | #if NET8_0_OR_GREATER |
| | 11 | | /// <summary> |
| | 12 | | /// Specifies whether a blank (empty) value is allowed for a property, typically used in dropdown editors. |
| | 13 | | /// </summary> |
| | 14 | | /// <param name="allow">if set to <c>true</c> allow blank values in the property.</param> |
| | 15 | | /// <param name="includeItem">if set to <c>true</c> include item in the dropdown to indicate blank.</param> |
| | 16 | | /// <param name="resourceItem">The resource item to use for displaying the blank item (Leaves it empty when this is |
| | 17 | | /// <remarks> |
| | 18 | | /// This attribute is used in conjunction with editors like <see cref="ResourcePathEditor"/> and converters |
| | 19 | | /// like <see cref="OnlySelectableTypeConverter"/> to optionally include a blank entry in the list of selectable val |
| | 20 | | /// </remarks> |
| | 21 | | /// <example> |
| | 22 | | /// <code language="csharp"> |
| | 23 | | /// [AllowBlank(includeItem: true, resourceItem: "Blank_DisplayText")] |
| | 24 | | /// [Editor(typeof(ResourcePathEditor), typeof(UITypeEditor))] |
| | 25 | | /// [TypeConverter(typeof(OnlySelectableTypeConverter))] |
| | 26 | | /// public string ResourcePath { get; set; } |
| | 27 | | /// </code> |
| | 28 | | /// </example> |
| | 29 | | /// <seealso cref="Attribute"/> |
| | 30 | | /// <seealso cref="ResourcePathEditor"/> |
| | 31 | | /// <seealso cref="OnlySelectableTypeConverter"/> |
| | 32 | | [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] |
| 92 | 33 | | public class AllowBlankAttribute(bool allow = true, bool includeItem = false, string resourceItem = "") : Attribute |
| | 34 | | { |
| | 35 | | #else |
| | 36 | | /// <summary> |
| | 37 | | /// Specifies whether a blank (empty) value is allowed for a property, typically used in dropdown editors. |
| | 38 | | /// </summary> |
| | 39 | | /// <remarks> |
| | 40 | | /// This attribute is used in conjunction with editors like <see cref="ResourcePathEditor"/> and converters |
| | 41 | | /// like <see cref="OnlySelectableTypeConverter"/> to optionally include a blank entry in the list of selectable val |
| | 42 | | /// </remarks> |
| | 43 | | /// <example> |
| | 44 | | /// <code language="csharp"> |
| | 45 | | /// [AllowBlank(includeItem: true, resourceItem: "Blank_DisplayText")] |
| | 46 | | /// [Editor(typeof(ResourcePathEditor), typeof(UITypeEditor))] |
| | 47 | | /// [TypeConverter(typeof(OnlySelectableTypeConverter))] |
| | 48 | | /// public string ResourcePath { get; set; } |
| | 49 | | /// </code> |
| | 50 | | /// </example> |
| | 51 | | /// <seealso cref="Attribute"/> |
| | 52 | | /// <seealso cref="ResourcePathEditor"/> |
| | 53 | | /// <seealso cref="OnlySelectableTypeConverter"/> |
| | 54 | | [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] |
| | 55 | | public class AllowBlankAttribute : Attribute |
| | 56 | | { |
| | 57 | | /// <summary> |
| | 58 | | /// Initializes a new instance of the <see cref="AllowBlankAttribute" /> class. |
| | 59 | | /// </summary> |
| | 60 | | /// <param name="allow">if set to <c>true</c> allow blank values in the property.</param> |
| | 61 | | /// <param name="includeItem">if set to <c>true</c> include item in the dropdown to indicate blank.</param> |
| | 62 | | /// <param name="resourceItem">The resource item to use for displaying the blank item (Leaves it empty when this |
| 16 | 63 | | public AllowBlankAttribute(bool allow = true, bool includeItem = false, string resourceItem = "") |
| 8 | 64 | | { |
| 16 | 65 | | Allow = allow; |
| 16 | 66 | | IncludeItem = includeItem; |
| 16 | 67 | | ResourceItem = resourceItem; |
| 16 | 68 | | } |
| | 69 | | #endif |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Gets a value indicating whether this <see cref="AllowBlankAttribute"/> is allowing blank values. |
| | 73 | | /// </summary> |
| | 74 | | /// <value> |
| | 75 | | /// <c>true</c> if allow blank values; otherwise, <c>false</c>. |
| | 76 | | /// </value> |
| | 77 | | public bool Allow |
| | 78 | | { |
| 80 | 79 | | get; |
| | 80 | | #if NET8_0_OR_GREATER |
| 92 | 81 | | } = allow; |
| | 82 | | #else |
| | 83 | | } |
| | 84 | | #endif |
| | 85 | | /// <summary> |
| | 86 | | /// Gets a value indicating whether to include item an item in the dropdown to indicate blank. |
| | 87 | | /// </summary> |
| | 88 | | /// <value> |
| | 89 | | /// <c>true</c> if including an item in the dropdown to indicate blank; otherwise, <c>false</c>. |
| | 90 | | /// </value> |
| | 91 | | public bool IncludeItem |
| | 92 | | { |
| 92 | 93 | | get; |
| | 94 | | #if NET8_0_OR_GREATER |
| 92 | 95 | | } = includeItem; |
| | 96 | | #else |
| | 97 | | } |
| | 98 | | #endif |
| | 99 | |
|
| | 100 | | /// <summary> |
| | 101 | | /// Gets the resource item to use for displaying the blank item (Leaves it empty when this is not provided). |
| | 102 | | /// </summary> |
| | 103 | | /// <value> |
| | 104 | | /// The resource item to use for displaying the blank item (Leaves it empty when this is not provided). |
| | 105 | | /// </value> |
| | 106 | | public string ResourceItem |
| | 107 | | { |
| 60 | 108 | | get; |
| | 109 | | #if NET8_0_OR_GREATER |
| 92 | 110 | | } = resourceItem; |
| | 111 | | #else |
| | 112 | | } |
| | 113 | | #endif |
| | 114 | |
|
| | 115 | | /// <summary> |
| | 116 | | /// Determines whether blank is allowed in the specified context. |
| | 117 | | /// </summary> |
| | 118 | | /// <param name="context">The context.</param> |
| | 119 | | /// <returns> |
| | 120 | | /// <c>true</c> if blank is allowed in the specified context; otherwise, <c>false</c>. |
| | 121 | | /// </returns> |
| | 122 | | public static bool IsBlankAllowed(ITypeDescriptorContext context) |
| 48 | 123 | | { |
| 56 | 124 | | var prop = Get(context); |
| 56 | 125 | | return prop != null && prop.Allow; |
| 48 | 126 | | } |
| | 127 | |
|
| | 128 | | /// <summary> |
| | 129 | | /// Gets the blank label. |
| | 130 | | /// </summary> |
| | 131 | | /// <param name="context">The context.</param> |
| | 132 | | /// <returns></returns> |
| | 133 | | public static string GetBlankLabel( |
| | 134 | | ITypeDescriptorContext context) |
| 56 | 135 | | { |
| 64 | 136 | | if (context?.PropertyDescriptor == null || context.Instance == null) |
| 20 | 137 | | return string.Empty; |
| | 138 | |
|
| | 139 | | // Check for AllowBlankAttribute on the property |
| 60 | 140 | | var allowBlankAttr = Get(context); |
| 60 | 141 | | if (allowBlankAttr == null || !allowBlankAttr.Allow) |
| 20 | 142 | | return string.Empty; |
| | 143 | |
|
| | 144 | | // Determine the baseName from the class's ResourcePathAttribute |
| 56 | 145 | | var resourcePathAttr = context.Instance.GetType() |
| 56 | 146 | | .GetCustomAttributes(typeof(ResourcePathAttribute), true); |
| | 147 | |
|
| 56 | 148 | | string baseName = null; |
| 56 | 149 | | if (resourcePathAttr.Length > 0 && resourcePathAttr[0] is ResourcePathAttribute rpa) |
| 36 | 150 | | baseName = rpa.ResourcePath; |
| | 151 | |
|
| | 152 | | // If we want to include an item in the list and have a resource item name, use it |
| 56 | 153 | | if (allowBlankAttr.IncludeItem && !string.IsNullOrEmpty(allowBlankAttr.ResourceItem)) |
| 40 | 154 | | { |
| | 155 | | try |
| 40 | 156 | | { |
| 48 | 157 | | if (!string.IsNullOrEmpty(baseName)) |
| 20 | 158 | | { |
| 28 | 159 | | var rootNamespace = context.Instance.GetType().Namespace.Split('.')[0]; |
| 28 | 160 | | var fullResourcePath = $"{rootNamespace}.{baseName}"; |
| | 161 | |
|
| 28 | 162 | | var manager = new ComponentResourceManager(context.Instance.GetType().Assembly.GetType(fullResou |
| 24 | 163 | | var localized = manager.GetString(allowBlankAttr.ResourceItem, CultureInfo.CurrentCulture); |
| 24 | 164 | | if (!string.IsNullOrEmpty(localized)) |
| 20 | 165 | | return localized; |
| 12 | 166 | | } |
| 40 | 167 | | } |
| 20 | 168 | | catch |
| 12 | 169 | | { |
| | 170 | | // Fall through to default |
| 20 | 171 | | } |
| 36 | 172 | | } |
| | 173 | |
|
| | 174 | | // Default to "(none)" if no localized string is found or we aren't including the label |
| 52 | 175 | | return allowBlankAttr.IncludeItem ? "(none)" : string.Empty; |
| 64 | 176 | | } |
| | 177 | |
|
| | 178 | | /// <summary> |
| | 179 | | /// Gets the specified context. |
| | 180 | | /// </summary> |
| | 181 | | /// <param name="context">The context.</param> |
| | 182 | | /// <returns></returns> |
| | 183 | | public static AllowBlankAttribute Get(ITypeDescriptorContext context) => |
| 112 | 184 | | context == null || context.Instance == null || context.PropertyDescriptor == null |
| 112 | 185 | | ? null |
| 112 | 186 | | : Support.Support.GetFirstCustomAttribute<AllowBlankAttribute>( |
| 112 | 187 | | Support.Support.GetPropertyInfo(context)); |
| | 188 | |
|
| | 189 | | /// <summary> |
| | 190 | | /// Converts to string. |
| | 191 | | /// </summary> |
| | 192 | | /// <returns> |
| | 193 | | /// A <see cref="string" /> that represents this instance. |
| | 194 | | /// </returns> |
| | 195 | | public override string ToString() => |
| 20 | 196 | | $"AllowBlank: {Allow}, IncludeItem: {IncludeItem}, ResourceItem: '{ResourceItem}'"; |
| | 197 | | } |
| | 198 | | } |