#region License Information /* HeuristicLab * Copyright (C) 2002-2018 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; using System.ComponentModel; using System.Drawing; using System.Linq; using System.Security.Permissions; using System.Windows.Forms; using HeuristicLab.MainForm; using HeuristicLab.MainForm.WindowsForms; using HeuristicLab.Problems.DataAnalysis.Implementation; namespace HeuristicLab.Problems.DataAnalysis.Views { [View("NamedIntervals View")] [Content(typeof(NamedIntervals), true)] public partial class NamedIntervalsView : AsynchronousContentView { public new NamedIntervals Content { get => (NamedIntervals)base.Content; set => base.Content = value; } public DataGridView DataGridView { get => dataGridView; } public NamedIntervalsView() { InitializeComponent(); setColumnNames(); dataGridView.AutoGenerateColumns = false; dataGridView.AllowUserToAddRows = false; } protected override void OnContentChanged() { base.OnContentChanged(); if (Content == null) { DataGridView.Rows.Clear(); DataGridView.Columns.Clear(); } else if (!DataGridView.IsCurrentCellInEditMode) { UpdateData(); } } protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); dataGridView.Enabled = Content != null; //dataGridView.ReadOnly = ReadOnly; } protected void UpdateData() { var variablesCount = Content.VariableIntervals.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.ColumnHeader); dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders); dataGridView.Enabled = true; } protected 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.VariableIntervals[key].LowerBound; dataGridView.Rows[i].Cells[1].Value = Content.VariableIntervals[key].UpperBound; } } protected void setRowsHeader() { for (var i = 0; i < Content.VariableIntervals.Count; ++i) { var item = Content.VariableIntervals.ElementAt(i); dataGridView.Rows[i].HeaderCell.Value = item.Key; } } protected 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.VariableIntervals[key].LowerBound; var upperBound = e.ColumnIndex == 1 ? double.Parse(dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) : Content.VariableIntervals[key].UpperBound; Content.VariableIntervals[key] = new Interval(lowerBound, upperBound); } private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { double value; if (dataGridView.Rows[e.RowIndex].IsNewRow) { return; } if (!double.TryParse(e.FormattedValue.ToString(), out value)) { e.Cancel = true; dataGridView.Rows[e.RowIndex].ErrorText = "Value must be a double value."; } else { if (e.ColumnIndex == 0 && value > double.Parse(dataGridView.Rows[e.RowIndex].Cells[1].Value.ToString()) || e.ColumnIndex == 1 && value < double.Parse(dataGridView.Rows[e.RowIndex].Cells[0].Value.ToString())) { 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; } } }