Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/RunCollectionViews/RunCollectionTableView.cs @ 11007

Last change on this file since 11007 was 9910, checked in by ascheibe, 11 years ago

#2099 renamed run collection views

File size: 9.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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("Table")]
32  [Content(typeof(RunCollection), false)]
33  public sealed partial class RunCollectionTableView : StringConvertibleMatrixView {
34    private int[] runToRowMapping;
35    private bool suppressUpdates = false;
36    public RunCollectionTableView() {
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.OptimizerNameChanged += 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.OptimizerNameChanged -= 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 (suppressUpdates) return;
104      if (InvokeRequired)
105        this.Invoke(new EventHandler(run_Changed), sender, e);
106      else {
107        IRun run = (IRun)sender;
108        UpdateRun(run);
109      }
110    }
111    #endregion
112
113    private void UpdateCaption() {
114      Caption = Content != null ? Content.OptimizerName + " Table" : ViewAttribute.GetViewName(GetType());
115    }
116
117    protected override void UpdateColumnHeaders() {
118      HashSet<string> visibleColumnNames = new HashSet<string>(dataGridView.Columns.OfType<DataGridViewColumn>()
119       .Where(c => c.Visible && !string.IsNullOrEmpty(c.HeaderText)).Select(c => c.HeaderText));
120
121      for (int i = 0; i < dataGridView.ColumnCount; i++) {
122        if (i < base.Content.ColumnNames.Count())
123          dataGridView.Columns[i].HeaderText = base.Content.ColumnNames.ElementAt(i);
124        else
125          dataGridView.Columns[i].HeaderText = "Column " + (i + 1);
126        dataGridView.Columns[i].Visible = visibleColumnNames.Count == 0 || visibleColumnNames.Contains(dataGridView.Columns[i].HeaderText);
127      }
128    }
129
130    private void UpdateRun(IRun run) {
131      foreach (int runIndex in GetIndexOfRun(run)) {
132        int rowIndex = runToRowMapping[runIndex];
133        this.dataGridView.Rows[rowIndex].Visible = run.Visible;
134        this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
135      }
136      this.UpdateRowHeaders();
137    }
138
139
140    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
141      if (InvokeRequired)
142        Invoke(new EventHandler(Content_UpdateOfRunsInProgressChanged), sender, e);
143      else {
144        suppressUpdates = Content.UpdateOfRunsInProgress;
145        if (!suppressUpdates) UpdateRowAttributes();
146      }
147    }
148
149    private IEnumerable<int> GetIndexOfRun(IRun run) {
150      int i = 0;
151      foreach (IRun actualRun in Content) {
152        if (actualRun == run)
153          yield return i;
154        i++;
155      }
156    }
157
158    private void dataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
159      if (e.RowIndex >= 0) {
160        IRun run = Content.ElementAt(runToRowMapping.ToList().IndexOf(e.RowIndex));
161        IContentView view = MainFormManager.MainForm.ShowContent(run);
162        if (view != null) {
163          view.ReadOnly = this.ReadOnly;
164          view.Locked = this.Locked;
165        }
166      }
167    }
168
169    protected override void ClearSorting() {
170      base.ClearSorting();
171      runToRowMapping = Enumerable.Range(0, Content.Count).ToArray();
172      UpdateRowAttributes();
173    }
174
175    protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
176      int[] newSortedIndex = Enumerable.Range(0, Content.Count).ToArray();
177      RunCollectionRowComparer rowComparer = new RunCollectionRowComparer();
178      if (sortedColumns.Count() != 0) {
179        rowComparer.SortedIndices = sortedColumns;
180        rowComparer.Matrix = Content;
181        Array.Sort(newSortedIndex, rowComparer);
182      }
183
184      runToRowMapping = new int[newSortedIndex.Length];
185      int i = 0;
186      foreach (int runIndex in newSortedIndex) {
187        runToRowMapping[runIndex] = i;
188        i++;
189      }
190      UpdateRowAttributes();
191      return newSortedIndex;
192    }
193
194    private void UpdateRowAttributes() {
195      int runIndex = 0;
196      foreach (IRun run in Content) {
197        int rowIndex = this.runToRowMapping[runIndex];
198        this.dataGridView.Rows[rowIndex].Visible = run.Visible;
199        this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
200        runIndex++;
201      }
202      rowsTextBox.Text = dataGridView.Rows.GetRowCount(DataGridViewElementStates.Visible).ToString();
203      UpdateRowHeaders();
204    }
205
206    public class RunCollectionRowComparer : IComparer<int> {
207      public RunCollectionRowComparer() {
208      }
209
210      private List<KeyValuePair<int, SortOrder>> sortedIndices;
211      public IEnumerable<KeyValuePair<int, SortOrder>> SortedIndices {
212        get { return this.sortedIndices; }
213        set { sortedIndices = new List<KeyValuePair<int, SortOrder>>(value); }
214      }
215      private RunCollection matrix;
216      public RunCollection Matrix {
217        get { return this.matrix; }
218        set { this.matrix = value; }
219      }
220
221      public int Compare(int x, int y) {
222        int result = 0;
223        IItem value1, value2;
224        IComparable comparable1, comparable2;
225
226        if (matrix == null)
227          throw new InvalidOperationException("Could not sort IStringConvertibleMatrix if the matrix member is null.");
228        if (sortedIndices == null)
229          return 0;
230
231        foreach (KeyValuePair<int, SortOrder> pair in sortedIndices.Where(p => p.Value != SortOrder.None)) {
232          value1 = matrix.GetValue(x, pair.Key);
233          value2 = matrix.GetValue(y, pair.Key);
234          comparable1 = value1 as IComparable;
235          comparable2 = value2 as IComparable;
236          if (comparable1 != null)
237            result = comparable1.CompareTo(comparable2);
238          else {
239            string string1 = value1 != null ? value1.ToString() : string.Empty;
240            string string2 = value2 != null ? value2.ToString() : string.Empty;
241            result = string1.CompareTo(string2);
242          }
243          if (pair.Value == SortOrder.Descending)
244            result *= -1;
245          if (result != 0)
246            return result;
247        }
248        return result;
249      }
250    }
251  }
252}
Note: See TracBrowser for help on using the repository browser.