// 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.Controls.Views; using Core.Common.Utils.Extensions; using Core.Common.Utils.Reflection; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.Common.Forms.GuiServices; using Ringtoets.HydraRing.Data; using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources; namespace Ringtoets.Common.Forms.Views { /// /// Base view for views which should be derived in order to get a consistent look and feel. /// /// The type of the row objects which are shown in the data table. public abstract partial class HydraulicBoundaryLocationsView : UserControl, ISelectionProvider where T : HydraulicBoundaryLocationRow { private const int locationCalculateColumnIndex = 0; private bool updatingDataSource; private IEnumerable locations; public event EventHandler SelectionChanged; /// /// Creates a new instance of . /// protected HydraulicBoundaryLocationsView() { InitializeComponent(); LocalizeControls(); InitializeEventHandlers(); } /// /// Gets or sets the . /// public IHydraulicBoundaryLocationCalculationGuiService CalculationGuiService { get; set; } /// /// Gets or sets the . /// public abstract IAssessmentSection AssessmentSection { get; set; } public virtual object Data { get { return locations; } set { locations = value as IEnumerable; UpdateDataGridViewDataSource(); } } public object Selection { get { return CreateSelectedItemFromCurrentRow(); } } protected override void OnLoad(EventArgs e) { base.OnLoad(e); InitializeDataGridView(); } protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } /// /// Updates the data source of the data table based on the . /// protected void UpdateDataGridViewDataSource() { updatingDataSource = true; SetDataSource(); updatingDataSource = false; UpdateCalculateForSelectedButton(); } /// /// Initializes the . /// protected virtual void InitializeDataGridView() { dataGridViewControl.AddCheckBoxColumn(TypeUtils.GetMemberName(row => row.ToCalculate), RingtoetsCommonFormsResources.HydraulicBoundaryLocationsView_Calculate); dataGridViewControl.AddTextBoxColumn(TypeUtils.GetMemberName(row => row.Name), RingtoetsCommonFormsResources.HydraulicBoundaryDatabase_Location_Name_DisplayName); dataGridViewControl.AddTextBoxColumn(TypeUtils.GetMemberName(row => row.Id), RingtoetsCommonFormsResources.HydraulicBoundaryDatabase_Location_Id_DisplayName); dataGridViewControl.AddTextBoxColumn(TypeUtils.GetMemberName(row => row.Location), RingtoetsCommonFormsResources.HydraulicBoundaryDatabase_Location_Coordinates_DisplayName); } /// /// Creates a new row that is added to the data table. /// /// The location for which to create a new row. /// The newly created row. protected abstract T CreateNewRow(HydraulicBoundaryLocation location); /// /// Creates a new object that is used as the object for from /// the currently selected row in the data table. /// /// The newly created object. protected abstract object CreateSelectedItemFromCurrentRow(); /// /// Handles the calculation of the . /// /// The enumeration of to use in the calculation. protected abstract void HandleCalculateSelectedLocations(IEnumerable locations); private void LocalizeControls() { CalculateForSelectedButton.Text = RingtoetsCommonFormsResources.HydraulicBoundaryLocationsView_CalculateForSelectedButton_Text; DeselectAllButton.Text = RingtoetsCommonFormsResources.HydraulicBoundaryLocationsView_DeselectAllButton_Text; SelectAllButton.Text = RingtoetsCommonFormsResources.HydraulicBoundaryLocationsView_SelectAllButton_Text; ButtonGroupBox.Text = RingtoetsCommonFormsResources.HydraulicBoundaryLocationsView_ButtonGroupBox_Text; } private IEnumerable GetHydraulicBoundaryLocationContextRows() { return dataGridViewControl.Rows.Cast().Select(row => (T) row.DataBoundItem); } /// /// Sets the datasource on the . /// private void SetDataSource() { dataGridViewControl.SetDataSource(locations != null ? locations.Select(CreateNewRow).ToArray() : null); } private void UpdateCalculateForSelectedButton() { CalculateForSelectedButton.Enabled = GetHydraulicBoundaryLocationContextRows().Any(r => r.ToCalculate); } private void InitializeEventHandlers() { dataGridViewControl.AddCellClickHandler(DataGridViewOnCellClick); dataGridViewControl.AddCellValueChangedHandler(DataGridViewCellValueChanged); } private void DataGridViewCellValueChanged(object sender, DataGridViewCellEventArgs e) { if (updatingDataSource || e.ColumnIndex != locationCalculateColumnIndex) { return; } UpdateCalculateForSelectedButton(); } private void DataGridViewOnCellClick(object sender, DataGridViewCellEventArgs e) { if (updatingDataSource) { return; } OnSelectionChanged(); } private void OnSelectionChanged() { if (SelectionChanged != null) { SelectionChanged(this, new EventArgs()); } } private IEnumerable GetSelectedHydraulicBoundaryLocationContext() { return GetHydraulicBoundaryLocationContextRows().Where(r => r.ToCalculate).Select(r => r.HydraulicBoundaryLocation); } #region Event handling private void SelectAllButton_Click(object sender, EventArgs e) { GetHydraulicBoundaryLocationContextRows().ForEachElementDo(row => row.ToCalculate = true); dataGridViewControl.RefreshDataGridView(); UpdateCalculateForSelectedButton(); } private void DeselectAllButton_Click(object sender, EventArgs e) { GetHydraulicBoundaryLocationContextRows().ForEachElementDo(row => row.ToCalculate = false); dataGridViewControl.RefreshDataGridView(); UpdateCalculateForSelectedButton(); } private void CalculateForSelectedButton_Click(object sender, EventArgs e) { if (CalculationGuiService == null) { return; } var selectedLocations = GetSelectedHydraulicBoundaryLocationContext(); HandleCalculateSelectedLocations(selectedLocations); } #endregion } }