Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4857 was 4819, checked in by mkommend, 14 years ago

Changed the behavior of the RunCollectionTabularView to apply always the selected filters (ticket #1251).

File size: 8.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      dataGridView.RowHeaderMouseDoubleClick += new DataGridViewCellMouseEventHandler(dataGridView_RowHeaderMouseDoubleClick);
38    }
39
40    public override bool ReadOnly {
41      get { return true; }
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    }
85    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
86      DeregisterRunEvents(e.Items);
87    }
88    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
89      RegisterRunEvents(e.Items);
90    }
91    private void run_Changed(object sender, EventArgs e) {
92      if (InvokeRequired)
93        this.Invoke(new EventHandler(run_Changed), sender, e);
94      else {
95        IRun run = (IRun)sender;
96        UpdateRun(run);
97      }
98    }
99    #endregion
100
101    protected override void UpdateColumnHeaders() {
102      HashSet<string> visibleColumnNames = new HashSet<string>(dataGridView.Columns.OfType<DataGridViewColumn>()
103       .Where(c => c.Visible && !string.IsNullOrEmpty(c.HeaderText)).Select(c => c.HeaderText));
104
105      for (int i = 0; i < dataGridView.ColumnCount; i++) {
106        if (i < base.Content.ColumnNames.Count())
107          dataGridView.Columns[i].HeaderText = base.Content.ColumnNames.ElementAt(i);
108        else
109          dataGridView.Columns[i].HeaderText = "Column " + (i + 1);
110        dataGridView.Columns[i].Visible = visibleColumnNames.Count == 0 || visibleColumnNames.Contains(dataGridView.Columns[i].HeaderText);
111      }
112    }
113
114    private void UpdateRun(IRun run) {
115      int runIndex = GetIndexOfRun(run);
116      int rowIndex = runToRowMapping[runIndex];
117      this.dataGridView.Rows[rowIndex].Visible = run.Visible;
118      this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
119      this.UpdateRowHeaders();
120    }
121
122    private int GetIndexOfRun(IRun run) {
123      int i = 0;
124      foreach (IRun actualRun in Content) {
125        if (actualRun == run)
126          return i;
127        i++;
128      }
129      throw new ArgumentException("Run " + run.Name + "could not be found in the RunCollection.");
130    }
131
132    private void dataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
133      if (e.RowIndex >= 0) {
134        IRun run = Content.ElementAt(runToRowMapping.ToList().IndexOf(e.RowIndex));
135        IContentView view = MainFormManager.MainForm.ShowContent(run);
136        if (view != null) {
137          view.ReadOnly = this.ReadOnly;
138          view.Locked = this.Locked;
139        }
140      }
141    }
142
143    protected override void ClearSorting() {
144      base.ClearSorting();
145      runToRowMapping = new int[Content.Count];
146      for (int i = 0; i < runToRowMapping.Length; i++)
147        runToRowMapping[i] = i;
148      UpdateRowAttributes();
149    }
150
151    protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
152      int[] newSortedIndex = Enumerable.Range(0, Content.Count).ToArray();
153      RunCollectionRowComparer rowComparer = new RunCollectionRowComparer();
154      if (sortedColumns.Count() != 0) {
155        rowComparer.SortedIndizes = sortedColumns;
156        rowComparer.Matrix = Content;
157        Array.Sort(newSortedIndex, rowComparer);
158      }
159
160      runToRowMapping = new int[newSortedIndex.Length];
161      int i = 0;
162      foreach (int runIndex in newSortedIndex) {
163        runToRowMapping[runIndex] = i;
164        i++;
165      }
166      UpdateRowAttributes();
167      return newSortedIndex;
168    }
169
170    private void UpdateRowAttributes() {
171      int runIndex = 0;
172      foreach (IRun run in Content) {
173        int rowIndex = this.runToRowMapping[runIndex];
174        this.dataGridView.Rows[rowIndex].Visible = run.Visible;
175        this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
176        runIndex++;
177      }
178    }
179
180    public class RunCollectionRowComparer : IComparer<int> {
181      public RunCollectionRowComparer() {
182      }
183
184      private List<KeyValuePair<int, SortOrder>> sortedIndizes;
185      public IEnumerable<KeyValuePair<int, SortOrder>> SortedIndizes {
186        get { return this.sortedIndizes; }
187        set { sortedIndizes = new List<KeyValuePair<int, SortOrder>>(value); }
188      }
189      private RunCollection matrix;
190      public RunCollection Matrix {
191        get { return this.matrix; }
192        set { this.matrix = value; }
193      }
194
195      public int Compare(int x, int y) {
196        int result = 0;
197        IItem value1, value2;
198        IComparable comparable1, comparable2;
199
200        if (matrix == null)
201          throw new InvalidOperationException("Could not sort IStringConvertibleMatrix if the matrix member is null.");
202        if (sortedIndizes == null)
203          return 0;
204
205        foreach (KeyValuePair<int, SortOrder> pair in sortedIndizes.Where(p => p.Value != SortOrder.None)) {
206          value1 = matrix.GetValue(x, pair.Key);
207          value2 = matrix.GetValue(y, pair.Key);
208          comparable1 = value1 as IComparable;
209          comparable2 = value2 as IComparable;
210          if (comparable1 != null)
211            result = comparable1.CompareTo(comparable2);
212          else {
213            string string1 = value1 != null ? value1.ToString() : string.Empty;
214            string string2 = value2 != null ? value2.ToString() : string.Empty;
215            result = string1.CompareTo(string2);
216          }
217          if (pair.Value == SortOrder.Descending)
218            result *= -1;
219          if (result != 0)
220            return result;
221        }
222        return result;
223      }
224    }
225  }
226}
Note: See TracBrowser for help on using the repository browser.