// 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.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using Core.Common.Base;
using Core.Common.Controls.DataGrid;
using Core.Common.Controls.Views;
using Core.Common.Util.Extensions;
using Ringtoets.AssemblyTool.Data;
using Ringtoets.Common.Data.Exceptions;
using Ringtoets.Common.Data.FailureMechanism;
using Ringtoets.Common.Forms.Controls;
using Ringtoets.Common.Forms.Properties;
using CoreCommonGuiResources = Core.Common.Gui.Properties.Resources;
namespace Ringtoets.Common.Forms.Views
{
///
/// The view for the .
///
/// The type of results which are presented by the
/// .
/// The type of the row that is used to show the data.
/// The type of the failure mechanism this view belongs to.
/// The type of the assembly result control in this view.
public abstract partial class FailureMechanismResultView : UserControl, IView
where TSectionResult : FailureMechanismSectionResult
where TSectionResultRow : FailureMechanismSectionResultRow
where TFailureMechanism : IFailureMechanism
where TAssemblyResultControl : AssemblyResultControl, new()
{
private readonly IObservableEnumerable failureMechanismSectionResults;
private readonly Observer failureMechanismObserver;
private readonly Observer failureMechanismSectionResultObserver;
private readonly RecursiveObserver, TSectionResult> failureMechanismSectionResultsObserver;
protected TAssemblyResultControl FailureMechanismAssemblyResultControl;
private IEnumerable sectionResultRows;
private bool rowUpdating;
///
/// Creates a new instance of .
///
/// The collection of to
/// show in the view.
/// The failure mechanism this view belongs to.
/// Thrown when any parameter is null.
protected FailureMechanismResultView(IObservableEnumerable failureMechanismSectionResults, TFailureMechanism failureMechanism)
{
if (failureMechanism == null)
{
throw new ArgumentNullException(nameof(failureMechanism));
}
if (failureMechanismSectionResults == null)
{
throw new ArgumentNullException(nameof(failureMechanismSectionResults));
}
InitializeComponent();
InitializeFailureMechanismAssemblyResultPanel();
FailureMechanism = failureMechanism;
this.failureMechanismSectionResults = failureMechanismSectionResults;
failureMechanismObserver = new Observer(UpdateSectionResultRows)
{
Observable = failureMechanism
};
failureMechanismSectionResultObserver = new Observer(UpdateView)
{
Observable = failureMechanismSectionResults
};
failureMechanismSectionResultsObserver = new RecursiveObserver, TSectionResult>(
UpdateSectionResultRows,
sr => sr)
{
Observable = failureMechanismSectionResults
};
}
///
/// Gets the failure mechanism.
///
public TFailureMechanism FailureMechanism { get; }
public object Data { get; set; }
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
AddDataGridColumns();
DataGridViewControl.CellFormatting += HandleCellStyling;
UpdateView();
}
///
/// Creates a display object for which is added to the
/// on the .
///
/// The for which to create a
/// display object.
/// A display object which can be added as a row to the .
protected abstract TSectionResultRow CreateFailureMechanismSectionResultRow(TSectionResult sectionResult);
protected override void Dispose(bool disposing)
{
failureMechanismObserver.Dispose();
failureMechanismSectionResultObserver.Dispose();
failureMechanismSectionResultsObserver.Dispose();
DataGridViewControl.CellFormatting -= HandleCellStyling;
RemoveSectionResultRowEvents();
if (disposing)
{
components?.Dispose();
}
base.Dispose(disposing);
}
///
/// Adds the columns to the view.
///
protected abstract void AddDataGridColumns();
///
/// Updates the content of the .
///
/// Thrown when the assembly result
/// could not be created.
protected abstract void UpdateAssemblyResultControl();
///
/// Updates all controls in the view.
///
protected void UpdateView()
{
UpdateDataGridViewDataSource();
UpdateFailureMechanismAssemblyResultControl();
}
///
/// Updates the data source of the data grid view with the current known failure mechanism section results.
///
private void UpdateDataGridViewDataSource()
{
DataGridViewControl.EndEdit();
RemoveSectionResultRowEvents();
sectionResultRows = failureMechanismSectionResults
.Select(CreateFailureMechanismSectionResultRow)
.Where(sr => sr != null)
.ToList();
DataGridViewControl.SetDataSource(sectionResultRows);
sectionResultRows?.ForEachElementDo(row =>
{
row.RowUpdated += RowUpdated;
row.RowUpdateDone += RowUpdateDone;
});
}
private void UpdateFailureMechanismAssemblyResultControl()
{
FailureMechanismAssemblyResultControl.ClearData();
FailureMechanismAssemblyResultControl.ClearError();
try
{
UpdateAssemblyResultControl();
}
catch (AssemblyException e)
{
FailureMechanismAssemblyResultControl.SetError(e.Message);
}
}
private void InitializeFailureMechanismAssemblyResultPanel()
{
infoIcon.BackgroundImage = CoreCommonGuiResources.information;
toolTip.SetToolTip(infoIcon, Resources.FailureMechanismResultView_InfoToolTip);
FailureMechanismAssemblyResultControl = new TAssemblyResultControl();
TableLayoutPanel.Controls.Add(FailureMechanismAssemblyResultControl, 1, 0);
}
private void RemoveSectionResultRowEvents()
{
sectionResultRows?.ForEachElementDo(row =>
{
row.RowUpdated -= RowUpdated;
row.RowUpdateDone -= RowUpdateDone;
});
}
private void RowUpdateDone(object sender, EventArgs eventArgs)
{
rowUpdating = false;
}
private void RowUpdated(object sender, EventArgs eventArgs)
{
rowUpdating = true;
DataGridViewControl.RefreshDataGridView();
UpdateFailureMechanismAssemblyResultControl();
}
private void HandleCellStyling(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewControl.FormatCellWithColumnStateDefinition(e.RowIndex, e.ColumnIndex);
}
private void UpdateSectionResultRows()
{
if (rowUpdating)
{
return;
}
sectionResultRows.ForEachElementDo(row => row.Update());
DataGridViewControl.RefreshDataGridView();
UpdateFailureMechanismAssemblyResultControl();
}
}
}