Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12005 was 11344, checked in by abeham, 10 years ago

#2120:

  • Parameters and Results are now ObservableDictionaries
  • PropertyChanged event handler replaces the Changed event handler
  • RunCollection listens to changed events to each run's parameters and results (8 additional event handlers per run)
File size: 10.0 KB
RevLine 
[3332]1#region License Information
2/* HeuristicLab
[11171]3 * Copyright (C) 2002-2014 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;
[4888]27using HeuristicLab.Core;
[4068]28using HeuristicLab.Data.Views;
[3332]29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.Optimization.Views {
[9910]32  [View("Table")]
[3332]33  [Content(typeof(RunCollection), false)]
[9910]34  public sealed partial class RunCollectionTableView : StringConvertibleMatrixView {
[4200]35    private int[] runToRowMapping;
[4883]36    private bool suppressUpdates = false;
[9910]37    public RunCollectionTableView() {
[3332]38      InitializeComponent();
[4771]39      dataGridView.RowHeaderMouseDoubleClick += new DataGridViewCellMouseEventHandler(dataGridView_RowHeaderMouseDoubleClick);
[3332]40    }
41
[3350]42    public override bool ReadOnly {
[4435]43      get { return true; }
[3350]44      set { /*not needed because results are always readonly */}
45    }
46
[3332]47    public new RunCollection Content {
[3423]48      get { return (RunCollection)base.Content; }
[3332]49      set { base.Content = value; }
50    }
51
[3614]52    protected override void OnContentChanged() {
53      base.OnContentChanged();
54      if (Content != null) {
[4200]55        runToRowMapping = Enumerable.Range(0, Content.Count).ToArray();
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
[4819]119    protected override void UpdateColumnHeaders() {
120      HashSet<string> visibleColumnNames = new HashSet<string>(dataGridView.Columns.OfType<DataGridViewColumn>()
121       .Where(c => c.Visible && !string.IsNullOrEmpty(c.HeaderText)).Select(c => c.HeaderText));
122
123      for (int i = 0; i < dataGridView.ColumnCount; i++) {
124        if (i < base.Content.ColumnNames.Count())
125          dataGridView.Columns[i].HeaderText = base.Content.ColumnNames.ElementAt(i);
126        else
127          dataGridView.Columns[i].HeaderText = "Column " + (i + 1);
128        dataGridView.Columns[i].Visible = visibleColumnNames.Count == 0 || visibleColumnNames.Contains(dataGridView.Columns[i].HeaderText);
129      }
130    }
131
[3614]132    private void UpdateRun(IRun run) {
[4883]133      foreach (int runIndex in GetIndexOfRun(run)) {
134        int rowIndex = runToRowMapping[runIndex];
135        this.dataGridView.Rows[rowIndex].Visible = run.Visible;
136        this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
137      }
[4200]138      this.UpdateRowHeaders();
[3448]139    }
140
[4883]141
[4888]142    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
[4883]143      if (InvokeRequired)
[4888]144        Invoke(new EventHandler(Content_UpdateOfRunsInProgressChanged), sender, e);
[4883]145      else {
[4888]146        suppressUpdates = Content.UpdateOfRunsInProgress;
[4883]147        if (!suppressUpdates) UpdateRowAttributes();
148      }
149    }
150
151    private IEnumerable<int> GetIndexOfRun(IRun run) {
[4200]152      int i = 0;
153      foreach (IRun actualRun in Content) {
154        if (actualRun == run)
[4883]155          yield return i;
[4200]156        i++;
157      }
158    }
159
[3332]160    private void dataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
[3546]161      if (e.RowIndex >= 0) {
[4200]162        IRun run = Content.ElementAt(runToRowMapping.ToList().IndexOf(e.RowIndex));
[3557]163        IContentView view = MainFormManager.MainForm.ShowContent(run);
[3423]164        if (view != null) {
165          view.ReadOnly = this.ReadOnly;
166          view.Locked = this.Locked;
167        }
168      }
[3332]169    }
[3447]170
[4518]171    protected override void ClearSorting() {
172      base.ClearSorting();
[4883]173      runToRowMapping = Enumerable.Range(0, Content.Count).ToArray();
[4523]174      UpdateRowAttributes();
[4518]175    }
176
[3447]177    protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
178      int[] newSortedIndex = Enumerable.Range(0, Content.Count).ToArray();
179      RunCollectionRowComparer rowComparer = new RunCollectionRowComparer();
180      if (sortedColumns.Count() != 0) {
[8139]181        rowComparer.SortedIndices = sortedColumns;
[3447]182        rowComparer.Matrix = Content;
183        Array.Sort(newSortedIndex, rowComparer);
184      }
[4200]185
186      runToRowMapping = new int[newSortedIndex.Length];
187      int i = 0;
188      foreach (int runIndex in newSortedIndex) {
189        runToRowMapping[runIndex] = i;
190        i++;
191      }
192      UpdateRowAttributes();
[3447]193      return newSortedIndex;
194    }
195
[4200]196    private void UpdateRowAttributes() {
197      int runIndex = 0;
198      foreach (IRun run in Content) {
199        int rowIndex = this.runToRowMapping[runIndex];
200        this.dataGridView.Rows[rowIndex].Visible = run.Visible;
201        this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
202        runIndex++;
203      }
[9506]204      rowsTextBox.Text = dataGridView.Rows.GetRowCount(DataGridViewElementStates.Visible).ToString();
[4883]205      UpdateRowHeaders();
[4200]206    }
207
[3447]208    public class RunCollectionRowComparer : IComparer<int> {
209      public RunCollectionRowComparer() {
210      }
211
[8139]212      private List<KeyValuePair<int, SortOrder>> sortedIndices;
213      public IEnumerable<KeyValuePair<int, SortOrder>> SortedIndices {
214        get { return this.sortedIndices; }
215        set { sortedIndices = new List<KeyValuePair<int, SortOrder>>(value); }
[3447]216      }
217      private RunCollection matrix;
218      public RunCollection Matrix {
219        get { return this.matrix; }
220        set { this.matrix = value; }
221      }
222
223      public int Compare(int x, int y) {
224        int result = 0;
225        IItem value1, value2;
226        IComparable comparable1, comparable2;
227
228        if (matrix == null)
229          throw new InvalidOperationException("Could not sort IStringConvertibleMatrix if the matrix member is null.");
[8139]230        if (sortedIndices == null)
[3447]231          return 0;
232
[8139]233        foreach (KeyValuePair<int, SortOrder> pair in sortedIndices.Where(p => p.Value != SortOrder.None)) {
[3447]234          value1 = matrix.GetValue(x, pair.Key);
235          value2 = matrix.GetValue(y, pair.Key);
236          comparable1 = value1 as IComparable;
237          comparable2 = value2 as IComparable;
238          if (comparable1 != null)
239            result = comparable1.CompareTo(comparable2);
240          else {
241            string string1 = value1 != null ? value1.ToString() : string.Empty;
242            string string2 = value2 != null ? value2.ToString() : string.Empty;
243            result = string1.CompareTo(string2);
244          }
245          if (pair.Value == SortOrder.Descending)
246            result *= -1;
247          if (result != 0)
248            return result;
249        }
250        return result;
251      }
252    }
[3332]253  }
254}
Note: See TracBrowser for help on using the repository browser.