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