Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4774 was 4771, checked in by mkommend, 14 years ago

Registered event on 'RowHeaderDoubleClicked' (ticket #1264).

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