// Copyright (C) Stichting Deltares 2018. 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.Collections.Generic; using System.Linq; using System.Windows.Forms; using Core.Common.Base; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.Common.Forms.Properties; using Ringtoets.Common.Forms.PropertyClasses; using CoreCommonBaseResources = Core.Common.Base.Properties.Resources; namespace Ringtoets.Common.Forms.ChangeHandlers { /// /// Class which properly handles data model changes due to a change of a /// failure mechanism property. /// public class FailureMechanismPropertyChangeHandler : IFailureMechanismPropertyChangeHandler where T : IFailureMechanism { public IEnumerable SetPropertyValueAfterConfirmation( T failureMechanism, TValue value, SetFailureMechanismPropertyValueDelegate setValue) { if (failureMechanism == null) { throw new ArgumentNullException(nameof(failureMechanism)); } if (value == null) { throw new ArgumentNullException(nameof(value)); } if (setValue == null) { throw new ArgumentNullException(nameof(setValue)); } var changedObjects = new List(); if (RequiresConfirmation(failureMechanism)) { if (ConfirmPropertyChange()) { setValue(failureMechanism, value); changedObjects.AddRange(PropertyChanged(failureMechanism)); changedObjects.Add(failureMechanism); } } else { setValue(failureMechanism, value); changedObjects.Add(failureMechanism); } return changedObjects; } /// /// Checks whether a call to would have any effect in the given /// . /// /// The failure mechanism to check for. /// true if would result in changes, /// false otherwise. /// Thrown when /// is null. protected virtual bool RequiresConfirmation(T failureMechanism) { if (failureMechanism == null) { throw new ArgumentNullException(nameof(failureMechanism)); } return failureMechanism.Calculations.Any(c => c.HasOutput); } /// /// Propagates the necessary changes to underlying data structure when a property has /// been changed for a failure mechanism. /// /// The failure mechanism to be updated. /// All objects that have been affected by the change. /// Thrown when /// is null. protected virtual IEnumerable PropertyChanged(T failureMechanism) { if (failureMechanism == null) { throw new ArgumentNullException(nameof(failureMechanism)); } var affected = new List(); foreach (ICalculation calculation in failureMechanism.Calculations.Where(c => c.HasOutput)) { affected.Add(calculation); calculation.ClearOutput(); } return affected; } /// /// Checks to see if the change of the failure mechanism property should occur or not. /// /// true if the change should occur, false otherwise. private static bool ConfirmPropertyChange() { DialogResult result = MessageBox.Show(Resources.FailureMechanismPropertyChangeHandler_Confirm_change_composition_and_clear_dependent_data, CoreCommonBaseResources.Confirm, MessageBoxButtons.OKCancel); return result == DialogResult.OK; } } }