// 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.Collections; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Windows.Forms; using Core.Common.Controls.TreeView; using Core.Common.Gui.Plugin; using Ringtoets.ClosingStructures.Data; using Ringtoets.ClosingStructures.Forms.PresentationObjects; using Ringtoets.ClosingStructures.Forms.Views; using Ringtoets.ClosingStructures.IO; using Ringtoets.Common.Data; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Data.Probability; using Ringtoets.Common.Forms.Helpers; using Ringtoets.Common.Forms.PresentationObjects; using Ringtoets.Common.Forms.TreeNodeInfos; using Ringtoets.HydraRing.IO; using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources; using RingtoetsCommonServiceResources = Ringtoets.Common.Service.Properties.Resources; using RingtoetsCommonIOResources = Ringtoets.Common.IO.Properties.Resources; using ClosingStructuresDataResources = Ringtoets.ClosingStructures.Data.Properties.Resources; using ClosingStructuresFormsResources = Ringtoets.ClosingStructures.Forms.Properties.Resources; namespace Ringtoets.ClosingStructures.Plugin { /// /// The plug-in for the . /// public class ClosingStructuresPlugin : PluginBase { public override IEnumerable GetViewInfos() { yield return new ViewInfo< FailureMechanismSectionResultContext, IEnumerable, ClosingStructuresFailureMechanismResultView> { GetViewName = (v, o) => RingtoetsCommonFormsResources.FailureMechanism_AssessmentResult_DisplayName, Image = RingtoetsCommonFormsResources.FailureMechanismSectionResultIcon, CloseForData = CloseFailureMechanismResultViewForData, GetViewData = context => context.WrappedData, AfterCreate = (view, context) => view.FailureMechanism = context.FailureMechanism }; } public override IEnumerable GetTreeNodeInfos() { yield return RingtoetsTreeNodeInfoFactory.CreateFailureMechanismContextTreeNodeInfo( FailureMechanismEnabledChildNodeObjects, FailureMechanismDisabledChildNodeObjects, FailureMechanismEnabledContextMenuStrip, FailureMechanismDisabledContextMenuStrip); yield return RingtoetsTreeNodeInfoFactory.CreateCalculationGroupContextTreeNodeInfo( CalculationGroupContextChildNodeObjects, CalculationGroupContextContextMenuStrip, CalculationGroupContextOnNodeRemoved); yield return RingtoetsTreeNodeInfoFactory.CreateCalculationContextTreeNodeInfo( CalculationContextChildNodeObjects, CalculationContextContextMenuStrip, CalculationContextOnNodeRemoved); yield return new TreeNodeInfo> { Text = context => RingtoetsCommonFormsResources.FailureMechanism_AssessmentResult_DisplayName, Image = context => RingtoetsCommonFormsResources.FailureMechanismSectionResultIcon, ContextMenuStrip = (nodeData, parentData, treeViewControl) => Gui.Get(nodeData, treeViewControl) .AddOpenItem() .Build() }; yield return new TreeNodeInfo { Text = context => RingtoetsCommonFormsResources.StructuresCollection_DisplayName, Image = context => RingtoetsCommonFormsResources.GeneralFolderIcon, ForeColor = context => context.WrappedData.Any() ? Color.FromKnownColor(KnownColor.ControlText) : Color.FromKnownColor(KnownColor.GrayText), ChildNodeObjects = context => context.WrappedData.Cast().ToArray(), ContextMenuStrip = (nodeData, parentData, treeViewControl) => Gui.Get(nodeData, treeViewControl) .AddImportItem() .AddSeparator() .AddExpandAllItem() .AddCollapseAllItem() .Build() }; yield return new TreeNodeInfo { Text = structure => structure.Name, Image = structure => RingtoetsCommonFormsResources.StructuresIcon, ContextMenuStrip = (structure, parentData, treeViewControl) => Gui.Get(structure, treeViewControl) .AddPropertiesItem() .Build() }; yield return new TreeNodeInfo { Text = inputContext => ClosingStructuresFormsResources.ClosingStructuresInputContext_NodeDisplayName, Image = inputContext => RingtoetsCommonFormsResources.GenericInputOutputIcon, ContextMenuStrip = (nodeData, parentData, treeViewControl) => Gui.Get(nodeData, treeViewControl) .AddPropertiesItem() .Build() }; } public override IEnumerable GetImportInfos() { yield return new ImportInfo { CreateFileImporter = (context, filePath) => new ClosingStructuresImporter(context.WrappedData, context.AssessmentSection.ReferenceLine, filePath), Name = RingtoetsCommonFormsResources.StructuresImporter_DisplayName, Category = RingtoetsCommonFormsResources.Ringtoets_Category, Image = RingtoetsCommonFormsResources.StructuresIcon, FileFilter = RingtoetsCommonIOResources.DataTypeDisplayName_shape_file_filter, IsEnabled = context => context.AssessmentSection.ReferenceLine != null }; } #region ViewInfo #region ClosingStructuresFailureMechanismResultView ViewInfo private static bool CloseFailureMechanismResultViewForData(ClosingStructuresFailureMechanismResultView view, object o) { var assessmentSection = o as IAssessmentSection; var failureMechanism = o as ClosingStructuresFailureMechanism; var failureMechanismContext = o as IFailureMechanismContext; if (assessmentSection != null) { return assessmentSection .GetFailureMechanisms() .OfType() .Any(fm => ReferenceEquals(view.Data, fm.SectionResults)); } if (failureMechanismContext != null) { failureMechanism = failureMechanismContext.WrappedData; } return failureMechanism != null && ReferenceEquals(view.Data, failureMechanism.SectionResults); } #endregion #endregion #region TreeNodeInfo #region ClosingStructuresFailureMechanismContext TreeNodeInfo private object[] FailureMechanismEnabledChildNodeObjects(ClosingStructuresFailureMechanismContext closingStructuresFailureMechanismContext) { ClosingStructuresFailureMechanism wrappedData = closingStructuresFailureMechanismContext.WrappedData; return new object[] { new CategoryTreeFolder(RingtoetsCommonFormsResources.FailureMechanism_Inputs_DisplayName, GetInputs(wrappedData, closingStructuresFailureMechanismContext.Parent), TreeFolderCategory.Input), new ClosingStructuresCalculationGroupContext(wrappedData.CalculationsGroup, wrappedData, closingStructuresFailureMechanismContext.Parent), new CategoryTreeFolder(RingtoetsCommonFormsResources.FailureMechanism_Outputs_DisplayName, GetOutputs(wrappedData), TreeFolderCategory.Output) }; } private object[] FailureMechanismDisabledChildNodeObjects(ClosingStructuresFailureMechanismContext closingStructuresFailureMechanismContext) { return new object[] { new CommentContext(closingStructuresFailureMechanismContext.WrappedData) }; } private static IList GetInputs(ClosingStructuresFailureMechanism failureMechanism, IAssessmentSection assessmentSection) { return new ArrayList { new FailureMechanismSectionsContext(failureMechanism, assessmentSection), new ClosingStructuresContext(failureMechanism.ClosingStructures, assessmentSection), new CommentContext(failureMechanism) }; } private static IList GetOutputs(ClosingStructuresFailureMechanism failureMechanism) { return new ArrayList { new FailureMechanismSectionResultContext(failureMechanism.SectionResults, failureMechanism) }; } private ContextMenuStrip FailureMechanismEnabledContextMenuStrip(ClosingStructuresFailureMechanismContext closingStructuresFailureMechanismContext, object parentData, TreeViewControl treeViewControl) { var builder = new RingtoetsContextMenuBuilder(Gui.Get(closingStructuresFailureMechanismContext, treeViewControl)); return builder.AddToggleRelevancyOfFailureMechanismItem(closingStructuresFailureMechanismContext, RemoveAllViewsForItem) .AddSeparator() .AddValidateAllCalculationsInFailureMechanismItem(closingStructuresFailureMechanismContext, c => ValidateAll(), ValidateAllDataAvailableAndGetErrorMessageForCalculationsInFailureMechanism) .AddPerformAllCalculationsInFailureMechanismItem(closingStructuresFailureMechanismContext, CalculateAll, ValidateAllDataAvailableAndGetErrorMessageForCalculationsInFailureMechanism) .AddClearAllCalculationOutputInFailureMechanismItem(closingStructuresFailureMechanismContext.WrappedData) .AddSeparator() .AddExpandAllItem() .AddCollapseAllItem() .AddSeparator() .AddPropertiesItem() .Build(); } private void RemoveAllViewsForItem(ClosingStructuresFailureMechanismContext failureMechanismContext) { Gui.ViewCommands.RemoveAllViewsForItem(failureMechanismContext); } private ContextMenuStrip FailureMechanismDisabledContextMenuStrip(ClosingStructuresFailureMechanismContext closingStructuresFailureMechanismContext, object parentData, TreeViewControl treeViewControl) { var builder = new RingtoetsContextMenuBuilder(Gui.Get(closingStructuresFailureMechanismContext, treeViewControl)); return builder.AddToggleRelevancyOfFailureMechanismItem(closingStructuresFailureMechanismContext, null) .AddSeparator() .AddExpandAllItem() .AddCollapseAllItem() .Build(); } private void ValidateAll() { //Add validation service - currently a place holder } private static string ValidateAllDataAvailableAndGetErrorMessageForCalculationsInFailureMechanism(ClosingStructuresFailureMechanismContext context) { return ValidateAllDataAvailableAndGetErrorMessage(context.Parent, context.WrappedData); } private void CalculateAll(ClosingStructuresFailureMechanismContext context) { CalculateAll(context.WrappedData, context.WrappedData.Calculations.OfType()); } #endregion #region ClosingStructuresCalculationGroupContext TreeNodeInfo private static object[] CalculationGroupContextChildNodeObjects(ClosingStructuresCalculationGroupContext context) { var childNodeObjects = new List(); foreach (ICalculationBase calculationItem in context.WrappedData.Children) { var calculation = calculationItem as ClosingStructuresCalculation; var group = calculationItem as CalculationGroup; if (calculation != null) { childNodeObjects.Add(new ClosingStructuresCalculationContext(calculation, context.FailureMechanism, context.AssessmentSection)); } else if (group != null) { childNodeObjects.Add(new ClosingStructuresCalculationGroupContext(group, context.FailureMechanism, context.AssessmentSection)); } else { childNodeObjects.Add(calculationItem); } } return childNodeObjects.ToArray(); } private ContextMenuStrip CalculationGroupContextContextMenuStrip(ClosingStructuresCalculationGroupContext context, object parentData, TreeViewControl treeViewControl) { var group = context.WrappedData; var builder = new RingtoetsContextMenuBuilder(Gui.Get(context, treeViewControl)); var isNestedGroup = parentData is ClosingStructuresCalculationGroupContext; builder.AddCreateCalculationGroupItem(group) .AddCreateCalculationItem(context, AddCalculation); if (!isNestedGroup) { builder .AddSeparator() .AddRemoveAllChildrenItem(group, Gui.ViewCommands); } builder.AddSeparator() .AddValidateAllCalculationsInGroupItem(context, c => ValidateAll(), ValidateAllDataAvailableAndGetErrorMessage) .AddPerformAllCalculationsInGroupItem(group, context, CalculateAll, ValidateAllDataAvailableAndGetErrorMessage) .AddClearAllCalculationOutputInGroupItem(group) .AddSeparator(); if (isNestedGroup) { builder.AddRenameItem() .AddDeleteItem() .AddSeparator(); } return builder.AddExpandAllItem() .AddCollapseAllItem() .AddSeparator() .AddPropertiesItem() .Build(); } private static string ValidateAllDataAvailableAndGetErrorMessage(ClosingStructuresCalculationGroupContext context) { return ValidateAllDataAvailableAndGetErrorMessage(context.AssessmentSection, context.FailureMechanism); } private static void CalculateAll(CalculationGroup group, ClosingStructuresCalculationGroupContext context) { CalculateAll(context.FailureMechanism, group.GetCalculations().OfType()); } private static void AddCalculation(ClosingStructuresCalculationGroupContext context) { var calculation = new ClosingStructuresCalculation { Name = NamingHelper.GetUniqueName(context.WrappedData.Children, ClosingStructuresDataResources.ClosingStructuresCalculation_DefaultName, c => c.Name) }; context.WrappedData.Children.Add(calculation); context.WrappedData.NotifyObservers(); } private static void CalculationGroupContextOnNodeRemoved(ClosingStructuresCalculationGroupContext context, object parentNodeData) { var parentGroupContext = (ClosingStructuresCalculationGroupContext) parentNodeData; parentGroupContext.WrappedData.Children.Remove(context.WrappedData); parentGroupContext.NotifyObservers(); } #endregion #region ClosingStructuresCalculationContext TreeNodeInfo private static object[] CalculationContextChildNodeObjects(ClosingStructuresCalculationContext context) { var childNodes = new List { new CommentContext(context.WrappedData), new ClosingStructuresInputContext(context.WrappedData.InputParameters, context.FailureMechanism, context.AssessmentSection) }; if (context.WrappedData.HasOutput) { childNodes.Add(context.WrappedData.Output); } else { childNodes.Add(new EmptyProbabilityAssessmentOutput()); } return childNodes.ToArray(); } private ContextMenuStrip CalculationContextContextMenuStrip(ClosingStructuresCalculationContext context, object parentData, TreeViewControl treeViewControl) { var builder = new RingtoetsContextMenuBuilder(Gui.Get(context, treeViewControl)); ClosingStructuresCalculation calculation = context.WrappedData; return builder.AddValidateCalculationItem(context, null, ValidateAllDataAvailableAndGetErrorMessageForCalculation) .AddPerformCalculationItem(calculation, context, null, ValidateAllDataAvailableAndGetErrorMessageForCalculation) .AddClearCalculationOutputItem(calculation) .AddSeparator() .AddRenameItem() .AddDeleteItem() .AddSeparator() .AddExpandAllItem() .AddCollapseAllItem() .AddSeparator() .AddPropertiesItem() .Build(); } private string ValidateAllDataAvailableAndGetErrorMessageForCalculation(ClosingStructuresCalculationContext context) { return ValidateAllDataAvailableAndGetErrorMessage(context.AssessmentSection, context.FailureMechanism); } private static void CalculationContextOnNodeRemoved(ClosingStructuresCalculationContext context, object parentData) { var calculationGroupContext = parentData as ClosingStructuresCalculationGroupContext; if (calculationGroupContext != null) { calculationGroupContext.WrappedData.Children.Remove(context.WrappedData); calculationGroupContext.NotifyObservers(); } } #endregion #endregion private static string ValidateAllDataAvailableAndGetErrorMessage(IAssessmentSection assessmentSection, ClosingStructuresFailureMechanism failureMechanism) { if (!failureMechanism.Sections.Any()) { return RingtoetsCommonFormsResources.Plugin_AllDataAvailable_No_failure_mechanism_sections_imported; } if (assessmentSection.HydraulicBoundaryDatabase == null) { return RingtoetsCommonFormsResources.Plugin_AllDataAvailable_No_hydraulic_boundary_database_imported; } var validationProblem = HydraulicDatabaseHelper.ValidatePathForCalculation(assessmentSection.HydraulicBoundaryDatabase.FilePath); if (!string.IsNullOrEmpty(validationProblem)) { return string.Format(RingtoetsCommonServiceResources.Hydraulic_boundary_database_connection_failed_0_, validationProblem); } return null; } private static void CalculateAll(ClosingStructuresFailureMechanism failureMechanism, IEnumerable calculations) { //Add calculate logic, part of WTI-554 } } }