1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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.ComponentModel;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Windows.Forms;
|
---|
27 | using HeuristicLab.Collections;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data.Views;
|
---|
30 | using HeuristicLab.MainForm;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Optimization.Views {
|
---|
33 | [View("Table")]
|
---|
34 | [Content(typeof(RunCollection), false)]
|
---|
35 | public sealed partial class RunCollectionTableView : StringConvertibleMatrixView {
|
---|
36 | private int[] runToRowMapping;
|
---|
37 | private bool suppressUpdates = false;
|
---|
38 | public RunCollectionTableView() {
|
---|
39 | InitializeComponent();
|
---|
40 | dataGridView.RowHeaderMouseDoubleClick += new DataGridViewCellMouseEventHandler(dataGridView_RowHeaderMouseDoubleClick);
|
---|
41 | }
|
---|
42 |
|
---|
43 | public override bool ReadOnly {
|
---|
44 | get { return true; }
|
---|
45 | set { /*not needed because results are always readonly */}
|
---|
46 | }
|
---|
47 |
|
---|
48 | public new RunCollection Content {
|
---|
49 | get { return (RunCollection)base.Content; }
|
---|
50 | set { base.Content = value; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | protected override void OnContentChanged() {
|
---|
54 | base.OnContentChanged();
|
---|
55 | if (Content != null) {
|
---|
56 | UpdateRowAttributes();
|
---|
57 | }
|
---|
58 | UpdateCaption();
|
---|
59 | }
|
---|
60 |
|
---|
61 | #region events
|
---|
62 | protected override void RegisterContentEvents() {
|
---|
63 | base.RegisterContentEvents();
|
---|
64 | Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
|
---|
65 | Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
|
---|
66 | Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
|
---|
67 | Content.UpdateOfRunsInProgressChanged += new EventHandler(Content_UpdateOfRunsInProgressChanged);
|
---|
68 | Content.OptimizerNameChanged += new EventHandler(Content_AlgorithmNameChanged);
|
---|
69 | RegisterRunEvents(Content);
|
---|
70 | }
|
---|
71 | private void RegisterRunEvents(IEnumerable<IRun> runs) {
|
---|
72 | foreach (IRun run in runs)
|
---|
73 | run.PropertyChanged += run_PropertyChanged;
|
---|
74 | }
|
---|
75 | protected override void DeregisterContentEvents() {
|
---|
76 | base.DeregisterContentEvents();
|
---|
77 | Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
|
---|
78 | Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
|
---|
79 | Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
|
---|
80 | Content.UpdateOfRunsInProgressChanged -= new EventHandler(Content_UpdateOfRunsInProgressChanged);
|
---|
81 | Content.OptimizerNameChanged -= new EventHandler(Content_AlgorithmNameChanged);
|
---|
82 | DeregisterRunEvents(Content);
|
---|
83 | }
|
---|
84 | private void DeregisterRunEvents(IEnumerable<IRun> runs) {
|
---|
85 | foreach (IRun run in runs)
|
---|
86 | run.PropertyChanged -= run_PropertyChanged;
|
---|
87 | }
|
---|
88 | private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
89 | DeregisterRunEvents(e.OldItems);
|
---|
90 | RegisterRunEvents(e.Items);
|
---|
91 | }
|
---|
92 | private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
93 | DeregisterRunEvents(e.Items);
|
---|
94 | }
|
---|
95 | private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
96 | RegisterRunEvents(e.Items);
|
---|
97 | }
|
---|
98 | private void Content_AlgorithmNameChanged(object sender, EventArgs e) {
|
---|
99 | if (InvokeRequired)
|
---|
100 | Invoke(new EventHandler(Content_AlgorithmNameChanged), sender, e);
|
---|
101 | else UpdateCaption();
|
---|
102 | }
|
---|
103 | private void run_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
104 | if (suppressUpdates) return;
|
---|
105 | if (InvokeRequired)
|
---|
106 | this.Invoke((Action<object, PropertyChangedEventArgs>)run_PropertyChanged, sender, e);
|
---|
107 | else {
|
---|
108 | IRun run = (IRun)sender;
|
---|
109 | if (e.PropertyName == "Color" || e.PropertyName == "Visible")
|
---|
110 | UpdateRun(run);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | #endregion
|
---|
114 |
|
---|
115 | private void UpdateCaption() {
|
---|
116 | Caption = Content != null ? Content.OptimizerName + " Table" : ViewAttribute.GetViewName(GetType());
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected override void UpdateData() {
|
---|
120 | if (suppressUpdates) return;
|
---|
121 | base.UpdateData();
|
---|
122 | }
|
---|
123 |
|
---|
124 | protected override void UpdateColumnHeaders() {
|
---|
125 | for (int i = 0; i < dataGridView.ColumnCount; i++) {
|
---|
126 | if (i < base.Content.ColumnNames.Count())
|
---|
127 | dataGridView.Columns[i].HeaderText = base.Content.ColumnNames.ElementAt(i);
|
---|
128 | else
|
---|
129 | dataGridView.Columns[i].HeaderText = "Column " + (i + 1);
|
---|
130 | }
|
---|
131 |
|
---|
132 | HashSet<string> visibleColumnNames = new HashSet<string>(dataGridView.Columns.OfType<DataGridViewColumn>()
|
---|
133 | .Where(c => c.Visible && !string.IsNullOrEmpty(c.HeaderText) && GetNumberOfDistinctValues(c.HeaderText) > 1).Select(c => c.HeaderText));
|
---|
134 |
|
---|
135 | for (int i = 0; i < dataGridView.ColumnCount; i++) {
|
---|
136 | dataGridView.Columns[i].Visible = visibleColumnNames.Count == 0 || visibleColumnNames.Contains(dataGridView.Columns[i].HeaderText);
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | // returns the number of different values for the parameter or result in the RunCollection
|
---|
141 | private int GetNumberOfDistinctValues(string columnName) {
|
---|
142 | Func<IRun, string, string> GetStringValue = (IRun r, string colName) => {
|
---|
143 | // also include missing values in the count
|
---|
144 | if (r.Parameters.ContainsKey(colName)) return r.Parameters[colName].ToString();
|
---|
145 | if (r.Results.ContainsKey(colName)) return r.Results[colName].ToString();
|
---|
146 | return string.Empty;
|
---|
147 | };
|
---|
148 | return Content.Select(r => GetStringValue(r, columnName)).Distinct().Count();
|
---|
149 | }
|
---|
150 |
|
---|
151 | private void UpdateRun(IRun run) {
|
---|
152 | foreach (int runIndex in GetIndexOfRun(run)) {
|
---|
153 | int rowIndex = runToRowMapping[runIndex];
|
---|
154 | this.dataGridView.Rows[rowIndex].Visible = run.Visible;
|
---|
155 | this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
|
---|
156 | }
|
---|
157 | this.UpdateRowHeaders();
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
|
---|
162 | if (InvokeRequired)
|
---|
163 | Invoke(new EventHandler(Content_UpdateOfRunsInProgressChanged), sender, e);
|
---|
164 | else {
|
---|
165 | suppressUpdates = Content.UpdateOfRunsInProgress;
|
---|
166 | if (!suppressUpdates) {
|
---|
167 | UpdateData();
|
---|
168 | UpdateRowAttributes();
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | private IEnumerable<int> GetIndexOfRun(IRun run) {
|
---|
174 | int i = 0;
|
---|
175 | foreach (IRun actualRun in Content) {
|
---|
176 | if (actualRun == run)
|
---|
177 | yield return i;
|
---|
178 | i++;
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | private void dataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
183 | if (e.RowIndex >= 0) {
|
---|
184 | IRun run = Content.ElementAt(runToRowMapping.ToList().IndexOf(e.RowIndex));
|
---|
185 | IContentView view = MainFormManager.MainForm.ShowContent(run);
|
---|
186 | if (view != null) {
|
---|
187 | view.ReadOnly = this.ReadOnly;
|
---|
188 | view.Locked = this.Locked;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | protected override void ClearSorting() {
|
---|
194 | base.ClearSorting();
|
---|
195 | UpdateRowAttributes();
|
---|
196 | }
|
---|
197 |
|
---|
198 | protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
|
---|
199 | int[] newSortedIndex = Enumerable.Range(0, Content.Count).ToArray();
|
---|
200 | RunCollectionRowComparer rowComparer = new RunCollectionRowComparer();
|
---|
201 | if (sortedColumns.Count() != 0) {
|
---|
202 | rowComparer.SortedIndices = sortedColumns;
|
---|
203 | rowComparer.Matrix = Content;
|
---|
204 | Array.Sort(newSortedIndex, rowComparer);
|
---|
205 | }
|
---|
206 |
|
---|
207 | runToRowMapping = new int[newSortedIndex.Length];
|
---|
208 | int i = 0;
|
---|
209 | foreach (int runIndex in newSortedIndex) {
|
---|
210 | runToRowMapping[runIndex] = i;
|
---|
211 | i++;
|
---|
212 | }
|
---|
213 | UpdateRowAttributes(rebuild: false);
|
---|
214 | return newSortedIndex;
|
---|
215 | }
|
---|
216 |
|
---|
217 | private void UpdateRowAttributes(bool rebuild = true) {
|
---|
218 | if (rebuild) runToRowMapping = Enumerable.Range(0, Content.Count).ToArray();
|
---|
219 | int runIndex = 0;
|
---|
220 | foreach (IRun run in Content) {
|
---|
221 | int rowIndex = this.runToRowMapping[runIndex];
|
---|
222 | this.dataGridView.Rows[rowIndex].Visible = run.Visible;
|
---|
223 | this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
|
---|
224 | runIndex++;
|
---|
225 | }
|
---|
226 | rowsTextBox.Text = dataGridView.Rows.GetRowCount(DataGridViewElementStates.Visible).ToString();
|
---|
227 | UpdateRowHeaders();
|
---|
228 | }
|
---|
229 |
|
---|
230 | public class RunCollectionRowComparer : IComparer<int> {
|
---|
231 | public RunCollectionRowComparer() {
|
---|
232 | }
|
---|
233 |
|
---|
234 | private List<KeyValuePair<int, SortOrder>> sortedIndices;
|
---|
235 | public IEnumerable<KeyValuePair<int, SortOrder>> SortedIndices {
|
---|
236 | get { return this.sortedIndices; }
|
---|
237 | set { sortedIndices = new List<KeyValuePair<int, SortOrder>>(value); }
|
---|
238 | }
|
---|
239 | private RunCollection matrix;
|
---|
240 | public RunCollection Matrix {
|
---|
241 | get { return this.matrix; }
|
---|
242 | set { this.matrix = value; }
|
---|
243 | }
|
---|
244 |
|
---|
245 | public int Compare(int x, int y) {
|
---|
246 | int result = 0;
|
---|
247 | IItem value1, value2;
|
---|
248 | IComparable comparable1, comparable2;
|
---|
249 |
|
---|
250 | if (matrix == null)
|
---|
251 | throw new InvalidOperationException("Could not sort IStringConvertibleMatrix if the matrix member is null.");
|
---|
252 | if (sortedIndices == null)
|
---|
253 | return 0;
|
---|
254 |
|
---|
255 | foreach (KeyValuePair<int, SortOrder> pair in sortedIndices.Where(p => p.Value != SortOrder.None)) {
|
---|
256 | value1 = matrix.GetValue(x, pair.Key);
|
---|
257 | value2 = matrix.GetValue(y, pair.Key);
|
---|
258 | comparable1 = value1 as IComparable;
|
---|
259 | comparable2 = value2 as IComparable;
|
---|
260 | if (comparable1 != null)
|
---|
261 | result = comparable1.CompareTo(comparable2);
|
---|
262 | else {
|
---|
263 | string string1 = value1 != null ? value1.ToString() : string.Empty;
|
---|
264 | string string2 = value2 != null ? value2.ToString() : string.Empty;
|
---|
265 | result = string1.CompareTo(string2);
|
---|
266 | }
|
---|
267 | if (pair.Value == SortOrder.Descending)
|
---|
268 | result *= -1;
|
---|
269 | if (result != 0)
|
---|
270 | return result;
|
---|
271 | }
|
---|
272 | return result;
|
---|
273 | }
|
---|
274 | }
|
---|
275 | }
|
---|
276 | }
|
---|