Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4682 was 4523, checked in by mkommend, 14 years ago

Corrected coloring of runs in RunCollectionTabularView (ticket #1213).

File size: 7.9 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;
[4068]26using HeuristicLab.Core;
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;
[3332]35    public RunCollectionTabularView() {
36      InitializeComponent();
37    }
38
[3350]39    public override bool ReadOnly {
[4435]40      get { return true; }
[3350]41      set { /*not needed because results are always readonly */}
42    }
43
[3332]44    public new RunCollection Content {
[3423]45      get { return (RunCollection)base.Content; }
[3332]46      set { base.Content = value; }
47    }
48
[3614]49    protected override void OnContentChanged() {
50      base.OnContentChanged();
51      if (Content != null) {
[4200]52        runToRowMapping = Enumerable.Range(0, Content.Count).ToArray();
53        UpdateRowAttributes();
[3614]54      }
55    }
56
[4200]57    #region events
[3448]58    protected override void RegisterContentEvents() {
59      base.RegisterContentEvents();
60      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
61      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
62      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
63      RegisterRunEvents(Content);
64    }
[4200]65    private void RegisterRunEvents(IEnumerable<IRun> runs) {
[3448]66      foreach (IRun run in runs)
67        run.Changed += new EventHandler(run_Changed);
68    }
69    protected override void DeregisterContentEvents() {
70      base.DeregisterContentEvents();
71      Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
72      Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
73      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
74      DeregisterRunEvents(Content);
75    }
[4200]76    private void DeregisterRunEvents(IEnumerable<IRun> runs) {
[3448]77      foreach (IRun run in runs)
78        run.Changed -= new EventHandler(run_Changed);
79    }
80    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
81      DeregisterRunEvents(e.OldItems);
82      RegisterRunEvents(e.Items);
83    }
84    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
[3449]85      DeregisterRunEvents(e.Items);
[3448]86    }
87    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
88      RegisterRunEvents(e.Items);
89    }
90    private void run_Changed(object sender, EventArgs e) {
[3632]91      if (InvokeRequired)
92        this.Invoke(new EventHandler(run_Changed), sender, e);
93      else {
94        IRun run = (IRun)sender;
95        UpdateRun(run);
96      }
[3614]97    }
[4200]98    #endregion
[3614]99
100    private void UpdateRun(IRun run) {
[4200]101      int runIndex = GetIndexOfRun(run);
102      int rowIndex = runToRowMapping[runIndex];
[3448]103      this.dataGridView.Rows[rowIndex].Visible = run.Visible;
104      this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
[4200]105      this.UpdateRowHeaders();
[3448]106    }
107
[4200]108    private int GetIndexOfRun(IRun run) {
109      int i = 0;
110      foreach (IRun actualRun in Content) {
111        if (actualRun == run)
112          return i;
113        i++;
114      }
115      throw new ArgumentException("Run " + run.Name + "could not be found in the RunCollection.");
116    }
117
[3332]118    private void dataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
[3546]119      if (e.RowIndex >= 0) {
[4200]120        IRun run = Content.ElementAt(runToRowMapping.ToList().IndexOf(e.RowIndex));
[3557]121        IContentView view = MainFormManager.MainForm.ShowContent(run);
[3423]122        if (view != null) {
123          view.ReadOnly = this.ReadOnly;
124          view.Locked = this.Locked;
125        }
126      }
[3332]127    }
[3447]128
[4518]129    protected override void ClearSorting() {
130      base.ClearSorting();
131      runToRowMapping = new int[Content.Count];
132      for (int i = 0; i < runToRowMapping.Length; i++)
133        runToRowMapping[i] = i;
[4523]134      UpdateRowAttributes();
[4518]135    }
136
[3447]137    protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
138      int[] newSortedIndex = Enumerable.Range(0, Content.Count).ToArray();
139      RunCollectionRowComparer rowComparer = new RunCollectionRowComparer();
140      if (sortedColumns.Count() != 0) {
141        rowComparer.SortedIndizes = sortedColumns;
142        rowComparer.Matrix = Content;
143        Array.Sort(newSortedIndex, rowComparer);
144      }
[4200]145
146      runToRowMapping = new int[newSortedIndex.Length];
147      int i = 0;
148      foreach (int runIndex in newSortedIndex) {
149        runToRowMapping[runIndex] = i;
150        i++;
151      }
152      UpdateRowAttributes();
[3447]153      return newSortedIndex;
154    }
155
[4200]156    private void UpdateRowAttributes() {
157      int runIndex = 0;
158      foreach (IRun run in Content) {
159        int rowIndex = this.runToRowMapping[runIndex];
160        this.dataGridView.Rows[rowIndex].Visible = run.Visible;
161        this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
162        runIndex++;
163      }
164    }
165
[3447]166    public class RunCollectionRowComparer : IComparer<int> {
167      public RunCollectionRowComparer() {
168      }
169
170      private List<KeyValuePair<int, SortOrder>> sortedIndizes;
171      public IEnumerable<KeyValuePair<int, SortOrder>> SortedIndizes {
172        get { return this.sortedIndizes; }
173        set { sortedIndizes = new List<KeyValuePair<int, SortOrder>>(value); }
174      }
175      private RunCollection matrix;
176      public RunCollection Matrix {
177        get { return this.matrix; }
178        set { this.matrix = value; }
179      }
180
181      public int Compare(int x, int y) {
182        int result = 0;
183        IItem value1, value2;
184        IComparable comparable1, comparable2;
185
186        if (matrix == null)
187          throw new InvalidOperationException("Could not sort IStringConvertibleMatrix if the matrix member is null.");
188        if (sortedIndizes == null)
189          return 0;
190
191        foreach (KeyValuePair<int, SortOrder> pair in sortedIndizes.Where(p => p.Value != SortOrder.None)) {
192          value1 = matrix.GetValue(x, pair.Key);
193          value2 = matrix.GetValue(y, pair.Key);
194          comparable1 = value1 as IComparable;
195          comparable2 = value2 as IComparable;
196          if (comparable1 != null)
197            result = comparable1.CompareTo(comparable2);
198          else {
199            string string1 = value1 != null ? value1.ToString() : string.Empty;
200            string string2 = value2 != null ? value2.ToString() : string.Empty;
201            result = string1.CompareTo(string2);
202          }
203          if (pair.Value == SortOrder.Descending)
204            result *= -1;
205          if (result != 0)
206            return result;
207        }
208        return result;
209      }
210    }
[3332]211  }
212}
Note: See TracBrowser for help on using the repository browser.