Index: Ringtoets/StabilityPointStructures/src/Ringtoets.StabilityPointStructures.Plugin/Ringtoets.StabilityPointStructures.Plugin.csproj =================================================================== diff -u -rb70abd70f1cd326d1a753c5879a065149ac2ae84 -rb78eb7cbd59f1bd31e72631b67b7e12b5b4477a5 --- Ringtoets/StabilityPointStructures/src/Ringtoets.StabilityPointStructures.Plugin/Ringtoets.StabilityPointStructures.Plugin.csproj (.../Ringtoets.StabilityPointStructures.Plugin.csproj) (revision b70abd70f1cd326d1a753c5879a065149ac2ae84) +++ Ringtoets/StabilityPointStructures/src/Ringtoets.StabilityPointStructures.Plugin/Ringtoets.StabilityPointStructures.Plugin.csproj (.../Ringtoets.StabilityPointStructures.Plugin.csproj) (revision b78eb7cbd59f1bd31e72631b67b7e12b5b4477a5) @@ -95,6 +95,11 @@ Ringtoets.Common.Service False + + {6A074D65-A81C-4C1C-8E24-F36C916E4ED7} + Ringtoets.Common.Utils + False + {70f8cc9c-5bc8-4fb2-b201-eae7fa8088c2} Ringtoets.HydraRing.Data Index: Ringtoets/StabilityPointStructures/src/Ringtoets.StabilityPointStructures.Plugin/StabilityPointStructuresPlugin.cs =================================================================== diff -u -r9463e81bcdb5801a11cbc391c73961438f697551 -rb78eb7cbd59f1bd31e72631b67b7e12b5b4477a5 --- Ringtoets/StabilityPointStructures/src/Ringtoets.StabilityPointStructures.Plugin/StabilityPointStructuresPlugin.cs (.../StabilityPointStructuresPlugin.cs) (revision 9463e81bcdb5801a11cbc391c73961438f697551) +++ Ringtoets/StabilityPointStructures/src/Ringtoets.StabilityPointStructures.Plugin/StabilityPointStructuresPlugin.cs (.../StabilityPointStructuresPlugin.cs) (revision b78eb7cbd59f1bd31e72631b67b7e12b5b4477a5) @@ -23,6 +23,7 @@ using System.Drawing; using System.Linq; using System.Windows.Forms; +using Core.Common.Base; using Core.Common.Controls.TreeView; using Core.Common.Gui.ContextMenu; using Core.Common.Gui.Plugin; @@ -31,9 +32,11 @@ using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Data.Probability; using Ringtoets.Common.Data.Structures; +using Ringtoets.Common.Forms; using Ringtoets.Common.Forms.Helpers; using Ringtoets.Common.Forms.PresentationObjects; using Ringtoets.Common.Forms.TreeNodeInfos; +using Ringtoets.Common.Utils; using Ringtoets.HydraRing.IO; using Ringtoets.StabilityPointStructures.Data; using Ringtoets.StabilityPointStructures.Forms.PresentationObjects; @@ -373,11 +376,9 @@ var builder = new RingtoetsContextMenuBuilder(Gui.Get(context, treeViewControl)); bool isNestedGroup = parentData is StabilityPointStructuresCalculationGroupContext; - StrictContextMenuItem generateCalculationsItem = CreateGenerateCalculationsItem(context); - if (!isNestedGroup) { - builder.AddCustomItem(generateCalculationsItem) + builder.AddCustomItem(CreateGenerateStabilityPointStructuresCalculationsItem(context)) .AddSeparator(); } @@ -412,18 +413,55 @@ .Build(); } - private static StrictContextMenuItem CreateGenerateCalculationsItem(StabilityPointStructuresCalculationGroupContext nodeData) + private StrictContextMenuItem CreateGenerateStabilityPointStructuresCalculationsItem(StabilityPointStructuresCalculationGroupContext nodeData) { - var generateCalculationsItem = new StrictContextMenuItem( - RingtoetsCommonFormsResources.CalculationGroup_Generate_Scenarios, - Resources.StabilityPointStructuresPlugin_CreateGenerateCalculationsItem_ToolTip, - RingtoetsCommonFormsResources.GenerateScenariosIcon, (o, args) => { }) + ObservableList closingStructures = nodeData.FailureMechanism.StabilityPointStructures; + bool structuresAvailable = closingStructures.Any(); + + string closingStructuresCalculationGroupContextToolTip = structuresAvailable + ? RingtoetsCommonFormsResources.StructuresPlugin_Generate_calculations_for_selected_structures + : RingtoetsCommonFormsResources.StructuresPlugin_No_structures_to_generate_for; + + return new StrictContextMenuItem(RingtoetsCommonFormsResources.CalculationsGroup_Generate_calculations, + closingStructuresCalculationGroupContextToolTip, + RingtoetsCommonFormsResources.GenerateScenariosIcon, + (sender, args) => { ShowStabilityPointStructuresSelectionDialog(nodeData); }) { - Enabled = false + Enabled = structuresAvailable }; - return generateCalculationsItem; } + private void ShowStabilityPointStructuresSelectionDialog(StabilityPointStructuresCalculationGroupContext nodeData) + { + using (var dialog = new StructureSelectionDialog(Gui.MainWindow, nodeData.FailureMechanism.StabilityPointStructures)) + { + dialog.ShowDialog(); + + if (dialog.SelectedItems.Any()) + { + GenerateStabilityPointStructuresCalculations(nodeData.FailureMechanism.SectionResults, dialog.SelectedItems, nodeData.WrappedData.Children); + nodeData.NotifyObservers(); + } + } + } + + private static void GenerateStabilityPointStructuresCalculations(IEnumerable sectionResults, IEnumerable structures, IList calculations) + { + foreach (var structure in structures) + { + var calculation = new StructuresCalculation + { + Name = NamingHelper.GetUniqueName(calculations, structure.Name, c => c.Name), + InputParameters = + { + Structure = (StabilityPointStructure)structure + } + }; + calculations.Add(calculation); + StructuresHelper.Update(sectionResults, calculation); + } + } + private static void CalculationGroupContextOnNodeRemoved(StabilityPointStructuresCalculationGroupContext context, object parentNodeData) { var parentGroupContext = (StabilityPointStructuresCalculationGroupContext) parentNodeData; Index: Ringtoets/StabilityPointStructures/test/Ringtoets.StabilityPointStructures.Forms.Test/TreeNodeInfos/StabilityPointStructuresCalculationGroupContextTreeNodeInfoTest.cs =================================================================== diff -u -ra14d1309ecf82ff5b0385a2f6e3b2c4a216a4184 -rb78eb7cbd59f1bd31e72631b67b7e12b5b4477a5 --- Ringtoets/StabilityPointStructures/test/Ringtoets.StabilityPointStructures.Forms.Test/TreeNodeInfos/StabilityPointStructuresCalculationGroupContextTreeNodeInfoTest.cs (.../StabilityPointStructuresCalculationGroupContextTreeNodeInfoTest.cs) (revision a14d1309ecf82ff5b0385a2f6e3b2c4a216a4184) +++ Ringtoets/StabilityPointStructures/test/Ringtoets.StabilityPointStructures.Forms.Test/TreeNodeInfos/StabilityPointStructuresCalculationGroupContextTreeNodeInfoTest.cs (.../StabilityPointStructuresCalculationGroupContextTreeNodeInfoTest.cs) (revision b78eb7cbd59f1bd31e72631b67b7e12b5b4477a5) @@ -24,10 +24,12 @@ using System.Windows.Forms; using Core.Common.Base; using Core.Common.Base.Geometry; +using Core.Common.Controls.DataGrid; using Core.Common.Controls.TreeView; using Core.Common.Gui; using Core.Common.Gui.Commands; using Core.Common.Gui.ContextMenu; +using Core.Common.Gui.Forms.MainWindow; using Core.Common.Gui.TestUtil.ContextMenu; using Core.Common.TestUtil; using NUnit.Extensions.Forms; @@ -37,8 +39,11 @@ using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.Common.Data.Structures; +using Ringtoets.Common.Forms; +using Ringtoets.Common.Forms.Helpers; using Ringtoets.HydraRing.Data; using Ringtoets.StabilityPointStructures.Data; +using Ringtoets.StabilityPointStructures.Data.TestUtil; using Ringtoets.StabilityPointStructures.Forms.PresentationObjects; using Ringtoets.StabilityPointStructures.Plugin; using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources; @@ -48,6 +53,7 @@ [TestFixture] public class StabilityPointStructuresCalculationGroupContextTreeNodeInfoTest : NUnitFormTest { + private const int contextMenuGenerateCalculationsIndexRootGroup = 0; private const int contextMenuAddCalculationGroupIndexRootGroup = 2; private const int contextMenuAddCalculationIndexRootGroup = 3; private const int contextMenuValidateAllIndexRootGroup = 7; @@ -226,6 +232,12 @@ { // Assert Assert.AreEqual(15, menu.Items.Count); + + TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuGenerateCalculationsIndexRootGroup, + "Genereer &berekeningen...", + "Er zijn geen kunstwerken beschikbaar om berekeningen voor te genereren.", + RingtoetsCommonFormsResources.GenerateScenariosIcon, + false); TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuAddCalculationGroupIndexRootGroup, "&Map toevoegen", "Voeg een nieuwe berekeningsmap toe aan deze berekeningsmap.", @@ -254,6 +266,41 @@ } [Test] + public void ContextMenuStrip_WithoutParentNodeWithStructuresImported_GenerateItemEnabledWithTooltip() + { + // Setup + var group = new CalculationGroup(); + var failureMechanism = new StabilityPointStructuresFailureMechanism(); + failureMechanism.StabilityPointStructures.Add(new TestStabilityPointStructure()); + var assessmentSectionStub = mocks.Stub(); + assessmentSectionStub.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase(); + var groupContext = new StabilityPointStructuresCalculationGroupContext(group, + failureMechanism, + assessmentSectionStub); + var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); + + using (var treeViewControl = new TreeViewControl()) + { + guiMock.Expect(g => g.Get(groupContext, treeViewControl)).Return(menuBuilder); + guiMock.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); + + mocks.ReplayAll(); + + // Call + using (ContextMenuStrip menu = info.ContextMenuStrip(groupContext, null, treeViewControl)) + { + // Assert + Assert.AreEqual(15, menu.Items.Count); + + TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuGenerateCalculationsIndexRootGroup, + "Genereer &berekeningen...", + "Genereer berekeningen op basis van geselecteerde kunstwerken.", + RingtoetsCommonFormsResources.GenerateScenariosIcon); + } + } + } + + [Test] public void ContextMenuStrip_NoFailureMechanismSections_ContextMenuItemCalculateAndValidateAllDisabledAndTooltipSet() { // Setup @@ -658,6 +705,189 @@ } [Test] + public void GivenCalculationsViewGenerateScenariosButtonClicked_WhenStabilityPointStructureSelectedAndDialogClosed_ThenCalculationsAddedWithStabilityPointStructureAssigned() + { + // Given + using (var treeViewControl = new TreeViewControl()) + { + var assessmentSectionMock = mocks.StrictMock(); + + StabilityPointStructure structure1 = new TestStabilityPointStructure("Structure 1"); + StabilityPointStructure structure2 = new TestStabilityPointStructure("Structure 2"); + + var failureMechanism = new StabilityPointStructuresFailureMechanism + { + StabilityPointStructures = + { + structure1, + structure2 + } + }; + + var nodeData = new StabilityPointStructuresCalculationGroupContext(failureMechanism.CalculationsGroup, + failureMechanism, + assessmentSectionMock); + + var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); + var mainWindow = mocks.Stub(); + + guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(menuBuilder); + guiMock.Expect(g => g.MainWindow).Return(mainWindow); + guiMock.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); + + mocks.ReplayAll(); + + plugin.Gui = guiMock; + + DialogBoxHandler = (name, wnd) => + { + var selectionDialog = (StructureSelectionDialog)new FormTester(name).TheObject; + var grid = (DataGridViewControl)new ControlTester("DataGridViewControl", selectionDialog).TheObject; + + grid.Rows[0].Cells[0].Value = true; + + new ButtonTester("DoForSelectedButton", selectionDialog).Click(); + }; + + using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) + { + // When + contextMenu.Items[contextMenuGenerateCalculationsIndexRootGroup].PerformClick(); + + // Then + var stabilityPointStructuresCalculations = failureMechanism.Calculations.OfType>().ToArray(); + Assert.AreEqual(1, stabilityPointStructuresCalculations.Length); + Assert.AreSame(structure1, stabilityPointStructuresCalculations[0].InputParameters.Structure); + } + } + } + + [Test] + public void GivenCalculationsViewGenerateScenariosButtonClicked_WhenCancelButtonClickedAndDialogClosed_ThenCalculationsNotUpdated() + { + // Given + using (var treeViewControl = new TreeViewControl()) + { + var assessmentSectionMock = mocks.StrictMock(); + + StabilityPointStructure structure1 = new TestStabilityPointStructure("Structure 1"); + StabilityPointStructure structure2 = new TestStabilityPointStructure("Structure 2"); + + var failureMechanism = new StabilityPointStructuresFailureMechanism + { + StabilityPointStructures = + { + structure1, + structure2 + } + }; + + var nodeData = new StabilityPointStructuresCalculationGroupContext(failureMechanism.CalculationsGroup, + failureMechanism, + assessmentSectionMock); + + var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); + var mainWindow = mocks.Stub(); + + guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(menuBuilder); + guiMock.Expect(g => g.MainWindow).Return(mainWindow); + guiMock.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); + + mocks.ReplayAll(); + + plugin.Gui = guiMock; + + DialogBoxHandler = (name, wnd) => + { + var selectionDialog = (StructureSelectionDialog)new FormTester(name).TheObject; + var grid = (DataGridViewControl)new ControlTester("DataGridViewControl", selectionDialog).TheObject; + + grid.Rows[0].Cells[0].Value = true; + + new ButtonTester("CustomCancelButton", selectionDialog).Click(); + }; + + using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) + { + // When + contextMenu.Items[contextMenuGenerateCalculationsIndexRootGroup].PerformClick(); + + // Then + Assert.AreEqual(0, failureMechanism.Calculations.OfType>().Count()); + } + } + } + + [Test] + public void GivenScenariosWithExistingCalculationWithSameName_WhenOkButtonClickedAndDialogClosed_ThenCalculationWithUniqueNameAdded() + { + // Given + using (var treeViewControl = new TreeViewControl()) + { + var assessmentSectionMock = mocks.StrictMock(); + + var existingCalculationName = "StabilityPoint structure"; + StabilityPointStructure stabilityPointStructure = new TestStabilityPointStructure(existingCalculationName); + + var failureMechanism = new StabilityPointStructuresFailureMechanism + { + StabilityPointStructures = + { + stabilityPointStructure + }, + CalculationsGroup = + { + Children = + { + new StructuresCalculation + { + Name = existingCalculationName + } + } + } + }; + + var nodeData = new StabilityPointStructuresCalculationGroupContext(failureMechanism.CalculationsGroup, + failureMechanism, + assessmentSectionMock); + + var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); + var mainWindow = mocks.Stub(); + + guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(menuBuilder); + guiMock.Expect(g => g.MainWindow).Return(mainWindow); + guiMock.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); + + mocks.ReplayAll(); + + plugin.Gui = guiMock; + + DialogBoxHandler = (name, wnd) => + { + var selectionDialog = (StructureSelectionDialog)new FormTester(name).TheObject; + var grid = (DataGridViewControl)new ControlTester("DataGridViewControl", selectionDialog).TheObject; + + grid.Rows[0].Cells[0].Value = true; + + new ButtonTester("DoForSelectedButton", selectionDialog).Click(); + }; + + using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) + { + string expectedNewName = NamingHelper.GetUniqueName(failureMechanism.CalculationsGroup.Children, existingCalculationName, c => c.Name); + + // When + contextMenu.Items[contextMenuGenerateCalculationsIndexRootGroup].PerformClick(); + + // Then + var stabilityPointStructuresCalculations = failureMechanism.Calculations.OfType>().ToArray(); + Assert.AreEqual(2, stabilityPointStructuresCalculations.Length); + Assert.AreEqual(expectedNewName, stabilityPointStructuresCalculations[1].Name); + } + } + } + + [Test] public void OnNodeRemoved_NestedCalculationGroup_RemoveGroupAndNotifyObservers() { // Setup