// 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.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
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.Utils;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Data.Calculation;
using Ringtoets.Common.Data.Hydraulics;
using Ringtoets.Common.Forms;
using Ringtoets.Common.Forms.ChangeHandlers;
using Ringtoets.Common.Forms.ExportInfos;
using Ringtoets.Common.Forms.Helpers;
using Ringtoets.Common.Forms.ImportInfos;
using Ringtoets.Common.Forms.PresentationObjects;
using Ringtoets.Common.Forms.TreeNodeInfos;
using Ringtoets.Common.Service;
using Ringtoets.Revetment.IO.Configurations;
using Ringtoets.StabilityStoneCover.Data;
using Ringtoets.StabilityStoneCover.Forms;
using Ringtoets.StabilityStoneCover.Forms.PresentationObjects;
using Ringtoets.StabilityStoneCover.Forms.PropertyClasses;
using Ringtoets.StabilityStoneCover.Forms.Views;
using Ringtoets.StabilityStoneCover.IO.Exporters;
using Ringtoets.StabilityStoneCover.Service;
using RingtoetsCommonDataResources = Ringtoets.Common.Data.Properties.Resources;
using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources;
namespace Ringtoets.StabilityStoneCover.Plugin
{
///
/// The plug-in for the .
///
public class StabilityStoneCoverPlugin : PluginBase
{
public override IEnumerable GetPropertyInfos()
{
yield return new PropertyInfo
{
CreateInstance = context => new StabilityStoneCoverFailureMechanismProperties
{
Data = context.WrappedData
}
};
yield return new PropertyInfo();
yield return new PropertyInfo
{
CreateInstance = context => new StabilityStoneCoverWaveConditionsInputContextProperties(
context,
new ObservablePropertyChangeHandler(context.Calculation, context.WrappedData))
};
}
public override IEnumerable GetViewInfos()
{
yield return new ViewInfo
{
GetViewName = (view, mechanism) => mechanism.WrappedData.Name,
Image = RingtoetsCommonFormsResources.CalculationIcon,
CloseForData = CloseStabilityStoneCoverFailureMechanismViewForData,
AdditionalDataCheck = context => context.WrappedData.IsRelevant
};
yield return new ViewInfo,
IEnumerable,
StabilityStoneCoverResultView>
{
GetViewName = (view, results) => 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(
WaveConditionsCalculationGroupContextChildNodeObjects,
WaveConditionsCalculationGroupContextContextMenuStrip,
WaveConditionsCalculationGroupContextOnNodeRemoved);
yield return RingtoetsTreeNodeInfoFactory.CreateCalculationContextTreeNodeInfo(
WaveConditionsCalculationContextChildNodeObjects,
WaveConditionsCalculationContextContextMenuStrip,
WaveConditionsCalculationContextOnNodeRemoved);
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.FailureMechanism_AssessmentResult_DisplayName,
Image = context => RingtoetsCommonFormsResources.FailureMechanismSectionResultIcon,
ContextMenuStrip = (nodeData, parentData, treeViewControl) => Gui.Get(nodeData, treeViewControl)
.AddOpenItem()
.Build()
};
}
public override IEnumerable GetImportInfos()
{
yield return RingtoetsImportInfoFactory.CreateCalculationConfigurationImportInfo(
(context, filePath) =>
new WaveConditionsCalculationConfigurationImporter(
filePath,
context.WrappedData,
context.HydraulicBoundaryLocations,
context.ForeshoreProfiles));
}
public override IEnumerable GetExportInfos()
{
yield return new ExportInfo
{
Name = RingtoetsCommonFormsResources.WaveConditionsExporter_DisplayName,
CreateFileExporter = (context, filePath) => new StabilityStoneCoverWaveConditionsExporter(context.WrappedData.GetCalculations().Cast(), filePath),
IsEnabled = context => context.WrappedData.GetCalculations().Cast().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 StabilityStoneCoverWaveConditionsExporter(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 StabilityStoneCoverCalculationConfigurationExporter(context.WrappedData.Children, filePath),
context => context.WrappedData.Children.Any());
yield return RingtoetsExportInfoFactory.CreateCalculationConfigurationExportInfo(
(context, filePath) => new StabilityStoneCoverCalculationConfigurationExporter(new[]
{
context.WrappedData
}, filePath));
}
#region ViewInfos
#region StabilityStoneCoverFailureMechanismView ViewInfo
private static bool CloseStabilityStoneCoverFailureMechanismViewForData(StabilityStoneCoverFailureMechanismView view, object o)
{
var assessmentSection = o as IAssessmentSection;
var failureMechanism = o as StabilityStoneCoverFailureMechanism;
var viewFailureMechanismContext = (StabilityStoneCoverFailureMechanismContext) view.Data;
StabilityStoneCoverFailureMechanism viewFailureMechanism = viewFailureMechanismContext.WrappedData;
return assessmentSection != null
? ReferenceEquals(viewFailureMechanismContext.Parent, assessmentSection)
: ReferenceEquals(viewFailureMechanism, failureMechanism);
}
#endregion
#region FailureMechanismSectionResultContext
private static bool CloseFailureMechanismResultViewForData(StabilityStoneCoverResultView view, object dataToCloseFor)
{
object viewData = view.Data;
var assessmentSection = dataToCloseFor as IAssessmentSection;
var failureMechanism = dataToCloseFor as StabilityStoneCoverFailureMechanism;
var failureMechanismContext = dataToCloseFor as IFailureMechanismContext;
if (assessmentSection != null)
{
return assessmentSection
.GetFailureMechanisms()
.OfType()
.Any(fm => ReferenceEquals(viewData, fm.SectionResults));
}
if (failureMechanismContext != null)
{
failureMechanism = failureMechanismContext.WrappedData;
}
return failureMechanism != null && ReferenceEquals(view.Data, failureMechanism.SectionResults);
}
#endregion
#endregion
#region TreeNodeInfos
#region StabilityStoneCoverFailureMechanismContext
private static object[] FailureMechanismEnabledChildNodeObjects(StabilityStoneCoverFailureMechanismContext failureMechanismContext)
{
StabilityStoneCoverFailureMechanism wrappedData = failureMechanismContext.WrappedData;
return new object[]
{
new CategoryTreeFolder(RingtoetsCommonFormsResources.FailureMechanism_Inputs_DisplayName, GetInputs(wrappedData, failureMechanismContext.Parent), TreeFolderCategory.Input),
new StabilityStoneCoverWaveConditionsCalculationGroupContext(wrappedData.WaveConditionsCalculationGroup, null, wrappedData, failureMechanismContext.Parent),
new CategoryTreeFolder(RingtoetsCommonFormsResources.FailureMechanism_Outputs_DisplayName, GetOutputs(wrappedData), TreeFolderCategory.Output)
};
}
private static object[] FailureMechanismDisabledChildNodeObjects(StabilityStoneCoverFailureMechanismContext failureMechanismContext)
{
return new object[]
{
failureMechanismContext.WrappedData.NotRelevantComments
};
}
private static IList GetInputs(StabilityStoneCoverFailureMechanism failureMechanism, IAssessmentSection assessmentSection)
{
return new ArrayList
{
new FailureMechanismSectionsContext(failureMechanism, assessmentSection),
new ForeshoreProfilesContext(failureMechanism.ForeshoreProfiles, failureMechanism, assessmentSection),
failureMechanism.InputComments
};
}
private static IList GetOutputs(StabilityStoneCoverFailureMechanism failureMechanism)
{
return new ArrayList
{
new FailureMechanismSectionResultContext(
failureMechanism.SectionResults, failureMechanism),
failureMechanism.OutputComments
};
}
private ContextMenuStrip FailureMechanismEnabledContextMenuStrip(StabilityStoneCoverFailureMechanismContext failureMechanismContext,
object parentData,
TreeViewControl treeViewControl)
{
var builder = new RingtoetsContextMenuBuilder(Gui.Get(failureMechanismContext, treeViewControl));
return builder.AddOpenItem()
.AddSeparator()
.AddToggleRelevancyOfFailureMechanismItem(failureMechanismContext, RemoveAllViewsForItem)
.AddSeparator()
.AddCollapseAllItem()
.AddExpandAllItem()
.AddSeparator()
.AddPropertiesItem()
.Build();
}
private void RemoveAllViewsForItem(StabilityStoneCoverFailureMechanismContext failureMechanismContext)
{
Gui.ViewCommands.RemoveAllViewsForItem(failureMechanismContext);
}
private ContextMenuStrip FailureMechanismDisabledContextMenuStrip(StabilityStoneCoverFailureMechanismContext failureMechanismContext,
object parentData,
TreeViewControl treeViewControl)
{
var builder = new RingtoetsContextMenuBuilder(Gui.Get(failureMechanismContext, treeViewControl));
return builder.AddToggleRelevancyOfFailureMechanismItem(failureMechanismContext, RemoveAllViewsForItem)
.AddSeparator()
.AddCollapseAllItem()
.AddExpandAllItem()
.Build();
}
#endregion
#region StabilityStoneCoverWaveConditionsCalculationGroupContext
private static object[] WaveConditionsCalculationGroupContextChildNodeObjects(StabilityStoneCoverWaveConditionsCalculationGroupContext nodeData)
{
var childNodeObjects = new List