// 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.Linq; using System.Windows.Forms; using Core.Common.Gui.ContextMenu; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.Common.Forms.Helpers; using Ringtoets.Common.Forms.PresentationObjects; using Ringtoets.Common.Forms.Properties; using RingtoetsCommonDataResources = Ringtoets.Common.Data.Properties.Resources; using BaseResources = Core.Common.Base.Properties.Resources; namespace Ringtoets.Common.Forms.TreeNodeInfos { /// /// This class represents a factory for creating . /// public class RingtoetsContextMenuItemFactory { /// /// Creates a which is bound to the action of adding new calculation groups. /// /// The calculation group to add the new calculation groups to. /// The created . public StrictContextMenuItem CreateAddCalculationGroupItem(CalculationGroup calculationGroup) { return new StrictContextMenuItem( Resources.CalculationGroup_Add_CalculationGroup, Resources.CalculationGroup_Add_CalculationGroup_Tooltip, Resources.AddFolderIcon, (o, args) => CreateCalculationGroup(calculationGroup)); } /// /// Creates a which is bound to the action of adding new calculations. /// /// The type of the calculation group context. /// The calculation group context belonging to the calculation group. /// The action for adding a calculation to the calculation group. /// The created . public StrictContextMenuItem CreateAddCalculationItem( TCalculationContext calculationGroupContext, Action addCalculationAction) where TCalculationContext : ICalculationContext { return new StrictContextMenuItem( Resources.CalculationGroup_Add_Calculation, Resources.CalculationGroup_Add_Calculation_Tooltip, Resources.FailureMechanismIcon, (o, args) => addCalculationAction(calculationGroupContext)); } /// /// Creates a which is bound to the action of clearing the output of all calculations in the calculation group. /// /// The calculation group to clear the output for. /// The created . public StrictContextMenuItem CreateClearAllCalculationOutputInGroupItem(CalculationGroup calculationGroup) { var clearAllItem = new StrictContextMenuItem( Resources.Clear_all_output, Resources.CalculationGroup_ClearOutput_ToolTip, Resources.ClearIcon, (o, args) => ClearAllCalculationOutputInGroup(calculationGroup)); if (!calculationGroup.HasOutput()) { clearAllItem.Enabled = false; clearAllItem.ToolTipText = Resources.CalculationGroup_ClearOutput_No_calculation_with_output_to_clear; } return clearAllItem; } /// /// Creates a which is bound to the action of performing all calculations in a calculation group. /// /// The type of the calculation group context. /// The calculation group to perform all calculations for. /// The calculation group context belonging to the calculation group. /// The action that performs all calculations. /// The created . public StrictContextMenuItem CreatePerformAllCalculationsInGroupItem( CalculationGroup calculationGroup, TCalculationContext calculationGroupContext, Action calculateAllAction) where TCalculationContext : ICalculationContext { var performAllItem = new StrictContextMenuItem( Resources.Calculate_all, Resources.CalculationGroup_CalculateAll_ToolTip, Resources.CalculateAllIcon, (o, args) => calculateAllAction(calculationGroup, calculationGroupContext)); if (!calculationGroup.GetCalculations().Any()) { performAllItem.Enabled = false; performAllItem.ToolTipText = Resources.CalculationGroup_CalculateAll_No_calculations_to_run; } return performAllItem; } /// /// 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 created . public StrictContextMenuItem CreatePerformCalculationItem( TCalculation calculation, TCalculationContext calculationContext, Action calculateAction) where TCalculationContext : ICalculationContext where TCalculation : ICalculation { return new StrictContextMenuItem( Resources.Calculate, Resources.Calculate_ToolTip, Resources.CalculateIcon, (o, args) => calculateAction(calculation, calculationContext)); } /// /// Creates a which is bound to the action of clearing the output of a calculation. /// /// The calculation to clear the output for. /// The created . public StrictContextMenuItem CreateClearCalculationOutputItem(ICalculation calculation) { var clearOutputItem = new StrictContextMenuItem( Resources.Clear_output, Resources.Clear_output_ToolTip, Resources.ClearIcon, (o, args) => ClearCalculationOutput(calculation)); if (!calculation.HasOutput) { clearOutputItem.Enabled = false; clearOutputItem.ToolTipText = Resources.ClearOutput_No_output_to_clear; } return clearOutputItem; } /// /// Creates a which is bound to the action of changing the relevancy state of a disabled failure mechanism. /// /// The failure mechanism context belonging to the failure mechanism. /// The created . public StrictContextMenuItem CreateDisabledChangeRelevancyItem(IFailureMechanismContext failureMechanismContext) { return new StrictContextMenuItem( Resources.FailureMechanismContextMenuStrip_Is_relevant, Resources.FailureMechanismContextMenuStrip_Is_relevant_Tooltip, Resources.Checkbox_empty, (o, args) => { failureMechanismContext.WrappedData.IsRelevant = true; failureMechanismContext.WrappedData.NotifyObservers(); }); } private static void CreateCalculationGroup(CalculationGroup calculationGroup) { calculationGroup.Children.Add(new CalculationGroup { Name = NamingHelper.GetUniqueName(calculationGroup.Children, RingtoetsCommonDataResources.CalculationGroup_DefaultName, c => c.Name) }); calculationGroup.NotifyObservers(); } private static void ClearAllCalculationOutputInGroup(CalculationGroup calculationGroup) { if (MessageBox.Show(Resources.CalculationGroup_ClearOutput_Are_you_sure_clear_all_output, BaseResources.Confirm, MessageBoxButtons.OKCancel) != DialogResult.OK) { return; } calculationGroup.ClearCalculationOutput(); } private static void ClearCalculationOutput(ICalculation calculation) { if (MessageBox.Show(Resources.Calculation_ContextMenuStrip_Are_you_sure_clear_output, BaseResources.Confirm, MessageBoxButtons.OKCancel) != DialogResult.OK) { return; } calculation.ClearOutput(); calculation.NotifyObservers(); } } }