// Copyright (C) Stichting Deltares 2021. All rights reserved.
//
// This file is part of Riskeer.
//
// Riskeer 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.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Core.Common.Base;
using Core.Common.Base.IO;
using Core.Common.Controls.TreeView;
using Core.Common.Util;
using Core.Gui.ContextMenu;
using Core.Gui.Forms.ProgressDialog;
using Core.Gui.Helpers;
using Core.Gui.Plugin;
using Riskeer.ClosingStructures.Data;
using Riskeer.ClosingStructures.Forms.PresentationObjects;
using Riskeer.ClosingStructures.Forms.PropertyClasses;
using Riskeer.ClosingStructures.Forms.Views;
using Riskeer.ClosingStructures.IO;
using Riskeer.ClosingStructures.IO.Configurations;
using Riskeer.ClosingStructures.Plugin.FileImporters;
using Riskeer.ClosingStructures.Service;
using Riskeer.Common.Data.AssessmentSection;
using Riskeer.Common.Data.Calculation;
using Riskeer.Common.Data.FailureMechanism;
using Riskeer.Common.Data.Structures;
using Riskeer.Common.Forms;
using Riskeer.Common.Forms.ChangeHandlers;
using Riskeer.Common.Forms.ExportInfos;
using Riskeer.Common.Forms.Helpers;
using Riskeer.Common.Forms.ImportInfos;
using Riskeer.Common.Forms.PresentationObjects;
using Riskeer.Common.Forms.PropertyClasses;
using Riskeer.Common.Forms.TreeNodeInfos;
using Riskeer.Common.Forms.UpdateInfos;
using Riskeer.Common.IO.FileImporters.MessageProviders;
using Riskeer.Common.IO.Structures;
using Riskeer.Common.Service;
using Riskeer.Common.Util;
using Riskeer.Common.Util.Helpers;
using RiskeerCommonFormsResources = Riskeer.Common.Forms.Properties.Resources;
using RiskeerCommonDataResources = Riskeer.Common.Data.Properties.Resources;
using RiskeerCommonIOResources = Riskeer.Common.IO.Properties.Resources;
namespace Riskeer.ClosingStructures.Plugin
{
///
/// The plug-in for the .
///
public class ClosingStructuresPlugin : PluginBase
{
public override IEnumerable GetPropertyInfos()
{
yield return new PropertyInfo
{
CreateInstance = context => new ClosingStructuresCalculationsProperties(context.WrappedData)
};
yield return new PropertyInfo
{
CreateInstance = context => new ClosingStructuresFailurePathProperties(context.WrappedData)
};
yield return new PropertyInfo();
yield return new PropertyInfo>
{
CreateInstance = context => new StructureCollectionProperties(context.WrappedData)
};
yield return new PropertyInfo
{
CreateInstance = context => new ClosingStructuresInputContextProperties(
context,
new ObservablePropertyChangeHandler(context.Calculation, context.WrappedData))
};
}
public override IEnumerable GetViewInfos()
{
yield return new ViewInfo
{
GetViewName = (view, context) => context.WrappedData.Name,
Image = RiskeerCommonFormsResources.FailureMechanismIcon,
CreateInstance = context => new ClosingStructuresFailureMechanismView(context.WrappedData, context.Parent)
};
yield return new ViewInfo
{
GetViewName = (view, context) => context.WrappedData.Name,
Image = RiskeerCommonFormsResources.FailureMechanismIcon,
CreateInstance = context => new ClosingStructuresFailurePathView(context.WrappedData, context.Parent),
CloseForData = CloseFailurePathViewForData
};
yield return new ViewInfo<
ProbabilityFailureMechanismSectionResultContext,
IObservableEnumerable,
ClosingStructuresFailureMechanismResultView>
{
GetViewName = (view, context) => RiskeerCommonFormsResources.FailureMechanism_AssessmentResult_DisplayName,
Image = RiskeerCommonFormsResources.FailureMechanismSectionResultIcon,
CloseForData = CloseFailureMechanismResultViewForData,
GetViewData = context => context.WrappedData,
CreateInstance = context => new ClosingStructuresFailureMechanismResultView(
context.WrappedData,
(ClosingStructuresFailureMechanism) context.FailureMechanism, context.AssessmentSection)
};
yield return new ViewInfo
{
GetViewData = context => context.WrappedData,
GetViewName = (view, context) => RiskeerCommonFormsResources.Scenarios_DisplayName,
Image = RiskeerCommonFormsResources.ScenariosIcon,
CreateInstance = context => new ClosingStructuresScenariosView(context.WrappedData, context.ParentFailureMechanism),
CloseForData = CloseScenariosViewForData
};
yield return new ViewInfo
{
CreateInstance = context => new ClosingStructuresCalculationsView(context.WrappedData, context.FailureMechanism, context.AssessmentSection),
GetViewData = context => context.WrappedData,
GetViewName = (view, context) => context.WrappedData.Name,
Image = RiskeerCommonFormsResources.GeneralFolderIcon,
AdditionalDataCheck = context => context.WrappedData == context.FailureMechanism.CalculationsGroup,
CloseForData = CloseCalculationsViewForData
};
}
public override IEnumerable GetTreeNodeInfos()
{
yield return RiskeerTreeNodeInfoFactory.CreateFailureMechanismStateContextTreeNodeInfo(
CalculationsChildNodeObjects,
CalculationsContextMenuStrip);
yield return RiskeerTreeNodeInfoFactory.CreateFailurePathContextTreeNodeInfo(
FailurePathEnabledChildNodeObjects,
FailurePathDisabledChildNodeObjects,
FailurePathEnabledContextMenuStrip,
FailurePathDisabledContextMenuStrip);
yield return RiskeerTreeNodeInfoFactory.CreateCalculationGroupContextTreeNodeInfo(
CalculationGroupContextChildNodeObjects,
CalculationGroupContextContextMenuStrip,
CalculationGroupContextOnNodeRemoved);
yield return RiskeerTreeNodeInfoFactory.CreateCalculationContextTreeNodeInfo(
CalculationContextChildNodeObjects,
CalculationContextContextMenuStrip,
CalculationContextOnNodeRemoved,
CalculationType.Probabilistic);
yield return new TreeNodeInfo>
{
Text = context => RiskeerCommonFormsResources.FailureMechanism_AssessmentResult_DisplayName,
Image = context => RiskeerCommonFormsResources.FailureMechanismSectionResultIcon,
ContextMenuStrip = (nodeData, parentData, treeViewControl) => Gui.Get(nodeData, treeViewControl)
.AddOpenItem()
.Build()
};
yield return new TreeNodeInfo
{
Text = context => RiskeerCommonFormsResources.StructuresCollection_DisplayName,
Image = context => RiskeerCommonFormsResources.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()
.AddUpdateItem()
.AddSeparator()
.AddCollapseAllItem()
.AddExpandAllItem()
.AddSeparator()
.AddPropertiesItem()
.Build()
};
yield return new TreeNodeInfo
{
Text = structure => structure.Name,
Image = structure => RiskeerCommonFormsResources.StructuresIcon,
ContextMenuStrip = (structure, parentData, treeViewControl) => Gui.Get(structure, treeViewControl)
.AddPropertiesItem()
.Build()
};
yield return new TreeNodeInfo
{
Text = inputContext => RiskeerCommonFormsResources.FailureMechanism_Inputs_DisplayName,
Image = inputContext => RiskeerCommonFormsResources.GenericInputOutputIcon,
ContextMenuStrip = (nodeData, parentData, treeViewControl) => Gui.Get(nodeData, treeViewControl)
.AddPropertiesItem()
.Build()
};
yield return new TreeNodeInfo
{
Text = context => RiskeerCommonFormsResources.Scenarios_DisplayName,
Image = context => RiskeerCommonFormsResources.ScenariosIcon,
ContextMenuStrip = (nodeData, parentData, treeViewControl) => Gui.Get(nodeData, treeViewControl)
.AddOpenItem()
.Build()
};
}
public override IEnumerable GetImportInfos()
{
yield return new ImportInfo
{
CreateFileImporter = (context, filePath) => CreateClosingStructuresImporter(
context,
filePath,
new ImportMessageProvider(),
new ClosingStructureReplaceDataStrategy(context.FailureMechanism)),
Name = RiskeerCommonFormsResources.StructuresImporter_DisplayName,
Category = RiskeerCommonFormsResources.Riskeer_Category,
Image = RiskeerCommonFormsResources.StructuresIcon,
FileFilterGenerator = CreateClosingStructureFileFilter(),
IsEnabled = context => context.AssessmentSection.ReferenceLine.Points.Any(),
VerifyUpdates = context => VerifyStructuresShouldUpdate(
context.FailureMechanism,
RiskeerCommonIOResources.VerifyStructuresShouldUpdate_When_importing_Calculation_with_Structure_data_output_will_be_cleared_confirm)
};
yield return RiskeerImportInfoFactory.CreateCalculationConfigurationImportInfo(
(context, filePath) => new ClosingStructuresCalculationConfigurationImporter(
filePath,
context.WrappedData,
context.AssessmentSection.HydraulicBoundaryDatabase.Locations,
context.AvailableForeshoreProfiles,
context.AvailableStructures));
}
public override IEnumerable GetUpdateInfos()
{
yield return new UpdateInfo
{
CreateFileImporter = (context, filePath) => CreateClosingStructuresImporter(
context,
filePath,
new UpdateMessageProvider(),
new ClosingStructureUpdateDataStrategy(context.FailureMechanism)),
Name = RiskeerCommonDataResources.StructureCollection_TypeDescriptor,
Category = RiskeerCommonFormsResources.Riskeer_Category,
Image = RiskeerCommonFormsResources.StructuresIcon,
FileFilterGenerator = CreateClosingStructureFileFilter(),
IsEnabled = c => c.FailureMechanism.ClosingStructures.SourcePath != null,
CurrentPath = context => context.WrappedData.SourcePath,
VerifyUpdates = context => VerifyStructuresShouldUpdate(
context.FailureMechanism,
RiskeerCommonIOResources.VerifyStructuresShouldUpdate_When_updating_Calculation_with_Structure_data_output_will_be_cleared_confirm)
};
yield return RiskeerUpdateInfoFactory.CreateFailureMechanismSectionsUpdateInfo<
ClosingStructuresFailureMechanismSectionsContext, ClosingStructuresFailureMechanism, ClosingStructuresFailureMechanismSectionResult>(
new ClosingStructuresFailureMechanismSectionResultUpdateStrategy());
}
public override IEnumerable GetExportInfos()
{
yield return RiskeerExportInfoFactory.CreateCalculationGroupConfigurationExportInfo(
(context, filePath) => new ClosingStructuresCalculationConfigurationExporter(context.WrappedData.Children, filePath),
context => context.WrappedData.Children.Any(),
GetInquiryHelper());
yield return RiskeerExportInfoFactory.CreateCalculationConfigurationExportInfo(
(context, filePath) => new ClosingStructuresCalculationConfigurationExporter(new[]
{
context.WrappedData
}, filePath),
GetInquiryHelper());
}
#region ViewInfos
private static bool CloseFailurePathViewForData(ClosingStructuresFailurePathView view, object dataToCloseFor)
{
var assessmentSection = dataToCloseFor as IAssessmentSection;
var failureMechanism = dataToCloseFor as ClosingStructuresFailureMechanism;
return assessmentSection != null
? ReferenceEquals(view.AssessmentSection, assessmentSection)
: ReferenceEquals(view.FailureMechanism, failureMechanism);
}
private static bool CloseFailureMechanismResultViewForData(ClosingStructuresFailureMechanismResultView view, object dataToCloseFor)
{
var failureMechanism = dataToCloseFor as ClosingStructuresFailureMechanism;
if (dataToCloseFor is IAssessmentSection assessmentSection)
{
failureMechanism = assessmentSection.GetFailureMechanisms()
.OfType()
.FirstOrDefault();
}
if (dataToCloseFor is IFailurePathContext failurePathContext)
{
failureMechanism = failurePathContext.WrappedData;
}
return failureMechanism != null && ReferenceEquals(view.FailureMechanism.SectionResults, failureMechanism.SectionResults);
}
private static bool CloseScenariosViewForData(ClosingStructuresScenariosView view, object dataToCloseFor)
{
var failureMechanism = dataToCloseFor as ClosingStructuresFailureMechanism;
if (dataToCloseFor is IAssessmentSection assessmentSection)
{
failureMechanism = assessmentSection.GetFailureMechanisms()
.OfType()
.FirstOrDefault();
}
if (dataToCloseFor is IFailurePathContext failurePathContext)
{
failureMechanism = failurePathContext.WrappedData;
}
return failureMechanism != null && ReferenceEquals(view.Data, failureMechanism.CalculationsGroup);
}
private static bool CloseCalculationsViewForData(ClosingStructuresCalculationsView view, object dataToCloseFor)
{
ClosingStructuresFailureMechanism failureMechanism = null;
if (dataToCloseFor is ClosingStructuresCalculationsContext failureMechanismContext)
{
failureMechanism = failureMechanismContext.WrappedData;
}
if (dataToCloseFor is IAssessmentSection assessmentSection)
{
failureMechanism = assessmentSection.GetFailureMechanisms()
.OfType()
.FirstOrDefault();
}
return failureMechanism != null && ReferenceEquals(view.Data, failureMechanism.CalculationsGroup);
}
#endregion
#region TreeNodeInfos
#region ClosingStructuresCalculationsContext TreeNodeInfo
private static object[] CalculationsChildNodeObjects(ClosingStructuresCalculationsContext context)
{
ClosingStructuresFailureMechanism failureMechanism = context.WrappedData;
IAssessmentSection assessmentSection = context.Parent;
return new object[]
{
new CategoryTreeFolder(RiskeerCommonFormsResources.FailureMechanism_Inputs_DisplayName,
GetCalculationsInputs(failureMechanism, assessmentSection), TreeFolderCategory.Input),
new ClosingStructuresCalculationGroupContext(failureMechanism.CalculationsGroup, null, failureMechanism, assessmentSection)
};
}
private static IEnumerable GetCalculationsInputs(ClosingStructuresFailureMechanism failureMechanism, IAssessmentSection assessmentSection)
{
return new object[]
{
new ForeshoreProfilesContext(failureMechanism.ForeshoreProfiles, failureMechanism, assessmentSection),
new ClosingStructuresContext(failureMechanism.ClosingStructures, failureMechanism, assessmentSection),
failureMechanism.InputComments
};
}
private ContextMenuStrip CalculationsContextMenuStrip(ClosingStructuresCalculationsContext context,
object parentData,
TreeViewControl treeViewControl)
{
IEnumerable> calculations = context.WrappedData
.Calculations
.Cast>();
IInquiryHelper inquiryHelper = GetInquiryHelper();
var builder = new RiskeerContextMenuBuilder(Gui.Get(context, treeViewControl));
return builder.AddOpenItem()
.AddSeparator()
.AddValidateAllCalculationsInFailureMechanismItem(
context,
ValidateAllInFailureMechanism,
EnableValidateAndCalculateMenuItemForFailureMechanism)
.AddPerformAllCalculationsInFailureMechanismItem(
context,
CalculateAllInFailureMechanism,
EnableValidateAndCalculateMenuItemForFailureMechanism)
.AddSeparator()
.AddClearAllCalculationOutputInFailureMechanismItem(context.WrappedData)
.AddClearIllustrationPointsOfCalculationsInFailureMechanismItem(() => IllustrationPointsHelper.HasIllustrationPoints(calculations),
CreateChangeHandler(inquiryHelper, calculations))
.AddSeparator()
.AddCollapseAllItem()
.AddExpandAllItem()
.AddSeparator()
.AddPropertiesItem()
.Build();
}
private static string EnableValidateAndCalculateMenuItemForFailureMechanism(ClosingStructuresCalculationsContext context)
{
return EnableValidateAndCalculateMenuItem(context.Parent);
}
private static void ValidateAllInFailureMechanism(ClosingStructuresCalculationsContext context)
{
ValidateAll(context.WrappedData.Calculations.OfType>(),
context.Parent);
}
private void CalculateAllInFailureMechanism(ClosingStructuresCalculationsContext context)
{
ActivityProgressDialogRunner.Run(Gui.MainWindow,
ClosingStructuresCalculationActivityFactory.CreateCalculationActivities(context.WrappedData, context.Parent));
}
#endregion
#region ClosingStructuresFailurePathContext TreeNodeInfo
private static object[] FailurePathEnabledChildNodeObjects(ClosingStructuresFailurePathContext context)
{
ClosingStructuresFailureMechanism failureMechanism = context.WrappedData;
IAssessmentSection assessmentSection = context.Parent;
return new object[]
{
new CategoryTreeFolder(RiskeerCommonFormsResources.FailureMechanism_Inputs_DisplayName,
GetFailurePathInputs(failureMechanism, assessmentSection), TreeFolderCategory.Input),
new CategoryTreeFolder(RiskeerCommonFormsResources.FailureMechanism_Outputs_DisplayName,
GetFailurePathOutputs(failureMechanism, assessmentSection), TreeFolderCategory.Output)
};
}
private static object[] FailurePathDisabledChildNodeObjects(ClosingStructuresFailurePathContext context)
{
return new object[]
{
context.WrappedData.NotRelevantComments
};
}
private static IEnumerable GetFailurePathInputs(ClosingStructuresFailureMechanism failureMechanism, IAssessmentSection assessmentSection)
{
return new object[]
{
new ClosingStructuresFailureMechanismSectionsContext(failureMechanism, assessmentSection),
failureMechanism.InputComments
};
}
private static IEnumerable GetFailurePathOutputs(ClosingStructuresFailureMechanism failureMechanism, IAssessmentSection assessmentSection)
{
return new object[]
{
new FailureMechanismAssemblyCategoriesContext(failureMechanism, assessmentSection, () => failureMechanism.GeneralInput.N),
new ClosingStructuresScenariosContext(failureMechanism.CalculationsGroup, failureMechanism),
new ProbabilityFailureMechanismSectionResultContext(
failureMechanism.SectionResults, failureMechanism, assessmentSection),
failureMechanism.OutputComments
};
}
private ContextMenuStrip FailurePathEnabledContextMenuStrip(ClosingStructuresFailurePathContext context,
object parentData,
TreeViewControl treeViewControl)
{
var builder = new RiskeerContextMenuBuilder(Gui.Get(context, treeViewControl));
return builder.AddOpenItem()
.AddSeparator()
.AddToggleRelevancyOfFailurePathItem(context, RemoveAllViewsForItem)
.AddSeparator()
.AddCollapseAllItem()
.AddExpandAllItem()
.AddSeparator()
.AddPropertiesItem()
.Build();
}
private ContextMenuStrip FailurePathDisabledContextMenuStrip(ClosingStructuresFailurePathContext context,
object parentData,
TreeViewControl treeViewControl)
{
var builder = new RiskeerContextMenuBuilder(Gui.Get(context, treeViewControl));
return builder.AddToggleRelevancyOfFailurePathItem(context, RemoveAllViewsForItem)
.AddSeparator()
.AddCollapseAllItem()
.AddExpandAllItem()
.AddSeparator()
.AddPropertiesItem()
.Build();
}
private void RemoveAllViewsForItem(ClosingStructuresFailurePathContext context)
{
Gui.ViewCommands.RemoveAllViewsForItem(context);
}
#endregion
#region ClosingStructuresCalculationGroupContext TreeNodeInfo
private static object[] CalculationGroupContextChildNodeObjects(ClosingStructuresCalculationGroupContext context)
{
var childNodeObjects = new List();
foreach (ICalculationBase calculationItem in context.WrappedData.Children)
{
if (calculationItem is StructuresCalculationScenario calculation)
{
childNodeObjects.Add(new ClosingStructuresCalculationScenarioContext(calculation,
context.WrappedData,
context.FailureMechanism,
context.AssessmentSection));
}
else if (calculationItem is CalculationGroup group)
{
childNodeObjects.Add(new ClosingStructuresCalculationGroupContext(group,
context.WrappedData,
context.FailureMechanism,
context.AssessmentSection));
}
else
{
childNodeObjects.Add(calculationItem);
}
}
return childNodeObjects.ToArray();
}
private ContextMenuStrip CalculationGroupContextContextMenuStrip(ClosingStructuresCalculationGroupContext context,
object parentData,
TreeViewControl treeViewControl)
{
CalculationGroup group = context.WrappedData;
IInquiryHelper inquiryHelper = GetInquiryHelper();
var builder = new RiskeerContextMenuBuilder(Gui.Get(context, treeViewControl));
bool isNestedGroup = parentData is ClosingStructuresCalculationGroupContext;
StructuresCalculation[] calculations = group
.GetCalculations()
.Cast>().ToArray();
if (!isNestedGroup)
{
builder.AddOpenItem()
.AddSeparator();
}
builder.AddImportItem()
.AddExportItem()
.AddSeparator();
if (isNestedGroup)
{
builder.AddDuplicateCalculationItem(group, context)
.AddSeparator();
}
else
{
builder.AddCustomItem(CreateGenerateClosingStructuresCalculationsItem(context))
.AddSeparator();
}
builder.AddCreateCalculationGroupItem(group)
.AddCreateCalculationItem(context, AddCalculation, CalculationType.Probabilistic)
.AddSeparator();
if (isNestedGroup)
{
builder.AddRenameItem();
}
builder.AddUpdateForeshoreProfileOfCalculationsItem(calculations, inquiryHelper,
SynchronizeCalculationWithForeshoreProfileHelper.UpdateForeshoreProfileDerivedCalculationInput)
.AddCustomItem(CreateUpdateAllStructuresItem(calculations))
.AddSeparator()
.AddValidateAllCalculationsInGroupItem(
context,
ValidateAllInCalculationGroup,
EnableValidateAndCalculateMenuItemForCalculationGroup)
.AddPerformAllCalculationsInGroupItem(
context,
CalculateAllInCalculationGroup,
EnableValidateAndCalculateMenuItemForCalculationGroup)
.AddSeparator()
.AddClearAllCalculationOutputInGroupItem(group)
.AddClearIllustrationPointsOfCalculationsInGroupItem(() => IllustrationPointsHelper.HasIllustrationPoints(calculations),
CreateChangeHandler(inquiryHelper, calculations));
if (isNestedGroup)
{
builder.AddDeleteItem();
}
else
{
builder.AddRemoveAllChildrenItem();
}
return builder.AddSeparator()
.AddCollapseAllItem()
.AddExpandAllItem()
.AddSeparator()
.AddPropertiesItem()
.Build();
}
private StrictContextMenuItem CreateUpdateAllStructuresItem(
IEnumerable> calculations)
{
var contextMenuEnabled = true;
string toolTipMessage = RiskeerCommonFormsResources.StructuresPlugin_CreateUpdateStructureItem_Update_all_calculations_with_Structure_Tooltip;
StructuresCalculation[] calculationsToUpdate = calculations
.Where(calc => calc.InputParameters.Structure != null
&& !calc.InputParameters.IsStructureInputSynchronized)
.ToArray();
if (!calculationsToUpdate.Any())
{
contextMenuEnabled = false;
toolTipMessage = RiskeerCommonFormsResources.CreateUpdateContextMenuItem_No_calculations_to_update_ToolTip;
}
return new StrictContextMenuItem(RiskeerCommonFormsResources.StructuresPlugin_CreateUpdateStructureItem_Update_all_Structures,
toolTipMessage,
RiskeerCommonFormsResources.UpdateItemIcon,
(o, args) => UpdateStructureDependentDataOfCalculations(calculationsToUpdate))
{
Enabled = contextMenuEnabled
};
}
private void UpdateStructureDependentDataOfCalculations(IEnumerable> calculations)
{
string message = RiskeerCommonFormsResources.VerifyUpdate_Confirm_calculation_outputs_cleared;
if (StructureDependentDataShouldUpdate(calculations, message))
{
foreach (StructuresCalculation calculation in calculations)
{
UpdateStructureDerivedCalculationInput(calculation);
}
}
}
private StrictContextMenuItem CreateGenerateClosingStructuresCalculationsItem(ClosingStructuresCalculationGroupContext nodeData)
{
bool structuresAvailable = nodeData.FailureMechanism.ClosingStructures.Any();
string closingStructuresCalculationGroupContextToolTip = structuresAvailable
? RiskeerCommonFormsResources.Generate_Calculations_for_selected_Structures
: RiskeerCommonFormsResources.No_Structures_to_generate_Calculations_for;
return new StrictContextMenuItem(RiskeerCommonFormsResources.CalculationGroup_Generate_calculations,
closingStructuresCalculationGroupContextToolTip,
RiskeerCommonFormsResources.GenerateScenariosIcon,
(sender, args) => ShowClosingStructuresSelectionDialog(nodeData))
{
Enabled = structuresAvailable
};
}
private void ShowClosingStructuresSelectionDialog(ClosingStructuresCalculationGroupContext nodeData)
{
using (var dialog = new StructureSelectionDialog(Gui.MainWindow, nodeData.FailureMechanism.ClosingStructures))
{
dialog.ShowDialog();
if (dialog.SelectedItems.Any())
{
StructureCalculationConfigurationHelper.GenerateCalculations(nodeData.WrappedData, dialog.SelectedItems.Cast());
nodeData.NotifyObservers();
}
}
}
private static void AddCalculation(ClosingStructuresCalculationGroupContext context)
{
var calculation = new StructuresCalculationScenario
{
Name = NamingHelper.GetUniqueName(context.WrappedData.Children, RiskeerCommonDataResources.Calculation_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();
}
private static string EnableValidateAndCalculateMenuItemForCalculationGroup(ClosingStructuresCalculationGroupContext context)
{
return EnableValidateAndCalculateMenuItem(context.AssessmentSection);
}
private static void ValidateAllInCalculationGroup(ClosingStructuresCalculationGroupContext context)
{
ValidateAll(context.WrappedData.GetCalculations().OfType>(), context.AssessmentSection);
}
private void CalculateAllInCalculationGroup(ClosingStructuresCalculationGroupContext context)
{
ActivityProgressDialogRunner.Run(Gui.MainWindow,
ClosingStructuresCalculationActivityFactory.CreateCalculationActivities(context.WrappedData,
context.FailureMechanism,
context.AssessmentSection));
}
#endregion
#region ClosingStructuresCalculationScenarioContext TreeNodeInfo
private static object[] CalculationContextChildNodeObjects(ClosingStructuresCalculationScenarioContext context)
{
StructuresCalculation calculation = context.WrappedData;
return new object[]
{
calculation.Comments,
new ClosingStructuresInputContext(calculation.InputParameters,
calculation,
context.FailureMechanism,
context.AssessmentSection),
new StructuresOutputContext(calculation)
};
}
private ContextMenuStrip CalculationContextContextMenuStrip(ClosingStructuresCalculationScenarioContext context,
object parentData,
TreeViewControl treeViewControl)
{
StructuresCalculation calculation = context.WrappedData;
var changeHandler = new ClearIllustrationPointsOfStructuresCalculationHandler(GetInquiryHelper(), calculation);
var builder = new RiskeerContextMenuBuilder(Gui.Get(context, treeViewControl));
return builder.AddExportItem()
.AddSeparator()
.AddDuplicateCalculationItem(calculation, context)
.AddSeparator()
.AddRenameItem()
.AddUpdateForeshoreProfileOfCalculationItem(calculation,
GetInquiryHelper(),
SynchronizeCalculationWithForeshoreProfileHelper.UpdateForeshoreProfileDerivedCalculationInput)
.AddCustomItem(CreateUpdateStructureItem(context))
.AddSeparator()
.AddValidateCalculationItem(
context,
Validate,
EnableValidateAndCalculateMenuItemForCalculation)
.AddPerformCalculationItem, ClosingStructuresCalculationScenarioContext>(
context,
Calculate,
EnableValidateAndCalculateMenuItemForCalculation)
.AddSeparator()
.AddClearCalculationOutputItem(calculation)
.AddClearIllustrationPointsOfCalculationItem(() => IllustrationPointsHelper.HasIllustrationPoints(calculation), changeHandler)
.AddDeleteItem()
.AddSeparator()
.AddCollapseAllItem()
.AddExpandAllItem()
.AddSeparator()
.AddPropertiesItem()
.Build();
}
private static string EnableValidateAndCalculateMenuItemForCalculation(ClosingStructuresCalculationScenarioContext context)
{
return EnableValidateAndCalculateMenuItem(context.AssessmentSection);
}
private static void Validate(ClosingStructuresCalculationScenarioContext context)
{
ClosingStructuresCalculationService.Validate(context.WrappedData, context.AssessmentSection);
}
private void Calculate(ClosingStructuresCalculationScenarioContext context)
{
ActivityProgressDialogRunner.Run(Gui.MainWindow,
ClosingStructuresCalculationActivityFactory.CreateCalculationActivity(context.WrappedData,
context.FailureMechanism,
context.AssessmentSection));
}
private static void CalculationContextOnNodeRemoved(ClosingStructuresCalculationScenarioContext context, object parentData)
{
if (parentData is ClosingStructuresCalculationGroupContext calculationGroupContext)
{
calculationGroupContext.WrappedData.Children.Remove(context.WrappedData);
calculationGroupContext.NotifyObservers();
}
}
private StrictContextMenuItem CreateUpdateStructureItem(ClosingStructuresCalculationScenarioContext context)
{
var contextMenuEnabled = true;
string toolTipMessage = RiskeerCommonFormsResources.Update_Calculation_with_Structure_ToolTip;
if (context.WrappedData.InputParameters.Structure == null)
{
contextMenuEnabled = false;
toolTipMessage = RiskeerCommonFormsResources.Structure_must_be_selected_ToolTip;
}
else if (context.WrappedData.InputParameters.IsStructureInputSynchronized)
{
contextMenuEnabled = false;
toolTipMessage = RiskeerCommonFormsResources.CalculationItem_No_changes_to_update_ToolTip;
}
return new StrictContextMenuItem(
RiskeerCommonFormsResources.Update_Structure_data,
toolTipMessage,
RiskeerCommonFormsResources.UpdateItemIcon,
(o, args) => UpdateStructureDependentDataOfCalculation(context.WrappedData))
{
Enabled = contextMenuEnabled
};
}
private void UpdateStructureDependentDataOfCalculation(StructuresCalculation calculation)
{
string message = RiskeerCommonFormsResources.VerifyUpdate_Confirm_calculation_output_cleared;
if (StructureDependentDataShouldUpdate(new[]
{
calculation
}, message))
{
UpdateStructureDerivedCalculationInput(calculation);
}
}
private bool StructureDependentDataShouldUpdate(IEnumerable> calculations, string query)
{
var changeHandler = new CalculationChangeHandler(calculations, query, GetInquiryHelper());
return !changeHandler.RequireConfirmation() || changeHandler.InquireConfirmation();
}
private static void UpdateStructureDerivedCalculationInput(StructuresCalculation calculation)
{
calculation.InputParameters.SynchronizeStructureInput();
var affectedObjects = new List
{
calculation.InputParameters
};
affectedObjects.AddRange(RiskeerCommonDataSynchronizationService.ClearCalculationOutput(calculation));
foreach (IObservable affectedObject in affectedObjects)
{
affectedObject.NotifyObservers();
}
}
#endregion
private ClearIllustrationPointsOfStructureCalculationCollectionChangeHandler CreateChangeHandler(
IInquiryHelper inquiryHelper, IEnumerable> calculations)
{
return new ClearIllustrationPointsOfStructureCalculationCollectionChangeHandler(inquiryHelper, calculations);
}
private static void ValidateAll(IEnumerable> closingStructuresCalculations, IAssessmentSection assessmentSection)
{
foreach (StructuresCalculation calculation in closingStructuresCalculations)
{
ClosingStructuresCalculationService.Validate(calculation, assessmentSection);
}
}
private static string EnableValidateAndCalculateMenuItem(IAssessmentSection assessmentSection)
{
return HydraulicBoundaryDatabaseConnectionValidator.Validate(assessmentSection.HydraulicBoundaryDatabase);
}
#endregion
#region ImportInfos
#region ClosingStructuresImporter
private static IFileImporter CreateClosingStructuresImporter(ClosingStructuresContext context,
string filePath,
IImporterMessageProvider importerMessageProvider,
IStructureUpdateStrategy structureUpdateStrategy)
{
return new ClosingStructuresImporter(context.WrappedData,
context.AssessmentSection.ReferenceLine,
filePath,
importerMessageProvider,
structureUpdateStrategy);
}
private static FileFilterGenerator CreateClosingStructureFileFilter()
{
return new FileFilterGenerator(
RiskeerCommonIOResources.Shape_file_filter_Extension,
RiskeerCommonIOResources.Shape_file_filter_Description);
}
private bool VerifyStructuresShouldUpdate(IFailureMechanism failureMechanism, string query)
{
var changeHandler = new FailureMechanismCalculationChangeHandler(failureMechanism, query, GetInquiryHelper());
return !changeHandler.RequireConfirmation() || changeHandler.InquireConfirmation();
}
#endregion
#endregion
}
}