Index: Riskeer/Piping/src/Riskeer.Piping.Forms/PipingSurfaceLineSelectionDialog.Designer.cs =================================================================== diff -u --- Riskeer/Piping/src/Riskeer.Piping.Forms/PipingSurfaceLineSelectionDialog.Designer.cs (revision 0) +++ Riskeer/Piping/src/Riskeer.Piping.Forms/PipingSurfaceLineSelectionDialog.Designer.cs (revision 3b80199ad28e287bc9c008bd8083b49483899f65) @@ -0,0 +1,186 @@ +// Copyright (C) Stichting Deltares 2019. All rights reserved. +// +// This file is part of Riskeer. +// +// Riskeer 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.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using Riskeer.Piping.Primitives; + +namespace Riskeer.Piping.Forms +{ + partial class PipingSurfaceLineSelectionDialog + { + /// + /// Required designer variable. + /// + protected System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.DataGridViewControl = new Core.Common.Controls.DataGrid.DataGridViewControl(); + this.ButtonGroupBox = new System.Windows.Forms.GroupBox(); + this.SelectDeselectPanel = new System.Windows.Forms.Panel(); + this.SelectAllButton = new System.Windows.Forms.Button(); + this.DeselectAllButton = new System.Windows.Forms.Button(); + this.DoForSelectedButton = new System.Windows.Forms.Button(); + this.CustomCancelButton = new System.Windows.Forms.Button(); + this.OkCancelButtonPanel = new System.Windows.Forms.Panel(); + this.ButtonGroupBox.SuspendLayout(); + this.SelectDeselectPanel.SuspendLayout(); + this.OkCancelButtonPanel.SuspendLayout(); + this.SuspendLayout(); + // + // DataGridViewControl + // + this.DataGridViewControl.Dock = System.Windows.Forms.DockStyle.Fill; + this.DataGridViewControl.Location = new System.Drawing.Point(3, 16); + this.DataGridViewControl.MultiSelect = true; + this.DataGridViewControl.Name = "DataGridViewControl"; + this.DataGridViewControl.Padding = new System.Windows.Forms.Padding(0, 0, 0, 5); + this.DataGridViewControl.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.RowHeaderSelect; + this.DataGridViewControl.Size = new System.Drawing.Size(261, 66); + this.DataGridViewControl.TabIndex = 0; + // + // ButtonGroupBox + // + this.ButtonGroupBox.Controls.Add(this.DataGridViewControl); + this.ButtonGroupBox.Controls.Add(this.SelectDeselectPanel); + this.ButtonGroupBox.Dock = System.Windows.Forms.DockStyle.Fill; + this.ButtonGroupBox.Location = new System.Drawing.Point(0, 0); + this.ButtonGroupBox.Name = "ButtonGroupBox"; + this.ButtonGroupBox.Size = new System.Drawing.Size(267, 124); + this.ButtonGroupBox.TabIndex = 7; + this.ButtonGroupBox.TabStop = false; + // + // SelectDeselectPanel + // + this.SelectDeselectPanel.Controls.Add(this.SelectAllButton); + this.SelectDeselectPanel.Controls.Add(this.DeselectAllButton); + this.SelectDeselectPanel.Dock = System.Windows.Forms.DockStyle.Bottom; + this.SelectDeselectPanel.Location = new System.Drawing.Point(3, 82); + this.SelectDeselectPanel.Name = "SelectDeselectPanel"; + this.SelectDeselectPanel.Size = new System.Drawing.Size(261, 39); + this.SelectDeselectPanel.TabIndex = 6; + // + // SelectAllButton + // + this.SelectAllButton.ImeMode = System.Windows.Forms.ImeMode.NoControl; + this.SelectAllButton.Location = new System.Drawing.Point(9, 6); + this.SelectAllButton.Name = "SelectAllButton"; + this.SelectAllButton.Size = new System.Drawing.Size(111, 23); + this.SelectAllButton.TabIndex = 5; + this.SelectAllButton.UseVisualStyleBackColor = true; + this.SelectAllButton.Click += new System.EventHandler(this.SelectAllButton_Click); + // + // DeselectAllButton + // + this.DeselectAllButton.ImeMode = System.Windows.Forms.ImeMode.NoControl; + this.DeselectAllButton.Location = new System.Drawing.Point(126, 6); + this.DeselectAllButton.Name = "DeselectAllButton"; + this.DeselectAllButton.Size = new System.Drawing.Size(111, 23); + this.DeselectAllButton.TabIndex = 4; + this.DeselectAllButton.UseVisualStyleBackColor = true; + this.DeselectAllButton.Click += new System.EventHandler(this.DeselectAllButton_Click); + // + // DoForSelectedButton + // + this.DoForSelectedButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.DoForSelectedButton.Enabled = false; + this.DoForSelectedButton.ImeMode = System.Windows.Forms.ImeMode.NoControl; + this.DoForSelectedButton.Location = new System.Drawing.Point(80, 6); + this.DoForSelectedButton.Name = "DoForSelectedButton"; + this.DoForSelectedButton.Size = new System.Drawing.Size(86, 23); + this.DoForSelectedButton.TabIndex = 3; + this.DoForSelectedButton.UseVisualStyleBackColor = true; + this.DoForSelectedButton.Click += new System.EventHandler(this.DoForSelectedButton_Click); + // + // CustomCancelButton + // + this.CustomCancelButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.CustomCancelButton.ImeMode = System.Windows.Forms.ImeMode.NoControl; + this.CustomCancelButton.Location = new System.Drawing.Point(172, 6); + this.CustomCancelButton.Name = "CustomCancelButton"; + this.CustomCancelButton.Size = new System.Drawing.Size(86, 23); + this.CustomCancelButton.TabIndex = 6; + this.CustomCancelButton.UseVisualStyleBackColor = true; + this.CustomCancelButton.Click += new System.EventHandler(this.CustomCancelButton_Click); + // + // OkCancelButtonPanel + // + this.OkCancelButtonPanel.Controls.Add(this.DoForSelectedButton); + this.OkCancelButtonPanel.Controls.Add(this.CustomCancelButton); + this.OkCancelButtonPanel.Dock = System.Windows.Forms.DockStyle.Bottom; + this.OkCancelButtonPanel.Location = new System.Drawing.Point(0, 124); + this.OkCancelButtonPanel.Name = "OkCancelButtonPanel"; + this.OkCancelButtonPanel.Size = new System.Drawing.Size(267, 35); + this.OkCancelButtonPanel.TabIndex = 8; + // + // SelectionDialogBase + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.AutoScroll = true; + this.AutoScrollMinSize = new System.Drawing.Size(240, 90); + this.ClientSize = new System.Drawing.Size(267, 159); + this.Controls.Add(this.ButtonGroupBox); + this.Controls.Add(this.OkCancelButtonPanel); + this.Name = "SelectionDialogBase"; + this.Text = "SelectionDialogBase"; + this.ButtonGroupBox.ResumeLayout(false); + this.SelectDeselectPanel.ResumeLayout(false); + this.OkCancelButtonPanel.ResumeLayout(false); + this.ResumeLayout(false); + } + + #endregion + + private Core.Common.Controls.DataGrid.DataGridViewControl DataGridViewControl; + private System.Windows.Forms.GroupBox ButtonGroupBox; + private System.Windows.Forms.Panel SelectDeselectPanel; + private System.Windows.Forms.Button SelectAllButton; + private System.Windows.Forms.Button DeselectAllButton; + private System.Windows.Forms.Button DoForSelectedButton; + private System.Windows.Forms.Button CustomCancelButton; + private System.Windows.Forms.Panel OkCancelButtonPanel; + } +} Index: Riskeer/Piping/src/Riskeer.Piping.Forms/PipingSurfaceLineSelectionDialog.cs =================================================================== diff -u -r86594ccd7329d320872573a1d066fe18959d3cea -r3b80199ad28e287bc9c008bd8083b49483899f65 --- Riskeer/Piping/src/Riskeer.Piping.Forms/PipingSurfaceLineSelectionDialog.cs (.../PipingSurfaceLineSelectionDialog.cs) (revision 86594ccd7329d320872573a1d066fe18959d3cea) +++ Riskeer/Piping/src/Riskeer.Piping.Forms/PipingSurfaceLineSelectionDialog.cs (.../PipingSurfaceLineSelectionDialog.cs) (revision 3b80199ad28e287bc9c008bd8083b49483899f65) @@ -23,7 +23,8 @@ using System.Collections.Generic; using System.Linq; using System.Windows.Forms; -using Riskeer.Common.Forms; +using Core.Common.Controls.Dialogs; +using Core.Common.Util.Extensions; using Riskeer.Common.Forms.Views; using Riskeer.Piping.Forms.Properties; using Riskeer.Piping.Primitives; @@ -35,26 +36,126 @@ /// A dialog which allows the user to make a selection from a given set of . Upon /// closing of the dialog, the selected can be obtained. /// - public class PipingSurfaceLineSelectionDialog : SelectionDialogBase + public partial class PipingSurfaceLineSelectionDialog : DialogBase { + private const int selectItemColumnIndex = 0; + /// /// Creates a new instance of . /// /// The parent of the dialog. /// The collection of to show in the dialog. /// Thrown when any parameter is null. public PipingSurfaceLineSelectionDialog(IWin32Window dialogParent, IEnumerable surfaceLines) - : base(dialogParent) + : base(dialogParent, RiskeerCommonFormsResources.GenerateScenariosIcon, 370, 550) { if (surfaceLines == null) { throw new ArgumentNullException(nameof(surfaceLines)); } + InitializeComponent(); + InitializeEventHandlers(); + Localize(); + + SelectedItems = new List(); + Text = Resources.PipingSurfaceLineSelectionDialog_Select_SurfaceLines; - InitializeDataGridView(RiskeerCommonFormsResources.SurfaceLine_DisplayName); + InitializeDataGridView(); - SetDataSource(surfaceLines.Select(sl => new SelectableRow(sl, sl.Name)).ToArray()); + DataGridViewControl.SetDataSource(surfaceLines.Select(sl => new SelectableRow(sl, sl.Name)).ToArray()); } + + /// + /// 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; } + + protected override Button GetCancelButton() + { + return CustomCancelButton; + } + + private void InitializeDataGridView() + { + DataGridViewControl.AddCheckBoxColumn(nameof(SelectableRow.Selected), + RiskeerCommonFormsResources.SelectionDialogBase_ColumnSelect_DisplayName); + DataGridViewControl.AddTextBoxColumn(nameof(SelectableRow.Name), + RiskeerCommonFormsResources.SurfaceLine_DisplayName, true, DataGridViewAutoSizeColumnMode.Fill); + } + + private void Localize() + { + SelectAllButton.Text = RiskeerCommonFormsResources.SelectionDialogBase_SelectionDialogBase_Select_all; + DeselectAllButton.Text = RiskeerCommonFormsResources.SelectionDialogBase_SelectionDialogBase_Deselect_all; + DoForSelectedButton.Text = RiskeerCommonFormsResources.SelectionDialogBase_SelectionDialogBase_Generate; + CustomCancelButton.Text = RiskeerCommonFormsResources.SelectionDialogBase_SelectionDialogBase_Cancel; + } + + private void SetSelectedItems() + { + SelectedItems = GetSelectedItems(); + } + + private IEnumerable> GetSelectableRows() + { + return DataGridViewControl.Rows.Cast().Select(row => row.DataBoundItem).Cast>().ToArray(); + } + + private IEnumerable GetSelectedItems() + { + return GetSelectableRows().Where(row => row.Selected).Select(row => row.Item).ToArray(); + } + + #region Event handling + + private void SelectAllButton_Click(object sender, EventArgs e) + { + GetSelectableRows().ForEachElementDo(row => row.Selected = true); + DataGridViewControl.RefreshDataGridView(); + UpdateDoForSelectedButton(); + } + + private void DeselectAllButton_Click(object sender, EventArgs e) + { + GetSelectableRows().ForEachElementDo(row => row.Selected = false); + DataGridViewControl.RefreshDataGridView(); + UpdateDoForSelectedButton(); + } + + private void InitializeEventHandlers() + { + DataGridViewControl.CellValueChanged += 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 = GetSelectableRows().Any(row => row.Selected); + } + + #endregion } } \ No newline at end of file Index: Riskeer/Piping/test/Riskeer.Piping.Forms.Test/PipingSurfaceLineSelectionDialogTest.cs =================================================================== diff -u -r86594ccd7329d320872573a1d066fe18959d3cea -r3b80199ad28e287bc9c008bd8083b49483899f65 --- Riskeer/Piping/test/Riskeer.Piping.Forms.Test/PipingSurfaceLineSelectionDialogTest.cs (.../PipingSurfaceLineSelectionDialogTest.cs) (revision 86594ccd7329d320872573a1d066fe18959d3cea) +++ Riskeer/Piping/test/Riskeer.Piping.Forms.Test/PipingSurfaceLineSelectionDialogTest.cs (.../PipingSurfaceLineSelectionDialogTest.cs) (revision 3b80199ad28e287bc9c008bd8083b49483899f65) @@ -23,9 +23,9 @@ using System.Linq; using System.Windows.Forms; using Core.Common.Controls.DataGrid; +using Core.Common.Controls.Dialogs; using NUnit.Extensions.Forms; using NUnit.Framework; -using Riskeer.Common.Forms; using Riskeer.Piping.Primitives; namespace Riskeer.Piping.Forms.Test @@ -36,55 +36,60 @@ private const int selectItemColumnIndex = 0; private const int nameColumnIndex = 1; + private Form testForm; + + [SetUp] + public void Setup() + { + testForm = new Form(); + } + + [TearDown] + public void TearDown() + { + testForm.Dispose(); + } + [Test] - public void Constructor_WithoutParent_ThrowsArgumentNullException() + public void Constructor_DialogParentNull_ThrowsArgumentNullException() { // Call - TestDelegate test = () => new PipingSurfaceLineSelectionDialog(null, Enumerable.Empty()); + void Call() => new PipingSurfaceLineSelectionDialog(null, Enumerable.Empty()); // Assert - string parameter = Assert.Throws(test).ParamName; - Assert.AreEqual("dialogParent", parameter); + var exception = Assert.Throws(Call); + Assert.AreEqual("dialogParent", exception.ParamName); } [Test] - public void Constructor_WithoutSurfaceLines_ThrowsArgumentNullException() + public void Constructor_SurfaceLinesNull_ThrowsArgumentNullException() { - // Setup - using (var viewParent = new Form()) - { - // Call - TestDelegate test = () => new PipingSurfaceLineSelectionDialog(viewParent, null); + // Call + void Call() => new PipingSurfaceLineSelectionDialog(testForm, null); - // Assert - string parameter = Assert.Throws(test).ParamName; - Assert.AreEqual("surfaceLines", parameter); - } + // Assert + var exception = Assert.Throws(Call); + Assert.AreEqual("surfaceLines", exception.ParamName); } [Test] - public void Constructor_WithParentAndSurfaceLines_DefaultProperties() + public void Constructor_ExpectedValues() { - // Setup - using (var viewParent = new Form()) + // Call + using (var dialog = new PipingSurfaceLineSelectionDialog(testForm, Enumerable.Empty())) { - // Call - using (var dialog = new PipingSurfaceLineSelectionDialog(viewParent, Enumerable.Empty())) - { - // Assert - Assert.IsInstanceOf>(dialog); - CollectionAssert.IsEmpty(dialog.SelectedItems); - Assert.AreEqual("Selecteer profielschematisaties", dialog.Text); - } + // Assert + Assert.IsInstanceOf(dialog); + CollectionAssert.IsEmpty(dialog.SelectedItems); + Assert.AreEqual("Selecteer profielschematisaties", dialog.Text); } } [Test] public void Constructor_DataGridViewCorrectlyInitialized() { - // Setup & Call - using (var viewParent = new Form()) - using (var dialog = new PipingSurfaceLineSelectionDialog(viewParent, Enumerable.Empty())) + // Call + using (var dialog = new PipingSurfaceLineSelectionDialog(testForm, Enumerable.Empty())) { dialog.Show(); @@ -114,12 +119,11 @@ public void Constructor_SurfaceLinesOneEntry_OneRowInGrid() { // Setup - const string testname = "testName"; - var pipingSurfaceLine = new PipingSurfaceLine(testname); + const string testName = "testName"; + var pipingSurfaceLine = new PipingSurfaceLine(testName); // Call - using (var viewParent = new Form()) - using (var dialog = new PipingSurfaceLineSelectionDialog(viewParent, new[] + using (var dialog = new PipingSurfaceLineSelectionDialog(testForm, new[] { pipingSurfaceLine })) @@ -130,8 +134,182 @@ var dataGridViewControl = (DataGridViewControl) new ControlTester("DataGridViewControl").TheObject; Assert.AreEqual(1, dataGridViewControl.Rows.Count); Assert.IsFalse((bool) dataGridViewControl.Rows[0].Cells[selectItemColumnIndex].Value); - Assert.AreEqual(testname, (string) dataGridViewControl.Rows[0].Cells[nameColumnIndex].Value); + Assert.AreEqual(testName, (string) dataGridViewControl.Rows[0].Cells[nameColumnIndex].Value); } } + + [Test] + public void GivenDialogWithSelectedItems_WhenCloseWithoutConfirmation_ThenReturnsEmptyCollection() + { + // Given + var surfaceLines = new[] + { + new PipingSurfaceLine("surface line 1"), + new PipingSurfaceLine("surface line 2") + }; + + using (var dialog = new PipingSurfaceLineSelectionDialog(testForm, surfaceLines)) + { + var selectionView = (DataGridViewControl) new ControlTester("DataGridViewControl", dialog).TheObject; + + dialog.Show(); + selectionView.Rows[0].Cells[0].Value = true; + + // When + dialog.Close(); + + // Then + CollectionAssert.IsEmpty(dialog.SelectedItems); + } + } + + [Test] + public void GivenDialogWithSelectedItems_WhenCancelButtonClicked_ThenReturnsEmptyCollection() + { + // Given + var surfaceLines = new[] + { + new PipingSurfaceLine("surface line 1"), + new PipingSurfaceLine("surface line 2") + }; + + using (var dialog = new PipingSurfaceLineSelectionDialog(testForm, surfaceLines)) + { + var selectionView = (DataGridViewControl) new ControlTester("DataGridViewControl", dialog).TheObject; + + dialog.Show(); + selectionView.Rows[0].Cells[0].Value = true; + + // When + var cancelButton = new ButtonTester("CustomCancelButton", dialog); + cancelButton.Click(); + + // Then + CollectionAssert.IsEmpty(dialog.SelectedItems); + } + } + + [Test] + public void GivenDialogWithSelectedItems_WhenDoForSelectedButtonClicked_ThenReturnsSelectedCollection() + { + // Given + var selectedSurfaceLine = new PipingSurfaceLine("surface line 1"); + PipingSurfaceLine[] surfaceLines = + { + selectedSurfaceLine, + new PipingSurfaceLine("surface line 2") + }; + + using (var dialog = new PipingSurfaceLineSelectionDialog(testForm, surfaceLines)) + { + var selectionView = (DataGridViewControl) new ControlTester("DataGridViewControl", dialog).TheObject; + + dialog.Show(); + + selectionView.Rows[0].Cells[0].Value = true; + + // When + var generateButton = new ButtonTester("DoForSelectedButton", dialog); + generateButton.Click(); + + // Then + CollectionAssert.AreEqual(new[] + { + selectedSurfaceLine + }, dialog.SelectedItems); + } + } + + [Test] + public void SelectAllButton_SelectAllButtonClicked_AllItemsSelected() + { + // Setup + var surfaceLines = new[] + { + new PipingSurfaceLine("surface line 1"), + new PipingSurfaceLine("surface line 2") + }; + + using (var dialog = new PipingSurfaceLineSelectionDialog(testForm, surfaceLines)) + { + dialog.Show(); + + var dataGridView = (DataGridViewControl) new ControlTester("DataGridViewControl", dialog).TheObject; + DataGridViewRowCollection rows = dataGridView.Rows; + var button = new ButtonTester("SelectAllButton", dialog); + + // Precondition + Assert.IsFalse((bool) rows[0].Cells[selectItemColumnIndex].Value); + Assert.IsFalse((bool) rows[1].Cells[selectItemColumnIndex].Value); + + // Call + button.Click(); + + // Assert + Assert.IsTrue((bool) rows[0].Cells[selectItemColumnIndex].Value); + Assert.IsTrue((bool) rows[1].Cells[selectItemColumnIndex].Value); + } + } + + [Test] + public void DeselectAllButton_AllItemsSelectedDeselectAllButtonClicked_AllItemsNotSelected() + { + // Setup + var surfaceLines = new[] + { + new PipingSurfaceLine("surface line 1"), + new PipingSurfaceLine("surface line 2") + }; + + using (var dialog = new PipingSurfaceLineSelectionDialog(testForm, surfaceLines)) + { + dialog.Show(); + + var dataGridView = (DataGridViewControl) new ControlTester("DataGridViewControl", dialog).TheObject; + DataGridViewRowCollection rows = dataGridView.Rows; + var button = new ButtonTester("DeselectAllButton", dialog); + + foreach (DataGridViewRow row in rows) + { + row.Cells[selectItemColumnIndex].Value = true; + } + + // Precondition + Assert.IsTrue((bool) rows[0].Cells[selectItemColumnIndex].Value); + Assert.IsTrue((bool) rows[1].Cells[selectItemColumnIndex].Value); + + // Call + button.Click(); + + // Assert + Assert.IsFalse((bool) rows[0].Cells[selectItemColumnIndex].Value); + Assert.IsFalse((bool) rows[1].Cells[selectItemColumnIndex].Value); + } + } + + [Test] + public void DoForSelectedButton_NoneSelected_DoForSelectedButtonDisabled() + { + // Setup + var surfaceLines = new[] + { + new PipingSurfaceLine("surface line 1"), + new PipingSurfaceLine("surface line 2") + }; + + using (var dialog = new PipingSurfaceLineSelectionDialog(testForm, surfaceLines)) + { + dialog.Show(); + + var buttonTester = new ButtonTester("DoForSelectedButton", dialog); + + // Call + var button = (Button) buttonTester.TheObject; + + // Assert + Assert.IsFalse(button.Enabled); + CollectionAssert.IsEmpty(dialog.SelectedItems); + } + } } } \ No newline at end of file