Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionTabularView.cs @ 8738

Last change on this file since 8738 was 8738, checked in by mkommend, 12 years ago

#1673: Added new property AlgorithmName to the RunCollection and synced the property with the name of the surrounding IOptimizer. The AlgorithmName is used by the RunCollectionViews as prefix for its caption if it was set.

File size: 9.8 KB
RevLine 
[3332]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 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 {
[3347]31  [View("RunCollection Tabular View")]
[3332]32  [Content(typeof(RunCollection), false)]
[4200]33  public sealed partial class RunCollectionTabularView : StringConvertibleMatrixView {
34    private int[] runToRowMapping;
[4883]35    private bool suppressUpdates = false;
[3332]36    public RunCollectionTabularView() {
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);
[8738]67      Content.AlgorithmNameChanged += 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);
[8738]80      Content.AlgorithmNameChanged -= 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) {
[3632]103      if (InvokeRequired)
104        this.Invoke(new EventHandler(run_Changed), sender, e);
105      else {
106        IRun run = (IRun)sender;
107        UpdateRun(run);
108      }
[3614]109    }
[4200]110    #endregion
[3614]111
[8738]112    private void UpdateCaption() {
113      Caption = Content != null ? Content.AlgorithmName + "Tabular View" : ViewAttribute.GetViewName(GetType());
114    }
115
[4819]116    protected override void UpdateColumnHeaders() {
117      HashSet<string> visibleColumnNames = new HashSet<string>(dataGridView.Columns.OfType<DataGridViewColumn>()
118       .Where(c => c.Visible && !string.IsNullOrEmpty(c.HeaderText)).Select(c => c.HeaderText));
119
120      for (int i = 0; i < dataGridView.ColumnCount; i++) {
121        if (i < base.Content.ColumnNames.Count())
122          dataGridView.Columns[i].HeaderText = base.Content.ColumnNames.ElementAt(i);
123        else
124          dataGridView.Columns[i].HeaderText = "Column " + (i + 1);
125        dataGridView.Columns[i].Visible = visibleColumnNames.Count == 0 || visibleColumnNames.Contains(dataGridView.Columns[i].HeaderText);
126      }
127    }
128
[3614]129    private void UpdateRun(IRun run) {
[4883]130      foreach (int runIndex in GetIndexOfRun(run)) {
131        int rowIndex = runToRowMapping[runIndex];
132        this.dataGridView.Rows[rowIndex].Visible = run.Visible;
133        this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
134      }
[4200]135      this.UpdateRowHeaders();
[3448]136    }
137
[4883]138
[4888]139    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
[4883]140      if (InvokeRequired)
[4888]141        Invoke(new EventHandler(Content_UpdateOfRunsInProgressChanged), sender, e);
[4883]142      else {
[4888]143        suppressUpdates = Content.UpdateOfRunsInProgress;
[4883]144        if (!suppressUpdates) UpdateRowAttributes();
145      }
146    }
147
148    private IEnumerable<int> GetIndexOfRun(IRun run) {
[4200]149      int i = 0;
150      foreach (IRun actualRun in Content) {
151        if (actualRun == run)
[4883]152          yield return i;
[4200]153        i++;
154      }
155    }
156
[3332]157    private void dataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
[3546]158      if (e.RowIndex >= 0) {
[4200]159        IRun run = Content.ElementAt(runToRowMapping.ToList().IndexOf(e.RowIndex));
[3557]160        IContentView view = MainFormManager.MainForm.ShowContent(run);
[3423]161        if (view != null) {
162          view.ReadOnly = this.ReadOnly;
163          view.Locked = this.Locked;
164        }
165      }
[3332]166    }
[3447]167
[4518]168    protected override void ClearSorting() {
169      base.ClearSorting();
[4883]170      runToRowMapping = Enumerable.Range(0, Content.Count).ToArray();
[4523]171      UpdateRowAttributes();
[4518]172    }
173
[3447]174    protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
175      int[] newSortedIndex = Enumerable.Range(0, Content.Count).ToArray();
176      RunCollectionRowComparer rowComparer = new RunCollectionRowComparer();
177      if (sortedColumns.Count() != 0) {
[8139]178        rowComparer.SortedIndices = sortedColumns;
[3447]179        rowComparer.Matrix = Content;
180        Array.Sort(newSortedIndex, rowComparer);
181      }
[4200]182
183      runToRowMapping = new int[newSortedIndex.Length];
184      int i = 0;
185      foreach (int runIndex in newSortedIndex) {
186        runToRowMapping[runIndex] = i;
187        i++;
188      }
189      UpdateRowAttributes();
[3447]190      return newSortedIndex;
191    }
192
[4200]193    private void UpdateRowAttributes() {
194      int runIndex = 0;
195      foreach (IRun run in Content) {
196        int rowIndex = this.runToRowMapping[runIndex];
197        this.dataGridView.Rows[rowIndex].Visible = run.Visible;
198        this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
199        runIndex++;
200      }
[4883]201      UpdateRowHeaders();
[4200]202    }
203
[3447]204    public class RunCollectionRowComparer : IComparer<int> {
205      public RunCollectionRowComparer() {
206      }
207
[8139]208      private List<KeyValuePair<int, SortOrder>> sortedIndices;
209      public IEnumerable<KeyValuePair<int, SortOrder>> SortedIndices {
210        get { return this.sortedIndices; }
211        set { sortedIndices = new List<KeyValuePair<int, SortOrder>>(value); }
[3447]212      }
213      private RunCollection matrix;
214      public RunCollection Matrix {
215        get { return this.matrix; }
216        set { this.matrix = value; }
217      }
218
219      public int Compare(int x, int y) {
220        int result = 0;
221        IItem value1, value2;
222        IComparable comparable1, comparable2;
223
224        if (matrix == null)
225          throw new InvalidOperationException("Could not sort IStringConvertibleMatrix if the matrix member is null.");
[8139]226        if (sortedIndices == null)
[3447]227          return 0;
228
[8139]229        foreach (KeyValuePair<int, SortOrder> pair in sortedIndices.Where(p => p.Value != SortOrder.None)) {
[3447]230          value1 = matrix.GetValue(x, pair.Key);
231          value2 = matrix.GetValue(y, pair.Key);
232          comparable1 = value1 as IComparable;
233          comparable2 = value2 as IComparable;
234          if (comparable1 != null)
235            result = comparable1.CompareTo(comparable2);
236          else {
237            string string1 = value1 != null ? value1.ToString() : string.Empty;
238            string string2 = value2 != null ? value2.ToString() : string.Empty;
239            result = string1.CompareTo(string2);
240          }
241          if (pair.Value == SortOrder.Descending)
242            result *= -1;
243          if (result != 0)
244            return result;
245        }
246        return result;
247      }
248    }
[3332]249  }
250}
Note: See TracBrowser for help on using the repository browser.