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