Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/NamedIntervalsView.cs @ 16634

Last change on this file since 16634 was 16634, checked in by gkronber, 5 years ago

#2971: disabled validation check in NamedIntervalsView because it is problematic when NamedIntervals are updated (e.g. while the algorithm is running)

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.ComponentModel;
24using System.Drawing;
25using System.Linq;
26using System.Security.Permissions;
27using System.Windows.Forms;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
30using HeuristicLab.Problems.DataAnalysis.Implementation;
31
32namespace HeuristicLab.Problems.DataAnalysis.Views {
33
34  [View("NamedIntervals View")]
35  [Content(typeof(NamedIntervals), true)]
36  public partial class NamedIntervalsView : AsynchronousContentView {
37
38    public new NamedIntervals Content {
39      get => (NamedIntervals)base.Content;
40      set => base.Content = value;
41    }
42
43    public DataGridView DataGridView {
44      get => dataGridView;
45    }
46
47    public NamedIntervalsView() {
48      InitializeComponent();
49      dataGridView.AutoGenerateColumns = false;
50      dataGridView.AllowUserToAddRows = false;
51    }
52
53    protected override void OnContentChanged() {
54      base.OnContentChanged();
55      if (Content == null) {
56        DataGridView.Rows.Clear();
57        DataGridView.Columns.Clear();
58      } else if (!DataGridView.IsCurrentCellInEditMode) {
59        UpdateData();
60      }
61    }
62
63    protected override void SetEnabledStateOfControls() {
64      base.SetEnabledStateOfControls();
65      dataGridView.Enabled = Content != null;
66      dataGridView.ReadOnly = ReadOnly;
67    }
68
69    protected void UpdateData() {
70      DataGridView.Rows.Clear();
71      DataGridView.Columns.Clear();
72      SetColumnNames();
73
74
75      var variablesCount = Content.VariableIntervals.Count;
76
77      DataGridViewRow[] rows = new DataGridViewRow[variablesCount];
78      for (var i = 0; i < variablesCount; ++i) {
79        var row = new DataGridViewRow();
80        rows[i] = row;
81      }
82      dataGridView.Rows.AddRange(rows);
83
84      SetRowsHeader();
85      FillRows();
86
87      dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
88      dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
89      dataGridView.Enabled = true;
90
91    }
92
93    protected void FillRows() {
94      for (var i = 0; i < dataGridView.RowCount; ++i) {
95        var key = (string)dataGridView.Rows[i].HeaderCell.Value;
96        dataGridView.Rows[i].Cells[0].Value = Content.VariableIntervals[key].LowerBound;
97        dataGridView.Rows[i].Cells[1].Value = Content.VariableIntervals[key].UpperBound;
98      }
99    }
100
101    protected void SetRowsHeader() {
102      for (var i = 0; i < Content.VariableIntervals.Count; ++i) {
103        var item = Content.VariableIntervals.ElementAt(i);
104        dataGridView.Rows[i].HeaderCell.Value = item.Key;
105      }
106    }
107
108    protected void SetColumnNames() {
109      dataGridView.Columns.Add("lowerBound", "Lower Bound");
110      dataGridView.Columns.Add("upperBound", "Upper Bound");
111    }
112
113    private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
114      var key = dataGridView.Rows[e.RowIndex].HeaderCell.Value.ToString();
115
116      if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
117
118      var lowerBound = e.ColumnIndex == 0 ? double.Parse(dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) : Content.VariableIntervals[key].LowerBound;
119      var upperBound = e.ColumnIndex == 1 ? double.Parse(dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) : Content.VariableIntervals[key].UpperBound;
120
121      // update if there was a change
122      if (lowerBound != Content.VariableIntervals[key].LowerBound ||
123        upperBound != Content.VariableIntervals[key].UpperBound)
124        Content.VariableIntervals[key] = new Interval(lowerBound, upperBound);
125    }
126
127    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
128      // DISABLED VALIDATION OF ENTRIES
129      // because it leads to problems when updating the table
130      // we can assume it's the users fault if he or she enters incorrect values
131
132      // if (dataGridView.Rows[e.RowIndex].IsNewRow) {
133      //   return;
134      // }
135      //
136      // if (!double.TryParse(e.FormattedValue.ToString(), out var value)) {
137      //   e.Cancel = true;
138      //   dataGridView.Rows[e.RowIndex].ErrorText = "Value must be a double value.";
139      // } else if (string.IsNullOrEmpty(dataGridView.Rows[e.RowIndex].Cells[0].Value.ToString()) ||
140      //   string.IsNullOrEmpty(dataGridView.Rows[e.RowIndex].Cells[1].Value.ToString())) {
141      //   // accept any value if one of the cells is still empty
142      // } else {
143      //   var left = double.Parse(dataGridView.Rows[e.RowIndex].Cells[0].Value.ToString());
144      //   var right = double.Parse(dataGridView.Rows[e.RowIndex].Cells[1].Value.ToString());
145      //   if (e.ColumnIndex == 1 && value < left || e.ColumnIndex == 0 && value > right) {
146      //     e.Cancel = true;
147      //     dataGridView.Rows[e.RowIndex].ErrorText = "Lower bound of interval must be smaller than upper bound.";
148      //   }
149      // }
150    }
151
152    private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
153      dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
154    }
155  }
156}
Note: See TracBrowser for help on using the repository browser.