// 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.Linq; using System.Windows.Forms; using Core.Common.Base; using Core.Common.Controls.Views; using Core.Common.Utils.Reflection; using Ringtoets.Common.Data.Contribution; using CommonGuiResources = Core.Common.Gui.Properties.Resources; using RingtoetsIntegrationFormsResources = Ringtoets.Integration.Forms.Properties.Resources; namespace Ringtoets.Integration.Forms.Views { /// /// View for the , from which the /// can be updated and the and /// can be seen in a grid. /// public partial class FailureMechanismContributionView : UserControl, IView, IObserver { private DataGridViewColumn probabilityPerYearColumn; private FailureMechanismContribution data; /// /// Creates a new instance of . /// public FailureMechanismContributionView() { InitializeComponent(); InitializeGridColumns(); BindNormChange(); BindNormInputLeave(); SubscribeEvents(); } public object Data { get { return data; } set { SetNormValue((FailureMechanismContribution)value); } } public void UpdateObserver() { SetNormText(); probabilityDistributionGrid.Invalidate(); } /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } UnsubscribeEvents(); base.Dispose(disposing); } private void SubscribeEvents() { probabilityDistributionGrid.CellFormatting += ProbabilityDistributionGridOnCellFormatting; } private void UnsubscribeEvents() { probabilityDistributionGrid.CellFormatting -= ProbabilityDistributionGridOnCellFormatting; } private void ProbabilityDistributionGridOnCellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.ColumnIndex == probabilityPerYearColumn.Index) { var contributionItem = data.Distribution.ElementAt(e.RowIndex); if (contributionItem.Contribution == 0.0) { e.Value = RingtoetsIntegrationFormsResources.FailureMechanismContributionView_ProbabilityPerYear_Not_applicable; e.FormattingApplied = true; } } } private void SetNormValue(FailureMechanismContribution value) { UnbindNormChange(); DetachFromData(); data = value; SetGridDataSource(); SetNormText(); AttachToData(); BindNormChange(); } private void SetGridDataSource() { if (data != null) { probabilityDistributionGrid.DataSource = data.Distribution; } } private void AttachToData() { if (data != null) { data.Attach(this); } } private void DetachFromData() { if (data != null) { data.Detach(this); } } private void BindNormChange() { normInput.ValueChanged += NormValueChanged; } private void UnbindNormChange() { normInput.ValueChanged -= NormValueChanged; } private void BindNormInputLeave() { normInput.Leave += NormInputLeave; } private void NormInputLeave(object sender, EventArgs e) { ResetTextIfEmtpy(); } private void NormValueChanged(object sender, EventArgs eventArgs) { data.Norm = Convert.ToInt32(normInput.Value); data.NotifyObservers(); } private void ResetTextIfEmtpy() { if (string.IsNullOrEmpty(normInput.Text)) { normInput.Text = string.Format("{0}", normInput.Value); } } private void SetNormText() { if (data != null) { normInput.Value = data.Norm; } } private void InitializeGridColumns() { var assessmentName = TypeUtils.GetMemberName(fmci => fmci.Assessment); var columnNameFormat = "column_{0}"; var assessmentColumn = new DataGridViewTextBoxColumn { DataPropertyName = assessmentName, HeaderText = CommonGuiResources.FailureMechanismContributionView_GridColumn_Assessment, Name = string.Format(columnNameFormat, assessmentName), AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader }; var contributionName = TypeUtils.GetMemberName(fmci => fmci.Contribution); var probabilityColumn = new DataGridViewTextBoxColumn { DataPropertyName = contributionName, HeaderText = CommonGuiResources.FailureMechanismContributionView_GridColumn_Contribution, Name = string.Format(columnNameFormat, contributionName), AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader }; var probabilitySpaceName = TypeUtils.GetMemberName(fmci => fmci.ProbabilitySpace); probabilityPerYearColumn = new DataGridViewTextBoxColumn { DataPropertyName = probabilitySpaceName, HeaderText = CommonGuiResources.FailureMechanismContributionView_GridColumn_ProbabilitySpace, Name = string.Format(columnNameFormat, probabilitySpaceName), AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill, MinimumWidth = 100 }; probabilityDistributionGrid.AutoGenerateColumns = false; probabilityDistributionGrid.Columns.AddRange(assessmentColumn, probabilityColumn, probabilityPerYearColumn); } } }