// Copyright (C) Stichting Deltares 2019. All rights reserved. // // This file is part of Riskeer. // // Riskeer is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Globalization; using System.Reflection; using Core.Common.Base.Data; namespace Core.Common.Gui.Converters { /// /// Attribute when using the to define what is /// shown as name and value for each element. /// [AttributeUsage(AttributeTargets.Property)] public class KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute : KeyValueElementAttribute { private readonly string unitPropertyName; /// /// Creates a new instance of . /// /// The name of the property to show as name. /// The name of the property to show as unit. /// The name of the property to show as value. /// Thrown when any parameter is null. public KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute(string namePropertyName, string unitPropertyName, string valuePropertyName) : base(namePropertyName, valuePropertyName) { if (unitPropertyName == null) { throw new ArgumentNullException(nameof(unitPropertyName)); } this.unitPropertyName = unitPropertyName; } public override string GetName(object source) { PropertyInfo namePropertyInfo = source.GetType().GetProperty(namePropertyName); if (namePropertyInfo == null) { throw new ArgumentException($"Name property '{namePropertyName}' was not found on type {source.GetType().Name}."); } PropertyInfo unitPropertyInfo = GetUnitPropertyInfo(source); object unit = unitPropertyInfo.GetValue(source, new object[0]); if (unit == null) { throw new ArgumentNullException(nameof(unit)); } return $"{Convert.ToString(namePropertyInfo.GetValue(source, new object[0]))} [{Convert.ToString(unit)}]"; } /// /// Gets the property value from the that is used /// as value. /// /// The source to obtain the property value of. /// The value used as value of the property. /// Thrown when the property used for the value of /// the is not found on the /// or if the value is not of type RoundedDouble /// public override string GetValue(object source) { PropertyInfo valuePropertyInfo = source.GetType().GetProperty(ValuePropertyName); if (valuePropertyInfo == null) { throw new ArgumentException($"Value property '{ValuePropertyName}' was not found on type {source.GetType().Name}."); } object valueProperty = valuePropertyInfo.GetValue(source, new object[0]); if (!(valueProperty is RoundedDouble)) { throw new ArgumentException($"Value property '{ValuePropertyName}' was not of type RoundedDouble."); } var doubleValue = (RoundedDouble) valueProperty; return doubleValue.ToString("0.#####", CultureInfo.CurrentCulture); } /// /// Gets the unit property info from the . /// /// The source to obtain the unit property info of. /// The of the property. /// Thrown when the property used for the unit of /// the is not found on the . /// private PropertyInfo GetUnitPropertyInfo(object source) { PropertyInfo unitPropertyInfo = source.GetType().GetProperty(unitPropertyName); if (unitPropertyInfo == null) { throw new ArgumentException($"Unit property '{unitPropertyName}' was not found on type {source.GetType().Name}."); } return unitPropertyInfo; } } }