// Copyright (C) Stichting Deltares 2016. 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.Collections.Generic; using System.Linq; using System.Windows.Forms; using Core.Common.Base; using Core.Common.Controls.DataGrid; using Core.Common.Controls.Views; using Ringtoets.Common.Data.FailureMechanism; using CoreCommonResources = Core.Common.Base.Properties.Resources; using CoreCommonControlsResources = Core.Common.Controls.Properties.Resources; namespace Ringtoets.Common.Forms.Views { /// /// The view for the . /// public abstract partial class FailureMechanismResultView : UserControl, IView where T : FailureMechanismSectionResult { private const int assessmentLayerOneColumnIndex = 1; private readonly IList failureMechanismSectionResultObservers; private readonly Observer failureMechanismObserver; private IEnumerable failureMechanismSectionResult; private IFailureMechanism failureMechanism; /// /// 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 object CreateFailureMechanismSectionResultRow(T sectionResult); /// /// Creates a new instance of . /// protected FailureMechanismResultView() { InitializeComponent(); failureMechanismObserver = new Observer(UpdataDataGridViewDataSource); failureMechanismSectionResultObservers = new List(); } /// /// Sets the failure mechanism. /// public virtual IFailureMechanism FailureMechanism { set { failureMechanism = value; failureMechanismObserver.Observable = failureMechanism; } } public object Data { get { return failureMechanismSectionResult; } set { FailureMechanismSectionResult = value as IEnumerable; if (failureMechanismSectionResult != null) { UpdataDataGridViewDataSource(); } else { DataGridViewControl.SetDataSource(null); } } } protected DataGridViewControl DataGridViewControl { get; private set; } protected override void Dispose(bool disposing) { FailureMechanism = null; FailureMechanismSectionResult = null; if (failureMechanismObserver != null) { failureMechanismObserver.Dispose(); } if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } /// /// Finds out whether the assessment section which is represented by the row at index /// has passed the level 1 assessment. /// /// The index of the row which has a section attached. /// false if assessment level 1 has passed, true otherwise. protected bool HasPassedLevelOne(int rowIndex) { return (bool) DataGridViewControl.GetCell(rowIndex, assessmentLayerOneColumnIndex).Value; } /// /// Updates the data source of the data grid view with the current known failure mechanism section results. /// protected void UpdataDataGridViewDataSource() { UpdateFailureMechanismSectionResultsObservers(); DataGridViewControl.EndEdit(); DataGridViewControl.SetDataSource(failureMechanismSectionResult.Select(CreateFailureMechanismSectionResultRow).ToList()); } /// /// Gets data that is visualized on the row a the given . /// /// The position of the row in the data source. /// The data bound to the row at index . protected object GetDataAtRow(int rowIndex) { return DataGridViewControl.GetRowFromIndex(rowIndex).DataBoundItem; } private IEnumerable FailureMechanismSectionResult { set { failureMechanismSectionResult = value; UpdateFailureMechanismSectionResultsObservers(); } } private void UpdateFailureMechanismSectionResultsObservers() { ClearSectionResultObservers(); if (failureMechanismSectionResult != null) { AddSectionResultObservers(); } } private void AddSectionResultObservers() { foreach (var sectionResult in failureMechanismSectionResult) { failureMechanismSectionResultObservers.Add(new Observer(DataGridViewControl.RefreshDataGridView) { Observable = sectionResult }); } } private void ClearSectionResultObservers() { foreach (var observer in failureMechanismSectionResultObservers) { observer.Dispose(); } failureMechanismSectionResultObservers.Clear(); } } }