using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Xml.Serialization; using Deltares.Dam.Data.UISupport; using Deltares.Standard; using Deltares.Standard.Attributes; using Deltares.Standard.EventPublisher; using Deltares.Standard.Language; using Deltares.Standard.Validation; namespace Deltares.Dam.Data { /// /// Represents the calculation specifications at project level for DAM. /// These are the main choices that specify the calculation /// public class DamProjectCalculationSpecification : IVisibleEnabled, IDomain, IRepairer, IDisposable { private readonly List damCalculationSpecifications; private static AnalysisType selectedAnalysisType = AnalysisType.AdaptGeometry; private ProbabilisticType selectedProbabilisticType; private DamFailureMechanismeCalculationSpecification currentSpecification = null; private StabilityKernelType selectedStabilityKernelType; private IVisibleEnabledProvider visibleEnabledProvider; public DamProjectCalculationSpecification() { damCalculationSpecifications = new List(); //waterLevelTimeSeriesFileName = @"d:\src\delftgeosystems\trunk\data\Dam\RRD\Groot Salland\DAM UI Testdata\inputshortstart_dam.xml"; DataEventPublisher.OnAfterChange += DataEventPublisher_OnAfterChange; } private void DataEventPublisher_OnAfterChange(object sender, PublishEventArgs e) { if (sender == this.currentSpecification) { DataEventPublisher.AfterChange(this); } } [ReadOnly(true)] //For now make read only as only 1 spec is allowed at this moment [Validate] public List DamCalculationSpecifications { get { if (currentSpecification != null && currentSpecification.FailureMechanismSystemType != FailureMechanismSystemType.Piping) { selectedProbabilisticType = ProbabilisticType.Deterministic; } return damCalculationSpecifications; } } /// /// Gets or sets the analysis type for serialization purpose only. /// This "dummy" property is and must be only used for serialization/deserialization purposes as the real static property /// SelectedAnalysisType is NOT serialized. This is way its name is deliberately strange. /// /// /// The analysis type for serialization purpose only. /// public AnalysisType AnalysisTypeForSerializationPurposeOnly { get { return selectedAnalysisType; } set { selectedAnalysisType = value; } } /// /// Gets or sets the type of the selected analysis. /// /// /// The type of the selected analysis. /// [Label("Analysis type")] public static AnalysisType SelectedAnalysisType { get { return selectedAnalysisType; } set { // Create local instance of DamProjectCalculationSpecification because event handlers need sender var r = new DamProjectCalculationSpecification(); DataEventPublisher.BeforeChange(r, "SelectedAnalysisType"); selectedAnalysisType = value; DataEventPublisher.AfterChange(r, "SelectedAnalysisType"); } } [Label("Probabilistic type")] public ProbabilisticType SelectedProbabilisticType { get { return selectedProbabilisticType; } set { DataEventPublisher.BeforeChange(this, "SelectedProbabilisticType"); selectedProbabilisticType = value; DamFailureMechanismeCalculationSpecification.ProbabilisticType = selectedProbabilisticType; DataEventPublisher.AfterChange(this, "SelectedProbabilisticType"); } } [XmlIgnore] public DamFailureMechanismeCalculationSpecification CurrentSpecification { get { if (currentSpecification == null && damCalculationSpecifications.Count > 0) { currentSpecification = damCalculationSpecifications[0]; } return currentSpecification; } set { DataEventPublisher.BeforeChange(this, "CurrentSpecification"); currentSpecification = value; DataEventPublisher.AfterChange(this, "CurrentSpecification"); } } public StabilityKernelType SelectedStabilityKernelType { get { return selectedStabilityKernelType; } set { DataEventPublisher.BeforeChange(this, x => x.SelectedStabilityKernelType); selectedStabilityKernelType = value; if (currentSpecification != null) { currentSpecification.StabilityKernelType = selectedStabilityKernelType; } DataEventPublisher.AfterChange(this, x => x.SelectedStabilityKernelType); } } [XmlIgnore] [Browsable(false)] public IVisibleEnabledProvider VisibleEnabledProvider { get { return visibleEnabledProvider; } set { visibleEnabledProvider = value; } } public bool IsVisible(string property) { if (damCalculationSpecifications.Count <= 0) return false; switch (property) { case "SelectedAnalysisType": return Location.DamProjectType == DamProjectType.Design; case "CurrentSpecification": { var bol = (damCalculationSpecifications.Count > 0) && (this.CurrentSpecification.FailureMechanismSystemType == FailureMechanismSystemType.StabilityInside || this.CurrentSpecification.FailureMechanismSystemType == FailureMechanismSystemType.StabilityOutside); return bol; } case "SelectedStabilityKernelType": { return visibleEnabledProvider != null && visibleEnabledProvider.IsVisible(this, "SelectedStabilityKernelType"); } default : return true; } } public bool IsEnabled(string property) { switch (property) { default: return true; } } public ICollection GetDomain(string property) { switch (property) { case "SelectedAnalysisType": return new[] { AnalysisType.AdaptGeometry, AnalysisType.NoAdaption }; case "SelectedProbabilisticType": return (ConfigurationManager.Instance.GetAvailableProbabilisticTypes( DamFailureMechanismeCalculationSpecification.DamProjectType)); default: return null; } } [Validate] public ValidationResult[] Validate() { if (damCalculationSpecifications.Count > 1) { return new[]{ new ValidationResult(ValidationResultType.Error, LocalizationManager.GetTranslatedText(this, "MaxOneCalculationSpecification"), this, "DamCalculationSpecifications", "DamCalculationSpecifications", this)}; } else { return new ValidationResult[0]; } } private void RepairTooManySpecifications() { for (int i = damCalculationSpecifications.Count - 1; i > 0 ; i--) { damCalculationSpecifications.Remove(damCalculationSpecifications[i]); } } public void Repair(object subject, string property, string id) { switch (id) { case "DamCalculationSpecifications": this.RepairTooManySpecifications(); break; } } public string GetRepairDescription(object subject, string property, string id) { switch (id) { case "DamCalculationSpecifications": return LocalizationManager.GetTranslatedText(this, "KeepFirstCalculationSpecification"); default: return ""; } } public void Dispose() { DataEventPublisher.OnAfterChange -= DataEventPublisher_OnAfterChange; } } }