Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5436 was 4888, checked in by swagner, 14 years ago

Trivial changes due to review of r4883 (#1284)

File size: 9.2 KB
RevLine 
[3332]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.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      }
57    }
58
[4200]59    #region events
[3448]60    protected override void RegisterContentEvents() {
61      base.RegisterContentEvents();
62      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
63      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
64      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
[4888]65      Content.UpdateOfRunsInProgressChanged += new EventHandler(Content_UpdateOfRunsInProgressChanged);
[3448]66      RegisterRunEvents(Content);
67    }
[4200]68    private void RegisterRunEvents(IEnumerable<IRun> runs) {
[3448]69      foreach (IRun run in runs)
70        run.Changed += new EventHandler(run_Changed);
71    }
72    protected override void DeregisterContentEvents() {
73      base.DeregisterContentEvents();
74      Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
75      Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
76      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
[4888]77      Content.UpdateOfRunsInProgressChanged -= new EventHandler(Content_UpdateOfRunsInProgressChanged);
[3448]78      DeregisterRunEvents(Content);
79    }
[4200]80    private void DeregisterRunEvents(IEnumerable<IRun> runs) {
[3448]81      foreach (IRun run in runs)
82        run.Changed -= new EventHandler(run_Changed);
83    }
84    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
85      DeregisterRunEvents(e.OldItems);
86      RegisterRunEvents(e.Items);
87    }
88    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
[3449]89      DeregisterRunEvents(e.Items);
[3448]90    }
91    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
92      RegisterRunEvents(e.Items);
93    }
94    private void run_Changed(object sender, EventArgs e) {
[3632]95      if (InvokeRequired)
96        this.Invoke(new EventHandler(run_Changed), sender, e);
97      else {
98        IRun run = (IRun)sender;
99        UpdateRun(run);
100      }
[3614]101    }
[4200]102    #endregion
[3614]103
[4819]104    protected override void UpdateColumnHeaders() {
105      HashSet<string> visibleColumnNames = new HashSet<string>(dataGridView.Columns.OfType<DataGridViewColumn>()
106       .Where(c => c.Visible && !string.IsNullOrEmpty(c.HeaderText)).Select(c => c.HeaderText));
107
108      for (int i = 0; i < dataGridView.ColumnCount; i++) {
109        if (i < base.Content.ColumnNames.Count())
110          dataGridView.Columns[i].HeaderText = base.Content.ColumnNames.ElementAt(i);
111        else
112          dataGridView.Columns[i].HeaderText = "Column " + (i + 1);
113        dataGridView.Columns[i].Visible = visibleColumnNames.Count == 0 || visibleColumnNames.Contains(dataGridView.Columns[i].HeaderText);
114      }
115    }
116
[3614]117    private void UpdateRun(IRun run) {
[4883]118      foreach (int runIndex in GetIndexOfRun(run)) {
119        int rowIndex = runToRowMapping[runIndex];
120        this.dataGridView.Rows[rowIndex].Visible = run.Visible;
121        this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
122      }
[4200]123      this.UpdateRowHeaders();
[3448]124    }
125
[4883]126
[4888]127    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
[4883]128      if (InvokeRequired)
[4888]129        Invoke(new EventHandler(Content_UpdateOfRunsInProgressChanged), sender, e);
[4883]130      else {
[4888]131        suppressUpdates = Content.UpdateOfRunsInProgress;
[4883]132        if (!suppressUpdates) UpdateRowAttributes();
133      }
134    }
135
136    private IEnumerable<int> GetIndexOfRun(IRun run) {
[4200]137      int i = 0;
138      foreach (IRun actualRun in Content) {
139        if (actualRun == run)
[4883]140          yield return i;
[4200]141        i++;
142      }
143    }
144
[3332]145    private void dataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
[3546]146      if (e.RowIndex >= 0) {
[4200]147        IRun run = Content.ElementAt(runToRowMapping.ToList().IndexOf(e.RowIndex));
[3557]148        IContentView view = MainFormManager.MainForm.ShowContent(run);
[3423]149        if (view != null) {
150          view.ReadOnly = this.ReadOnly;
151          view.Locked = this.Locked;
152        }
153      }
[3332]154    }
[3447]155
[4518]156    protected override void ClearSorting() {
157      base.ClearSorting();
[4883]158      runToRowMapping = Enumerable.Range(0, Content.Count).ToArray();
[4523]159      UpdateRowAttributes();
[4518]160    }
161
[3447]162    protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
163      int[] newSortedIndex = Enumerable.Range(0, Content.Count).ToArray();
164      RunCollectionRowComparer rowComparer = new RunCollectionRowComparer();
165      if (sortedColumns.Count() != 0) {
166        rowComparer.SortedIndizes = sortedColumns;
167        rowComparer.Matrix = Content;
168        Array.Sort(newSortedIndex, rowComparer);
169      }
[4200]170
171      runToRowMapping = new int[newSortedIndex.Length];
172      int i = 0;
173      foreach (int runIndex in newSortedIndex) {
174        runToRowMapping[runIndex] = i;
175        i++;
176      }
177      UpdateRowAttributes();
[3447]178      return newSortedIndex;
179    }
180
[4200]181    private void UpdateRowAttributes() {
182      int runIndex = 0;
183      foreach (IRun run in Content) {
184        int rowIndex = this.runToRowMapping[runIndex];
185        this.dataGridView.Rows[rowIndex].Visible = run.Visible;
186        this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
187        runIndex++;
188      }
[4883]189      UpdateRowHeaders();
[4200]190    }
191
[3447]192    public class RunCollectionRowComparer : IComparer<int> {
193      public RunCollectionRowComparer() {
194      }
195
196      private List<KeyValuePair<int, SortOrder>> sortedIndizes;
197      public IEnumerable<KeyValuePair<int, SortOrder>> SortedIndizes {
198        get { return this.sortedIndizes; }
199        set { sortedIndizes = new List<KeyValuePair<int, SortOrder>>(value); }
200      }
201      private RunCollection matrix;
202      public RunCollection Matrix {
203        get { return this.matrix; }
204        set { this.matrix = value; }
205      }
206
207      public int Compare(int x, int y) {
208        int result = 0;
209        IItem value1, value2;
210        IComparable comparable1, comparable2;
211
212        if (matrix == null)
213          throw new InvalidOperationException("Could not sort IStringConvertibleMatrix if the matrix member is null.");
214        if (sortedIndizes == null)
215          return 0;
216
217        foreach (KeyValuePair<int, SortOrder> pair in sortedIndizes.Where(p => p.Value != SortOrder.None)) {
218          value1 = matrix.GetValue(x, pair.Key);
219          value2 = matrix.GetValue(y, pair.Key);
220          comparable1 = value1 as IComparable;
221          comparable2 = value2 as IComparable;
222          if (comparable1 != null)
223            result = comparable1.CompareTo(comparable2);
224          else {
225            string string1 = value1 != null ? value1.ToString() : string.Empty;
226            string string2 = value2 != null ? value2.ToString() : string.Empty;
227            result = string1.CompareTo(string2);
228          }
229          if (pair.Value == SortOrder.Descending)
230            result *= -1;
231          if (result != 0)
232            return result;
233        }
234        return result;
235      }
236    }
[3332]237  }
238}
Note: See TracBrowser for help on using the repository browser.