// 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.Common.Data.IllustrationPoints;
using Ringtoets.Common.Forms.GuiServices;
using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources;
namespace Ringtoets.Common.Forms.Views
{
///
/// Base view for views which should be derived in
/// order to get a consistent look and feel.
///
public abstract partial class HydraulicBoundaryLocationsView : LocationsView
{
protected readonly Func getCalculationFunc;
private readonly ObservableList locations;
private readonly Observer hydraulicBoundaryLocationsObserver;
private readonly RecursiveObserver, HydraulicBoundaryLocation> hydraulicBoundaryLocationObserver;
///
/// Creates a new instance of .
///
/// The locations to show in the view.
/// for obtaining a
/// based on .
/// The assessment section which the locations belong to.
/// Thrown when any input parameter is null.
protected HydraulicBoundaryLocationsView(ObservableList locations,
Func getCalculationFunc,
IAssessmentSection assessmentSection)
{
if (locations == null)
{
throw new ArgumentNullException(nameof(locations));
}
if (getCalculationFunc == null)
{
throw new ArgumentNullException(nameof(getCalculationFunc));
}
if (assessmentSection == null)
{
throw new ArgumentNullException(nameof(assessmentSection));
}
AssessmentSection = assessmentSection;
hydraulicBoundaryLocationsObserver = new Observer(UpdateDataGridViewDataSource);
hydraulicBoundaryLocationObserver = new RecursiveObserver, HydraulicBoundaryLocation>(HandleHydraulicBoundaryLocationUpdate, list => list);
this.locations = locations;
this.getCalculationFunc = getCalculationFunc;
hydraulicBoundaryLocationsObserver.Observable = locations;
hydraulicBoundaryLocationObserver.Observable = locations;
UpdateDataGridViewDataSource();
}
public override object Data { get; set; }
///
/// Gets or sets the .
///
public IHydraulicBoundaryLocationCalculationGuiService CalculationGuiService { get; set; }
protected override void Dispose(bool disposing)
{
hydraulicBoundaryLocationsObserver.Dispose();
hydraulicBoundaryLocationObserver.Dispose();
base.Dispose(disposing);
}
protected override void InitializeDataGridView()
{
base.InitializeDataGridView();
dataGridViewControl.AddCheckBoxColumn(nameof(HydraulicBoundaryLocationRow.IncludeIllustrationPoints),
RingtoetsCommonFormsResources.HydraulicBoundaryLocationCalculationInput_IncludeIllustrationPoints_DisplayName);
dataGridViewControl.AddTextBoxColumn(nameof(HydraulicBoundaryLocationRow.Name),
RingtoetsCommonFormsResources.HydraulicBoundaryDatabase_Location_Name_DisplayName);
dataGridViewControl.AddTextBoxColumn(nameof(HydraulicBoundaryLocationRow.Id),
RingtoetsCommonFormsResources.HydraulicBoundaryDatabase_Location_Id_DisplayName);
dataGridViewControl.AddTextBoxColumn(nameof(HydraulicBoundaryLocationRow.Location),
RingtoetsCommonFormsResources.HydraulicBoundaryDatabase_Location_Coordinates_DisplayName);
}
///
/// Handles the calculation of the .
///
/// The enumeration of to use in the calculation.
protected abstract void HandleCalculateSelectedLocations(IEnumerable locations);
protected override void CalculateForSelectedRows()
{
if (CalculationGuiService == null)
{
return;
}
IEnumerable selectedLocations = GetSelectedCalculatableObjects();
HandleCalculateSelectedLocations(selectedLocations);
}
protected override void SetDataSource()
{
dataGridViewControl.SetDataSource(locations?.Select(CreateNewRow).ToArray());
}
protected override IEnumerable GetIllustrationPointControlItems()
{
DataGridViewRow currentRow = dataGridViewControl.CurrentRow;
if (currentRow == null)
{
return Enumerable.Empty();
}
HydraulicBoundaryLocation location = ((HydraulicBoundaryLocationRow) currentRow.DataBoundItem).CalculatableObject;
HydraulicBoundaryLocationCalculation hydraulicBoundaryLocationCalculation = getCalculationFunc(location);
HydraulicBoundaryLocationOutput hydraulicBoundaryLocationOutput = hydraulicBoundaryLocationCalculation.Output;
if (hydraulicBoundaryLocationCalculation.HasOutput
&& hydraulicBoundaryLocationOutput.HasGeneralResult)
{
return hydraulicBoundaryLocationOutput.GeneralResult.TopLevelIllustrationPoints.Select(
topLevelSubMechanismIllustrationPoint =>
{
SubMechanismIllustrationPoint subMechanismIllustrationPoint =
topLevelSubMechanismIllustrationPoint.SubMechanismIllustrationPoint;
return new IllustrationPointControlItem(topLevelSubMechanismIllustrationPoint,
topLevelSubMechanismIllustrationPoint.WindDirection.Name,
topLevelSubMechanismIllustrationPoint.ClosingSituation,
subMechanismIllustrationPoint.Stochasts,
subMechanismIllustrationPoint.Beta);
}).ToArray();
}
return Enumerable.Empty();
}
///
/// Creates a new row that is added to the data table.
///
/// The location for which to create a new row.
/// The newly created row.
/// Thrown when is null or
/// returns null.
private HydraulicBoundaryLocationRow CreateNewRow(HydraulicBoundaryLocation location)
{
return new HydraulicBoundaryLocationRow(location, getCalculationFunc(location));
}
}
}