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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Windows.Forms;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Data.Views;
|
---|
28 | using HeuristicLab.MainForm;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Optimization.Views {
|
---|
32 | [View("RunCollection Tabular View")]
|
---|
33 | [Content(typeof(RunCollection), false)]
|
---|
34 | public sealed partial class RunCollectionTabularView : StringConvertibleMatrixView {
|
---|
35 | private int[] runToRowMapping;
|
---|
36 | private bool suppressUpdates = false;
|
---|
37 | public RunCollectionTabularView() {
|
---|
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 | }
|
---|
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.UpdateOfRunsInProgress += new EventHandler<EventArgs<bool>>(Content_UpdateOfRunsInProgress);
|
---|
67 | RegisterRunEvents(Content);
|
---|
68 | }
|
---|
69 | private void RegisterRunEvents(IEnumerable<IRun> runs) {
|
---|
70 | foreach (IRun run in runs)
|
---|
71 | run.Changed += new EventHandler(run_Changed);
|
---|
72 | }
|
---|
73 | protected override void DeregisterContentEvents() {
|
---|
74 | base.DeregisterContentEvents();
|
---|
75 | Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
|
---|
76 | Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
|
---|
77 | Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
|
---|
78 | Content.UpdateOfRunsInProgress -= new EventHandler<EventArgs<bool>>(Content_UpdateOfRunsInProgress);
|
---|
79 | DeregisterRunEvents(Content);
|
---|
80 | }
|
---|
81 | private void DeregisterRunEvents(IEnumerable<IRun> runs) {
|
---|
82 | foreach (IRun run in runs)
|
---|
83 | run.Changed -= new EventHandler(run_Changed);
|
---|
84 | }
|
---|
85 | private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
86 | DeregisterRunEvents(e.OldItems);
|
---|
87 | RegisterRunEvents(e.Items);
|
---|
88 | }
|
---|
89 | private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
90 | DeregisterRunEvents(e.Items);
|
---|
91 | }
|
---|
92 | private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
93 | RegisterRunEvents(e.Items);
|
---|
94 | }
|
---|
95 | private void run_Changed(object sender, EventArgs e) {
|
---|
96 | if (InvokeRequired)
|
---|
97 | this.Invoke(new EventHandler(run_Changed), sender, e);
|
---|
98 | else {
|
---|
99 | IRun run = (IRun)sender;
|
---|
100 | UpdateRun(run);
|
---|
101 | }
|
---|
102 | }
|
---|
103 | #endregion
|
---|
104 |
|
---|
105 | protected override void UpdateColumnHeaders() {
|
---|
106 | HashSet<string> visibleColumnNames = new HashSet<string>(dataGridView.Columns.OfType<DataGridViewColumn>()
|
---|
107 | .Where(c => c.Visible && !string.IsNullOrEmpty(c.HeaderText)).Select(c => c.HeaderText));
|
---|
108 |
|
---|
109 | for (int i = 0; i < dataGridView.ColumnCount; i++) {
|
---|
110 | if (i < base.Content.ColumnNames.Count())
|
---|
111 | dataGridView.Columns[i].HeaderText = base.Content.ColumnNames.ElementAt(i);
|
---|
112 | else
|
---|
113 | dataGridView.Columns[i].HeaderText = "Column " + (i + 1);
|
---|
114 | dataGridView.Columns[i].Visible = visibleColumnNames.Count == 0 || visibleColumnNames.Contains(dataGridView.Columns[i].HeaderText);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | private void UpdateRun(IRun run) {
|
---|
119 | foreach (int runIndex in GetIndexOfRun(run)) {
|
---|
120 | int rowIndex = runToRowMapping[runIndex];
|
---|
121 | this.dataGridView.Rows[rowIndex].Visible = run.Visible;
|
---|
122 | this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
|
---|
123 | }
|
---|
124 | this.UpdateRowHeaders();
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | private void Content_UpdateOfRunsInProgress(object sender, Common.EventArgs<bool> e) {
|
---|
129 | if (InvokeRequired)
|
---|
130 | Invoke(new EventHandler<EventArgs<bool>>(Content_UpdateOfRunsInProgress), sender, e);
|
---|
131 | else {
|
---|
132 | suppressUpdates = e.Value;
|
---|
133 | if (!suppressUpdates) UpdateRowAttributes();
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | private IEnumerable<int> GetIndexOfRun(IRun run) {
|
---|
138 | int i = 0;
|
---|
139 | foreach (IRun actualRun in Content) {
|
---|
140 | if (actualRun == run)
|
---|
141 | yield return i;
|
---|
142 | i++;
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | private void dataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
147 | if (e.RowIndex >= 0) {
|
---|
148 | IRun run = Content.ElementAt(runToRowMapping.ToList().IndexOf(e.RowIndex));
|
---|
149 | IContentView view = MainFormManager.MainForm.ShowContent(run);
|
---|
150 | if (view != null) {
|
---|
151 | view.ReadOnly = this.ReadOnly;
|
---|
152 | view.Locked = this.Locked;
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | protected override void ClearSorting() {
|
---|
158 | base.ClearSorting();
|
---|
159 | runToRowMapping = Enumerable.Range(0, Content.Count).ToArray();
|
---|
160 | UpdateRowAttributes();
|
---|
161 | }
|
---|
162 |
|
---|
163 | protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
|
---|
164 | int[] newSortedIndex = Enumerable.Range(0, Content.Count).ToArray();
|
---|
165 | RunCollectionRowComparer rowComparer = new RunCollectionRowComparer();
|
---|
166 | if (sortedColumns.Count() != 0) {
|
---|
167 | rowComparer.SortedIndizes = sortedColumns;
|
---|
168 | rowComparer.Matrix = Content;
|
---|
169 | Array.Sort(newSortedIndex, rowComparer);
|
---|
170 | }
|
---|
171 |
|
---|
172 | runToRowMapping = new int[newSortedIndex.Length];
|
---|
173 | int i = 0;
|
---|
174 | foreach (int runIndex in newSortedIndex) {
|
---|
175 | runToRowMapping[runIndex] = i;
|
---|
176 | i++;
|
---|
177 | }
|
---|
178 | UpdateRowAttributes();
|
---|
179 | return newSortedIndex;
|
---|
180 | }
|
---|
181 |
|
---|
182 | private void UpdateRowAttributes() {
|
---|
183 | int runIndex = 0;
|
---|
184 | foreach (IRun run in Content) {
|
---|
185 | int rowIndex = this.runToRowMapping[runIndex];
|
---|
186 | this.dataGridView.Rows[rowIndex].Visible = run.Visible;
|
---|
187 | this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
|
---|
188 | runIndex++;
|
---|
189 | }
|
---|
190 | UpdateRowHeaders();
|
---|
191 | }
|
---|
192 |
|
---|
193 | public class RunCollectionRowComparer : IComparer<int> {
|
---|
194 | public RunCollectionRowComparer() {
|
---|
195 | }
|
---|
196 |
|
---|
197 | private List<KeyValuePair<int, SortOrder>> sortedIndizes;
|
---|
198 | public IEnumerable<KeyValuePair<int, SortOrder>> SortedIndizes {
|
---|
199 | get { return this.sortedIndizes; }
|
---|
200 | set { sortedIndizes = new List<KeyValuePair<int, SortOrder>>(value); }
|
---|
201 | }
|
---|
202 | private RunCollection matrix;
|
---|
203 | public RunCollection Matrix {
|
---|
204 | get { return this.matrix; }
|
---|
205 | set { this.matrix = value; }
|
---|
206 | }
|
---|
207 |
|
---|
208 | public int Compare(int x, int y) {
|
---|
209 | int result = 0;
|
---|
210 | IItem value1, value2;
|
---|
211 | IComparable comparable1, comparable2;
|
---|
212 |
|
---|
213 | if (matrix == null)
|
---|
214 | throw new InvalidOperationException("Could not sort IStringConvertibleMatrix if the matrix member is null.");
|
---|
215 | if (sortedIndizes == null)
|
---|
216 | return 0;
|
---|
217 |
|
---|
218 | foreach (KeyValuePair<int, SortOrder> pair in sortedIndizes.Where(p => p.Value != SortOrder.None)) {
|
---|
219 | value1 = matrix.GetValue(x, pair.Key);
|
---|
220 | value2 = matrix.GetValue(y, pair.Key);
|
---|
221 | comparable1 = value1 as IComparable;
|
---|
222 | comparable2 = value2 as IComparable;
|
---|
223 | if (comparable1 != null)
|
---|
224 | result = comparable1.CompareTo(comparable2);
|
---|
225 | else {
|
---|
226 | string string1 = value1 != null ? value1.ToString() : string.Empty;
|
---|
227 | string string2 = value2 != null ? value2.ToString() : string.Empty;
|
---|
228 | result = string1.CompareTo(string2);
|
---|
229 | }
|
---|
230 | if (pair.Value == SortOrder.Descending)
|
---|
231 | result *= -1;
|
---|
232 | if (result != 0)
|
---|
233 | return result;
|
---|
234 | }
|
---|
235 | return result;
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|
239 | }
|
---|