// 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; 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 Core.Common.Utils.Reflection; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Forms.Helpers; using Ringtoets.GrassCoverErosionInwards.Data; using Ringtoets.GrassCoverErosionInwards.Utils; using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources; namespace Ringtoets.GrassCoverErosionInwards.Forms.Views { /// /// Creates a new instance of . /// public partial class GrassCoverErosionInwardsScenariosView : UserControl, IView { private const int calculationsColumnIndex = 1; private readonly RecursiveObserver calculationInputObserver; private readonly RecursiveObserver calculationGroupObserver; private readonly Observer failureMechanismObserver; private GrassCoverErosionInwardsFailureMechanism failureMechanism; private CalculationGroup data; /// /// Creates a new instance of the class. /// public GrassCoverErosionInwardsScenariosView() { InitializeComponent(); AddDataGridColumns(); failureMechanismObserver = new Observer(UpdateDataGridViewDataSource); // The concat is needed to observe the input of calculations in child groups. calculationInputObserver = new RecursiveObserver(UpdateDataGridViewDataSource, cg => cg.Children.Concat(cg.Children.OfType().Select(c => c.GetObservableInput()))); calculationGroupObserver = new RecursiveObserver(UpdateDataGridViewDataSource, c => c.Children); } /// /// Gets or sets the failure mechanism. /// public GrassCoverErosionInwardsFailureMechanism FailureMechanism { get { return failureMechanism; } set { failureMechanism = value; failureMechanismObserver.Observable = failureMechanism; UpdateDataGridViewDataSource(); } } public object Data { get { return data; } set { data = value as CalculationGroup; calculationInputObserver.Observable = data; calculationGroupObserver.Observable = data; UpdateDataGridViewDataSource(); } } protected override void OnLoad(EventArgs e) { // Necessary to correctly load the content of the dropdown lists of the comboboxes... UpdateDataGridViewDataSource(); base.OnLoad(e); } protected override void Dispose(bool disposing) { if (failureMechanismObserver != null) { failureMechanismObserver.Dispose(); } calculationInputObserver.Dispose(); calculationGroupObserver.Dispose(); if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } private void AddDataGridColumns() { dataGridViewControl.AddTextBoxColumn(TypeUtils.GetMemberName(sr => sr.Name), RingtoetsCommonFormsResources.FailureMechanismResultView_InitializeDataGridView_Section_name, true); dataGridViewControl.AddComboBoxColumn>( TypeUtils.GetMemberName(sr => sr.Calculation), Properties.Resources.GrassCoverErosionInwardsScenariosView_AddDataGridColumns_Calculation, null, TypeUtils.GetMemberName>(wrapper => wrapper.WrappedObject), TypeUtils.GetMemberName>(wrapper => wrapper.DisplayName)); } private void UpdateDataGridViewDataSource() { dataGridViewControl.EndEdit(); if (FailureMechanism == null || FailureMechanism.SectionResults == null || data == null || data.Children == null) { dataGridViewControl.SetDataSource(null); return; } using (new SuspendDataGridViewColumnResizes(dataGridViewControl.GetColumnFromIndex(calculationsColumnIndex))) { var columnItems = ((DataGridViewComboBoxColumn) dataGridViewControl.GetColumnFromIndex(calculationsColumnIndex)).Items; var items = data.GetCalculations().OfType().Select(c => new DataGridViewComboBoxItemWrapper(c)); SetItemsOnObjectCollection(columnItems, items.Cast().ToArray()); } dataGridViewControl.SetDataSource(FailureMechanism.SectionResults.Select(sectionResult => new GrassCoverErosionInwardsScenarioRow(sectionResult)).ToList()); using (new SuspendDataGridViewColumnResizes(dataGridViewControl.GetColumnFromIndex(calculationsColumnIndex))) { UpdateDataGridViewDataComboBoxesContent(); } } private void UpdateDataGridViewDataComboBoxesContent() { Dictionary> calculationsPerSegmentName = GrassCoverErosionInwardsHelper.CollectCalculationsPerSegment(failureMechanism.SectionResults, data.GetCalculations().OfType()); foreach (DataGridViewRow dataGridViewRow in dataGridViewControl.Rows) { FillAvailableCalculationsList(dataGridViewRow, calculationsPerSegmentName); } } private void FillAvailableCalculationsList(DataGridViewRow dataGridViewRow, Dictionary> calculationsPerSegmentName) { var rowData = (GrassCoverErosionInwardsScenarioRow) dataGridViewRow.DataBoundItem; string sectionName = rowData.Name; var items = new List> { new DataGridViewComboBoxItemWrapper(null) }; if (calculationsPerSegmentName.ContainsKey(sectionName)) { items.AddRange(calculationsPerSegmentName[sectionName].Select(c => new DataGridViewComboBoxItemWrapper(c))); } var cell = (DataGridViewComboBoxCell) dataGridViewRow.Cells[calculationsColumnIndex]; SetItemsOnObjectCollection(cell.Items, items.Cast().ToArray()); } private static void SetItemsOnObjectCollection(DataGridViewComboBoxCell.ObjectCollection objectCollection, object[] comboBoxItems) { objectCollection.Clear(); objectCollection.AddRange(comboBoxItems); } } }