// 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;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Core.Common.Base.Data;
using Core.Common.Base.IO;
using Core.Common.Controls.TreeView;
using Core.Common.Controls.Views;
using Core.Common.Gui;
using Core.Common.Gui.ContextMenu;
using Core.Common.Gui.Forms;
using Core.Common.Gui.Forms.ProgressDialog;
using Core.Common.Gui.Plugin;
using Core.Common.IO.Exceptions;
using log4net;
using Ringtoets.Common.Data;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Data.Calculation;
using Ringtoets.Common.Data.Contribution;
using Ringtoets.Common.Data.FailureMechanism;
using Ringtoets.Common.Data.Probability;
using Ringtoets.Common.Forms.Helpers;
using Ringtoets.Common.Forms.PresentationObjects;
using Ringtoets.Common.Forms.PropertyClasses;
using Ringtoets.Common.Forms.TreeNodeInfos;
using Ringtoets.Common.Forms.Views;
using Ringtoets.Common.IO;
using Ringtoets.Common.IO.Exceptions;
using Ringtoets.GrassCoverErosionInwards.Data;
using Ringtoets.GrassCoverErosionInwards.Forms.PresentationObjects;
using Ringtoets.HeightStructures.Data;
using Ringtoets.HeightStructures.Forms.PresentationObjects;
using Ringtoets.HydraRing.Data;
using Ringtoets.HydraRing.IO;
using Ringtoets.Integration.Data;
using Ringtoets.Integration.Data.StandAlone;
using Ringtoets.Integration.Data.StandAlone.SectionResults;
using Ringtoets.Integration.Forms;
using Ringtoets.Integration.Forms.Commands;
using Ringtoets.Integration.Forms.PresentationObjects;
using Ringtoets.Integration.Forms.PropertyClasses;
using Ringtoets.Integration.Forms.Views;
using Ringtoets.Integration.Forms.Views.SectionResultViews;
using Ringtoets.Integration.Plugin.FileImporters;
using Ringtoets.Integration.Plugin.Properties;
using Ringtoets.Integration.Service;
using Ringtoets.Piping.Data;
using Ringtoets.Piping.Forms.PresentationObjects;
using RingtoetsDataResources = Ringtoets.Integration.Data.Properties.Resources;
using RingtoetsFormsResources = Ringtoets.Integration.Forms.Properties.Resources;
using RingtoetsCommonDataResources = Ringtoets.Common.Data.Properties.Resources;
using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources;
using UtilsResources = Core.Common.Utils.Properties.Resources;
using BaseResources = Core.Common.Base.Properties.Resources;
using GuiResources = Core.Common.Gui.Properties.Resources;
namespace Ringtoets.Integration.Plugin
{
///
/// The plug-in for the Ringtoets application.
///
public class RingtoetsPlugin : PluginBase
{
private static readonly ILog log = LogManager.GetLogger(typeof(PluginBase));
#region failureMechanismAssociations
private static readonly IEnumerable failureMechanismAssociations = new[]
{
new FailureMechanismContextAssociation(
typeof(PipingFailureMechanism),
(mechanism, assessmentSection) => new PipingFailureMechanismContext(
(PipingFailureMechanism) mechanism,
assessmentSection)
),
new FailureMechanismContextAssociation(
typeof(GrassCoverErosionInwardsFailureMechanism),
(mechanism, assessmentSection) => new GrassCoverErosionInwardsFailureMechanismContext(
(GrassCoverErosionInwardsFailureMechanism) mechanism,
assessmentSection)
),
new FailureMechanismContextAssociation(
typeof(HeightStructuresFailureMechanism),
(mechanism, assessmentSection) => new HeightStructuresFailureMechanismContext(
(HeightStructuresFailureMechanism) mechanism,
assessmentSection)
),
new FailureMechanismContextAssociation(
typeof(DuneErosionFailureMechanism),
(mechanism, assessmentSection) => new FailureMechanismContext(
mechanism,
assessmentSection)
),
new FailureMechanismContextAssociation(
typeof(GrassCoverErosionOutwardsFailureMechanism),
(mechanism, assessmentSection) => new FailureMechanismContext(
mechanism,
assessmentSection)
),
new FailureMechanismContextAssociation(
typeof(GrassCoverSlipOffInwardsFailureMechanism),
(mechanism, assessmentSection) => new FailureMechanismContext(
mechanism,
assessmentSection)
),
new FailureMechanismContextAssociation(
typeof(GrassCoverSlipOffOutwardsFailureMechanism),
(mechanism, assessmentSection) => new FailureMechanismContext(
mechanism,
assessmentSection)
),
new FailureMechanismContextAssociation(
typeof(MicrostabilityFailureMechanism),
(mechanism, assessmentSection) => new FailureMechanismContext(
mechanism,
assessmentSection)
),
new FailureMechanismContextAssociation(
typeof(PipingStructureFailureMechanism),
(mechanism, assessmentSection) => new FailureMechanismContext(
mechanism,
assessmentSection)
),
new FailureMechanismContextAssociation(
typeof(StabilityStoneCoverFailureMechanism),
(mechanism, assessmentSection) => new FailureMechanismContext(
mechanism,
assessmentSection)
),
new FailureMechanismContextAssociation(
typeof(TechnicalInnovationFailureMechanism),
(mechanism, assessmentSection) => new FailureMechanismContext(
mechanism,
assessmentSection)
),
new FailureMechanismContextAssociation(
typeof(StrengthStabilityLengthwiseConstructionFailureMechanism),
(mechanism, assessmentSection) => new FailureMechanismContext(
mechanism,
assessmentSection)
),
new FailureMechanismContextAssociation(
typeof(WaterPressureAsphaltCoverFailureMechanism),
(mechanism, assessmentSection) => new FailureMechanismContext(
mechanism,
assessmentSection)
),
new FailureMechanismContextAssociation(
typeof(WaveImpactAsphaltCoverFailureMechanism),
(mechanism, assessmentSection) => new FailureMechanismContext(
mechanism,
assessmentSection)
),
new FailureMechanismContextAssociation(
typeof(ClosingStructureFailureMechanism),
(mechanism, assessmentSection) => new FailureMechanismContext(
mechanism,
assessmentSection)
),
new FailureMechanismContextAssociation(
typeof(MacrostabilityInwardsFailureMechanism),
(mechanism, assessmentSection) => new FailureMechanismContext(
mechanism,
assessmentSection)
),
new FailureMechanismContextAssociation(
typeof(MacrostabilityOutwardsFailureMechanism),
(mechanism, assessmentSection) => new FailureMechanismContext(
mechanism,
assessmentSection)
),
new FailureMechanismContextAssociation(
typeof(StrengthStabilityPointConstructionFailureMechanism),
(mechanism, assessmentSection) => new FailureMechanismContext(
mechanism,
assessmentSection)
)
};
#endregion
public override IRibbonCommandHandler RibbonCommandHandler
{
get
{
return new RingtoetsRibbon();
}
}
public override IGui Gui
{
get
{
return base.Gui;
}
set
{
RemoveOnOpenProjectListener(base.Gui);
base.Gui = value;
AddOnOpenProjectListener(value);
}
}
public IAssessmentSection GetAssessmentSectionFromFile()
{
if (Gui == null)
{
return null;
}
IAssessmentSection assessmentSection = null;
try
{
var assessmentSectionHandler = new AssessmentSectionFromFileCommandHandler(Gui.MainWindow);
var path = RingtoetsSettingsHelper.GetCommonDocumentsRingtoetsShapeFileDirectory();
assessmentSection = assessmentSectionHandler.CreateAssessmentSectionFromFile(path);
}
catch (CriticalFileValidationException exception)
{
MessageBox.Show(exception.Message, BaseResources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
log.Warn(exception.Message, exception.InnerException);
}
catch (CriticalFileReadException exception)
{
log.Error(exception.Message, exception.InnerException);
}
return assessmentSection;
}
public void SetAssessmentSectionToProject(RingtoetsProject ringtoetsProject, AssessmentSection assessmentSection)
{
if (ringtoetsProject == null)
{
throw new ArgumentNullException("ringtoetsProject");
}
if (assessmentSection == null)
{
throw new ArgumentNullException("assessmentSection");
}
assessmentSection.Name = GetUniqueForAssessmentSectionName(ringtoetsProject.Items, assessmentSection.Name);
ringtoetsProject.Items.Add(assessmentSection);
ringtoetsProject.NotifyObservers();
if (Gui != null && (Gui.Selection == null || !Gui.Selection.Equals(assessmentSection)))
{
Gui.Selection = assessmentSection;
Gui.DocumentViewController.OpenViewForData(assessmentSection);
}
}
///
/// Returns all instances provided for data of .
///
public override IEnumerable GetPropertyInfos()
{
yield return new PropertyInfo();
yield return new PropertyInfo();
yield return new PropertyInfo, StandAloneFailureMechanismContextProperties>();
yield return new PropertyInfo, CalculationGroupContextProperties>();
yield return new PropertyInfo, CalculationContextProperties>();
yield return new PropertyInfo();
}
///
/// Returns all instances provided for data of .
///
public override IEnumerable GetViewInfos()
{
yield return new ViewInfo
{
GetViewName = (v, o) => RingtoetsDataResources.FailureMechanismContribution_DisplayName,
GetViewData = context => context.WrappedData,
Image = RingtoetsCommonFormsResources.FailureMechanismContributionIcon,
CloseForData = CloseFailureMechanismContributionViewForData,
AfterCreate = (view, context) =>
{
view.AssessmentSection = context.Parent;
view.ViewCommands = Gui.ViewCommands;
}
};
yield return new ViewInfo
{
GetViewName = (v, o) => RingtoetsFormsResources.AssessmentSectionMap_DisplayName,
Image = RingtoetsFormsResources.Map
};
yield return CreateFailureMechanismResultViewInfo<
DuneErosionFailureMechanismSectionResult,
DuneErosionResultView>();
yield return CreateFailureMechanismResultViewInfo<
GrassCoverErosionOutwardsFailureMechanismSectionResult,
GrassCoverErosionOutwardsResultView>();
yield return CreateFailureMechanismResultViewInfo<
GrassCoverSlipOffInwardsFailureMechanismSectionResult,
GrassCoverSlipOffInwardsResultView>();
yield return CreateFailureMechanismResultViewInfo<
GrassCoverSlipOffOutwardsFailureMechanismSectionResult,
GrassCoverSlipOffOutwardsResultView>();
yield return CreateFailureMechanismResultViewInfo<
MicrostabilityFailureMechanismSectionResult,
MicrostabilityResultView>();
yield return CreateFailureMechanismResultViewInfo<
StabilityStoneCoverFailureMechanismSectionResult,
StabilityStoneCoverResultView>();
yield return CreateFailureMechanismResultViewInfo<
PipingStructureFailureMechanismSectionResult,
PipingStructureResultView>();
yield return CreateFailureMechanismResultViewInfo<
TechnicalInnovationFailureMechanismSectionResult,
TechnicalInnovationResultView>();
yield return CreateFailureMechanismResultViewInfo<
StrengthStabilityLengthwiseConstructionFailureMechanismSectionResult,
StrengthStabilityLengthwiseConstructionResultView>();
yield return CreateFailureMechanismResultViewInfo<
WaterPressureAsphaltCoverFailureMechanismSectionResult,
WaterPressureAsphaltCoverResultView>();
yield return CreateFailureMechanismResultViewInfo<
WaveImpactAsphaltCoverFailureMechanismSectionResult,
WaveImpactAsphaltCoverResultView>();
yield return CreateFailureMechanismResultViewInfo<
MacrostabilityInwardsFailureMechanismSectionResult,
MacrostabilityInwardsResultView>();
yield return CreateFailureMechanismResultViewInfo<
MacrostabilityOutwardsFailureMechanismSectionResult,
MacrostabilityOutwardsResultView>();
yield return CreateFailureMechanismResultViewInfo<
StrengthStabilityPointConstructionFailureMechanismSectionResult,
StrengthStabilityPointConstructionResultView>();
yield return CreateFailureMechanismResultViewInfo<
ClosingStructureFailureMechanismSectionResult,
ClosingStructureResultView>();
yield return new ViewInfo, ICommentable, CommentView>
{
GetViewName = (v, o) => Resources.Comment_DisplayName,
GetViewData = context => context.WrappedData,
Image = RingtoetsCommonFormsResources.EditDocumentIcon,
CloseForData = CloseCommentViewForData
};
}
public override IEnumerable GetFileImporters()
{
yield return new ReferenceLineImporter();
yield return new FailureMechanismSectionsImporter();
}
public override IEnumerable GetDataItemInfos()
{
IAssessmentSection assessmentSection = GetAssessmentSectionFromFile();
if (assessmentSection == null)
{
return Enumerable.Empty();
}
return new DataItemInfo[]
{
new DataItemInfo
{
Name = RingtoetsFormsResources.AssessmentSection_DisplayName,
Category = RingtoetsCommonFormsResources.Ringtoets_Category,
Image = RingtoetsFormsResources.AssessmentSectionFolderIcon,
CreateData = owner =>
{
var project = (RingtoetsProject) owner;
assessmentSection.Name = GetUniqueForAssessmentSectionName(project.Items, assessmentSection.Name);
return assessmentSection;
}
}
};
}
///
/// Gets the child data instances that have definitions of some parent data object.
///
/// The parent data object.
/// Sequence of child data.
public override IEnumerable