// 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.Collections.Generic; using System.Linq; using System.Windows.Forms; using Core.Common.Base; using Core.Common.Controls.DataGrid; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Forms.Builders; using Ringtoets.Common.Forms.Helpers; using Ringtoets.Common.Forms.Views; using Ringtoets.Common.Primitives; using Ringtoets.GrassCoverErosionInwards.Data; namespace Ringtoets.GrassCoverErosionInwards.Forms.Views { /// /// The view for the . /// public class GrassCoverErosionInwardsFailureMechanismResultView : FailureMechanismResultView { private const int simpleAssessmentResultIndex = 1; private const int detailedAssessmentResultIndex = 2; private const int detailedAssessmentProbabilityIndex = 3; private const int tailorMadeAssessmentResultIndex = 4; private const int tailorMadeAssessmentProbabilityIndex = 5; private const int simpleAssemblyCategoryGroupIndex = 6; private const int detailedAssemblyCategoryGroupIndex = 7; private const int tailorMadeAssemblyCategoryGroupIndex = 8; private const int combinedAssemblyCategoryGroupIndex = 9; private const int combinedAssemblyProbabilityIndex = 10; private const int manualAssemblyProbabilityIndex = 12; private readonly IAssessmentSection assessmentSection; private readonly RecursiveObserver calculationInputObserver; private readonly RecursiveObserver calculationOutputObserver; private readonly RecursiveObserver calculationGroupObserver; /// /// /// Creates a new instance of . /// /// The assessment section the failure mechanism section results belongs to. public GrassCoverErosionInwardsFailureMechanismResultView( IObservableEnumerable failureMechanismSectionResults, GrassCoverErosionInwardsFailureMechanism failureMechanism, IAssessmentSection assessmentSection) : base(failureMechanismSectionResults, failureMechanism) { if (assessmentSection == null) { throw new ArgumentNullException(nameof(assessmentSection)); } this.assessmentSection = assessmentSection; // The concat is needed to observe the input of calculations in child groups. calculationInputObserver = new RecursiveObserver( UpdateDataGridViewDataSource, cg => cg.Children.Concat(cg.Children .OfType() .Select(c => c.InputParameters))); calculationOutputObserver = new RecursiveObserver( UpdateDataGridViewDataSource, cg => cg.Children.Concat(cg.Children .OfType() .Select(c => c.Output))); calculationGroupObserver = new RecursiveObserver( UpdateDataGridViewDataSource, c => c.Children); CalculationGroup observableGroup = failureMechanism.CalculationsGroup; calculationInputObserver.Observable = observableGroup; calculationOutputObserver.Observable = observableGroup; calculationGroupObserver.Observable = observableGroup; FormattingRules = CreateFormattingRules(); } protected IEnumerable> FormattingRules { get; } protected override void Dispose(bool disposing) { DataGridViewControl.CellFormatting -= ShowAssessmentLayerErrors; DataGridViewControl.CellFormatting -= HandleCellStyling; calculationInputObserver.Dispose(); calculationOutputObserver.Dispose(); calculationGroupObserver.Dispose(); base.Dispose(disposing); } protected override GrassCoverErosionInwardsFailureMechanismSectionResultRow CreateFailureMechanismSectionResultRow(GrassCoverErosionInwardsFailureMechanismSectionResult sectionResult) { return new GrassCoverErosionInwardsFailureMechanismSectionResultRow(sectionResult, FailureMechanism, assessmentSection); } protected override void AddDataGridColumns() { FailureMechanismSectionResultViewColumnBuilder.AddSectionNameColumn( DataGridViewControl, nameof(GrassCoverErosionInwardsFailureMechanismSectionResultRow.Name)); FailureMechanismSectionResultViewColumnBuilder.AddSimpleAssessmentResultValidityOnlyColumn( DataGridViewControl, nameof(GrassCoverErosionInwardsFailureMechanismSectionResultRow.SimpleAssessmentResult)); FailureMechanismSectionResultViewColumnBuilder.AddDetailedAssessmentResultColumn( DataGridViewControl, nameof(GrassCoverErosionInwardsFailureMechanismSectionResultRow.DetailedAssessmentResult)); FailureMechanismSectionResultViewColumnBuilder.AddDetailedAssessmentProbabilityColumn( DataGridViewControl, nameof(GrassCoverErosionInwardsFailureMechanismSectionResultRow.DetailedAssessmentProbability)); FailureMechanismSectionResultViewColumnBuilder.AddTailorMadeAssessmentProbabilityCalculationResultColumn( DataGridViewControl, nameof(GrassCoverErosionInwardsFailureMechanismSectionResultRow.TailorMadeAssessmentResult)); FailureMechanismSectionResultViewColumnBuilder.AddTailorMadeAssessmentProbabilityColumn( DataGridViewControl, nameof(GrassCoverErosionInwardsFailureMechanismSectionResultRow.TailorMadeAssessmentProbability)); FailureMechanismSectionResultViewColumnBuilder.AddSimpleAssemblyCategoryGroupColumn( DataGridViewControl, nameof(GrassCoverErosionInwardsFailureMechanismSectionResultRow.SimpleAssemblyCategoryGroup)); FailureMechanismSectionResultViewColumnBuilder.AddDetailedAssemblyCategoryGroupColumn( DataGridViewControl, nameof(GrassCoverErosionInwardsFailureMechanismSectionResultRow.DetailedAssemblyCategoryGroup)); FailureMechanismSectionResultViewColumnBuilder.AddTailorMadeAssemblyCategoryGroupColumn( DataGridViewControl, nameof(GrassCoverErosionInwardsFailureMechanismSectionResultRow.TailorMadeAssemblyCategoryGroup)); FailureMechanismSectionResultViewColumnBuilder.AddCombinedAssemblyCategoryGroupColumn( DataGridViewControl, nameof(GrassCoverErosionInwardsFailureMechanismSectionResultRow.CombinedAssemblyCategoryGroup)); FailureMechanismSectionResultViewColumnBuilder.AddCombinedAssemblyProbabilityColumn( DataGridViewControl, nameof(GrassCoverErosionInwardsFailureMechanismSectionResultRow.CombinedAssemblyProbability)); FailureMechanismSectionResultViewColumnBuilder.AddUseManualAssemblyCategoryGroupColumn( DataGridViewControl, nameof(GrassCoverErosionInwardsFailureMechanismSectionResultRow.UseManualAssemblyProbability)); FailureMechanismSectionResultViewColumnBuilder.AddManualAssemblyProbabilityColumn( DataGridViewControl, nameof(GrassCoverErosionInwardsFailureMechanismSectionResultRow.ManualAssemblyProbability)); } protected override void BindEvents() { base.BindEvents(); DataGridViewControl.CellFormatting += ShowAssessmentLayerErrors; DataGridViewControl.CellFormatting += HandleCellStyling; } private void ShowAssessmentLayerErrors(object sender, DataGridViewCellFormattingEventArgs e) { if (e.ColumnIndex != detailedAssessmentProbabilityIndex) { return; } GrassCoverErosionInwardsFailureMechanismSectionResultRow resultRow = GetDataAtRow(e.RowIndex); DataGridViewCell currentDataGridViewCell = DataGridViewControl.GetCell(e.RowIndex, e.ColumnIndex); GrassCoverErosionInwardsCalculation normativeCalculation = resultRow.GetSectionResultCalculation(); if (resultRow.UseManualAssemblyProbability || resultRow.DetailedAssessmentResult == DetailedAssessmentResultType.NotAssessed) { currentDataGridViewCell.ErrorText = string.Empty; return; } FailureMechanismSectionResultRowHelper.SetDetailedAssessmentError(currentDataGridViewCell, resultRow.SimpleAssessmentResult, resultRow.DetailedAssessmentProbability, normativeCalculation); } private void HandleCellStyling(object sender, DataGridViewCellFormattingEventArgs e) { GrassCoverErosionInwardsFailureMechanismSectionResultRow row = GetDataAtRow(e.RowIndex); IEnumerable> rules = FormattingRules; IEnumerable> formattingRules = rules.Where(r => r.ColumnIndices.Contains(e.ColumnIndex)); if (formattingRules.Any()) { if (formattingRules.Any(formattingRule => formattingRule.Rule(row))) { DataGridViewControl.DisableCell(e.RowIndex, e.ColumnIndex); } else { DataGridViewControl.RestoreCell(e.RowIndex, e.ColumnIndex); } } } private static IEnumerable> CreateFormattingRules() { return new[] { new DataGridViewColumnFormattingRule( new[] { detailedAssessmentResultIndex, detailedAssessmentProbabilityIndex, tailorMadeAssessmentResultIndex, tailorMadeAssessmentProbabilityIndex }, row => FailureMechanismResultViewHelper.SimpleAssessmentIsSufficient(row.SimpleAssessmentResult)), new DataGridViewColumnFormattingRule( new[] { detailedAssessmentProbabilityIndex }, row => row.DetailedAssessmentResult != DetailedAssessmentResultType.Probability), new DataGridViewColumnFormattingRule( new[] { tailorMadeAssessmentProbabilityIndex }, row => row.TailorMadeAssessmentResult != TailorMadeAssessmentProbabilityCalculationResultType.Probability), new DataGridViewColumnFormattingRule( new[] { manualAssemblyProbabilityIndex }, row => !row.UseManualAssemblyProbability), new DataGridViewColumnFormattingRule( new[] { simpleAssessmentResultIndex, detailedAssessmentResultIndex, detailedAssessmentProbabilityIndex, tailorMadeAssessmentResultIndex, tailorMadeAssessmentProbabilityIndex, simpleAssemblyCategoryGroupIndex, detailedAssemblyCategoryGroupIndex, tailorMadeAssemblyCategoryGroupIndex, combinedAssemblyCategoryGroupIndex, combinedAssemblyProbabilityIndex }, row => row.UseManualAssemblyProbability) }; } } }