// Copyright (C) Stichting Deltares 2017. 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.Drawing;
using System.Linq;
using System.Windows.Forms;
using Core.Common.Base;
using Core.Common.Controls.TreeView;
using Core.Common.Gui;
using Core.Common.Gui.ContextMenu;
using Core.Common.Gui.Forms.ProgressDialog;
using Core.Common.Gui.Plugin;
using Core.Common.Util;
using Ringtoets.AssemblyTool.Data;
using Ringtoets.Common.Data.AssemblyTool;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Data.Calculation;
using Ringtoets.Common.Data.FailureMechanism;
using Ringtoets.Common.Data.Hydraulics;
using Ringtoets.Common.Forms;
using Ringtoets.Common.Forms.ChangeHandlers;
using Ringtoets.Common.Forms.ExportInfos;
using Ringtoets.Common.Forms.GuiServices;
using Ringtoets.Common.Forms.Helpers;
using Ringtoets.Common.Forms.ImportInfos;
using Ringtoets.Common.Forms.PresentationObjects;
using Ringtoets.Common.Forms.TreeNodeInfos;
using Ringtoets.Common.Plugin;
using Ringtoets.Common.Service;
using Ringtoets.GrassCoverErosionOutwards.Data;
using Ringtoets.GrassCoverErosionOutwards.Forms;
using Ringtoets.GrassCoverErosionOutwards.Forms.PresentationObjects;
using Ringtoets.GrassCoverErosionOutwards.Forms.PropertyClasses;
using Ringtoets.GrassCoverErosionOutwards.Forms.Views;
using Ringtoets.GrassCoverErosionOutwards.IO.Exporters;
using Ringtoets.GrassCoverErosionOutwards.Plugin.Properties;
using Ringtoets.GrassCoverErosionOutwards.Service;
using Ringtoets.GrassCoverErosionOutwards.Service.MessageProviders;
using Ringtoets.Revetment.Data;
using Ringtoets.Revetment.Forms.Views;
using Ringtoets.Revetment.IO.Configurations;
using RingtoetsGrassCoverErosionOutwardsFormsResources = Ringtoets.GrassCoverErosionOutwards.Forms.Properties.Resources;
using RingtoetsCommonDataResources = Ringtoets.Common.Data.Properties.Resources;
using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources;
using RingtoetsCommonIoResources = Ringtoets.Common.IO.Properties.Resources;
namespace Ringtoets.GrassCoverErosionOutwards.Plugin
{
///
/// The plug-in for the .
///
public class GrassCoverErosionOutwardsPlugin : PluginBase
{
private IHydraulicBoundaryLocationCalculationGuiService hydraulicBoundaryLocationCalculationGuiService;
public override IEnumerable GetPropertyInfos()
{
yield return new PropertyInfo
{
CreateInstance = context => new GrassCoverErosionOutwardsFailureMechanismProperties(
context.WrappedData,
new GrassCoverErosionOutwardsFailureMechanismPropertyChangeHandler())
};
yield return new PropertyInfo
{
CreateInstance = context => new GrassCoverErosionOutwardsDesignWaterLevelCalculationsProperties(context.WrappedData)
};
yield return new PropertyInfo
{
CreateInstance = context => new GrassCoverErosionOutwardsWaveHeightCalculationsProperties(context.WrappedData)
};
yield return new PropertyInfo();
yield return new PropertyInfo
{
CreateInstance = context => new GrassCoverErosionOutwardsWaveConditionsInputContextProperties(
context,
() => context.FailureMechanism.GetAssessmentLevel(context.AssessmentSection,
context.Calculation.InputParameters.HydraulicBoundaryLocation,
context.Calculation.InputParameters.CategoryType),
new ObservablePropertyChangeHandler(context.Calculation, context.WrappedData))
};
yield return new PropertyInfo
{
CreateInstance = context => new GrassCoverErosionOutwardsDesignWaterLevelCalculationProperties(context.WrappedData)
};
yield return new PropertyInfo
{
CreateInstance = context => new GrassCoverErosionOutwardsWaveHeightCalculationProperties(context.WrappedData)
};
}
public override IEnumerable GetImportInfos()
{
yield return RingtoetsImportInfoFactory.CreateCalculationConfigurationImportInfo(
(context, filePath) =>
new WaveConditionsCalculationConfigurationImporter(
filePath,
context.WrappedData,
context.HydraulicBoundaryLocations,
context.ForeshoreProfiles));
}
public override IEnumerable GetViewInfos()
{
yield return new ViewInfo
{
GetViewName = (view, context) => context.WrappedData.Name,
Image = RingtoetsCommonFormsResources.CalculationIcon,
CloseForData = CloseGrassCoverErosionOutwardsFailureMechanismViewForData,
AdditionalDataCheck = context => context.WrappedData.IsRelevant,
CreateInstance = context => new GrassCoverErosionOutwardsFailureMechanismView(context.WrappedData, context.Parent)
};
yield return new ViewInfo<
FailureMechanismSectionResultContext,
IObservableEnumerable,
GrassCoverErosionOutwardsFailureMechanismResultView>
{
GetViewName = (view, context) => RingtoetsCommonFormsResources.FailureMechanism_AssessmentResult_DisplayName,
Image = RingtoetsCommonFormsResources.FailureMechanismSectionResultIcon,
CloseForData = CloseFailureMechanismResultViewForData,
GetViewData = context => context.WrappedData,
CreateInstance = context => new GrassCoverErosionOutwardsFailureMechanismResultView(
context.WrappedData,
(GrassCoverErosionOutwardsFailureMechanism) context.FailureMechanism)
};
yield return new ViewInfo<
GrassCoverErosionOutwardsDesignWaterLevelCalculationsContext,
IObservableEnumerable,
GrassCoverErosionOutwardsDesignWaterLevelCalculationsView>
{
GetViewName = (view, context) => $"{Resources.GrassCoverErosionOutwardsDesignWaterLevelCalculationsGroupContext_DisplayName} - {context.CategoryBoundaryName}",
GetViewData = context => context.WrappedData,
Image = RingtoetsCommonFormsResources.GenericInputOutputIcon,
CreateInstance = context => new GrassCoverErosionOutwardsDesignWaterLevelCalculationsView(context.WrappedData,
context.FailureMechanism,
context.AssessmentSection,
context.GetNormFunc),
AfterCreate = (view, context) => { view.CalculationGuiService = hydraulicBoundaryLocationCalculationGuiService; },
CloseForData = (view, data) => CloseHydraulicBoundaryCalculationsViewForData(view.AssessmentSection, data)
};
yield return new ViewInfo<
GrassCoverErosionOutwardsWaveHeightCalculationsContext,
IObservableEnumerable,
GrassCoverErosionOutwardsWaveHeightCalculationsView>
{
GetViewName = (view, context) => $"{Resources.GrassCoverErosionOutwardsWaveHeightCalculationsGroupContext_DisplayName} - {context.CategoryBoundaryName}",
GetViewData = context => context.WrappedData,
Image = RingtoetsCommonFormsResources.GenericInputOutputIcon,
CreateInstance = context => new GrassCoverErosionOutwardsWaveHeightCalculationsView(context.WrappedData,
context.FailureMechanism,
context.AssessmentSection,
context.GetNormFunc),
AfterCreate = (view, context) => { view.CalculationGuiService = hydraulicBoundaryLocationCalculationGuiService; },
CloseForData = (view, data) => CloseHydraulicBoundaryCalculationsViewForData(view.AssessmentSection, data)
};
yield return new ViewInfo,
WaveConditionsInputView>
{
Image = RingtoetsCommonFormsResources.GenericInputOutputIcon,
GetViewName = (view, context) => RingtoetsCommonFormsResources.Calculation_Input,
GetViewData = context => context.Calculation,
CloseForData = RingtoetsPluginHelper.ShouldCloseViewWithCalculationData,
CreateInstance = context => new WaveConditionsInputView(new GrassCoverErosionOutwardsWaveConditionsInputViewStyle(),
() => context.FailureMechanism.GetAssessmentLevel(
context.AssessmentSection,
context.Calculation.InputParameters.HydraulicBoundaryLocation,
context.Calculation.InputParameters.CategoryType))
};
}
public override IEnumerable GetTreeNodeInfos()
{
yield return RingtoetsTreeNodeInfoFactory.CreateFailureMechanismContextTreeNodeInfo(
FailureMechanismEnabledChildNodeObjects,
FailureMechanismDisabledChildNodeObjects,
FailureMechanismEnabledContextMenuStrip,
FailureMechanismDisabledContextMenuStrip);
yield return RingtoetsTreeNodeInfoFactory.CreateCalculationGroupContextTreeNodeInfo(
WaveConditionsCalculationGroupChildrenNodeObjects,
WaveConditionsCalculationGroupContextMenuStrip,
WaveConditionsCalculationGroupContextOnNodeRemoved);
yield return RingtoetsTreeNodeInfoFactory.CreateCalculationContextTreeNodeInfo(
WaveConditionsCalculationContextChildNodeObjects,
WaveConditionsCalculationContextMenuStrip,
WaveConditionsCalculationContextOnNodeRemoved);
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 => RingtoetsCommonDataResources.HydraulicBoundaryConditions_DisplayName,
Image = context => RingtoetsCommonFormsResources.GeneralFolderIcon,
ChildNodeObjects = GetHydraulicBoundaryDatabaseContextChildNodeObjects,
ForeColor = context => context.AssessmentSection.HydraulicBoundaryDatabase.IsLinked()
? Color.FromKnownColor(KnownColor.ControlText)
: Color.FromKnownColor(KnownColor.GrayText),
ContextMenuStrip = (nodeData, parentData, treeViewControl) => Gui.Get(nodeData, treeViewControl)
.AddExportItem()
.AddSeparator()
.AddCollapseAllItem()
.AddExpandAllItem()
.Build()
};
yield return new TreeNodeInfo
{
Text = context => Resources.GrassCoverErosionOutwardsDesignWaterLevelCalculationsGroupContext_DisplayName,
Image = context => RingtoetsCommonFormsResources.GeneralFolderIcon,
ContextMenuStrip = GrassCoverErosionOutwardsDesignWaterLevelCalculationsGroupContextMenuStrip,
ChildNodeObjects = DesignWaterlevelCalculationsGroupContextChildNodeObjects
};
yield return new TreeNodeInfo
{
Text = context => Resources.GrassCoverErosionOutwardsWaveHeightCalculationsGroupContext_DisplayName,
Image = context => RingtoetsCommonFormsResources.GeneralFolderIcon,
ContextMenuStrip = GrassCoverErosionOutwardsWaveHeightCalculationsGroupContextMenuStrip,
ChildNodeObjects = WaveHeightCalculationsGroupContextChildNodeObjects
};
yield return new TreeNodeInfo
{
Text = context => context.CategoryBoundaryName,
Image = context => RingtoetsCommonFormsResources.GenericInputOutputIcon,
ContextMenuStrip = GrassCoverErosionOutwardsDesignWaterLevelCalculationsContextMenuStrip
};
yield return new TreeNodeInfo
{
Text = context => context.CategoryBoundaryName,
Image = context => RingtoetsCommonFormsResources.GenericInputOutputIcon,
ContextMenuStrip = GrassCoverErosionOutwardsWaveHeightCalculationsContextMenuStrip
};
yield return new TreeNodeInfo
{
Text = emptyOutput => RingtoetsCommonFormsResources.CalculationOutput_DisplayName,
Image = emptyOutput => RingtoetsCommonFormsResources.GeneralOutputIcon,
ForeColor = emptyOutput => Color.FromKnownColor(KnownColor.GrayText),
ContextMenuStrip = (nodeData, parentData, treeViewControl) => Gui.Get(nodeData, treeViewControl)
.AddPropertiesItem()
.Build()
};
yield return new TreeNodeInfo
{
Text = emptyOutput => RingtoetsCommonFormsResources.CalculationOutput_DisplayName,
Image = emptyOutput => RingtoetsCommonFormsResources.GeneralOutputIcon,
ContextMenuStrip = (nodeData, parentData, treeViewControl) => Gui.Get(nodeData, treeViewControl)
.AddPropertiesItem()
.Build()
};
yield return new TreeNodeInfo
{
Text = context => RingtoetsCommonFormsResources.Calculation_Input,
Image = context => RingtoetsCommonFormsResources.GenericInputOutputIcon,
ContextMenuStrip = (nodeData, parentData, treeViewControl) => Gui.Get(nodeData, treeViewControl)
.AddOpenItem()
.AddSeparator()
.AddPropertiesItem()
.Build()
};
}
public override IEnumerable GetExportInfos()
{
yield return new ExportInfo
{
Name = RingtoetsCommonFormsResources.HydraulicBoundaryLocationsExporter_DisplayName,
CreateFileExporter = (context, filePath) =>
new GrassCoverErosionOutwardsHydraulicBoundaryLocationsExporter(context.FailureMechanism, context.AssessmentSection,
filePath),
IsEnabled = context => context.WrappedData.Locations.Count > 0,
FileFilterGenerator = new FileFilterGenerator(RingtoetsCommonIoResources.Shape_file_filter_Extension,
RingtoetsCommonIoResources.Shape_file_filter_Description)
};
yield return new ExportInfo
{
Name = RingtoetsCommonFormsResources.WaveConditionsExporter_DisplayName,
CreateFileExporter = (context, filePath) =>
{
IEnumerable calculations = context.FailureMechanism.WaveConditionsCalculationGroup.GetCalculations().Cast();
return new GrassCoverErosionOutwardsWaveConditionsExporter(calculations, filePath);
},
IsEnabled = context => context.FailureMechanism.WaveConditionsCalculationGroup.GetCalculations().Any(c => c.HasOutput),
FileFilterGenerator = new FileFilterGenerator(RingtoetsCommonFormsResources.DataTypeDisplayName_csv_file_filter_Extension,
RingtoetsCommonFormsResources.DataTypeDisplayName_csv_file_filter_Description)
};
yield return new ExportInfo
{
Name = RingtoetsCommonFormsResources.WaveConditionsExporter_DisplayName,
CreateFileExporter = (context, filePath) =>
{
IEnumerable calculations = context.WrappedData.GetCalculations().Cast();
return new GrassCoverErosionOutwardsWaveConditionsExporter(calculations, filePath);
},
IsEnabled = context => context.WrappedData.GetCalculations().Any(c => c.HasOutput),
FileFilterGenerator = new FileFilterGenerator(RingtoetsCommonFormsResources.DataTypeDisplayName_csv_file_filter_Extension,
RingtoetsCommonFormsResources.DataTypeDisplayName_csv_file_filter_Description)
};
yield return new ExportInfo
{
Name = RingtoetsCommonFormsResources.WaveConditionsExporter_DisplayName,
CreateFileExporter = (context, filePath) => new GrassCoverErosionOutwardsWaveConditionsExporter(new[]
{
context.WrappedData
}, filePath),
IsEnabled = context => context.WrappedData.HasOutput,
FileFilterGenerator = new FileFilterGenerator(RingtoetsCommonFormsResources.DataTypeDisplayName_csv_file_filter_Extension,
RingtoetsCommonFormsResources.DataTypeDisplayName_csv_file_filter_Description)
};
yield return RingtoetsExportInfoFactory.CreateCalculationGroupConfigurationExportInfo(
(context, filePath) => new GrassCoverErosionOutwardsCalculationConfigurationExporter(context.WrappedData.Children, filePath),
context => context.WrappedData.Children.Any());
yield return RingtoetsExportInfoFactory.CreateCalculationConfigurationExportInfo(
(context, filePath) => new GrassCoverErosionOutwardsCalculationConfigurationExporter(new[]
{
context.WrappedData
}, filePath));
}
public override void Activate()
{
base.Activate();
if (Gui == null)
{
throw new InvalidOperationException("Gui cannot be null");
}
hydraulicBoundaryLocationCalculationGuiService = new HydraulicBoundaryLocationCalculationGuiService(Gui.MainWindow);
}
#region ViewInfos
#region GrassCoverErosionOutwardsFailureMechanismView ViewInfo
private static bool CloseGrassCoverErosionOutwardsFailureMechanismViewForData(GrassCoverErosionOutwardsFailureMechanismView view, object data)
{
var assessmentSection = data as IAssessmentSection;
var failureMechanism = data as GrassCoverErosionOutwardsFailureMechanism;
return assessmentSection != null
? ReferenceEquals(view.AssessmentSection, assessmentSection)
: ReferenceEquals(view.FailureMechanism, failureMechanism);
}
#endregion
#region GrassCoverErosionOutwardsFailureMechanismResultView ViewInfo
private static bool CloseFailureMechanismResultViewForData(GrassCoverErosionOutwardsFailureMechanismResultView view, object o)
{
var assessmentSection = o as IAssessmentSection;
var failureMechanism = o as GrassCoverErosionOutwardsFailureMechanism;
var failureMechanismContext = o as IFailureMechanismContext;
if (assessmentSection != null)
{
return assessmentSection
.GetFailureMechanisms()
.OfType()
.Any(fm => ReferenceEquals(view.FailureMechanism.SectionResults, fm.SectionResults));
}
if (failureMechanismContext != null)
{
failureMechanism = failureMechanismContext.WrappedData;
}
return failureMechanism != null && ReferenceEquals(view.FailureMechanism.SectionResults, failureMechanism.SectionResults);
}
#endregion
private static bool CloseHydraulicBoundaryCalculationsViewForData(IAssessmentSection viewAssessmentSection,
object dataToCloseFor)
{
GrassCoverErosionOutwardsFailureMechanism viewFailureMechanism = viewAssessmentSection.GetFailureMechanisms()
.OfType()
.Single();
var failureMechanismContext = dataToCloseFor as GrassCoverErosionOutwardsFailureMechanismContext;
var assessmentSection = dataToCloseFor as IAssessmentSection;
var failureMechanism = dataToCloseFor as GrassCoverErosionOutwardsFailureMechanism;
if (assessmentSection != null)
{
failureMechanism = ((IAssessmentSection) dataToCloseFor).GetFailureMechanisms().OfType().Single();
}
if (failureMechanismContext != null)
{
failureMechanism = failureMechanismContext.Parent.GetFailureMechanisms().OfType().Single();
}
return failureMechanism != null && ReferenceEquals(failureMechanism, viewFailureMechanism);
}
#endregion
#region TreeNodeInfos
#region GrassCoverErosionOutwardsFailureMechanismContext TreeNodeInfo
private static object[] FailureMechanismEnabledChildNodeObjects(GrassCoverErosionOutwardsFailureMechanismContext failureMechanismContext)
{
GrassCoverErosionOutwardsFailureMechanism failureMechanism = failureMechanismContext.WrappedData;
return new object[]
{
new CategoryTreeFolder(RingtoetsCommonFormsResources.FailureMechanism_Inputs_DisplayName, GetInputs(failureMechanism, failureMechanismContext.Parent), TreeFolderCategory.Input),
new GrassCoverErosionOutwardsHydraulicBoundaryDatabaseContext(failureMechanismContext.Parent.HydraulicBoundaryDatabase, failureMechanism, failureMechanismContext.Parent),
new CategoryTreeFolder(RingtoetsCommonFormsResources.FailureMechanism_Outputs_DisplayName, GetOutputs(failureMechanism), TreeFolderCategory.Output)
};
}
private static object[] FailureMechanismDisabledChildNodeObjects(GrassCoverErosionOutwardsFailureMechanismContext failureMechanismContext)
{
return new object[]
{
failureMechanismContext.WrappedData.NotRelevantComments
};
}
private static IEnumerable