Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4216 was 4200, checked in by mkommend, 14 years ago

improved the performance of RunCollections by caching some properties and corrected some bugs in the RunCollectionTabularView (ticket #1144)

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