Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Views/3.4/IntervalCollectionView.cs @ 17607

Last change on this file since 17607 was 17579, checked in by mkommend, 5 years ago

#2971: Merged branch into trunk.

File size: 5.4 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.Windows.Forms;
24using HeuristicLab.MainForm;
25using HeuristicLab.MainForm.WindowsForms;
26
27namespace HeuristicLab.Problems.DataAnalysis.Views {
28
29  [View("IntervalCollection View")]
30  [Content(typeof(IntervalCollection), true)]
31  public sealed partial class IntervalCollectionView : AsynchronousContentView {
32
33    public new IntervalCollection Content {
34      get => (IntervalCollection)base.Content;
35      set => base.Content = value;
36    }
37
38    private DataGridView DataGridView {
39      get => dataGridView;
40    }
41
42    public IntervalCollectionView() {
43      InitializeComponent();
44      dataGridView.AutoGenerateColumns = false;
45      dataGridView.AllowUserToAddRows = false;
46    }
47
48    protected override void OnContentChanged() {
49      base.OnContentChanged();
50      if (Content == null) {
51        DataGridView.Rows.Clear();
52        DataGridView.Columns.Clear();
53      } else {
54        UpdateData();
55      }
56    }
57
58    protected override void SetEnabledStateOfControls() {
59      base.SetEnabledStateOfControls();
60      dataGridView.Enabled = Content != null;
61      dataGridView.ReadOnly = ReadOnly;
62    }
63
64    private void UpdateData() {
65      DataGridView.Rows.Clear();
66      DataGridView.Columns.Clear();
67      SetColumnNames();
68
69
70      var variablesCount = Content.Count;
71
72      DataGridViewRow[] rows = new DataGridViewRow[variablesCount];
73      for (var i = 0; i < variablesCount; ++i) {
74        var row = new DataGridViewRow();
75        rows[i] = row;
76      }
77      dataGridView.Rows.AddRange(rows);
78
79      var j = 0;
80      foreach (var variableInterval in Content.GetVariableIntervals()) {
81        dataGridView.Rows[j].HeaderCell.Value = variableInterval.Item1;
82        dataGridView.Rows[j].Cells[0].Value = variableInterval.Item2.LowerBound;
83        dataGridView.Rows[j].Cells[1].Value = variableInterval.Item2.UpperBound;
84        j++;
85      }
86
87      dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
88      dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
89      dataGridView.Enabled = true;
90      //Disable Sorting
91      foreach (DataGridViewColumn column in dataGridView.Columns) {
92        column.SortMode = DataGridViewColumnSortMode.NotSortable;
93      }
94    }
95
96    private void SetColumnNames() {
97      dataGridView.Columns.Add("lowerBound", "Lower Bound");
98      dataGridView.Columns.Add("upperBound", "Upper Bound");
99    }
100
101    private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
102      if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
103
104      var key = dataGridView.Rows[e.RowIndex].HeaderCell.Value.ToString();
105      var gridData = dataGridView[e.ColumnIndex, e.RowIndex].Value;
106      //Cells maybe null during initialization
107      var lowerBound = Content.GetInterval(key).LowerBound;
108      var upperBound = Content.GetInterval(key).UpperBound;
109
110      //Check if the input data is a string (user-defined input)
111      //if so parse the toString() of the value, otherwise do a hard-cast
112      //to not loose the double precision
113      double parsedValue;
114      if (gridData is string) {
115        parsedValue = double.Parse(gridData.ToString());
116      } else {
117        parsedValue = (double)gridData;
118      }
119
120      if (e.ColumnIndex == 0) lowerBound = parsedValue;
121      if (e.ColumnIndex == 1) upperBound = parsedValue;
122
123      var newInterval = new Interval(lowerBound, upperBound);
124
125      // update if there was a change
126      if (!Content.GetInterval(key).Equals(newInterval))
127        Content.SetInterval(key, newInterval);
128    }
129
130    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
131      if (!double.TryParse(e.FormattedValue.ToString(), out var value)) {
132        e.Cancel = true;
133        dataGridView.Rows[e.RowIndex].ErrorText = "Value must be a double value.";
134        return;
135      }
136
137      var lowerBound = double.Parse(dataGridView.Rows[e.RowIndex].Cells[0].Value.ToString());
138      var upperBound = double.Parse(dataGridView.Rows[e.RowIndex].Cells[1].Value.ToString());
139
140      if (e.ColumnIndex == 0 && value > upperBound || e.ColumnIndex == 1 && value < lowerBound) {
141        e.Cancel = true;
142        dataGridView.Rows[e.RowIndex].ErrorText = "Lower bound of interval must be smaller than upper bound.";
143        return;
144      }
145
146      dataGridView[e.ColumnIndex, e.RowIndex].Value = value;
147    }
148
149    private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
150      dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.