Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2971: Corrected naming of IntervalCollection and according view.

File size: 5.3 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("IntervalCollection View")]
31  [Content(typeof(IntervalCollection), true)]
32  public sealed partial class IntervalCollectionView : AsynchronousContentView {
33
34    public new IntervalCollection Content {
35      get => (IntervalCollection)base.Content;
36      set => base.Content = value;
37    }
38
39    public DataGridView DataGridView {
40      get => dataGridView;
41    }
42
43    public IntervalCollectionView() {
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 {
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.GetIntervals().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.AllCells);
84      dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
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.GetInterval(key).LowerBound;
96        dataGridView.Rows[i].Cells[1].Value = Content.GetInterval(key).UpperBound;
97      }
98    }
99
100    private void SetRowsHeader() {
101      for (var i = 0; i < Content.GetIntervals().Count; ++i) {
102        var item = Content.GetIntervals().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.GetInterval(key).LowerBound;
118      var upperBound = e.ColumnIndex == 1 ? double.Parse(dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) : Content.GetInterval(key).UpperBound;
119
120      // update if there was a change
121      if (lowerBound != Content.GetInterval(key).LowerBound ||
122        upperBound != Content.GetInterval(key).UpperBound)
123        Content.SetInterval(key, new Interval(lowerBound, upperBound));
124    }
125
126    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
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.";
130        return;
131      } else {
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) {
135          e.Cancel = true;
136          dataGridView.Rows[e.RowIndex].ErrorText = "Lower bound of interval must be smaller than upper bound.";
137        }
138      }
139    }
140
141    private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
142      dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.