Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17203 was 17145, checked in by mkommend, 5 years ago

#2971: Corrected naming of IntervalCollection and according view.

File size: 5.3 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.Linq;
24using System.Windows.Forms;
[12574]25using HeuristicLab.MainForm;
[16544]26using HeuristicLab.MainForm.WindowsForms;
[7141]27
[16544]28namespace HeuristicLab.Problems.DataAnalysis.Views {
[7141]29
[17145]30  [View("IntervalCollection View")]
[16904]31  [Content(typeof(IntervalCollection), true)]
32  public sealed partial class IntervalCollectionView : AsynchronousContentView {
[7141]33
[16904]34    public new IntervalCollection Content {
35      get => (IntervalCollection)base.Content;
[16634]36      set => base.Content = value;
[7141]37    }
38
[16544]39    public DataGridView DataGridView {
40      get => dataGridView;
[12574]41    }
[7141]42
[16904]43    public IntervalCollectionView() {
[16544]44      InitializeComponent();
45      dataGridView.AutoGenerateColumns = false;
46      dataGridView.AllowUserToAddRows = false;
[12574]47    }
[7141]48
[16544]49    protected override void OnContentChanged() {
50      base.OnContentChanged();
51      if (Content == null) {
52        DataGridView.Rows.Clear();
53        DataGridView.Columns.Clear();
[16887]54      } else {
[16544]55        UpdateData();
56      }
[7141]57    }
58
[16544]59    protected override void SetEnabledStateOfControls() {
60      base.SetEnabledStateOfControls();
61      dataGridView.Enabled = Content != null;
[16586]62      dataGridView.ReadOnly = ReadOnly;
[7141]63    }
64
[16862]65    private void UpdateData() {
[16627]66      DataGridView.Rows.Clear();
67      DataGridView.Columns.Clear();
68      SetColumnNames();
69
70
[16927]71      var variablesCount = Content.GetIntervals().Count;
[12574]72
[16544]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);
[12574]79
[16634]80      SetRowsHeader();
[16544]81      FillRows();
82
[16887]83      dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
84      dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
[16544]85      dataGridView.Enabled = true;
[16862]86      //Disable Sorting
87      foreach (DataGridViewColumn column in dataGridView.Columns) {
88        column.SortMode = DataGridViewColumnSortMode.NotSortable;
89      }
[16544]90    }
91
[16862]92    private void FillRows() {
[16544]93      for (var i = 0; i < dataGridView.RowCount; ++i) {
94        var key = (string)dataGridView.Rows[i].HeaderCell.Value;
[16927]95        dataGridView.Rows[i].Cells[0].Value = Content.GetInterval(key).LowerBound;
96        dataGridView.Rows[i].Cells[1].Value = Content.GetInterval(key).UpperBound;
[12574]97      }
[16544]98    }
[12574]99
[16862]100    private void SetRowsHeader() {
[16927]101      for (var i = 0; i < Content.GetIntervals().Count; ++i) {
102        var item = Content.GetIntervals().ElementAt(i);
[16544]103        dataGridView.Rows[i].HeaderCell.Value = item.Key;
104      }
[7141]105    }
106
[16862]107    private void SetColumnNames() {
[16544]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) {
[16545]113      var key = dataGridView.Rows[e.RowIndex].HeaderCell.Value.ToString();
114
115      if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
116
[16927]117      var lowerBound = e.ColumnIndex == 0 ? double.Parse(dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) : Content.GetInterval(key).LowerBound;
118      var upperBound = e.ColumnIndex == 1 ? double.Parse(dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) : Content.GetInterval(key).UpperBound;
[16545]119
[16634]120      // update if there was a change
[16927]121      if (lowerBound != Content.GetInterval(key).LowerBound ||
122        upperBound != Content.GetInterval(key).UpperBound)
123        Content.SetInterval(key, new Interval(lowerBound, upperBound));
[16544]124    }
125
126    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
[16730]127      if (!double.TryParse(e.FormattedValue.ToString(), out var value)) {
128        e.Cancel = true;
129        dataGridView.Rows[e.RowIndex].ErrorText = "Value must be a double value.";
[16888]130        return;
[16921]131      } else {
[16888]132        var lowerBound = double.Parse(dataGridView.Rows[e.RowIndex].Cells[0].Value.ToString());
133        var upperBound = double.Parse(dataGridView.Rows[e.RowIndex].Cells[1].Value.ToString());
134        if (e.ColumnIndex == 1 && value < lowerBound || e.ColumnIndex == 0 && value > upperBound) {
[16730]135          e.Cancel = true;
136          dataGridView.Rows[e.RowIndex].ErrorText = "Lower bound of interval must be smaller than upper bound.";
137        }
138      }
[7141]139    }
[16544]140
141    private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
142      dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
143    }
[7141]144  }
145}
Note: See TracBrowser for help on using the repository browser.