Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Optimization.Views/3.3/RunCollectionTabularView.cs @ 9310

Last change on this file since 9310 was 4523, checked in by mkommend, 14 years ago

Corrected coloring of runs in RunCollectionTabularView (ticket #1213).

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