Index: Riskeer/Common/src/Riskeer.Common.Forms/TreeNodeInfos/RiskeerContextMenuItemFactory.cs
===================================================================
diff -u -ra54668f67eba8b3498b65e9117a2e0af0b9840c8 -re3e21a866796ff70c0b7b28dfba268aae164ff33
--- Riskeer/Common/src/Riskeer.Common.Forms/TreeNodeInfos/RiskeerContextMenuItemFactory.cs (.../RiskeerContextMenuItemFactory.cs) (revision a54668f67eba8b3498b65e9117a2e0af0b9840c8)
+++ Riskeer/Common/src/Riskeer.Common.Forms/TreeNodeInfos/RiskeerContextMenuItemFactory.cs (.../RiskeerContextMenuItemFactory.cs) (revision e3e21a866796ff70c0b7b28dfba268aae164ff33)
@@ -294,23 +294,26 @@
///
/// Creates a which is bound to the action of clearing the output of all calculations in a failure mechanism.
///
- /// The failure mechanism to clear the output for.
+ /// The function to determine whether the context menu item should be enabled.
+ /// Object responsible for clearing the calculation output.
/// The created .
- public static StrictContextMenuItem CreateClearAllCalculationOutputInFailureMechanismItem(IFailureMechanism failureMechanism)
+ public static StrictContextMenuItem CreateClearAllCalculationOutputInFailureMechanismItem(
+ Func isContextItemEnabledFunc, IClearCalculationOutputChangeHandler changeHandler)
{
- var clearAllItem = new StrictContextMenuItem(
+ bool enabled = isContextItemEnabledFunc();
+
+ string toolTip = enabled
+ ? Resources.Clear_all_output_ToolTip
+ : Resources.CalculationGroup_ClearOutput_No_calculation_with_output_to_clear;
+
+ return new StrictContextMenuItem(
Resources.Clear_all_output,
- Resources.Clear_all_output_ToolTip,
+ toolTip,
Resources.ClearIcon,
- (o, args) => ClearAllCalculationOutputInFailureMechanism(failureMechanism));
-
- if (!failureMechanism.Calculations.Any(c => c.HasOutput))
+ (o, args) => ClearAllCalculationOutputInFailureMechanism(changeHandler))
{
- clearAllItem.Enabled = false;
- clearAllItem.ToolTipText = Resources.CalculationGroup_ClearOutput_No_calculation_with_output_to_clear;
- }
-
- return clearAllItem;
+ Enabled = enabled
+ };
}
///
@@ -640,18 +643,13 @@
}
}
- private static void ClearAllCalculationOutputInFailureMechanism(IFailureMechanism failureMechanism)
+ private static void ClearAllCalculationOutputInFailureMechanism(IClearCalculationOutputChangeHandler changeHandler)
{
- if (MessageBox.Show(Resources.FailureMechanism_ContextMenuStrip_Are_you_sure_clear_all_output, BaseResources.Confirm, MessageBoxButtons.OKCancel) != DialogResult.OK)
+ if (changeHandler.InquireConfirmation())
{
- return;
+ IEnumerable affectedObjects = changeHandler.ClearCalculations();
+ affectedObjects.ForEachElementDo(o => o.NotifyObservers());
}
-
- foreach (ICalculation calc in failureMechanism.Calculations.Where(c => c.HasOutput))
- {
- calc.ClearOutput();
- calc.NotifyObservers();
- }
}
private static void CreateCalculationGroup(CalculationGroup calculationGroup)
Index: Riskeer/Common/test/Riskeer.Common.Forms.Test/TreeNodeInfos/RiskeerContextMenuItemFactoryTest.cs
===================================================================
diff -u -ra54668f67eba8b3498b65e9117a2e0af0b9840c8 -re3e21a866796ff70c0b7b28dfba268aae164ff33
--- Riskeer/Common/test/Riskeer.Common.Forms.Test/TreeNodeInfos/RiskeerContextMenuItemFactoryTest.cs (.../RiskeerContextMenuItemFactoryTest.cs) (revision a54668f67eba8b3498b65e9117a2e0af0b9840c8)
+++ Riskeer/Common/test/Riskeer.Common.Forms.Test/TreeNodeInfos/RiskeerContextMenuItemFactoryTest.cs (.../RiskeerContextMenuItemFactoryTest.cs) (revision e3e21a866796ff70c0b7b28dfba268aae164ff33)
@@ -312,18 +312,12 @@
{
// Setup
var mocks = new MockRepository();
- var calculationWithOutput = mocks.StrictMock();
- calculationWithOutput.Expect(c => c.HasOutput).Return(true);
- var failureMechanism = mocks.StrictMock();
- failureMechanism.Expect(fm => fm.Calculations).Return(new[]
- {
- calculationWithOutput
- });
-
+ var changeHandler = mocks.Stub();
mocks.ReplayAll();
// Call
- StrictContextMenuItem toolStripItem = RiskeerContextMenuItemFactory.CreateClearAllCalculationOutputInFailureMechanismItem(failureMechanism);
+ StrictContextMenuItem toolStripItem = RiskeerContextMenuItemFactory.CreateClearAllCalculationOutputInFailureMechanismItem(
+ () => true, changeHandler);
// Assert
Assert.AreEqual("&Wis alle uitvoer...", toolStripItem.Text);
@@ -339,17 +333,12 @@
{
// Setup
var mocks = new MockRepository();
- var calculationWithoutOutput = mocks.StrictMock();
- calculationWithoutOutput.Expect(c => c.HasOutput).Return(false);
- var failureMechanism = mocks.StrictMock();
- failureMechanism.Expect(fm => fm.Calculations).Return(new[]
- {
- calculationWithoutOutput
- });
+ var changeHandler = mocks.Stub();
mocks.ReplayAll();
// Call
- StrictContextMenuItem toolStripItem = RiskeerContextMenuItemFactory.CreateClearAllCalculationOutputInFailureMechanismItem(failureMechanism);
+ StrictContextMenuItem toolStripItem = RiskeerContextMenuItemFactory.CreateClearAllCalculationOutputInFailureMechanismItem(
+ () => false, changeHandler);
// Assert
Assert.AreEqual("&Wis alle uitvoer...", toolStripItem.Text);
@@ -361,84 +350,54 @@
}
[Test]
- public void CreateClearAllCalculationOutputInFailureMechanismItem_PerformClickOnCreatedItemAndConfirmChange_CalculationOutputClearedAndObserversNotified()
+ public void GivenCreateClearAllCalculationOutputInFailureMechanismItem_WhenClickPerformedAndActionContinued_ThenClearCalculationsPerformedAndObserversNotified()
{
- // Setup
+ // Given
var mocks = new MockRepository();
- var calculationWithOutputMock1 = mocks.StrictMock();
- var calculationWithOutputMock2 = mocks.StrictMock();
- var calculationWithoutOutput = mocks.StrictMock();
+ var calculation1 = mocks.StrictMock();
+ var calculation2 = mocks.StrictMock();
+ var calculation3 = mocks.StrictMock();
- calculationWithOutputMock1.Stub(c => c.HasOutput).Return(true);
- calculationWithOutputMock2.Stub(c => c.HasOutput).Return(true);
- calculationWithoutOutput.Stub(c => c.HasOutput).Return(false);
-
- calculationWithOutputMock1.Expect(c => c.ClearOutput());
- calculationWithOutputMock1.Expect(c => c.NotifyObservers());
- calculationWithOutputMock2.Expect(c => c.ClearOutput());
- calculationWithOutputMock2.Expect(c => c.NotifyObservers());
-
- var failureMechanism = new TestFailureMechanism(new[]
+ var changeHandler = mocks.StrictMock();
+ changeHandler.Expect(ch => ch.InquireConfirmation()).Return(true);
+ changeHandler.Expect(ch => ch.ClearCalculations()).Return(new[]
{
- calculationWithOutputMock1,
- calculationWithOutputMock2,
- calculationWithoutOutput
+ calculation1,
+ calculation2,
+ calculation3
});
+ calculation1.Expect(c => c.NotifyObservers());
+ calculation2.Expect(c => c.NotifyObservers());
+ calculation3.Expect(c => c.NotifyObservers());
mocks.ReplayAll();
- DialogBoxHandler = (name, wnd) =>
- {
- var messageBox = new MessageBoxTester(wnd);
+ StrictContextMenuItem toolStripItem = RiskeerContextMenuItemFactory.CreateClearAllCalculationOutputInFailureMechanismItem(
+ () => true, changeHandler);
- messageBox.ClickOk();
- };
-
- StrictContextMenuItem toolStripItem = RiskeerContextMenuItemFactory.CreateClearAllCalculationOutputInFailureMechanismItem(failureMechanism);
-
- // Call
+ // When
toolStripItem.PerformClick();
- // Assert
+ // Then
mocks.VerifyAll();
}
[Test]
- public void CreateClearAllCalculationOutputInFailureMechanismItem_PerformClickOnCreatedItemAndCancelChange_CalculationOutputNotCleared()
+ public void GivenCreateClearAllCalculationOutputInFailureMechanismItem_WhenWhenClickPerformedAndActionCancelled_ThenNothingHappens()
{
- // Setup
+ // Given
var mocks = new MockRepository();
- var calculationWithOutputMock1 = mocks.StrictMock();
- var calculationWithOutputMock2 = mocks.StrictMock();
- var calculationWithoutOutput = mocks.StrictMock();
-
- calculationWithOutputMock1.Stub(c => c.HasOutput).Return(true);
- calculationWithOutputMock2.Stub(c => c.HasOutput).Return(true);
- calculationWithoutOutput.Stub(c => c.HasOutput).Return(false);
-
- var failureMechanism = mocks.StrictMock();
- failureMechanism.Expect(fm => fm.Calculations).Return(new[]
- {
- calculationWithOutputMock1,
- calculationWithOutputMock2,
- calculationWithoutOutput
- });
-
+ var changeHandler = mocks.StrictMock();
+ changeHandler.Expect(ch => ch.InquireConfirmation()).Return(false);
mocks.ReplayAll();
- DialogBoxHandler = (name, wnd) =>
- {
- var messageBox = new MessageBoxTester(wnd);
+ StrictContextMenuItem toolStripItem = RiskeerContextMenuItemFactory.CreateClearAllCalculationOutputInFailureMechanismItem(
+ () => true, changeHandler);
- messageBox.ClickCancel();
- };
-
- StrictContextMenuItem toolStripItem = RiskeerContextMenuItemFactory.CreateClearAllCalculationOutputInFailureMechanismItem(failureMechanism);
-
- // Call
+ // When
toolStripItem.PerformClick();
- // Assert
+ // Then
mocks.VerifyAll();
}