Index: Ringtoets/Common/src/Ringtoets.Common.Forms/TreeNodeInfos/RingtoetsContextMenuItemFactory.cs =================================================================== diff -u -rc796a449aa7622c4c2386279a7f9fb410f610444 -rd08c15413bbb6009c6da8ee05833a4c3531b358c --- Ringtoets/Common/src/Ringtoets.Common.Forms/TreeNodeInfos/RingtoetsContextMenuItemFactory.cs (.../RingtoetsContextMenuItemFactory.cs) (revision c796a449aa7622c4c2386279a7f9fb410f610444) +++ Ringtoets/Common/src/Ringtoets.Common.Forms/TreeNodeInfos/RingtoetsContextMenuItemFactory.cs (.../RingtoetsContextMenuItemFactory.cs) (revision d08c15413bbb6009c6da8ee05833a4c3531b358c) @@ -100,72 +100,131 @@ /// The calculation group to perform all calculations for. /// The calculation group context belonging to the calculation group. /// The action that performs all calculations. - /// The func for performing additional validation checks. + /// The function which determines whether the item should be enabled. If the + /// item should not be enabled, then the reason for that should be returned by the function and will be shown as a tooltip. + /// If the item should be enabled then the function should return a null or empty string. /// The created . - /// When returns a string, the item will be disabled and the string will be shown in the tooltip. public static StrictContextMenuItem CreatePerformAllCalculationsInGroupItem( CalculationGroup calculationGroup, TCalculationContext calculationGroupContext, Action calculateAllAction, - Func additionalValidationFunc) + Func enableMenuItemFunction) where TCalculationContext : ICalculationContext { - var validationText = additionalValidationFunc(calculationGroupContext); - - var performAllItem = new StrictContextMenuItem( + var menuItem = new StrictContextMenuItem( Resources.Calculate_all, Resources.CalculationGroup_CalculateAll_ToolTip, Resources.CalculateAllIcon, (o, args) => calculateAllAction(calculationGroup, calculationGroupContext)); if (!calculationGroupContext.WrappedData.GetCalculations().Any()) { - performAllItem.Enabled = false; - performAllItem.ToolTipText = Resources.CalculationGroup_CalculateAll_No_calculations_to_run; + menuItem.Enabled = false; + menuItem.ToolTipText = Resources.CalculationGroup_CalculateAll_No_calculations_to_run; } - else if (!string.IsNullOrEmpty(validationText)) + else { - performAllItem.Enabled = false; - performAllItem.ToolTipText = validationText; + SetStateWithEnableFunction(calculationGroupContext, enableMenuItemFunction, menuItem); } - return performAllItem; + return menuItem; } /// + /// Creates a which is bound to the action of validating the input of each calculation + /// in a calculation group. + /// + /// The type of the calculation group context. + /// The calculation group to validate all calculations for. + /// The calculation group context belonging to the calculation group. + /// The action that validates all calculations. + /// The function which determines whether the item should be enabled. If the + /// item should not be enabled, then the reason for that should be returned by the function and will be shown as a tooltip. + /// If the item should be enabled then the function should return a null or empty string. + /// The created . + public static StrictContextMenuItem CreateValidateAllCalculationsInGroupItem( + CalculationGroup calculationGroup, + TCalculationContext calculationGroupContext, + Action validateAllAction, + Func enableMenuItemFunction) + where TCalculationContext : ICalculationContext + { + var menuItem = new StrictContextMenuItem( + Resources.Validate_all, + Resources.CalculationGroup_Validate_all_ToolTip, + Resources.ValidateAllIcon, + (o, args) => validateAllAction(calculationGroup, calculationGroupContext)); + + if (!calculationGroupContext.WrappedData.GetCalculations().Any()) + { + menuItem.Enabled = false; + menuItem.ToolTipText = Resources.ValidateAll_No_calculations_to_validate; + } + else + { + SetStateWithEnableFunction(calculationGroupContext, enableMenuItemFunction, menuItem); + } + + return menuItem; + } + + /// /// Creates a which is bound to the action of performing a calculation. /// /// The type of the calculation. /// The type of the calculation context. /// The calculation to perform. /// The calculation context belonging to the calculation. /// The action that performs the calculation. - /// The func for performing additional validation checks. + /// The function which determines whether the item should be enabled. If the + /// item should not be enabled, then the reason for that should be returned by the function and will be shown as a tooltip. + /// If the item should be enabled then the function should return a null or empty string. /// The created . - /// When returns a string, the item will be disabled and the string will be shown in the tooltip. public static StrictContextMenuItem CreatePerformCalculationItem( TCalculation calculation, TCalculationContext calculationContext, Action calculateAction, - Func additionalValidationFunc) + Func enableMenuItemFunction) where TCalculationContext : ICalculationContext where TCalculation : ICalculation { - var validationText = additionalValidationFunc(calculationContext); - var nodeEnabled = string.IsNullOrEmpty(validationText); - - return new StrictContextMenuItem( + var menuItem = new StrictContextMenuItem( Resources.Calculate, Resources.Calculate_ToolTip, Resources.CalculateIcon, - (o, args) => calculateAction(calculation, calculationContext)) - { - Enabled = nodeEnabled, - ToolTipText = nodeEnabled ? Resources.Calculate_ToolTip : validationText - }; + (o, args) => calculateAction(calculation, calculationContext)); + + SetStateWithEnableFunction(calculationContext, enableMenuItemFunction, menuItem); + return menuItem; } /// + /// Creates a which is bound to the action of validating the input of a calculation. + /// + /// The type of the calculation. + /// The calculation to validate. + /// The action that performs the validation. + /// The function which determines whether the item should be enabled. If the + /// item should not be enabled, then the reason for that should be returned by the function and will be shown as a tooltip. + /// If the item should be enabled then the function should return a null or empty string. + /// The created . + public static StrictContextMenuItem CreateValidateCalculationItem( + TCalculation calculation, + Action validateAction, + Func enableMenuItemFunction) + where TCalculation : ICalculation + { + var menuItem = new StrictContextMenuItem( + Resources.Validate, + Resources.Validate_ToolTip, + Resources.ValidateIcon, + (o, args) => validateAction(calculation)); + + SetStateWithEnableFunction(calculation, enableMenuItemFunction, menuItem); + return menuItem; + } + + /// /// Creates a which is bound to the action of clearing the output of a calculation. /// /// The calculation to clear the output for. @@ -214,38 +273,72 @@ /// The type of the failure mechanism context. /// The failure mechanism context belonging to the failure mechanism. /// The action that performs all calculations. - /// The func for performing additional validation checks. + /// The function which determines whether the item should be enabled. If the + /// item should not be enabled, then the reason for that should be returned by the function and will be shown as a tooltip. + /// If the item should be enabled then the function should return a null or empty string. /// The created . - /// When returns a string, the item will be disabled and the string will be shown in the tooltip. + /// When returns a string, the item will be disabled and the string will be shown in the tooltip. public static StrictContextMenuItem CreatePerformAllCalculationsInFailureMechanismItem( TFailureMechanismContext failureMechanismContext, Action calculateAllAction, - Func additionalValidationFunc) + Func enableMenuItemFunction) where TFailureMechanismContext : IFailureMechanismContext { - var validationText = additionalValidationFunc(failureMechanismContext); - - var performAllItem = new StrictContextMenuItem( + var menuItem = new StrictContextMenuItem( Resources.Calculate_all, Resources.Calculate_all_ToolTip, Resources.CalculateAllIcon, (o, args) => calculateAllAction(failureMechanismContext)); if (!failureMechanismContext.WrappedData.Calculations.Any()) { - performAllItem.Enabled = false; - performAllItem.ToolTipText = Resources.FailureMechanism_CreateCalculateAllItem_No_calculations_to_run; + menuItem.Enabled = false; + menuItem.ToolTipText = Resources.FailureMechanism_CreateCalculateAllItem_No_calculations_to_run; } - else if (!string.IsNullOrEmpty(validationText)) + else { - performAllItem.Enabled = false; - performAllItem.ToolTipText = validationText; + SetStateWithEnableFunction(failureMechanismContext, enableMenuItemFunction, menuItem); } - return performAllItem; + return menuItem; } /// + /// Creates a which is bound to the action of performing all calculations in a failure mechanism. + /// + /// The type of the failure mechanism. + /// The failure mechanism to validate the calculations of. + /// The action that validates all calculations. + /// The function which determines whether the item should be enabled. If the + /// item should not be enabled, then the reason for that should be returned by the function and will be shown as a tooltip. + /// If the item should be enabled then the function should return a null or empty string. + /// The created . + public static StrictContextMenuItem CreateValidateAllCalculationsInFailureMechanismItem( + TFailureMechanism failureMechanism, + Action validateAllAction, + Func enableMenuItemFunction) + where TFailureMechanism : IFailureMechanism + { + var menuItem = new StrictContextMenuItem( + Resources.Validate_all, + Resources.FailureMechanism_Validate_all_ToolTip, + Resources.ValidateAllIcon, + (o, args) => validateAllAction(failureMechanism)); + + if (!failureMechanism.Calculations.Any()) + { + menuItem.Enabled = false; + menuItem.ToolTipText = Resources.ValidateAll_No_calculations_to_validate; + } + else + { + SetStateWithEnableFunction(failureMechanism, enableMenuItemFunction, menuItem); + } + + return menuItem; + } + + /// /// Creates a which is bound to the action of changing the relevance of a failure mechanism. /// /// The type of the failure mechanism context. @@ -275,6 +368,16 @@ }); } + private static void SetStateWithEnableFunction(T context, Func enableFunction, StrictContextMenuItem menuItem) + { + var validationText = enableFunction != null ? enableFunction(context) : null; + if (!string.IsNullOrEmpty(validationText)) + { + menuItem.Enabled = false; + menuItem.ToolTipText = validationText; + } + } + private static void ClearAllCalculationOutputInFailureMechanism(IFailureMechanism failureMechanism) { if (MessageBox.Show(Resources.FailureMechanism_ContextMenuStrip_Are_you_sure_clear_all_output, BaseResources.Confirm, MessageBoxButtons.OKCancel) != DialogResult.OK)