// Copyright (C) Stichting Deltares 2017. 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.ComponentModel; using System.Drawing; using System.Windows.Forms; using Core.Common.Controls.DataGrid; using Ringtoets.AssemblyTool.Data; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Forms.Properties; using Ringtoets.Common.Forms.Views; using Ringtoets.Common.Primitives; namespace Ringtoets.Common.Forms.Helpers { /// /// Helper class for updating states of a /// in a . /// public static class FailureMechanismSectionResultRowHelper { /// /// Sets the when the detailed assessment fails. /// /// The current data grid view cell. /// The value representing the simple assessment result type. /// The value representing the probability of the detailed assessment. /// The set for the /// section result. May be null if the section result does not have a calculation set. /// Thrown when is null. public static void SetDetailedAssessmentError(DataGridViewCell dataGridViewCell, SimpleAssessmentResultType simpleAssessmentResult, double detailedAssessmentProbability, ICalculation normativeCalculation) { if (dataGridViewCell == null) { throw new ArgumentNullException(nameof(dataGridViewCell)); } if (simpleAssessmentResult == SimpleAssessmentResultType.NotApplicable || simpleAssessmentResult == SimpleAssessmentResultType.ProbabilityNegligible) { dataGridViewCell.ErrorText = string.Empty; return; } dataGridViewCell.ErrorText = GetDetailedAssessmentError(detailedAssessmentProbability, normativeCalculation); } /// /// Sets the when the detailed assessment fails. /// /// The current data grid view cell. /// The value representing the simple assessment result. /// The value representing the probability of the detailed assessment. /// The set for the /// section result. May be null if the section result does not have a calculation set. /// Thrown when is null. public static void SetDetailedAssessmentError(DataGridViewCell dataGridViewCell, SimpleAssessmentResultValidityOnlyType simpleAssessmentResult, double detailedAssessmentProbability, ICalculation normativeCalculation) { if (dataGridViewCell == null) { throw new ArgumentNullException(nameof(dataGridViewCell)); } if (simpleAssessmentResult == SimpleAssessmentResultValidityOnlyType.NotApplicable) { dataGridViewCell.ErrorText = string.Empty; return; } dataGridViewCell.ErrorText = GetDetailedAssessmentError(detailedAssessmentProbability, normativeCalculation); } /// /// Gets the error text to display when the detailed assessment fails. /// /// The value representing the probability of the detailed assessment. /// The set for the /// section result. May be null if the section result does not have a calculation set. public static string GetDetailedAssessmentError(double detailedAssessmentProbability, ICalculation normativeCalculation) { if (normativeCalculation == null) { return Resources.FailureMechanismResultView_DataGridViewCellFormatting_Calculation_not_set; } CalculationScenarioStatus calculationScenarioStatus = GetCalculationStatus(normativeCalculation, detailedAssessmentProbability); if (calculationScenarioStatus == CalculationScenarioStatus.NotCalculated) { return Resources.FailureMechanismResultView_DataGridViewCellFormatting_Calculation_not_calculated; } if (calculationScenarioStatus == CalculationScenarioStatus.Failed) { return Resources.FailureMechanismResultView_DataGridViewCellFormatting_Calculation_must_have_valid_output; } return string.Empty; } /// /// Helper method that determines whether the simple assessment is sufficient. /// /// The simple assessment result to check. /// true when the simple assessment is /// or , false otherwise. public static bool SimpleAssessmentIsSufficient(SimpleAssessmentResultType simpleAssessmentResult) { return simpleAssessmentResult == SimpleAssessmentResultType.ProbabilityNegligible || simpleAssessmentResult == SimpleAssessmentResultType.NotApplicable; } /// /// Helper method that determines whether the simple assessment is sufficient. /// /// The simple assessment result to check. /// true when the simple assessment is , /// false otherwise. public static bool SimpleAssessmentIsSufficient(SimpleAssessmentResultValidityOnlyType simpleAssessmentResult) { return simpleAssessmentResult == SimpleAssessmentResultValidityOnlyType.NotApplicable; } /// /// Helper method that determines whether the detailed assessment /// is . /// /// The detailed assessment result to check. /// true when the detailed assessment is /// , false otherwise. public static bool DetailedAssessmentResultIsProbability(DetailedAssessmentResultType detailedAssessmentResult) { return detailedAssessmentResult == DetailedAssessmentResultType.Probability; } /// /// Helper method that determines whether the tailor made assessment /// is . /// /// The tailor made assessment result to check. /// true when the tailor made assessment /// is , false otherwise. public static bool TailorMadeAssessmentResultIsProbability(TailorMadeAssessmentProbabilityCalculationResultType tailorMadeAssessmentResult) { return tailorMadeAssessmentResult == TailorMadeAssessmentProbabilityCalculationResultType.Probability; } /// /// Helper method that sets the style of a based on a /// . /// /// The column state definition to set the style for. /// The assembly category group to base the style on. /// Thrown when /// has an invalid value for . /// Thrown when /// is not supported. /// Thrown when /// is null. public static void SetAssemblyCategoryGroupStyle(DataGridViewColumnStateDefinition columnStateDefinition, FailureMechanismSectionAssemblyCategoryGroup assemblyCategoryGroup) { if (columnStateDefinition == null) { throw new ArgumentNullException(nameof(columnStateDefinition)); } columnStateDefinition.Style = new CellStyle( Color.FromKnownColor(KnownColor.ControlText), GetCategoryGroupColor(assemblyCategoryGroup)); } /// /// Helper method that sets the state of the /// based on . /// /// The column state definition to set the state for. /// Indicator whether the column should be disabled or not. /// Thrown when /// is null. public static void SetColumnState(DataGridViewColumnStateDefinition columnStateDefinition, bool shouldDisable) { if (columnStateDefinition == null) { throw new ArgumentNullException(nameof(columnStateDefinition)); } if (shouldDisable) { DisableColumn(columnStateDefinition); } else { EnableColumn(columnStateDefinition); } } /// /// Helper method that enables the . /// /// The column state definition to enable. /// Indicator whether the column should be read-only or not. /// Thrown when /// is null. public static void EnableColumn(DataGridViewColumnStateDefinition columnStateDefinition, bool readOnly = false) { if (columnStateDefinition == null) { throw new ArgumentNullException(nameof(columnStateDefinition)); } columnStateDefinition.ReadOnly = readOnly; columnStateDefinition.Style = CellStyle.Enabled; } /// /// Helper method that disables the . /// /// The column state definition to enable. /// Thrown when /// is null. public static void DisableColumn(DataGridViewColumnStateDefinition columnStateDefinition) { if (columnStateDefinition == null) { throw new ArgumentNullException(nameof(columnStateDefinition)); } columnStateDefinition.ReadOnly = true; columnStateDefinition.Style = CellStyle.Disabled; } /// /// Gets the color for a category group. /// /// The category group to get the color for. /// The corresponding to the given category group. /// Thrown when /// has an invalid value for . /// Thrown when /// is not supported. private static Color GetCategoryGroupColor(FailureMechanismSectionAssemblyCategoryGroup assemblyCategoryGroup) { if (!Enum.IsDefined(typeof(FailureMechanismSectionAssemblyCategoryGroup), assemblyCategoryGroup)) { throw new InvalidEnumArgumentException(nameof(assemblyCategoryGroup), (int) assemblyCategoryGroup, typeof(FailureMechanismSectionAssemblyCategoryGroup)); } switch (assemblyCategoryGroup) { case FailureMechanismSectionAssemblyCategoryGroup.Iv: return Color.FromArgb(0, 255, 0); case FailureMechanismSectionAssemblyCategoryGroup.IIv: return Color.FromArgb(118, 147, 60); case FailureMechanismSectionAssemblyCategoryGroup.IIIv: return Color.FromArgb(255, 255, 0); case FailureMechanismSectionAssemblyCategoryGroup.IVv: return Color.FromArgb(204, 192, 218); case FailureMechanismSectionAssemblyCategoryGroup.Vv: return Color.FromArgb(255, 153, 0); case FailureMechanismSectionAssemblyCategoryGroup.VIv: return Color.FromArgb(255, 0, 0); case FailureMechanismSectionAssemblyCategoryGroup.VIIv: case FailureMechanismSectionAssemblyCategoryGroup.None: case FailureMechanismSectionAssemblyCategoryGroup.NotApplicable: return Color.FromArgb(255, 255, 255); default: throw new NotSupportedException(); } } private static CalculationScenarioStatus GetCalculationStatus(ICalculation calculation, double detailedAssessmentProbability) { if (!calculation.HasOutput) { return CalculationScenarioStatus.NotCalculated; } if (double.IsNaN(detailedAssessmentProbability)) { return CalculationScenarioStatus.Failed; } return CalculationScenarioStatus.Done; } } }