Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionTableView.cs @ 12888

Last change on this file since 12888 was 12692, checked in by abeham, 9 years ago

#2270:

  • Fixed bug in RunCollectionChartAggregationView regarding null value
  • Fixed bug in RunCollectionTableView when runs were added during UpdateOfRunsInProgress
  • Added suppressUpdates to RunCollectionView and listening to UpdateOfRunsInProgress
  • Made sure that RunCollection would reset updateOfRunsInProgress to the same value as before when changing it in internal methods
File size: 10.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Core;
28using HeuristicLab.Data.Views;
29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.Optimization.Views {
32  [View("Table")]
33  [Content(typeof(RunCollection), false)]
34  public sealed partial class RunCollectionTableView : StringConvertibleMatrixView {
35    private int[] runToRowMapping;
36    private bool suppressUpdates = false;
37    public RunCollectionTableView() {
38      InitializeComponent();
39      dataGridView.RowHeaderMouseDoubleClick += new DataGridViewCellMouseEventHandler(dataGridView_RowHeaderMouseDoubleClick);
40    }
41
42    public override bool ReadOnly {
43      get { return true; }
44      set { /*not needed because results are always readonly */}
45    }
46
47    public new RunCollection Content {
48      get { return (RunCollection)base.Content; }
49      set { base.Content = value; }
50    }
51
52    protected override void OnContentChanged() {
53      base.OnContentChanged();
54      if (Content != null) {
55        UpdateRowAttributes();
56      }
57      UpdateCaption();
58    }
59
60    #region events
61    protected override void RegisterContentEvents() {
62      base.RegisterContentEvents();
63      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
64      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
65      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
66      Content.UpdateOfRunsInProgressChanged += new EventHandler(Content_UpdateOfRunsInProgressChanged);
67      Content.OptimizerNameChanged += new EventHandler(Content_AlgorithmNameChanged);
68      RegisterRunEvents(Content);
69    }
70    private void RegisterRunEvents(IEnumerable<IRun> runs) {
71      foreach (IRun run in runs)
72        run.PropertyChanged += run_PropertyChanged;
73    }
74    protected override void DeregisterContentEvents() {
75      base.DeregisterContentEvents();
76      Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
77      Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
78      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
79      Content.UpdateOfRunsInProgressChanged -= new EventHandler(Content_UpdateOfRunsInProgressChanged);
80      Content.OptimizerNameChanged -= new EventHandler(Content_AlgorithmNameChanged);
81      DeregisterRunEvents(Content);
82    }
83    private void DeregisterRunEvents(IEnumerable<IRun> runs) {
84      foreach (IRun run in runs)
85        run.PropertyChanged -= run_PropertyChanged;
86    }
87    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
88      DeregisterRunEvents(e.OldItems);
89      RegisterRunEvents(e.Items);
90    }
91    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
92      DeregisterRunEvents(e.Items);
93    }
94    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
95      RegisterRunEvents(e.Items);
96    }
97    private void Content_AlgorithmNameChanged(object sender, EventArgs e) {
98      if (InvokeRequired)
99        Invoke(new EventHandler(Content_AlgorithmNameChanged), sender, e);
100      else UpdateCaption();
101    }
102    private void run_PropertyChanged(object sender, PropertyChangedEventArgs e) {
103      if (suppressUpdates) return;
104      if (InvokeRequired)
105        this.Invoke((Action<object, PropertyChangedEventArgs>)run_PropertyChanged, sender, e);
106      else {
107        IRun run = (IRun)sender;
108        if (e.PropertyName == "Color" || e.PropertyName == "Visible")
109          UpdateRun(run);
110      }
111    }
112    #endregion
113
114    private void UpdateCaption() {
115      Caption = Content != null ? Content.OptimizerName + " Table" : ViewAttribute.GetViewName(GetType());
116    }
117
118    protected override void UpdateData() {
119      if (suppressUpdates) return;
120      base.UpdateData();
121    }
122
123    protected override void UpdateColumnHeaders() {
124      HashSet<string> visibleColumnNames = new HashSet<string>(dataGridView.Columns.OfType<DataGridViewColumn>()
125       .Where(c => c.Visible && !string.IsNullOrEmpty(c.HeaderText)).Select(c => c.HeaderText));
126
127      for (int i = 0; i < dataGridView.ColumnCount; i++) {
128        if (i < base.Content.ColumnNames.Count())
129          dataGridView.Columns[i].HeaderText = base.Content.ColumnNames.ElementAt(i);
130        else
131          dataGridView.Columns[i].HeaderText = "Column " + (i + 1);
132        dataGridView.Columns[i].Visible = visibleColumnNames.Count == 0 || visibleColumnNames.Contains(dataGridView.Columns[i].HeaderText);
133      }
134    }
135
136    private void UpdateRun(IRun run) {
137      foreach (int runIndex in GetIndexOfRun(run)) {
138        int rowIndex = runToRowMapping[runIndex];
139        this.dataGridView.Rows[rowIndex].Visible = run.Visible;
140        this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
141      }
142      this.UpdateRowHeaders();
143    }
144
145
146    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
147      if (InvokeRequired)
148        Invoke(new EventHandler(Content_UpdateOfRunsInProgressChanged), sender, e);
149      else {
150        suppressUpdates = Content.UpdateOfRunsInProgress;
151        if (!suppressUpdates) {
152          UpdateData();
153          UpdateRowAttributes();
154        }
155      }
156    }
157
158    private IEnumerable<int> GetIndexOfRun(IRun run) {
159      int i = 0;
160      foreach (IRun actualRun in Content) {
161        if (actualRun == run)
162          yield return i;
163        i++;
164      }
165    }
166
167    private void dataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
168      if (e.RowIndex >= 0) {
169        IRun run = Content.ElementAt(runToRowMapping.ToList().IndexOf(e.RowIndex));
170        IContentView view = MainFormManager.MainForm.ShowContent(run);
171        if (view != null) {
172          view.ReadOnly = this.ReadOnly;
173          view.Locked = this.Locked;
174        }
175      }
176    }
177
178    protected override void ClearSorting() {
179      base.ClearSorting();
180      UpdateRowAttributes();
181    }
182
183    protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
184      int[] newSortedIndex = Enumerable.Range(0, Content.Count).ToArray();
185      RunCollectionRowComparer rowComparer = new RunCollectionRowComparer();
186      if (sortedColumns.Count() != 0) {
187        rowComparer.SortedIndices = sortedColumns;
188        rowComparer.Matrix = Content;
189        Array.Sort(newSortedIndex, rowComparer);
190      }
191
192      runToRowMapping = new int[newSortedIndex.Length];
193      int i = 0;
194      foreach (int runIndex in newSortedIndex) {
195        runToRowMapping[runIndex] = i;
196        i++;
197      }
198      UpdateRowAttributes(rebuild: false);
199      return newSortedIndex;
200    }
201
202    private void UpdateRowAttributes(bool rebuild = true) {
203      if (rebuild) runToRowMapping = Enumerable.Range(0, Content.Count).ToArray();
204      int runIndex = 0;
205      foreach (IRun run in Content) {
206        int rowIndex = this.runToRowMapping[runIndex];
207        this.dataGridView.Rows[rowIndex].Visible = run.Visible;
208        this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
209        runIndex++;
210      }
211      rowsTextBox.Text = dataGridView.Rows.GetRowCount(DataGridViewElementStates.Visible).ToString();
212      UpdateRowHeaders();
213    }
214
215    public class RunCollectionRowComparer : IComparer<int> {
216      public RunCollectionRowComparer() {
217      }
218
219      private List<KeyValuePair<int, SortOrder>> sortedIndices;
220      public IEnumerable<KeyValuePair<int, SortOrder>> SortedIndices {
221        get { return this.sortedIndices; }
222        set { sortedIndices = new List<KeyValuePair<int, SortOrder>>(value); }
223      }
224      private RunCollection matrix;
225      public RunCollection Matrix {
226        get { return this.matrix; }
227        set { this.matrix = value; }
228      }
229
230      public int Compare(int x, int y) {
231        int result = 0;
232        IItem value1, value2;
233        IComparable comparable1, comparable2;
234
235        if (matrix == null)
236          throw new InvalidOperationException("Could not sort IStringConvertibleMatrix if the matrix member is null.");
237        if (sortedIndices == null)
238          return 0;
239
240        foreach (KeyValuePair<int, SortOrder> pair in sortedIndices.Where(p => p.Value != SortOrder.None)) {
241          value1 = matrix.GetValue(x, pair.Key);
242          value2 = matrix.GetValue(y, pair.Key);
243          comparable1 = value1 as IComparable;
244          comparable2 = value2 as IComparable;
245          if (comparable1 != null)
246            result = comparable1.CompareTo(comparable2);
247          else {
248            string string1 = value1 != null ? value1.ToString() : string.Empty;
249            string string2 = value2 != null ? value2.ToString() : string.Empty;
250            result = string1.CompareTo(string2);
251          }
252          if (pair.Value == SortOrder.Descending)
253            result *= -1;
254          if (result != 0)
255            return result;
256        }
257        return result;
258      }
259    }
260  }
261}
Note: See TracBrowser for help on using the repository browser.