// 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 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 DuneLocationsView : DuneLocationsViewBase
{
private readonly Observer duneLocationsObserver;
private readonly Observer failureMechanismObserver;
private readonly IObservableEnumerable calculations;
private readonly RecursiveObserver, DuneLocationCalculation> duneLocationObserver;
///
/// 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.
/// Thrown when any parameter is null.
public DuneLocationsView(IObservableEnumerable calculations,
DuneErosionFailureMechanism failureMechanism,
IAssessmentSection assessmentSection)
{
if (calculations == null)
{
throw new ArgumentNullException(nameof(calculations));
}
if (failureMechanism == null)
{
throw new ArgumentNullException(nameof(failureMechanism));
}
if (assessmentSection == null)
{
throw new ArgumentNullException(nameof(assessmentSection));
}
InitializeComponent();
this.calculations = calculations;
FailureMechanism = failureMechanism;
AssessmentSection = assessmentSection;
duneLocationsObserver = new Observer(UpdateDataGridViewDataSource)
{
Observable = calculations
};
duneLocationObserver = 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
/// locations 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)
{
duneLocationsObserver.Dispose();
duneLocationObserver.Dispose();
failureMechanismObserver.Dispose();
base.Dispose(disposing);
}
protected override void InitializeDataGridView()
{
base.InitializeDataGridView();
dataGridViewControl.AddTextBoxColumn(nameof(DuneLocationRow.Name),
RingtoetsCommonFormsResources.HydraulicBoundaryDatabase_Location_Name_DisplayName);
dataGridViewControl.AddTextBoxColumn(nameof(DuneLocationRow.Id),
RingtoetsCommonFormsResources.HydraulicBoundaryDatabase_Location_Id_DisplayName);
dataGridViewControl.AddTextBoxColumn(nameof(DuneLocationRow.Location),
RingtoetsCommonFormsResources.HydraulicBoundaryDatabase_Location_Coordinates_DisplayName);
dataGridViewControl.AddTextBoxColumn(nameof(DuneLocationRow.CoastalAreaId),
Resources.DuneLocation_CoastalAreaId_DisplayName);
dataGridViewControl.AddTextBoxColumn(nameof(DuneLocationRow.Offset),
Resources.DuneLocation_Offset_DisplayName);
dataGridViewControl.AddTextBoxColumn(nameof(DuneLocationRow.WaterLevel),
Resources.DuneLocation_WaterLevel_DisplayName);
dataGridViewControl.AddTextBoxColumn(nameof(DuneLocationRow.WaveHeight),
Resources.DuneLocation_WaveHeight_DisplayName);
dataGridViewControl.AddTextBoxColumn(nameof(DuneLocationRow.WavePeriod),
Resources.DuneLocation_WavePeriod_DisplayName);
dataGridViewControl.AddTextBoxColumn(nameof(DuneLocationRow.D50),
Resources.DuneLocation_D50_DisplayName);
}
protected override object CreateSelectedItemFromCurrentRow()
{
DataGridViewRow currentRow = dataGridViewControl.CurrentRow;
return ((DuneLocationRow) currentRow?.DataBoundItem)?.CalculatableObject;
}
protected override void SetDataSource()
{
dataGridViewControl.SetDataSource(calculations?.Select(calc => new DuneLocationRow(calc)).ToArray());
}
protected override void CalculateForSelectedRows()
{
if (CalculationGuiService != null)
{
IEnumerable selectedCalculations = GetSelectedCalculatableObjects();
HandleCalculateSelectedLocations(selectedCalculations);
}
}
protected override string ValidateCalculatableObjects()
{
if (FailureMechanism != null && FailureMechanism.Contribution <= 0)
{
return RingtoetsCommonFormsResources.Contribution_of_failure_mechanism_zero;
}
return base.ValidateCalculatableObjects();
}
private void HandleCalculateSelectedLocations(IEnumerable calculations)
{
CalculationGuiService.Calculate(calculations,
AssessmentSection.HydraulicBoundaryDatabase.FilePath,
AssessmentSection.HydraulicBoundaryDatabase.EffectivePreprocessorDirectory(),
FailureMechanism.GetMechanismSpecificNorm(AssessmentSection.FailureMechanismContribution.Norm));
}
}
}