Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12449 was 12077, checked in by abeham, 10 years ago

#2270: Added suppressUpdates check for Content_Reset events

File size: 10.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.ComponentModel;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Core;
28using HeuristicLab.Data.Views;
29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.Optimization.Views {
32  [View("Table")]
33  [Content(typeof(RunCollection), false)]
34  public sealed partial class RunCollectionTableView : StringConvertibleMatrixView {
35    private int[] runToRowMapping;
36    private bool suppressUpdates = false;
37    public RunCollectionTableView() {
38      InitializeComponent();
39      dataGridView.RowHeaderMouseDoubleClick += new DataGridViewCellMouseEventHandler(dataGridView_RowHeaderMouseDoubleClick);
40    }
41
42    public override bool ReadOnly {
43      get { return true; }
44      set { /*not needed because results are always readonly */}
45    }
46
47    public new RunCollection Content {
48      get { return (RunCollection)base.Content; }
49      set { base.Content = value; }
50    }
51
52    protected override void OnContentChanged() {
53      base.OnContentChanged();
54      if (Content != null) {
55        runToRowMapping = Enumerable.Range(0, Content.Count).ToArray();
56        UpdateRowAttributes();
57      }
58      UpdateCaption();
59    }
60
61    #region events
62    protected override void RegisterContentEvents() {
63      base.RegisterContentEvents();
64      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
65      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
66      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
67      Content.UpdateOfRunsInProgressChanged += new EventHandler(Content_UpdateOfRunsInProgressChanged);
68      Content.OptimizerNameChanged += new EventHandler(Content_AlgorithmNameChanged);
69      RegisterRunEvents(Content);
70    }
71    private void RegisterRunEvents(IEnumerable<IRun> runs) {
72      foreach (IRun run in runs)
73        run.PropertyChanged += run_PropertyChanged;
74    }
75    protected override void DeregisterContentEvents() {
76      base.DeregisterContentEvents();
77      Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
78      Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
79      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
80      Content.UpdateOfRunsInProgressChanged -= new EventHandler(Content_UpdateOfRunsInProgressChanged);
81      Content.OptimizerNameChanged -= new EventHandler(Content_AlgorithmNameChanged);
82      DeregisterRunEvents(Content);
83    }
84    private void DeregisterRunEvents(IEnumerable<IRun> runs) {
85      foreach (IRun run in runs)
86        run.PropertyChanged -= run_PropertyChanged;
87    }
88    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
89      DeregisterRunEvents(e.OldItems);
90      RegisterRunEvents(e.Items);
91    }
92    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
93      DeregisterRunEvents(e.Items);
94    }
95    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
96      RegisterRunEvents(e.Items);
97    }
98    private void Content_AlgorithmNameChanged(object sender, EventArgs e) {
99      if (InvokeRequired)
100        Invoke(new EventHandler(Content_AlgorithmNameChanged), sender, e);
101      else UpdateCaption();
102    }
103    private void run_PropertyChanged(object sender, PropertyChangedEventArgs e) {
104      if (suppressUpdates) return;
105      if (InvokeRequired)
106        this.Invoke((Action<object, PropertyChangedEventArgs>)run_PropertyChanged, sender, e);
107      else {
108        IRun run = (IRun)sender;
109        if (e.PropertyName == "Color" || e.PropertyName == "Visible")
110          UpdateRun(run);
111      }
112    }
113    #endregion
114
115    private void UpdateCaption() {
116      Caption = Content != null ? Content.OptimizerName + " Table" : ViewAttribute.GetViewName(GetType());
117    }
118
119    protected override void UpdateData() {
120      if (suppressUpdates) return;
121      base.UpdateData();
122    }
123
124    protected override void UpdateColumnHeaders() {
125      HashSet<string> visibleColumnNames = new HashSet<string>(dataGridView.Columns.OfType<DataGridViewColumn>()
126       .Where(c => c.Visible && !string.IsNullOrEmpty(c.HeaderText)).Select(c => c.HeaderText));
127
128      for (int i = 0; i < dataGridView.ColumnCount; i++) {
129        if (i < base.Content.ColumnNames.Count())
130          dataGridView.Columns[i].HeaderText = base.Content.ColumnNames.ElementAt(i);
131        else
132          dataGridView.Columns[i].HeaderText = "Column " + (i + 1);
133        dataGridView.Columns[i].Visible = visibleColumnNames.Count == 0 || visibleColumnNames.Contains(dataGridView.Columns[i].HeaderText);
134      }
135    }
136
137    private void UpdateRun(IRun run) {
138      foreach (int runIndex in GetIndexOfRun(run)) {
139        int rowIndex = runToRowMapping[runIndex];
140        this.dataGridView.Rows[rowIndex].Visible = run.Visible;
141        this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
142      }
143      this.UpdateRowHeaders();
144    }
145
146
147    private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
148      if (InvokeRequired)
149        Invoke(new EventHandler(Content_UpdateOfRunsInProgressChanged), sender, e);
150      else {
151        suppressUpdates = Content.UpdateOfRunsInProgress;
152        if (!suppressUpdates) {
153          UpdateRowAttributes();
154          UpdateData();
155        }
156      }
157    }
158
159    private IEnumerable<int> GetIndexOfRun(IRun run) {
160      int i = 0;
161      foreach (IRun actualRun in Content) {
162        if (actualRun == run)
163          yield return i;
164        i++;
165      }
166    }
167
168    private void dataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
169      if (e.RowIndex >= 0) {
170        IRun run = Content.ElementAt(runToRowMapping.ToList().IndexOf(e.RowIndex));
171        IContentView view = MainFormManager.MainForm.ShowContent(run);
172        if (view != null) {
173          view.ReadOnly = this.ReadOnly;
174          view.Locked = this.Locked;
175        }
176      }
177    }
178
179    protected override void ClearSorting() {
180      base.ClearSorting();
181      runToRowMapping = Enumerable.Range(0, Content.Count).ToArray();
182      UpdateRowAttributes();
183    }
184
185    protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
186      int[] newSortedIndex = Enumerable.Range(0, Content.Count).ToArray();
187      RunCollectionRowComparer rowComparer = new RunCollectionRowComparer();
188      if (sortedColumns.Count() != 0) {
189        rowComparer.SortedIndices = sortedColumns;
190        rowComparer.Matrix = Content;
191        Array.Sort(newSortedIndex, rowComparer);
192      }
193
194      runToRowMapping = new int[newSortedIndex.Length];
195      int i = 0;
196      foreach (int runIndex in newSortedIndex) {
197        runToRowMapping[runIndex] = i;
198        i++;
199      }
200      UpdateRowAttributes();
201      return newSortedIndex;
202    }
203
204    private void UpdateRowAttributes() {
205      int runIndex = 0;
206      foreach (IRun run in Content) {
207        int rowIndex = this.runToRowMapping[runIndex];
208        this.dataGridView.Rows[rowIndex].Visible = run.Visible;
209        this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
210        runIndex++;
211      }
212      rowsTextBox.Text = dataGridView.Rows.GetRowCount(DataGridViewElementStates.Visible).ToString();
213      UpdateRowHeaders();
214    }
215
216    public class RunCollectionRowComparer : IComparer<int> {
217      public RunCollectionRowComparer() {
218      }
219
220      private List<KeyValuePair<int, SortOrder>> sortedIndices;
221      public IEnumerable<KeyValuePair<int, SortOrder>> SortedIndices {
222        get { return this.sortedIndices; }
223        set { sortedIndices = new List<KeyValuePair<int, SortOrder>>(value); }
224      }
225      private RunCollection matrix;
226      public RunCollection Matrix {
227        get { return this.matrix; }
228        set { this.matrix = value; }
229      }
230
231      public int Compare(int x, int y) {
232        int result = 0;
233        IItem value1, value2;
234        IComparable comparable1, comparable2;
235
236        if (matrix == null)
237          throw new InvalidOperationException("Could not sort IStringConvertibleMatrix if the matrix member is null.");
238        if (sortedIndices == null)
239          return 0;
240
241        foreach (KeyValuePair<int, SortOrder> pair in sortedIndices.Where(p => p.Value != SortOrder.None)) {
242          value1 = matrix.GetValue(x, pair.Key);
243          value2 = matrix.GetValue(y, pair.Key);
244          comparable1 = value1 as IComparable;
245          comparable2 = value2 as IComparable;
246          if (comparable1 != null)
247            result = comparable1.CompareTo(comparable2);
248          else {
249            string string1 = value1 != null ? value1.ToString() : string.Empty;
250            string string2 = value2 != null ? value2.ToString() : string.Empty;
251            result = string1.CompareTo(string2);
252          }
253          if (pair.Value == SortOrder.Descending)
254            result *= -1;
255          if (result != 0)
256            return result;
257        }
258        return result;
259      }
260    }
261  }
262}
Note: See TracBrowser for help on using the repository browser.