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
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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    private bool suppressUpdates = false;
36    public RunCollectionTabularView() {
37      InitializeComponent();
38      dataGridView.RowHeaderMouseDoubleClick += new DataGridViewCellMouseEventHandler(dataGridView_RowHeaderMouseDoubleClick);
39    }
40
41    public override bool ReadOnly {
42      get { return true; }
43      set { /*not needed because results are always readonly */}
44    }
45
46    public new RunCollection Content {
47      get { return (RunCollection)base.Content; }
48      set { base.Content = value; }
49    }
50
51    protected override void OnContentChanged() {
52      base.OnContentChanged();
53      if (Content != null) {
54        runToRowMapping = Enumerable.Range(0, Content.Count).ToArray();
55        UpdateRowAttributes();
56      }
57      UpdateCaption();
58    }
59
60    #region events
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);
66      Content.UpdateOfRunsInProgressChanged += new EventHandler(Content_UpdateOfRunsInProgressChanged);
67      Content.AlgorithmNameChanged += new EventHandler(Content_AlgorithmNameChanged);
68      RegisterRunEvents(Content);
69    }
70    private void RegisterRunEvents(IEnumerable<IRun> runs) {
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);
79      Content.UpdateOfRunsInProgressChanged -= new EventHandler(Content_UpdateOfRunsInProgressChanged);
80      Content.AlgorithmNameChanged -= new EventHandler(Content_AlgorithmNameChanged);
81      DeregisterRunEvents(Content);
82    }
83    private void DeregisterRunEvents(IEnumerable<IRun> runs) {
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) {
92      DeregisterRunEvents(e.Items);
93    }
94    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
95      RegisterRunEvents(e.Items);
96    }
97    private void Content_AlgorithmNameChanged(object sender, EventArgs e) {
98      if (InvokeRequired)
99        Invoke(new EventHandler(Content_AlgorithmNameChanged), sender, e);
100      else UpdateCaption();
101    }
102    private void run_Changed(object sender, EventArgs e) {
103      if (InvokeRequired)
104        this.Invoke(new EventHandler(run_Changed), sender, e);
105      else {
106        IRun run = (IRun)sender;
107        UpdateRun(run);
108      }
109    }
110    #endregion
111
112    private void UpdateCaption() {
113      Caption = Content != null ? Content.AlgorithmName + "Tabular View" : ViewAttribute.GetViewName(GetType());
114    }
115
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
129    private void UpdateRun(IRun run) {
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      }
135      this.UpdateRowHeaders();
136    }
137
138
139    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
140      if (InvokeRequired)
141        Invoke(new EventHandler(Content_UpdateOfRunsInProgressChanged), sender, e);
142      else {
143        suppressUpdates = Content.UpdateOfRunsInProgress;
144        if (!suppressUpdates) UpdateRowAttributes();
145      }
146    }
147
148    private IEnumerable<int> GetIndexOfRun(IRun run) {
149      int i = 0;
150      foreach (IRun actualRun in Content) {
151        if (actualRun == run)
152          yield return i;
153        i++;
154      }
155    }
156
157    private void dataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
158      if (e.RowIndex >= 0) {
159        IRun run = Content.ElementAt(runToRowMapping.ToList().IndexOf(e.RowIndex));
160        IContentView view = MainFormManager.MainForm.ShowContent(run);
161        if (view != null) {
162          view.ReadOnly = this.ReadOnly;
163          view.Locked = this.Locked;
164        }
165      }
166    }
167
168    protected override void ClearSorting() {
169      base.ClearSorting();
170      runToRowMapping = Enumerable.Range(0, Content.Count).ToArray();
171      UpdateRowAttributes();
172    }
173
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) {
178        rowComparer.SortedIndices = sortedColumns;
179        rowComparer.Matrix = Content;
180        Array.Sort(newSortedIndex, rowComparer);
181      }
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();
190      return newSortedIndex;
191    }
192
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      }
201      UpdateRowHeaders();
202    }
203
204    public class RunCollectionRowComparer : IComparer<int> {
205      public RunCollectionRowComparer() {
206      }
207
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); }
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.");
226        if (sortedIndices == null)
227          return 0;
228
229        foreach (KeyValuePair<int, SortOrder> pair in sortedIndices.Where(p => p.Value != SortOrder.None)) {
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    }
249  }
250}
Note: See TracBrowser for help on using the repository browser.