// 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.Linq; using System.Windows.Forms; using Core.Common.Base; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.Common.Data.Hydraulics; using Ringtoets.DuneErosion.Data; using Ringtoets.DuneErosion.Forms.GuiServices; using Ringtoets.DuneErosion.Forms.Properties; using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources; namespace Ringtoets.DuneErosion.Forms.Views { /// /// View for the . /// public partial class DuneLocationCalculationsView : DuneLocationCalculationsViewBase { private readonly Observer failureMechanismObserver; private readonly Observer duneLocationCalculationsObserver; private readonly IObservableEnumerable calculations; private readonly Func getNormFunc; private readonly RecursiveObserver, DuneLocationCalculation> duneLocationCalculationObserver; /// /// Creates a new instance of . /// /// The calculations to show in the view. /// The failure mechanism which the calculations belong to. /// The assessment section which the calculations belong to. /// for getting the norm to use during calculations. /// Thrown when any parameter is null. public DuneLocationCalculationsView(IObservableEnumerable calculations, DuneErosionFailureMechanism failureMechanism, IAssessmentSection assessmentSection, Func getNormFunc) { if (calculations == null) { throw new ArgumentNullException(nameof(calculations)); } if (failureMechanism == null) { throw new ArgumentNullException(nameof(failureMechanism)); } if (assessmentSection == null) { throw new ArgumentNullException(nameof(assessmentSection)); } if (getNormFunc == null) { throw new ArgumentNullException(nameof(getNormFunc)); } InitializeComponent(); this.calculations = calculations; this.getNormFunc = getNormFunc; FailureMechanism = failureMechanism; AssessmentSection = assessmentSection; duneLocationCalculationsObserver = new Observer(UpdateDataGridViewDataSource) { Observable = calculations }; duneLocationCalculationObserver = new RecursiveObserver, DuneLocationCalculation>(dataGridViewControl.RefreshDataGridView, list => list) { Observable = calculations }; failureMechanismObserver = new Observer(UpdateCalculateForSelectedButton) { Observable = failureMechanism }; UpdateDataGridViewDataSource(); } public override object Data { get; set; } /// /// Gets the assessment section. /// public IAssessmentSection AssessmentSection { get; } /// /// Gets the for which the /// calculations are shown. /// public DuneErosionFailureMechanism FailureMechanism { get; } /// /// Gets or sets the /// to perform calculations with. /// public DuneLocationCalculationGuiService CalculationGuiService { get; set; } protected override void Dispose(bool disposing) { duneLocationCalculationsObserver.Dispose(); duneLocationCalculationObserver.Dispose(); failureMechanismObserver.Dispose(); base.Dispose(disposing); } protected override void InitializeDataGridView() { base.InitializeDataGridView(); dataGridViewControl.AddTextBoxColumn(nameof(DuneLocationCalculationRow.Name), RingtoetsCommonFormsResources.HydraulicBoundaryDatabase_Location_Name_DisplayName); dataGridViewControl.AddTextBoxColumn(nameof(DuneLocationCalculationRow.Id), RingtoetsCommonFormsResources.HydraulicBoundaryDatabase_Location_Id_DisplayName); dataGridViewControl.AddTextBoxColumn(nameof(DuneLocationCalculationRow.Location), RingtoetsCommonFormsResources.HydraulicBoundaryDatabase_Location_Coordinates_DisplayName); dataGridViewControl.AddTextBoxColumn(nameof(DuneLocationCalculationRow.CoastalAreaId), Resources.DuneLocation_CoastalAreaId_DisplayName); dataGridViewControl.AddTextBoxColumn(nameof(DuneLocationCalculationRow.Offset), Resources.DuneLocation_Offset_DisplayName); dataGridViewControl.AddTextBoxColumn(nameof(DuneLocationCalculationRow.WaterLevel), Resources.DuneLocationCalculationOutput_WaterLevel_DisplayName); dataGridViewControl.AddTextBoxColumn(nameof(DuneLocationCalculationRow.WaveHeight), Resources.DuneLocationCalculationOutput_WaveHeight_DisplayName); dataGridViewControl.AddTextBoxColumn(nameof(DuneLocationCalculationRow.WavePeriod), Resources.DuneLocationCalculationOutput_WavePeriod_DisplayName); dataGridViewControl.AddTextBoxColumn(nameof(DuneLocationCalculationRow.D50), Resources.DuneLocation_D50_DisplayName); } protected override object CreateSelectedItemFromCurrentRow() { DataGridViewRow currentRow = dataGridViewControl.CurrentRow; return ((DuneLocationCalculationRow) currentRow?.DataBoundItem)?.CalculatableObject; } protected override void SetDataSource() { dataGridViewControl.SetDataSource(calculations?.Select(calc => new DuneLocationCalculationRow(calc)).ToArray()); } protected override void CalculateForSelectedRows() { CalculationGuiService?.Calculate(GetSelectedCalculatableObjects(), AssessmentSection.HydraulicBoundaryDatabase.FilePath, AssessmentSection.HydraulicBoundaryDatabase.EffectivePreprocessorDirectory(), getNormFunc()); } protected override string ValidateCalculatableObjects() { if (FailureMechanism != null && FailureMechanism.Contribution <= 0) { return RingtoetsCommonFormsResources.Contribution_of_failure_mechanism_zero; } return base.ValidateCalculatableObjects(); } } }