#region License Information /* HeuristicLab * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab 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. * * HeuristicLab 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 HeuristicLab. If not, see . */ #endregion using System.Linq; using System.Windows.Forms; using HeuristicLab.MainForm; using HeuristicLab.MainForm.WindowsForms; namespace HeuristicLab.Problems.DataAnalysis.Views { [View("IntervalCollection View")] [Content(typeof(IntervalCollection), true)] public sealed partial class IntervalCollectionView : AsynchronousContentView { public new IntervalCollection Content { get => (IntervalCollection)base.Content; set => base.Content = value; } public DataGridView DataGridView { get => dataGridView; } public IntervalCollectionView() { InitializeComponent(); dataGridView.AutoGenerateColumns = false; dataGridView.AllowUserToAddRows = false; } protected override void OnContentChanged() { base.OnContentChanged(); if (Content == null) { DataGridView.Rows.Clear(); DataGridView.Columns.Clear(); } else { UpdateData(); } } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); dataGridView.Enabled = Content != null; dataGridView.ReadOnly = ReadOnly; } private void UpdateData() { DataGridView.Rows.Clear(); DataGridView.Columns.Clear(); SetColumnNames(); var variablesCount = Content.GetIntervals().Count; DataGridViewRow[] rows = new DataGridViewRow[variablesCount]; for (var i = 0; i < variablesCount; ++i) { var row = new DataGridViewRow(); rows[i] = row; } dataGridView.Rows.AddRange(rows); SetRowsHeader(); FillRows(); dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders); dataGridView.Enabled = true; //Disable Sorting foreach (DataGridViewColumn column in dataGridView.Columns) { column.SortMode = DataGridViewColumnSortMode.NotSortable; } } private void FillRows() { for (var i = 0; i < dataGridView.RowCount; ++i) { var key = (string)dataGridView.Rows[i].HeaderCell.Value; dataGridView.Rows[i].Cells[0].Value = Content.GetInterval(key).LowerBound; dataGridView.Rows[i].Cells[1].Value = Content.GetInterval(key).UpperBound; } } private void SetRowsHeader() { for (var i = 0; i < Content.GetIntervals().Count; ++i) { var item = Content.GetIntervals().ElementAt(i); dataGridView.Rows[i].HeaderCell.Value = item.Key; } } private void SetColumnNames() { dataGridView.Columns.Add("lowerBound", "Lower Bound"); dataGridView.Columns.Add("upperBound", "Upper Bound"); } private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) { var key = dataGridView.Rows[e.RowIndex].HeaderCell.Value.ToString(); if (e.ColumnIndex < 0 || e.RowIndex < 0) return; var lowerBound = e.ColumnIndex == 0 ? double.Parse(dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) : Content.GetInterval(key).LowerBound; var upperBound = e.ColumnIndex == 1 ? double.Parse(dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) : Content.GetInterval(key).UpperBound; // update if there was a change if (lowerBound != Content.GetInterval(key).LowerBound || upperBound != Content.GetInterval(key).UpperBound) Content.SetInterval(key, new Interval(lowerBound, upperBound)); } private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (!double.TryParse(e.FormattedValue.ToString(), out var value)) { e.Cancel = true; dataGridView.Rows[e.RowIndex].ErrorText = "Value must be a double value."; return; } else { var lowerBound = double.Parse(dataGridView.Rows[e.RowIndex].Cells[0].Value.ToString()); var upperBound = double.Parse(dataGridView.Rows[e.RowIndex].Cells[1].Value.ToString()); if (e.ColumnIndex == 1 && value < lowerBound || e.ColumnIndex == 0 && value > upperBound) { e.Cancel = true; dataGridView.Rows[e.RowIndex].ErrorText = "Lower bound of interval must be smaller than upper bound."; } } } private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) { dataGridView.Rows[e.RowIndex].ErrorText = string.Empty; } } }