// 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.Base;
using Core.Common.Controls.TreeView;
using Core.Common.Gui.ContextMenu;
using Core.Common.Gui.Forms.ProgressDialog;
using Core.Common.Gui.Plugin;
using Ringtoets.Common.Data;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Data.Calculation;
using Ringtoets.Common.Data.Probability;
using Ringtoets.Common.Data.Structures;
using Ringtoets.Common.Forms;
using Ringtoets.Common.Forms.Helpers;
using Ringtoets.Common.Forms.PresentationObjects;
using Ringtoets.Common.Forms.TreeNodeInfos;
using Ringtoets.Common.IO.HydraRing;
using Ringtoets.Common.Utils;
using Ringtoets.HeightStructures.Data;
using Ringtoets.HeightStructures.Forms.PresentationObjects;
using Ringtoets.HeightStructures.Forms.PropertyClasses;
using Ringtoets.HeightStructures.Forms.Views;
using Ringtoets.HeightStructures.IO;
using Ringtoets.HeightStructures.Service;
using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources;
using RingtoetsCommonDataResources = Ringtoets.Common.Data.Properties.Resources;
using RingtoetsCommonServiceResources = Ringtoets.Common.Service.Properties.Resources;
using RingtoetsCommonIOResources = Ringtoets.Common.IO.Properties.Resources;
namespace Ringtoets.HeightStructures.Plugin
{
///
/// The plug-in for the .
///
public class HeightStructuresPlugin : PluginBase
{
public override IEnumerable GetPropertyInfos()
{
yield return new PropertyInfo
{
GetObjectPropertiesData = context => context.WrappedData
};
yield return new PropertyInfo();
yield return new PropertyInfo();
}
public override IEnumerable GetImportInfos()
{
yield return new ImportInfo
{
CreateFileImporter = (context, filePath) => new HeightStructuresImporter(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
};
}
public override IEnumerable GetViewInfos()
{
yield return new ViewInfo<
HeightStructuresScenariosContext,
CalculationGroup,
HeightStructuresScenariosView>
{
GetViewData = context => context.WrappedData,
GetViewName = (view, calculationGroup) => RingtoetsCommonFormsResources.Scenarios_DisplayName,
AfterCreate = (view, context) => view.FailureMechanism = context.ParentFailureMechanism,
CloseForData = CloseScenariosViewForData,
Image = RingtoetsCommonFormsResources.ScenariosIcon
};
yield return new ViewInfo<
FailureMechanismSectionResultContext,
IEnumerable,
HeightStructuresFailureMechanismResultView>
{
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 = inputContext => RingtoetsCommonFormsResources.FailureMechanism_Inputs_DisplayName,
Image = inputContext => RingtoetsCommonFormsResources.GenericInputOutputIcon,
ContextMenuStrip = (nodeData, parentData, treeViewControl) => Gui.Get(nodeData, treeViewControl)
.AddPropertiesItem()
.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)
.AddDeleteChildrenItem()
.AddSeparator()
.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)
.AddDeleteItem()
.AddSeparator()
.AddPropertiesItem()
.Build(),
CanRemove = CanRemoveHeightStructure,
OnNodeRemoved = OnHeightStructureRemoved
};
yield return new TreeNodeInfo
{
Text = context => RingtoetsCommonFormsResources.Scenarios_DisplayName,
Image = context => RingtoetsCommonFormsResources.ScenariosIcon,
ContextMenuStrip = (nodeData, parentData, treeViewControl) => Gui.Get(nodeData, treeViewControl)
.AddOpenItem()
.Build()
};
yield return new TreeNodeInfo>
{
Text = context => RingtoetsCommonFormsResources.FailureMechanism_AssessmentResult_DisplayName,
Image = context => RingtoetsCommonFormsResources.FailureMechanismSectionResultIcon,
ContextMenuStrip = (nodeData, parentData, treeViewControl) => Gui.Get(nodeData, treeViewControl)
.AddOpenItem()
.Build()
};
}
private void CalculateAll(HeightStructuresFailureMechanism failureMechanism,
IEnumerable> calculations,
IAssessmentSection assessmentSection)
{
ActivityProgressDialogRunner.Run(Gui.MainWindow,
calculations.Select(calc => new HeightStructuresCalculationActivity(calc,
assessmentSection.HydraulicBoundaryDatabase.FilePath,
failureMechanism,
assessmentSection)).ToArray());
}
private static void ValidateAll(IEnumerable> heightStructuresCalculations, IAssessmentSection assessmentSection)
{
foreach (var calculation in heightStructuresCalculations)
{
HeightStructuresCalculationService.Validate(calculation, assessmentSection);
}
}
private static string ValidateAllDataAvailableAndGetErrorMessage(IAssessmentSection assessmentSection, HeightStructuresFailureMechanism 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;
}
#region HeightStructure TreeNodeInfo
private bool CanRemoveHeightStructure(HeightStructure nodeData, object parentData)
{
return parentData is HeightStructuresContext;
}
private void OnHeightStructureRemoved(HeightStructure nodeData, object parentData)
{
var parentContext = (HeightStructuresContext) parentData;
var changedObservables = new List();
StructuresCalculation[] heightStructureCalculations = parentContext.ParentFailureMechanism.Calculations
.Cast>()
.ToArray();
StructuresCalculation[] calculationWithRemovedHeightStructure = heightStructureCalculations
.Where(c => ReferenceEquals(c.InputParameters.Structure, nodeData))
.ToArray();
foreach (StructuresCalculation calculation in calculationWithRemovedHeightStructure)
{
calculation.InputParameters.Structure = null;
StructuresHelper.Delete(parentContext.ParentFailureMechanism.SectionResults, calculation, heightStructureCalculations);
changedObservables.Add(calculation.InputParameters);
}
parentContext.WrappedData.Remove(nodeData);
changedObservables.Add(parentContext.WrappedData);
foreach (IObservable observable in changedObservables)
{
observable.NotifyObservers();
}
}
#endregion
#region ViewInfo
#region HeightStructuresFailureMechanismResultView ViewInfo
#region HeightStructuresScenariosView ViewInfo
private static bool CloseScenariosViewForData(HeightStructuresScenariosView view, object removedData)
{
var failureMechanism = removedData as HeightStructuresFailureMechanism;
var failureMechanismContext = removedData as HeightStructuresFailureMechanismContext;
if (failureMechanismContext != null)
{
failureMechanism = failureMechanismContext.WrappedData;
}
var assessmentSection = removedData as IAssessmentSection;
if (assessmentSection != null)
{
failureMechanism = assessmentSection.GetFailureMechanisms()
.OfType()
.FirstOrDefault();
}
return failureMechanism != null && ReferenceEquals(view.Data, failureMechanism.CalculationsGroup);
}
#endregion
private static bool CloseFailureMechanismResultViewForData(HeightStructuresFailureMechanismResultView view, object o)
{
var assessmentSection = o as IAssessmentSection;
var failureMechanism = o as HeightStructuresFailureMechanism;
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 TreeNodeInfos
#region HeightStructuresFailureMechanismContext TreeNodeInfo
private static object[] FailureMechanismEnabledChildNodeObjects(HeightStructuresFailureMechanismContext context)
{
HeightStructuresFailureMechanism wrappedData = context.WrappedData;
return new object[]
{
new CategoryTreeFolder(RingtoetsCommonFormsResources.FailureMechanism_Inputs_DisplayName, GetInputs(wrappedData, context.Parent), TreeFolderCategory.Input),
new HeightStructuresCalculationGroupContext(wrappedData.CalculationsGroup, wrappedData, context.Parent),
new CategoryTreeFolder(RingtoetsCommonFormsResources.FailureMechanism_Outputs_DisplayName, GetOutputs(wrappedData), TreeFolderCategory.Output)
};
}
private static IList GetInputs(HeightStructuresFailureMechanism failureMechanism, IAssessmentSection assessmentSection)
{
return new ArrayList
{
new FailureMechanismSectionsContext(failureMechanism, assessmentSection),
new ForeshoreProfilesContext(failureMechanism.ForeshoreProfiles, failureMechanism, assessmentSection),
new HeightStructuresContext(failureMechanism.HeightStructures, failureMechanism, assessmentSection),
new CommentContext(failureMechanism)
};
}
private static IList GetOutputs(HeightStructuresFailureMechanism failureMechanism)
{
return new ArrayList
{
new HeightStructuresScenariosContext(failureMechanism.CalculationsGroup, failureMechanism),
new FailureMechanismSectionResultContext(failureMechanism.SectionResults, failureMechanism)
};
}
private static object[] FailureMechanismDisabledChildNodeObjects(HeightStructuresFailureMechanismContext context)
{
return new object[]
{
new CommentContext(context.WrappedData)
};
}
private ContextMenuStrip FailureMechanismEnabledContextMenuStrip(HeightStructuresFailureMechanismContext context, object parentData, TreeViewControl treeViewControl)
{
var builder = new RingtoetsContextMenuBuilder(Gui.Get(context, treeViewControl));
return builder.AddToggleRelevancyOfFailureMechanismItem(context, RemoveAllViewsForItem)
.AddSeparator()
.AddValidateAllCalculationsInFailureMechanismItem(
context,
ValidateAll,
ValidateAllDataAvailableAndGetErrorMessageForCalculationsInFailureMechanism)
.AddPerformAllCalculationsInFailureMechanismItem(context, CalculateAll, ValidateAllDataAvailableAndGetErrorMessageForCalculationsInFailureMechanism)
.AddClearAllCalculationOutputInFailureMechanismItem(context.WrappedData)
.AddSeparator()
.AddExpandAllItem()
.AddCollapseAllItem()
.AddSeparator()
.AddPropertiesItem()
.Build();
}
private void RemoveAllViewsForItem(HeightStructuresFailureMechanismContext context)
{
Gui.ViewCommands.RemoveAllViewsForItem(context);
}
private ContextMenuStrip FailureMechanismDisabledContextMenuStrip(HeightStructuresFailureMechanismContext context, object parentData, TreeViewControl treeViewControl)
{
var builder = new RingtoetsContextMenuBuilder(Gui.Get(context, treeViewControl));
return builder.AddToggleRelevancyOfFailureMechanismItem(context, null)
.AddSeparator()
.AddExpandAllItem()
.AddCollapseAllItem()
.Build();
}
private static string ValidateAllDataAvailableAndGetErrorMessageForCalculationsInFailureMechanism(HeightStructuresFailureMechanismContext context)
{
return ValidateAllDataAvailableAndGetErrorMessage(context.Parent, context.WrappedData);
}
private static void ValidateAll(HeightStructuresFailureMechanismContext context)
{
ValidateAll(context.WrappedData.Calculations.OfType>(),
context.Parent);
}
private void CalculateAll(HeightStructuresFailureMechanismContext context)
{
CalculateAll(context.WrappedData, context.WrappedData.Calculations.OfType>(), context.Parent);
}
#endregion
#region HeightStructuresCalculationGroupContext TreeNodeInfo
private static object[] CalculationGroupContextChildNodeObjects(HeightStructuresCalculationGroupContext context)
{
var childNodeObjects = new List();
foreach (ICalculationBase calculationItem in context.WrappedData.Children)
{
var calculation = calculationItem as StructuresCalculation;
var group = calculationItem as CalculationGroup;
if (calculation != null)
{
childNodeObjects.Add(new HeightStructuresCalculationContext(calculation,
context.FailureMechanism,
context.AssessmentSection));
}
else if (group != null)
{
childNodeObjects.Add(new HeightStructuresCalculationGroupContext(group,
context.FailureMechanism,
context.AssessmentSection));
}
else
{
childNodeObjects.Add(calculationItem);
}
}
return childNodeObjects.ToArray();
}
private ContextMenuStrip CalculationGroupContextContextMenuStrip(HeightStructuresCalculationGroupContext context, object parentData, TreeViewControl treeViewControl)
{
var group = context.WrappedData;
var builder = new RingtoetsContextMenuBuilder(Gui.Get(context, treeViewControl));
var isNestedGroup = parentData is HeightStructuresCalculationGroupContext;
if (!isNestedGroup)
{
builder.AddCustomItem(CreateGenerateHeightStructuresCalculationsItem(context))
.AddSeparator();
}
builder.AddCreateCalculationGroupItem(group)
.AddCreateCalculationItem(context, AddCalculation);
if (!isNestedGroup)
{
builder.AddSeparator()
.AddRemoveAllChildrenItem();
}
builder.AddSeparator()
.AddValidateAllCalculationsInGroupItem(
context,
ValidateAll,
ValidateAllDataAvailableAndGetErrorMessageForCalculationsInGroup)
.AddPerformAllCalculationsInGroupItem(group, context, CalculateAll, ValidateAllDataAvailableAndGetErrorMessageForCalculationsInGroup)
.AddClearAllCalculationOutputInGroupItem(group)
.AddSeparator();
if (isNestedGroup)
{
builder.AddRenameItem()
.AddDeleteItem()
.AddSeparator();
}
return builder.AddExpandAllItem()
.AddCollapseAllItem()
.AddSeparator()
.AddPropertiesItem()
.Build();
}
private StrictContextMenuItem CreateGenerateHeightStructuresCalculationsItem(HeightStructuresCalculationGroupContext nodeData)
{
ObservableList heightStructures = nodeData.FailureMechanism.HeightStructures;
bool structuresAvailable = heightStructures.Any();
string heightStructuresCalculationGroupContextToolTip = structuresAvailable
? RingtoetsCommonFormsResources.StructuresPlugin_Generate_calculations_for_selected_structures
: RingtoetsCommonFormsResources.StructuresPlugin_No_structures_to_generate_for;
return new StrictContextMenuItem(RingtoetsCommonFormsResources.CalculationGroup_Generate_calculations,
heightStructuresCalculationGroupContextToolTip,
RingtoetsCommonFormsResources.GenerateScenariosIcon,
(sender, args) => { ShowHeightStructuresSelectionDialog(nodeData); })
{
Enabled = structuresAvailable
};
}
private void ShowHeightStructuresSelectionDialog(HeightStructuresCalculationGroupContext nodeData)
{
using (var dialog = new StructureSelectionDialog(Gui.MainWindow, nodeData.FailureMechanism.HeightStructures))
{
dialog.ShowDialog();
if (dialog.SelectedItems.Any())
{
GenerateHeightStructuresCalculations(
nodeData.FailureMechanism.SectionResults,
dialog.SelectedItems.Cast(),
nodeData.WrappedData.Children);
nodeData.NotifyObservers();
}
}
}
private static void GenerateHeightStructuresCalculations(IEnumerable sectionResults, IEnumerable structures, IList calculations)
{
foreach (var structure in structures)
{
var calculation = new StructuresCalculation
{
Name = NamingHelper.GetUniqueName(calculations, structure.Name, c => c.Name),
InputParameters =
{
Structure = structure
}
};
calculations.Add(calculation);
StructuresHelper.Update(sectionResults, calculation);
}
}
private static void CalculationGroupContextOnNodeRemoved(HeightStructuresCalculationGroupContext context, object parentNodeData)
{
var parentGroupContext = (HeightStructuresCalculationGroupContext) parentNodeData;
parentGroupContext.WrappedData.Children.Remove(context.WrappedData);
var heightStructuresCalculations = context.FailureMechanism.Calculations.Cast>().ToArray();
foreach (var calculation in context.WrappedData.GetCalculations().Cast>())
{
StructuresHelper.Delete(context.FailureMechanism.SectionResults,
calculation,
heightStructuresCalculations);
}
parentGroupContext.NotifyObservers();
}
private static void AddCalculation(HeightStructuresCalculationGroupContext context)
{
var calculation = new StructuresCalculation
{
Name = NamingHelper.GetUniqueName(context.WrappedData.Children, RingtoetsCommonDataResources.Calculation_DefaultName, c => c.Name)
};
context.WrappedData.Children.Add(calculation);
context.WrappedData.NotifyObservers();
}
private static string ValidateAllDataAvailableAndGetErrorMessageForCalculationsInGroup(HeightStructuresCalculationGroupContext context)
{
return ValidateAllDataAvailableAndGetErrorMessage(context.AssessmentSection, context.FailureMechanism);
}
private static void ValidateAll(HeightStructuresCalculationGroupContext context)
{
ValidateAll(context.WrappedData.GetCalculations().OfType>(), context.AssessmentSection);
}
private void CalculateAll(CalculationGroup group, HeightStructuresCalculationGroupContext context)
{
CalculateAll(context.FailureMechanism, group.GetCalculations().OfType>(), context.AssessmentSection);
}
#endregion
#region HeightStructuresCalculationContext TreeNodeInfo
private static object[] CalculationContextChildNodeObjects(HeightStructuresCalculationContext context)
{
var childNodes = new List
{
new CommentContext(context.WrappedData),
new HeightStructuresInputContext(context.WrappedData.InputParameters,
context.WrappedData,
context.FailureMechanism,
context.AssessmentSection)
};
if (context.WrappedData.HasOutput)
{
childNodes.Add(context.WrappedData.Output);
}
else
{
childNodes.Add(new EmptyProbabilityAssessmentOutput());
}
return childNodes.ToArray();
}
private ContextMenuStrip CalculationContextContextMenuStrip(HeightStructuresCalculationContext context, object parentData, TreeViewControl treeViewControl)
{
var builder = new RingtoetsContextMenuBuilder(Gui.Get(context, treeViewControl));
StructuresCalculation calculation = context.WrappedData;
return builder.AddValidateCalculationItem(
context,
Validate,
ValidateAllDataAvailableAndGetErrorMessageForCalculation)
.AddPerformCalculationItem(calculation, context, Calculate, ValidateAllDataAvailableAndGetErrorMessageForCalculation)
.AddClearCalculationOutputItem(calculation)
.AddSeparator()
.AddRenameItem()
.AddDeleteItem()
.AddSeparator()
.AddExpandAllItem()
.AddCollapseAllItem()
.AddSeparator()
.AddPropertiesItem()
.Build();
}
private static string ValidateAllDataAvailableAndGetErrorMessageForCalculation(HeightStructuresCalculationContext context)
{
return ValidateAllDataAvailableAndGetErrorMessage(context.AssessmentSection, context.FailureMechanism);
}
private static void Validate(HeightStructuresCalculationContext context)
{
HeightStructuresCalculationService.Validate(context.WrappedData, context.AssessmentSection);
}
private void Calculate(StructuresCalculation calculation, HeightStructuresCalculationContext context)
{
ActivityProgressDialogRunner.Run(Gui.MainWindow, new HeightStructuresCalculationActivity(calculation,
context.AssessmentSection.HydraulicBoundaryDatabase.FilePath,
context.FailureMechanism,
context.AssessmentSection));
}
private static void CalculationContextOnNodeRemoved(HeightStructuresCalculationContext context, object parentData)
{
var calculationGroupContext = parentData as HeightStructuresCalculationGroupContext;
if (calculationGroupContext != null)
{
calculationGroupContext.WrappedData.Children.Remove(context.WrappedData);
StructuresHelper.Delete(context.FailureMechanism.SectionResults, context.WrappedData, context.FailureMechanism.Calculations.Cast>());
calculationGroupContext.NotifyObservers();
}
}
#endregion
#endregion
}
}