// 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.Controls.Views;
using Ringtoets.Common.Data.Hydraulics.IllustrationPoints;
using Ringtoets.Common.Forms.Properties;
namespace Ringtoets.Common.Forms.Views
{
///
/// Control to show illustration points in a table view.
///
public partial class IllustrationPointsTableControl : UserControl, ISelectionProvider
{
private const int closingSituationColumnIndex = 1;
private GeneralResultSubMechanismIllustrationPoint data;
public event EventHandler SelectionChanged;
///
/// Creates a new instance of .
///
public IllustrationPointsTableControl()
{
InitializeComponent();
InitializeEventHandlers();
}
///
/// Gets or sets the data of the control.
///
public GeneralResultSubMechanismIllustrationPoint Data
{
get
{
return data;
}
set
{
data = value;
illustrationPointsDataGridViewControl.SetDataSource(data != null
? CreateRows()
: null);
UpdateClosingStructureVisibility();
}
}
public object Selection
{
get
{
DataGridViewRow currentRow = illustrationPointsDataGridViewControl.CurrentRow;
return ((IllustrationPointRow) currentRow?.DataBoundItem)?.IllustrationPoint;
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
InitializeDataGridView();
}
private void InitializeEventHandlers()
{
illustrationPointsDataGridViewControl.AddCurrentCellChangedHandler(DataGridViewOnCurrentCellChangedHandler);
}
private void DataGridViewOnCurrentCellChangedHandler(object sender, EventArgs e)
{
OnSelectionChanged();
}
private void UpdateClosingStructureVisibility()
{
if (data != null)
{
if (data.TopLevelSubMechanismIllustrationPoints.Any(ip => ip.ClosingSituation != data.TopLevelSubMechanismIllustrationPoints.First().ClosingSituation))
{
illustrationPointsDataGridViewControl.SetColumnVisibility(closingSituationColumnIndex, true);
return;
}
}
illustrationPointsDataGridViewControl.SetColumnVisibility(closingSituationColumnIndex, false);
}
private void InitializeDataGridView()
{
illustrationPointsDataGridViewControl.AddTextBoxColumn(nameof(IllustrationPointRow.WindDirection),
Resources.IllustrationPoint_WindDirection_DisplayName,
true);
illustrationPointsDataGridViewControl.AddTextBoxColumn(nameof(IllustrationPointRow.ClosingSituation),
Resources.IllustrationPoint_ClosingSituation_DisplayName,
true);
illustrationPointsDataGridViewControl.AddTextBoxColumn(nameof(IllustrationPointRow.Probability),
Resources.IllustrationPoint_CalculatedProbability_DisplayName,
true);
illustrationPointsDataGridViewControl.AddTextBoxColumn(nameof(IllustrationPointRow.Reliability),
Resources.IllustrationPoint_CalculatedReliability_DisplayName,
true);
illustrationPointsDataGridViewControl.SetColumnVisibility(closingSituationColumnIndex, false);
}
private List CreateRows()
{
return data.TopLevelSubMechanismIllustrationPoints
.Select(illustrationPoint => new IllustrationPointRow(illustrationPoint))
.ToList();
}
private void OnSelectionChanged()
{
SelectionChanged?.Invoke(this, EventArgs.Empty);
}
}
}