summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrakeWPF/Utilities/EnumHelper.cs
diff options
context:
space:
mode:
authorScott <[email protected]>2015-09-26 21:05:40 +0100
committerScott <[email protected]>2015-09-26 21:30:32 +0100
commit1320d77d36f096c7aa5b1697d3aaf9aa35d5e199 (patch)
tree1a9964eac27b7689b39a2e5adbf019a7295b6a9b /win/CS/HandBrakeWPF/Utilities/EnumHelper.cs
parente703a7961f12a3e02c475754862a1f4a57a04646 (diff)
App Services Tidyup Contd
Moving all the helper and utility classes to the gui project
Diffstat (limited to 'win/CS/HandBrakeWPF/Utilities/EnumHelper.cs')
-rw-r--r--win/CS/HandBrakeWPF/Utilities/EnumHelper.cs160
1 files changed, 160 insertions, 0 deletions
diff --git a/win/CS/HandBrakeWPF/Utilities/EnumHelper.cs b/win/CS/HandBrakeWPF/Utilities/EnumHelper.cs
new file mode 100644
index 000000000..29202efe6
--- /dev/null
+++ b/win/CS/HandBrakeWPF/Utilities/EnumHelper.cs
@@ -0,0 +1,160 @@
+// --------------------------------------------------------------------------------------------------------------------
+// <copyright file="EnumHelper.cs" company="HandBrake Project (http://handbrake.fr)">
+// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
+// </copyright>
+// <summary>
+// Enum Helpers
+// </summary>
+// --------------------------------------------------------------------------------------------------------------------
+
+namespace HandBrakeWPF.Utilities
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Collections.ObjectModel;
+ using System.ComponentModel;
+ using System.ComponentModel.DataAnnotations;
+ using System.Diagnostics;
+ using System.Linq;
+ using System.Reflection;
+
+ using HandBrake.ApplicationServices.Attributes;
+
+ /// <summary>
+ /// Enum Helpers
+ /// </summary>
+ /// <typeparam name="T">
+ /// The Type Parameter
+ /// </typeparam>
+ public class EnumHelper<T>
+ {
+ /// <summary>
+ /// Get the description of an Enum
+ /// </summary>
+ /// <param name="value">
+ /// The value.
+ /// </param>
+ /// <returns>
+ /// The Description string
+ /// </returns>
+ public static string GetDescription(T value)
+ {
+ FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
+ DescriptionAttribute[] attributes =
+ (DescriptionAttribute[])fieldInfo.GetCustomAttributes(
+ typeof(DescriptionAttribute), false);
+ return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
+ }
+
+ /// <summary>
+ /// Get the Display Value of the Enum Model
+ /// </summary>
+ /// <param name="value">An Enum with Display Attributes</param>
+ /// <returns>A string name</returns>
+ public static string GetDisplay(T value)
+ {
+ FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
+ DisplayAttribute[] attributes = (DisplayAttribute[])fieldInfo.GetCustomAttributes(typeof(DisplayAttribute), false);
+
+ return (attributes.Length > 0) ? attributes[0].Name : value.ToString();
+ }
+
+ /// <summary>
+ /// Get the Enumeration for a given Enum Description
+ /// </summary>
+ /// <param name="description">The String description</param>
+ /// <returns>The Enum Value</returns>
+ public static T GetValue(string description)
+ {
+ return GetValue(description, false);
+ }
+
+ /// <summary>
+ /// Get the Enumeration for a given Enum Description
+ /// </summary>
+ /// <param name="description">The String description</param>
+ /// <param name="insensitiveCase">Turn of sensitivity to cases.</param>
+ /// <returns>The Enum Value</returns>
+ public static T GetValue(string description, bool insensitiveCase)
+ {
+ foreach (T val in Enum.GetValues(typeof(T)))
+ {
+ string currDescription = GetDescription(val);
+ string currDisplay = GetDisplay(val);
+ string shortName = GetShortName(val);
+ if (currDescription == description || currDisplay == description || shortName == description)
+ {
+ return val;
+ }
+
+ if (insensitiveCase && (currDescription.ToLower() == description.ToLower() || currDisplay.ToLower() == description.ToLower() || shortName.ToLower() == description.ToLower()))
+ {
+ return val;
+ }
+ }
+
+ Debug.WriteLine("EnumHelper.GetValue: The Description for the enum was not recognized: " + description);
+
+ return default(T);
+ }
+
+ /// <summary>
+ /// The get short name.
+ /// </summary>
+ /// <param name="value">
+ /// The value.
+ /// </param>
+ /// <returns>
+ /// The <see cref="string"/>.
+ /// </returns>
+ public static string GetShortName(T value)
+ {
+ FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
+ ShortName[] attributes = (ShortName[])fieldInfo.GetCustomAttributes(typeof(ShortName), false);
+
+ return (attributes.Length > 0) ? attributes[0].Name : value.ToString();
+ }
+
+ /// <summary>
+ /// Return a list of all the enum values.
+ /// </summary>
+ /// <returns>
+ /// An Enum Oject List
+ /// </returns>
+ public static IEnumerable<T> GetEnumList()
+ {
+ return Enum.GetValues(typeof(T)).Cast<T>().ToList();
+ }
+
+ /// <summary>
+ /// Get a list of string names for each enum value.
+ /// </summary>
+ /// <param name="enumType">
+ /// The enum type.
+ /// </param>
+ /// <returns>
+ /// A collection of strings that represent all the enum values
+ /// </returns>
+ public static IEnumerable<string> GetEnumDisplayValues(Type enumType)
+ {
+ var strings = new Collection<string>();
+ foreach (T e in Enum.GetValues(enumType))
+ strings.Add(GetDisplay(e));
+ return strings;
+ }
+
+ /// <summary>
+ /// Get a list of string names for each enum value passed in.
+ /// </summary>
+ /// <param name="items">
+ /// The items.
+ /// </param>
+ /// <returns>
+ /// A collection of strings that represent all the enum values
+ /// </returns>
+ public static IEnumerable<string> GetEnumDisplayValuesSubset(IEnumerable<T> items)
+ {
+ return items.Select(GetDisplay).ToList();
+ }
+ }
+}