Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3623 was 3614, checked in by mkommend, 15 years ago

implemented first version of RunConstraints (ticket #970)

File size: 6.9 KB
RevLine 
[3332]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.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.MainForm;
31using HeuristicLab.Data.Views;
32using HeuristicLab.Collections;
[3447]33using HeuristicLab.Core;
[3332]34
35namespace HeuristicLab.Optimization.Views {
[3347]36  [View("RunCollection Tabular View")]
[3332]37  [Content(typeof(RunCollection), false)]
38  public partial class RunCollectionTabularView : StringConvertibleMatrixView {
39    public RunCollectionTabularView() {
40      InitializeComponent();
41      Caption = "Run Collection";
42      this.dataGridView.RowHeaderMouseDoubleClick += new DataGridViewCellMouseEventHandler(dataGridView_RowHeaderMouseDoubleClick);
[3350]43      base.ReadOnly = true;
[3332]44    }
45
[3350]46    public override bool ReadOnly {
47      get { return base.ReadOnly; }
48      set { /*not needed because results are always readonly */}
49    }
50
[3332]51    public new RunCollection Content {
[3423]52      get { return (RunCollection)base.Content; }
[3332]53      set { base.Content = value; }
54    }
55
[3614]56    protected override void OnContentChanged() {
57      base.OnContentChanged();
58      if (Content != null) {
59        foreach (IRun run in Content)
60          UpdateRun(run);
61      }
62    }
63
[3448]64    protected override void RegisterContentEvents() {
65      base.RegisterContentEvents();
66      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
67      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
68      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
69      RegisterRunEvents(Content);
70    }
71    protected virtual void RegisterRunEvents(IEnumerable<IRun> runs) {
72      foreach (IRun run in runs)
73        run.Changed += new EventHandler(run_Changed);
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      DeregisterRunEvents(Content);
81    }
82    protected virtual void DeregisterRunEvents(IEnumerable<IRun> runs) {
83      foreach (IRun run in runs)
84        run.Changed -= new EventHandler(run_Changed);
85    }
86    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
87      DeregisterRunEvents(e.OldItems);
88      RegisterRunEvents(e.Items);
89    }
90    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
[3449]91      DeregisterRunEvents(e.Items);
[3448]92    }
93    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
94      RegisterRunEvents(e.Items);
95    }
96    private void run_Changed(object sender, EventArgs e) {
97      IRun run = (IRun)sender;
[3614]98      UpdateRun(run);
99    }
100
101    private void UpdateRun(IRun run) {
[3448]102      int rowIndex = Content.ToList().IndexOf(run);
103      rowIndex = virtualRowIndizes[rowIndex];
104      this.dataGridView.Rows[rowIndex].Visible = run.Visible;
105      this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
106    }
107
[3332]108    private void dataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
[3546]109      if (e.RowIndex >= 0) {
110        IRun run = Content.ElementAt(virtualRowIndizes[e.RowIndex]);
[3557]111        IContentView view = MainFormManager.MainForm.ShowContent(run);
[3423]112        if (view != null) {
113          view.ReadOnly = this.ReadOnly;
114          view.Locked = this.Locked;
115        }
116      }
[3332]117    }
[3447]118
119    protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
120      int[] newSortedIndex = Enumerable.Range(0, Content.Count).ToArray();
121      RunCollectionRowComparer rowComparer = new RunCollectionRowComparer();
122      if (sortedColumns.Count() != 0) {
123        rowComparer.SortedIndizes = sortedColumns;
124        rowComparer.Matrix = Content;
125        Array.Sort(newSortedIndex, rowComparer);
126      }
127      return newSortedIndex;
128    }
129
130    public class RunCollectionRowComparer : IComparer<int> {
131      public RunCollectionRowComparer() {
132      }
133
134      private List<KeyValuePair<int, SortOrder>> sortedIndizes;
135      public IEnumerable<KeyValuePair<int, SortOrder>> SortedIndizes {
136        get { return this.sortedIndizes; }
137        set { sortedIndizes = new List<KeyValuePair<int, SortOrder>>(value); }
138      }
139      private RunCollection matrix;
140      public RunCollection Matrix {
141        get { return this.matrix; }
142        set { this.matrix = value; }
143      }
144
145      public int Compare(int x, int y) {
146        int result = 0;
147        IItem value1, value2;
148        IComparable comparable1, comparable2;
149
150        if (matrix == null)
151          throw new InvalidOperationException("Could not sort IStringConvertibleMatrix if the matrix member is null.");
152        if (sortedIndizes == null)
153          return 0;
154
155        foreach (KeyValuePair<int, SortOrder> pair in sortedIndizes.Where(p => p.Value != SortOrder.None)) {
156          value1 = matrix.GetValue(x, pair.Key);
157          value2 = matrix.GetValue(y, pair.Key);
158          comparable1 = value1 as IComparable;
159          comparable2 = value2 as IComparable;
160          if (comparable1 != null)
161            result = comparable1.CompareTo(comparable2);
162          else {
163            string string1 = value1 != null ? value1.ToString() : string.Empty;
164            string string2 = value2 != null ? value2.ToString() : string.Empty;
165            result = string1.CompareTo(string2);
166          }
167          if (pair.Value == SortOrder.Descending)
168            result *= -1;
169          if (result != 0)
170            return result;
171        }
172        return result;
173      }
174    }
[3332]175  }
176}
Note: See TracBrowser for help on using the repository browser.