// Copyright (C) Stichting Deltares 2018. All rights reserved.
//
// This file is part of the application DAM - UI.
//
// DAM - UI 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.Windows.Forms;
using Deltares.Dam.Data;
using Deltares.Standard;
using Deltares.Standard.EventPublisher;
using Deltares.Standard.EventPublisher.Enum;
using Deltares.Standard.Forms;
using Deltares.Standard.Forms.DExpress;
namespace Deltares.Dam.Forms
{
public partial class DamProjectCalculationSpecificationPropertyControl : UserControl, IPropertyControl
{
private DamProjectCalculationSpecification damProjectCalculationSpecification;
private DamProjectData damProjectData;
private bool firstEdit;
public DamProjectCalculationSpecificationPropertyControl()
{
InitializeComponent();
// Make sure PropertyEditorReactionType is set to Ignore (Design time this does NOT work TODO for Grid component).
Grid.PropertyEditorReactionType = PropertyEditorReactionType.Ignore;
Name = "Calculation";
BindSupport.BindTextAndValue(this, ProbabilisticTypeLabel,
ProbabilisticTypeComboBox, p => p.SelectedProbabilisticType);
BindSupport.Bind(this, Grid, p => p.DamCalculationSpecifications);
DataEventPublisher.OnSelectionChanged += DataEventPublisher_OnSubSelectionChanged;
DataEventPublisher.OnAfterChange += DataEventPublisher_OnAfterChange;
FormsSupport.RepairRightAnchoredControls(this);
FormsSupport.AdjustSizeToContents(damProjectCalculationOptionsPropertyControl1);
firstEdit = true;
}
private void DataEventPublisher_OnAfterChange(object sender, PublishEventArgs e)
{
var specification = sender as DamFailureMechanismeCalculationSpecification;
if (specification != null)
{
if (damProjectCalculationSpecification != null)
{
damProjectCalculationSpecification.CurrentSpecification = specification;
SetSelectedDamCalculationSpecification();
}
}
}
#region IPropertyControl Members
public bool IsVisible
{
get
{
return damProjectData != null &&
(damProjectData.DamProjectType == DamProjectType.Calamity ||
damProjectData.DamProjectType == DamProjectType.Design);
}
}
public object SelectedObject
{
get { return damProjectData; }
set
{
var data = value as DamProjectData;
if (data != null)
{
damProjectData = data;
if (damProjectData.DamProjectType != DamProjectType.Assessment)
{
damProjectCalculationSpecification = damProjectData.DamProjectCalculationSpecification;
BindSupport.Assign(this, damProjectCalculationSpecification);
// enable other Stability kernel options (WTI and converted .NET)
BindSupport.Assign(stabilityKernelTypeSpecificationsControl1, damProjectData.DamProjectCalculationSpecification);
// for now, always set first specification
SetSelectedDamCalculationSpecification();
if (damProjectData.DamProjectType == DamProjectType.Design)
{
Grid.ShowToolbar = false;
}
}
}
}
}
#endregion
private void SetSelectedDamCalculationSpecification()
{
var selection = damProjectCalculationSpecification.CurrentSpecification;
if (selection != null)
{
selection.FailureMechanismeParamatersMStab.MStabParameters.SlipCircleDefinition.Specification =
selection;
BindSupport.Assign(damProjectCalculationOptionsPropertyControl1, selection);
if (firstEdit)
{
// Ensure that all edits of the damProjectCalculationOptionsPropertyControl1 are updated at start
damProjectCalculationOptionsPropertyControl1.SelectedObject = selection;
}
if (selection.FailureMechanismSystemType != FailureMechanismSystemType.Piping && firstEdit)
{
// this is a hack to force update on ill placed edits.
Width = Width + 1;
firstEdit = false;
}
}
}
private void DataEventPublisher_OnSubSelectionChanged(object sender, PublishEventArgs e)
{
if (((SelectionEventArgs) e).PropertyEditorReactionType == PropertyEditorReactionType.Ignore)
{
var item = sender as DamFailureMechanismeCalculationSpecification;
if (item != null)
{
if (damProjectCalculationSpecification.DamCalculationSpecifications.Contains(item))
{
damProjectCalculationSpecification.CurrentSpecification = item;
SetSelectedDamCalculationSpecification();
}
}
}
}
}
}