// 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.DataGrid; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Forms.Helpers; using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources; using RingtoetsCommonDataResources = Ringtoets.Common.Data.Properties.Resources; namespace Ringtoets.Common.Forms { /// /// Control for displaying and configuring scenarios. /// public partial class ScenarioSelectionControl : UserControl { private const int calculationsColumnIndex = 1; /// /// Creates a new instance of . /// public ScenarioSelectionControl() { InitializeComponent(); AddDataGridColumns(); } /// /// Update the source of the grid view by adding rows and filling the combo boxes for each row with the allowed calculations. /// /// The type of rows to be added to the grid view. /// The collection of calculations known in the failure mechanism. /// The collection of rows to be added to the grid view. /// The allowed calculations grouped by the name of the failure mechanism sections. /// Thrown when any input parameter is null. public void UpdateDataGridViewDataSource( IEnumerable calculations, IEnumerable scenarioRows, IDictionary> calculationsPerSection) where T : IScenarioRow { if (calculations == null) { throw new ArgumentNullException(nameof(calculations)); } if (scenarioRows == null) { throw new ArgumentNullException(nameof(scenarioRows)); } if (calculationsPerSection == null) { throw new ArgumentNullException(nameof(calculationsPerSection)); } using (new SuspendDataGridViewColumnResizes(dataGridViewControl.GetColumnFromIndex(calculationsColumnIndex))) { dataGridViewControl.SetDataSource(scenarioRows.ToArray()); DataGridViewComboBoxCell.ObjectCollection columnItems = ((DataGridViewComboBoxColumn) dataGridViewControl.GetColumnFromIndex(calculationsColumnIndex)).Items; IEnumerable> items = calculations.Select(c => new DataGridViewComboBoxItemWrapper(c)); SetItemsOnObjectCollection(columnItems, items.Cast().ToArray()); UpdateDataGridViewDataComboBoxesContent(calculationsPerSection); } } /// /// Empties the data grid view. /// public void ClearDataSource() { dataGridViewControl.SetDataSource(null); } /// /// Ends the edit mode of the grid view. /// public void EndEdit() { dataGridViewControl.EndEdit(); } private void UpdateDataGridViewDataComboBoxesContent(IDictionary> calculationsPerSegmentName) { foreach (DataGridViewRow dataGridViewRow in dataGridViewControl.Rows) { FillAvailableCalculationsList(dataGridViewRow, calculationsPerSegmentName); } } private void AddDataGridColumns() { dataGridViewControl.AddTextBoxColumn(nameof(IScenarioRow.Name), RingtoetsCommonFormsResources.Section_DisplayName, true); dataGridViewControl.AddComboBoxColumn>( nameof(IScenarioRow.Calculation), RingtoetsCommonDataResources.ICalculation_DisplayName, null, nameof(DataGridViewComboBoxItemWrapper.WrappedObject), nameof(DataGridViewComboBoxItemWrapper.DisplayName)); } private static void FillAvailableCalculationsList(DataGridViewRow dataGridViewRow, IDictionary> calculationsPerSegmentName) { var rowData = (IScenarioRow) 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)).ToArray()); } 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); } } }