Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionTableView.cs @ 11170

Last change on this file since 11170 was 11170, checked in by ascheibe, 10 years ago

#2115 updated copyright year in stable branch

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