// Copyright (C) Stichting Deltares 2016. All rights reserved. // // This file is part of Ringtoets. // // Ringtoets is free software: you can redistribute it and/or modify // it under the terms of the GNU 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 General Public License for more details. // // You should have received a copy of the GNU 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.Collections.Generic; using Core.Common.Base; using Core.Common.Base.Data; using Core.Common.Gui.Attributes; using Core.Common.Gui.PropertyBag; using Core.Common.Utils.Attributes; using Ringtoets.Common.Data.Probabilistics; using Ringtoets.Common.Forms.ChangeHandlers; using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources; namespace Ringtoets.Common.Forms.PropertyClasses { /// /// Properties class for implementations of . /// public abstract class DistributionPropertiesBase : ObjectProperties where TDistribution : IDistribution { private const string meanPropertyName = nameof(Mean); private const string standardDeviationPropertyName = nameof(StandardDeviation); private readonly bool isMeanReadOnly; private readonly bool isStandardDeviationReadOnly; private readonly IObservablePropertyChangeHandler changeHandler; /// /// Creates a new instance of /// in which the properties of are displayed read-only. /// /// The to create the properties for. protected DistributionPropertiesBase(TDistribution distribution) : this(DistributionPropertiesReadOnly.All, distribution, null) {} /// /// Creates a new instance of . /// /// Indicates which properties, if any, should be marked as read-only. /// The to create the properties for. /// The handler responsible for handling effects of a property change. /// Thrown when is null /// Any number of properties in this class is editable and the /// is null. protected DistributionPropertiesBase(DistributionPropertiesReadOnly propertiesReadOnly, TDistribution distribution, IObservablePropertyChangeHandler handler) { if (distribution == null) { throw new ArgumentNullException(nameof(distribution)); } if (!propertiesReadOnly.HasFlag(DistributionPropertiesReadOnly.All)) { if (handler == null) { throw new ArgumentException(@"Change handler required if changes are possible.", nameof(handler)); } } Data = distribution; isMeanReadOnly = propertiesReadOnly.HasFlag(DistributionPropertiesReadOnly.Mean); isStandardDeviationReadOnly = propertiesReadOnly.HasFlag(DistributionPropertiesReadOnly.StandardDeviation); changeHandler = handler; } [PropertyOrder(1)] [ResourcesDisplayName(typeof(RingtoetsCommonFormsResources), nameof(RingtoetsCommonFormsResources.Distribution_DistributionType_DisplayName))] [ResourcesDescription(typeof(RingtoetsCommonFormsResources), nameof(RingtoetsCommonFormsResources.Distribution_DistributionType_Description))] public abstract string DistributionType { get; } [PropertyOrder(2)] [DynamicReadOnly] [ResourcesDisplayName(typeof(RingtoetsCommonFormsResources), nameof(RingtoetsCommonFormsResources.NormalDistribution_Mean_DisplayName))] public virtual RoundedDouble Mean { get { return data.Mean; } set { if (isMeanReadOnly) { throw new InvalidOperationException("Mean is set to be read-only."); } PropertyChangeHelper.ChangePropertyAndNotify(() => data.Mean = value, changeHandler); } } [PropertyOrder(3)] [DynamicReadOnly] [ResourcesDisplayName(typeof(RingtoetsCommonFormsResources), nameof(RingtoetsCommonFormsResources.NormalDistribution_StandardDeviation_DisplayName))] public virtual RoundedDouble StandardDeviation { get { return data.StandardDeviation; } set { if (isStandardDeviationReadOnly) { throw new InvalidOperationException("StandardDeviation is set to be read-only."); } PropertyChangeHelper.ChangePropertyAndNotify(() => data.StandardDeviation = value, changeHandler); } } [DynamicReadOnlyValidationMethod] public bool DynamicReadOnlyValidationMethod(string propertyName) { return propertyName == meanPropertyName ? isMeanReadOnly : propertyName == standardDeviationPropertyName && isStandardDeviationReadOnly; } public override string ToString() { return $"{Mean} ({RingtoetsCommonFormsResources.NormalDistribution_StandardDeviation_DisplayName} = {StandardDeviation})"; } } }