// 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; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using Core.Common.Controls.Dialogs; using Core.Common.Utils.Extensions; using Core.Common.Utils.Reflection; using Ringtoets.Common.Forms.Properties; using Ringtoets.Common.Forms.Views; namespace Ringtoets.Common.Forms { /// /// Base class for selection dialogs which should be derived in order to get a consistent look and feel. /// public abstract partial class SelectionDialogBase : DialogBase where T : class { private const int selectItemColumnIndex = 0; /// /// Creates a new instance of . /// /// The owner of the dialog. /// Thrown when is null. protected SelectionDialogBase(IWin32Window dialogParent) : base(dialogParent, Resources.GenerateScenariosIcon, 370, 550) { InitializeComponent(); InitializeEventHandlers(); SelectedItems = new List(); } /// /// Gets a collection of selected if they were selected /// in the dialog and a confirmation was given. If no confirmation was given or no /// was selected, then an empty collection is returned. /// public IEnumerable SelectedItems { get; private set; } /// /// Sets the datasource on the . /// protected void SetDataSource(object data) { DataGridViewControl.SetDataSource(data); } protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } protected override Button GetCancelButton() { return CustomCancelButton; } private void SetSelectedItems() { SelectedItems = GetSelectedItems(); } private IEnumerable> GetSelectableRow() { return DataGridViewControl.Rows.Cast().Select(row => row.DataBoundItem).Cast>(); } private IEnumerable GetSelectedItems() { return GetSelectableRow().Where(row => row.Selected).Select(row => row.Item).ToArray(); } /// /// Initializes the . /// protected void InitializeDataGridView(string nameColumnHeader) { DataGridViewControl.AddCheckBoxColumn(TypeUtils.GetMemberName>(row => row.Selected), Resources.SelectionDialogBase_ColumnSelect_DisplayName); DataGridViewControl.AddTextBoxColumn(TypeUtils.GetMemberName>(row => row.Name), nameColumnHeader, true, DataGridViewAutoSizeColumnMode.Fill); } #region Event handling private void SelectAllButton_Click(object sender, EventArgs e) { GetSelectableRow().ForEachElementDo(row => row.Selected = true); DataGridViewControl.RefreshDataGridView(); UpdateDoForSelectedButton(); } private void DeselectAllButton_Click(object sender, EventArgs e) { GetSelectableRow().ForEachElementDo(row => row.Selected = false); DataGridViewControl.RefreshDataGridView(); UpdateDoForSelectedButton(); } private void InitializeEventHandlers() { DataGridViewControl.AddCellValueChangedHandler(DataGridViewCellValueChanged); } private void DataGridViewCellValueChanged(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex != selectItemColumnIndex) { return; } UpdateDoForSelectedButton(); } private void DoForSelectedButton_Click(object sender, EventArgs e) { SetSelectedItems(); Close(); } private void CustomCancelButton_Click(object sender, EventArgs e) { Close(); } private void UpdateDoForSelectedButton() { DoForSelectedButton.Enabled = GetSelectableRow().Any(row => row.Selected); } #endregion } }