311 lines
13 KiB
C#
311 lines
13 KiB
C#
/*
|
|
*
|
|
* (c) Copyright Ascensio System Limited 2010-2018
|
|
*
|
|
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
|
|
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
|
|
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
|
|
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
|
|
*
|
|
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
|
|
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
|
|
*
|
|
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
|
|
*
|
|
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
|
|
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
|
|
*
|
|
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
|
|
* relevant author attributions when distributing the software. If the display of the logo in its graphic
|
|
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
|
|
* in every copy of the program you distribute.
|
|
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
|
|
*
|
|
*/
|
|
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Globalization;
|
|
|
|
namespace ASC.Web.Core.Helpers
|
|
{
|
|
/// <summary>
|
|
/// Defines a type converter for enum values that converts enum values to
|
|
/// and from string representations using resources
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This class makes localization of display values for enums in a project easy. Simply
|
|
/// derive a class from this class and pass the ResourceManagerin the constructor.
|
|
///
|
|
/// Then define the enum values in the resource editor. The names of
|
|
/// the resources are simply the enum value prefixed by the enum type name with an
|
|
/// underscore separator eg MyEnum_MyValue. You can then use the TypeConverter attribute
|
|
/// to make the LocalizedEnumConverter the default TypeConverter for the enums in your
|
|
/// project.
|
|
/// </remarks>
|
|
public class ResourceEnumConverter : System.ComponentModel.EnumConverter
|
|
{
|
|
private class LookupTable : Dictionary<string, object> { }
|
|
private readonly Dictionary<CultureInfo, LookupTable> _lookupTables = new Dictionary<CultureInfo, LookupTable>();
|
|
private readonly System.Resources.ResourceManager _resourceManager;
|
|
private readonly bool _isFlagEnum = false;
|
|
private readonly Array _flagValues;
|
|
|
|
/// <summary>
|
|
/// Get the lookup table for the given culture (creating if necessary)
|
|
/// </summary>
|
|
/// <param name="culture"></param>
|
|
/// <returns></returns>
|
|
private LookupTable GetLookupTable(CultureInfo culture)
|
|
{
|
|
if (culture == null)
|
|
culture = CultureInfo.CurrentCulture;
|
|
|
|
if (!_lookupTables.TryGetValue(culture, out var result))
|
|
{
|
|
result = new LookupTable();
|
|
foreach (var value in GetStandardValues())
|
|
{
|
|
var text = GetValueText(culture, value);
|
|
if (text != null)
|
|
{
|
|
result.Add(text, value);
|
|
}
|
|
}
|
|
_lookupTables.Add(culture, result);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Return the text to display for a simple value in the given culture
|
|
/// </summary>
|
|
/// <param name="culture">The culture to get the text for</param>
|
|
/// <param name="value">The enum value to get the text for</param>
|
|
/// <returns>The localized text</returns>
|
|
private string GetValueText(CultureInfo culture, object value)
|
|
{
|
|
var type = value.GetType();
|
|
var resourceName = string.Format("{0}_{1}", type.Name, value.ToString());
|
|
var result = _resourceManager.GetString(resourceName, culture);
|
|
if (result == null)
|
|
result = resourceName;
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Return true if the given value is can be represented using a single bit
|
|
/// </summary>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
private bool IsSingleBitValue(ulong value)
|
|
{
|
|
return value switch
|
|
{
|
|
0 => false,
|
|
1 => true,
|
|
_ => ((value & (value - 1)) == 0),
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Return the text to display for a flag value in the given culture
|
|
/// </summary>
|
|
/// <param name="culture">The culture to get the text for</param>
|
|
/// <param name="value">The flag enum value to get the text for</param>
|
|
/// <returns>The localized text</returns>
|
|
private string GetFlagValueText(CultureInfo culture, object value)
|
|
{
|
|
// if there is a standard value then use it
|
|
//
|
|
if (Enum.IsDefined(value.GetType(), value))
|
|
{
|
|
return GetValueText(culture, value);
|
|
}
|
|
|
|
// otherwise find the combination of flag bit values
|
|
// that makes up the value
|
|
//
|
|
ulong lValue = Convert.ToUInt32(value);
|
|
string result = null;
|
|
foreach (var flagValue in _flagValues)
|
|
{
|
|
ulong lFlagValue = Convert.ToUInt32(flagValue);
|
|
if (IsSingleBitValue(lFlagValue))
|
|
{
|
|
if ((lFlagValue & lValue) == lFlagValue)
|
|
{
|
|
var valueText = GetValueText(culture, flagValue);
|
|
if (result == null)
|
|
{
|
|
result = valueText;
|
|
}
|
|
else
|
|
{
|
|
result = string.Format("{0}, {1}", result, valueText);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Return the Enum value for a simple (non-flagged enum)
|
|
/// </summary>
|
|
/// <param name="culture">The culture to convert using</param>
|
|
/// <param name="text">The text to convert</param>
|
|
/// <returns>The enum value</returns>
|
|
private object GetValue(CultureInfo culture, string text)
|
|
{
|
|
var lookupTable = GetLookupTable(culture);
|
|
lookupTable.TryGetValue(text, out var result);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Return the Enum value for a flagged enum
|
|
/// </summary>
|
|
/// <param name="culture">The culture to convert using</param>
|
|
/// <param name="text">The text to convert</param>
|
|
/// <returns>The enum value</returns>
|
|
private object GetFlagValue(CultureInfo culture, string text)
|
|
{
|
|
var lookupTable = GetLookupTable(culture);
|
|
var textValues = text.Split(',');
|
|
ulong result = 0;
|
|
foreach (var textValue in textValues)
|
|
{
|
|
var trimmedTextValue = textValue.Trim();
|
|
if (!lookupTable.TryGetValue(trimmedTextValue, out var value))
|
|
{
|
|
return null;
|
|
}
|
|
result |= Convert.ToUInt32(value);
|
|
}
|
|
return Enum.ToObject(EnumType, result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a new instance of the converter using translations from the given resource manager
|
|
/// </summary>
|
|
/// <param name="type"></param>
|
|
/// <param name="resourceManager"></param>
|
|
public ResourceEnumConverter(Type type, System.Resources.ResourceManager resourceManager)
|
|
: base(type)
|
|
{
|
|
_resourceManager = resourceManager;
|
|
var flagAttributes = type.GetCustomAttributes(typeof(FlagsAttribute), true);
|
|
_isFlagEnum = flagAttributes.Length > 0;
|
|
if (_isFlagEnum)
|
|
{
|
|
_flagValues = Enum.GetValues(type);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert string values to enum values
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
/// <param name="culture"></param>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
|
|
{
|
|
if (value is string @string)
|
|
{
|
|
var result = (_isFlagEnum) ?
|
|
GetFlagValue(culture, @string) : GetValue(culture, @string);
|
|
if (result == null)
|
|
{
|
|
result = base.ConvertFrom(context, culture, value);
|
|
}
|
|
return result;
|
|
}
|
|
else
|
|
{
|
|
return base.ConvertFrom(context, culture, value);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert the enum value to a string
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
/// <param name="culture"></param>
|
|
/// <param name="value"></param>
|
|
/// <param name="destinationType"></param>
|
|
/// <returns></returns>
|
|
public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
|
|
{
|
|
if (value != null && destinationType == typeof(string))
|
|
{
|
|
object result = (_isFlagEnum) ?
|
|
GetFlagValueText(culture, value) : GetValueText(culture, value);
|
|
return result;
|
|
}
|
|
else
|
|
{
|
|
return base.ConvertTo(context, culture, value, destinationType);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert the given enum value to string using the registered type converter
|
|
/// </summary>
|
|
/// <param name="value">The enum value to convert to string</param>
|
|
/// <returns>The localized string value for the enum</returns>
|
|
static public string ConvertToString(Enum value)
|
|
{
|
|
var converter = TypeDescriptor.GetConverter(value.GetType());
|
|
return converter.ConvertToString(value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Return a list of the enum values and their associated display text for the given enum type
|
|
/// </summary>
|
|
/// <param name="enumType">The enum type to get the values for</param>
|
|
/// <param name="culture">The culture to get the text for</param>
|
|
/// <returns>
|
|
/// A list of KeyValuePairs where the key is the enum value and the value is the text to display
|
|
/// </returns>
|
|
/// <remarks>
|
|
/// This method can be used to provide localized binding to enums in ASP.NET applications. Unlike
|
|
/// windows forms the standard ASP.NET controls do not use TypeConverters to convert from enum values
|
|
/// to the displayed text. You can bind an ASP.NET control to the list returned by this method by setting
|
|
/// the DataValueField to "Key" and theDataTextField to "Value".
|
|
/// </remarks>
|
|
static public List<KeyValuePair<Enum, string>> GetValues(Type enumType, CultureInfo culture)
|
|
{
|
|
var result = new List<KeyValuePair<Enum, string>>();
|
|
var converter = TypeDescriptor.GetConverter(enumType);
|
|
foreach (Enum value in Enum.GetValues(enumType))
|
|
{
|
|
var pair = new KeyValuePair<Enum, string>(value, converter.ConvertToString(null, culture, value));
|
|
result.Add(pair);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Return a list of the enum values and their associated display text for the given enum type in the current UI Culture
|
|
/// </summary>
|
|
/// <param name="enumType">The enum type to get the values for</param>
|
|
/// <returns>
|
|
/// A list of KeyValuePairs where the key is the enum value and the value is the text to display
|
|
/// </returns>
|
|
/// <remarks>
|
|
/// This method can be used to provide localized binding to enums in ASP.NET applications. Unlike
|
|
/// windows forms the standard ASP.NET controls do not use TypeConverters to convert from enum values
|
|
/// to the displayed text. You can bind an ASP.NET control to the list returned by this method by setting
|
|
/// the DataValueField to "Key" and theDataTextField to "Value".
|
|
/// </remarks>
|
|
static public List<KeyValuePair<Enum, string>> GetValues(Type enumType)
|
|
{
|
|
return GetValues(enumType, CultureInfo.CurrentUICulture);
|
|
}
|
|
}
|
|
}
|