Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionTableView.cs @ 13042

Last change on this file since 13042 was 13036, checked in by gkronber, 9 years ago

#2473: hiding of columns that have only one single value for all runs in the RunCollectionTableView

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