Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Views/3.4/IntervalCollectionView.cs @ 17607

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

#2971: Removed groupbox in IntervalCollectionView.

File size: 5.4 KB
RevLine 
[12574]1#region License Information
2/* HeuristicLab
[16844]3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[12574]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
[16830]22
[16544]23using System.Windows.Forms;
[12574]24using HeuristicLab.MainForm;
[16544]25using HeuristicLab.MainForm.WindowsForms;
[7141]26
[16544]27namespace HeuristicLab.Problems.DataAnalysis.Views {
[7141]28
[17145]29  [View("IntervalCollection View")]
[16904]30  [Content(typeof(IntervalCollection), true)]
31  public sealed partial class IntervalCollectionView : AsynchronousContentView {
[7141]32
[16904]33    public new IntervalCollection Content {
34      get => (IntervalCollection)base.Content;
[16634]35      set => base.Content = value;
[7141]36    }
37
[16544]38    public DataGridView DataGridView {
39      get => dataGridView;
[12574]40    }
[7141]41
[16904]42    public IntervalCollectionView() {
[16544]43      InitializeComponent();
44      dataGridView.AutoGenerateColumns = false;
45      dataGridView.AllowUserToAddRows = false;
[12574]46    }
[7141]47
[16544]48    protected override void OnContentChanged() {
49      base.OnContentChanged();
50      if (Content == null) {
51        DataGridView.Rows.Clear();
52        DataGridView.Columns.Clear();
[16887]53      } else {
[16544]54        UpdateData();
55      }
[7141]56    }
57
[16544]58    protected override void SetEnabledStateOfControls() {
59      base.SetEnabledStateOfControls();
60      dataGridView.Enabled = Content != null;
[16586]61      dataGridView.ReadOnly = ReadOnly;
[7141]62    }
63
[16862]64    private void UpdateData() {
[16627]65      DataGridView.Rows.Clear();
66      DataGridView.Columns.Clear();
67      SetColumnNames();
68
69
[17564]70      var variablesCount = Content.Count;
[12574]71
[16544]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);
[12574]78
[17565]79      var j = 0;
[17564]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      }
[16544]86
[16887]87      dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
88      dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
[16544]89      dataGridView.Enabled = true;
[16862]90      //Disable Sorting
91      foreach (DataGridViewColumn column in dataGridView.Columns) {
92        column.SortMode = DataGridViewColumnSortMode.NotSortable;
93      }
[16544]94    }
95
[16862]96    private void SetColumnNames() {
[16544]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) {
[17565]102      if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
103
[16545]104      var key = dataGridView.Rows[e.RowIndex].HeaderCell.Value.ToString();
[17565]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;
[16545]109
[17565]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 {
[17572]117        parsedValue = (double)gridData;
[17565]118      }
[16545]119
[17565]120      if (e.ColumnIndex == 0) lowerBound = parsedValue;
121      if (e.ColumnIndex == 1) upperBound = parsedValue;
[16545]122
[17565]123      var newInterval = new Interval(lowerBound, upperBound);
124
[16634]125      // update if there was a change
[17565]126      if (!Content.GetInterval(key).Equals(newInterval))
127        Content.SetInterval(key, newInterval);
[16544]128    }
129
130    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
[16730]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.";
[16888]134        return;
[16730]135      }
[17565]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());
[17572]139
[17565]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;
[7141]147    }
[16544]148
149    private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
150      dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
151    }
[7141]152  }
153}
Note: See TracBrowser for help on using the repository browser.