Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4518 was 4518, checked in by mkommend, 13 years ago

Corrected behavior of RunCollection and changed minor parts in the views (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    }
135
136    protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
137      int[] newSortedIndex = Enumerable.Range(0, Content.Count).ToArray();
138      RunCollectionRowComparer rowComparer = new RunCollectionRowComparer();
139      if (sortedColumns.Count() != 0) {
140        rowComparer.SortedIndizes = sortedColumns;
141        rowComparer.Matrix = Content;
142        Array.Sort(newSortedIndex, rowComparer);
143      }
144
145      runToRowMapping = new int[newSortedIndex.Length];
146      int i = 0;
147      foreach (int runIndex in newSortedIndex) {
148        runToRowMapping[runIndex] = i;
149        i++;
150      }
151      UpdateRowAttributes();
152      return newSortedIndex;
153    }
154
155    private void UpdateRowAttributes() {
156      int runIndex = 0;
157      foreach (IRun run in Content) {
158        int rowIndex = this.runToRowMapping[runIndex];
159        this.dataGridView.Rows[rowIndex].Visible = run.Visible;
160        this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
161        runIndex++;
162      }
163    }
164
165    public class RunCollectionRowComparer : IComparer<int> {
166      public RunCollectionRowComparer() {
167      }
168
169      private List<KeyValuePair<int, SortOrder>> sortedIndizes;
170      public IEnumerable<KeyValuePair<int, SortOrder>> SortedIndizes {
171        get { return this.sortedIndizes; }
172        set { sortedIndizes = new List<KeyValuePair<int, SortOrder>>(value); }
173      }
174      private RunCollection matrix;
175      public RunCollection Matrix {
176        get { return this.matrix; }
177        set { this.matrix = value; }
178      }
179
180      public int Compare(int x, int y) {
181        int result = 0;
182        IItem value1, value2;
183        IComparable comparable1, comparable2;
184
185        if (matrix == null)
186          throw new InvalidOperationException("Could not sort IStringConvertibleMatrix if the matrix member is null.");
187        if (sortedIndizes == null)
188          return 0;
189
190        foreach (KeyValuePair<int, SortOrder> pair in sortedIndizes.Where(p => p.Value != SortOrder.None)) {
191          value1 = matrix.GetValue(x, pair.Key);
192          value2 = matrix.GetValue(y, pair.Key);
193          comparable1 = value1 as IComparable;
194          comparable2 = value2 as IComparable;
195          if (comparable1 != null)
196            result = comparable1.CompareTo(comparable2);
197          else {
198            string string1 = value1 != null ? value1.ToString() : string.Empty;
199            string string2 = value2 != null ? value2.ToString() : string.Empty;
200            result = string1.CompareTo(string2);
201          }
202          if (pair.Value == SortOrder.Descending)
203            result *= -1;
204          if (result != 0)
205            return result;
206        }
207        return result;
208      }
209    }
210  }
211}
Note: See TracBrowser for help on using the repository browser.