| | 1 | | using PropertyGridHelpers.Enums; |
| | 2 | | using PropertyGridHelpers.UIEditors; |
| | 3 | | using System; |
| | 4 | | using System.ComponentModel; |
| | 5 | |
|
| | 6 | | namespace PropertyGridHelpers.Attributes |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Associates an image with an Enum field for display in a property grid. |
| | 10 | | /// </summary> |
| | 11 | | /// <remarks> |
| | 12 | | /// This attribute allows an Enum field to have an associated image, which can be used |
| | 13 | | /// in UI components such as property grids. The image is identified by a text-based key |
| | 14 | | /// and can be stored as an embedded resource or in an external location. |
| | 15 | | /// |
| | 16 | | /// To display the image in a property grid, apply this attribute to the Enum items and |
| | 17 | | /// use <see cref="ImageTextUIEditor"/> as the UI editor for the corresponding property. |
| | 18 | | /// </remarks> |
| | 19 | | /// <example> |
| | 20 | | /// <code> |
| | 21 | | /// public enum Status |
| | 22 | | /// { |
| | 23 | | /// [EnumImage("PendingIcon.png")] |
| | 24 | | /// Pending, |
| | 25 | | /// |
| | 26 | | /// [EnumImage("ApprovedIcon.png")] |
| | 27 | | /// Approved, |
| | 28 | | /// |
| | 29 | | /// [EnumImage("RejectedIcon.png", ImageLocation.External)] |
| | 30 | | /// Rejected |
| | 31 | | /// } |
| | 32 | | /// </code> |
| | 33 | | /// </example> |
| | 34 | | /// <seealso cref="Attribute"/> |
| | 35 | | /// <seealso cref="ImageTextUIEditor"/> |
| | 36 | | [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)] |
| | 37 | | public class EnumImageAttribute : Attribute |
| | 38 | | { |
| | 39 | | /// <summary> |
| | 40 | | /// Initializes a new instance of the <see cref="EnumImageAttribute"/> class |
| | 41 | | /// with default values (null image, embedded location). |
| | 42 | | /// </summary> |
| 20 | 43 | | public EnumImageAttribute() |
| 12 | 44 | | { |
| 20 | 45 | | EnumImage = null; |
| 20 | 46 | | ImageLocation = ImageLocation.Embedded; |
| 20 | 47 | | } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Initializes a new instance of the <see cref="EnumImageAttribute"/> class |
| | 51 | | /// with the specified image location. |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="imageLocation">The storage location of the image (embedded or external).</param> |
| 24 | 54 | | public EnumImageAttribute(ImageLocation imageLocation) |
| 16 | 55 | | { |
| 24 | 56 | | EnumImage = null; |
| 24 | 57 | | ImageLocation = imageLocation; |
| 24 | 58 | | } |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Initializes a new instance of the <see cref="EnumImageAttribute"/> class |
| | 62 | | /// with a specified image identifier and location. |
| | 63 | | /// </summary> |
| | 64 | | /// <param name="text">The name or resource key of the image.</param> |
| | 65 | | /// <param name="imageLocation">The storage location of the image (default: embedded).</param> |
| 112 | 66 | | public EnumImageAttribute(string text, ImageLocation imageLocation = ImageLocation.Embedded) |
| 104 | 67 | | { |
| 112 | 68 | | EnumImage = text; |
| 112 | 69 | | ImageLocation = imageLocation; |
| 112 | 70 | | } |
| | 71 | |
|
| | 72 | | /// <summary> |
| | 73 | | /// Gets the name or resource key of the image associated with the Enum item. |
| | 74 | | /// </summary> |
| | 75 | | /// <value> |
| | 76 | | /// The name or resource key of the image associated with the Enum item. |
| | 77 | | /// </value> |
| | 78 | | public string EnumImage |
| | 79 | | { |
| 60 | 80 | | get; |
| | 81 | | } |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Gets the storage location of the associated image. |
| | 85 | | /// </summary> |
| | 86 | | /// <value> |
| | 87 | | /// The storage location of the associated image. |
| | 88 | | /// </value> |
| | 89 | | public ImageLocation ImageLocation |
| | 90 | | { |
| 44 | 91 | | get; |
| | 92 | | } |
| | 93 | |
|
| | 94 | | /// <summary> |
| | 95 | | /// Retrieves the name of the image associated with a given Enum value. |
| | 96 | | /// </summary> |
| | 97 | | /// <param name="value">The Enum value.</param> |
| | 98 | | /// <returns>The image name if found, otherwise the Enum name.</returns> |
| | 99 | | public static string GetEnumImage(Enum value) |
| 60 | 100 | | { |
| 68 | 101 | | var attribute = Get(value); |
| 68 | 102 | | if (attribute == null) |
| 20 | 103 | | return string.Empty; |
| 64 | 104 | | var result = attribute.EnumImage; |
| 64 | 105 | | if (string.IsNullOrEmpty(result)) |
| 20 | 106 | | result = Enum.GetName(value.GetType(), value); |
| 64 | 107 | | return result; |
| 60 | 108 | | } |
| | 109 | |
|
| | 110 | | /// <summary> |
| | 111 | | /// Determines whether an <see cref="EnumImageAttribute"/> exists for the given Enum value. |
| | 112 | | /// </summary> |
| | 113 | | /// <param name="value">The Enum value.</param> |
| | 114 | | /// <returns><c>true</c> if an image attribute is associated with the value; otherwise, <c>false</c>.</returns> |
| 24 | 115 | | public static bool Exists(Enum value) => Get(value) != null; |
| | 116 | |
|
| | 117 | | /// <summary> |
| | 118 | | /// Retrieves the <see cref="EnumImageAttribute"/> associated with a given Enum value. |
| | 119 | | /// </summary> |
| | 120 | | /// <param name="value">The Enum value.</param> |
| | 121 | | /// <returns>The corresponding <see cref="EnumImageAttribute"/> instance, or <c>null</c> if none is found.</retu |
| | 122 | | public static EnumImageAttribute Get(Enum value) => |
| 136 | 123 | | value == null ? null |
| 136 | 124 | | : Support.Support.GetFirstCustomAttribute<EnumImageAttribute>( |
| 136 | 125 | | Support.Support.GetEnumField(value)); |
| | 126 | |
|
| | 127 | | /// <summary> |
| | 128 | | /// Gets the specified context. |
| | 129 | | /// </summary> |
| | 130 | | /// <param name="context">The context.</param> |
| | 131 | | /// <returns></returns> |
| | 132 | | public static EnumImageAttribute Get(ITypeDescriptorContext context) => |
| 32 | 133 | | context == null || context.Instance == null || context.PropertyDescriptor == null |
| 32 | 134 | | ? null |
| 32 | 135 | | : Get((Enum)context.PropertyDescriptor.GetValue(context.Instance)); |
| | 136 | | } |
| | 137 | | } |