Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16862 was 16862, checked in by chaider, 5 years ago

#2971 Changed some views

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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
22
23using System.Linq;
24using System.Windows.Forms;
25using HeuristicLab.MainForm;
26using HeuristicLab.MainForm.WindowsForms;
27
28namespace HeuristicLab.Problems.DataAnalysis.Views {
29
30  [View("NamedIntervals View")]
31  [Content(typeof(NamedIntervals), true)]
32  public sealed partial class NamedIntervalsView : AsynchronousContentView {
33
34    public new NamedIntervals Content {
35      get => (NamedIntervals)base.Content;
36      set => base.Content = value;
37    }
38
39    public DataGridView DataGridView {
40      get => dataGridView;
41    }
42
43    public NamedIntervalsView() {
44      InitializeComponent();
45      dataGridView.AutoGenerateColumns = false;
46      dataGridView.AllowUserToAddRows = false;
47    }
48
49    protected override void OnContentChanged() {
50      base.OnContentChanged();
51      if (Content == null) {
52        DataGridView.Rows.Clear();
53        DataGridView.Columns.Clear();
54      } else if (!DataGridView.IsCurrentCellInEditMode) {
55        UpdateData();
56      }
57    }
58
59    protected override void SetEnabledStateOfControls() {
60      base.SetEnabledStateOfControls();
61      dataGridView.Enabled = Content != null;
62      dataGridView.ReadOnly = ReadOnly;
63    }
64
65    private void UpdateData() {
66      DataGridView.Rows.Clear();
67      DataGridView.Columns.Clear();
68      SetColumnNames();
69
70
71      var variablesCount = Content.VariableIntervals.Count;
72
73      DataGridViewRow[] rows = new DataGridViewRow[variablesCount];
74      for (var i = 0; i < variablesCount; ++i) {
75        var row = new DataGridViewRow();
76        rows[i] = row;
77      }
78      dataGridView.Rows.AddRange(rows);
79
80      SetRowsHeader();
81      FillRows();
82
83      dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
84      dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
85      dataGridView.Enabled = true;
86      //Disable Sorting
87      foreach (DataGridViewColumn column in dataGridView.Columns) {
88        column.SortMode = DataGridViewColumnSortMode.NotSortable;
89      }
90    }
91
92    private void FillRows() {
93      for (var i = 0; i < dataGridView.RowCount; ++i) {
94        var key = (string)dataGridView.Rows[i].HeaderCell.Value;
95        dataGridView.Rows[i].Cells[0].Value = Content.VariableIntervals[key].LowerBound;
96        dataGridView.Rows[i].Cells[1].Value = Content.VariableIntervals[key].UpperBound;
97      }
98    }
99
100    private void SetRowsHeader() {
101      for (var i = 0; i < Content.VariableIntervals.Count; ++i) {
102        var item = Content.VariableIntervals.ElementAt(i);
103        dataGridView.Rows[i].HeaderCell.Value = item.Key;
104      }
105    }
106
107    private void SetColumnNames() {
108      dataGridView.Columns.Add("lowerBound", "Lower Bound");
109      dataGridView.Columns.Add("upperBound", "Upper Bound");
110    }
111
112    private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
113      var key = dataGridView.Rows[e.RowIndex].HeaderCell.Value.ToString();
114
115      if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
116
117      var lowerBound = e.ColumnIndex == 0 ? double.Parse(dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) : Content.VariableIntervals[key].LowerBound;
118      var upperBound = e.ColumnIndex == 1 ? double.Parse(dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) : Content.VariableIntervals[key].UpperBound;
119
120      // update if there was a change
121      if (lowerBound != Content.VariableIntervals[key].LowerBound ||
122        upperBound != Content.VariableIntervals[key].UpperBound)
123        Content.VariableIntervals[key] = new Interval(lowerBound, upperBound);
124    }
125
126    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
127      // DISABLED VALIDATION OF ENTRIES
128      // because it leads to problems when updating the table
129      // we can assume it's the users fault if he or she enters incorrect values
130
131      if (dataGridView.Rows[e.RowIndex].IsNewRow) {
132        return;
133      }
134     
135      if (!double.TryParse(e.FormattedValue.ToString(), out var value)) {
136        e.Cancel = true;
137        dataGridView.Rows[e.RowIndex].ErrorText = "Value must be a double value.";
138      } else if (string.IsNullOrEmpty(dataGridView.Rows[e.RowIndex].Cells[0].Value.ToString()) ||
139        string.IsNullOrEmpty(dataGridView.Rows[e.RowIndex].Cells[1].Value.ToString())) {
140        // accept any value if one of the cells is still empty
141      } else {
142        var left = double.Parse(dataGridView.Rows[e.RowIndex].Cells[0].Value.ToString());
143        var right = double.Parse(dataGridView.Rows[e.RowIndex].Cells[1].Value.ToString());
144        if (e.ColumnIndex == 1 && value < left || e.ColumnIndex == 0 && value > right) {
145          e.Cancel = true;
146          dataGridView.Rows[e.RowIndex].ErrorText = "Lower bound of interval must be smaller than upper bound.";
147        }
148      }
149    }
150
151    private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
152      dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
153    }
154  }
155}
Note: See TracBrowser for help on using the repository browser.