Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3632 was 3632, checked in by mkommend, 14 years ago

corrected RunCollectionConstraints and the according views (ticket#970)

File size: 7.1 KB
Line 
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;
33using HeuristicLab.Core;
34
35namespace HeuristicLab.Optimization.Views {
36  [View("RunCollection Tabular View")]
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);
43      base.ReadOnly = true;
44    }
45
46    public override bool ReadOnly {
47      get { return base.ReadOnly; }
48      set { /*not needed because results are always readonly */}
49    }
50
51    public new RunCollection Content {
52      get { return (RunCollection)base.Content; }
53      set { base.Content = value; }
54    }
55
56    protected override void OnContentChanged() {
57      base.OnContentChanged();
58      if (Content != null) {
59        foreach (IRun run in Content)
60          UpdateRun(run);
61      }
62    }
63
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) {
91      DeregisterRunEvents(e.Items);
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      if (InvokeRequired)
98        this.Invoke(new EventHandler(run_Changed), sender, e);
99      else {
100        IRun run = (IRun)sender;
101        UpdateRun(run);
102      }
103    }
104
105    private void UpdateRun(IRun run) {
106      int rowIndex = Content.ToList().IndexOf(run);
107      rowIndex = virtualRowIndizes[rowIndex];
108      this.dataGridView.Rows[rowIndex].Visible = run.Visible;
109      this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
110      this.rowsTextBox.Text = this.Content.Count(r => r.Visible).ToString();
111    }
112
113    private void dataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
114      if (e.RowIndex >= 0) {
115        IRun run = Content.ElementAt(virtualRowIndizes[e.RowIndex]);
116        IContentView view = MainFormManager.MainForm.ShowContent(run);
117        if (view != null) {
118          view.ReadOnly = this.ReadOnly;
119          view.Locked = this.Locked;
120        }
121      }
122    }
123
124    protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
125      int[] newSortedIndex = Enumerable.Range(0, Content.Count).ToArray();
126      RunCollectionRowComparer rowComparer = new RunCollectionRowComparer();
127      if (sortedColumns.Count() != 0) {
128        rowComparer.SortedIndizes = sortedColumns;
129        rowComparer.Matrix = Content;
130        Array.Sort(newSortedIndex, rowComparer);
131      }
132      return newSortedIndex;
133    }
134
135    public class RunCollectionRowComparer : IComparer<int> {
136      public RunCollectionRowComparer() {
137      }
138
139      private List<KeyValuePair<int, SortOrder>> sortedIndizes;
140      public IEnumerable<KeyValuePair<int, SortOrder>> SortedIndizes {
141        get { return this.sortedIndizes; }
142        set { sortedIndizes = new List<KeyValuePair<int, SortOrder>>(value); }
143      }
144      private RunCollection matrix;
145      public RunCollection Matrix {
146        get { return this.matrix; }
147        set { this.matrix = value; }
148      }
149
150      public int Compare(int x, int y) {
151        int result = 0;
152        IItem value1, value2;
153        IComparable comparable1, comparable2;
154
155        if (matrix == null)
156          throw new InvalidOperationException("Could not sort IStringConvertibleMatrix if the matrix member is null.");
157        if (sortedIndizes == null)
158          return 0;
159
160        foreach (KeyValuePair<int, SortOrder> pair in sortedIndizes.Where(p => p.Value != SortOrder.None)) {
161          value1 = matrix.GetValue(x, pair.Key);
162          value2 = matrix.GetValue(y, pair.Key);
163          comparable1 = value1 as IComparable;
164          comparable2 = value2 as IComparable;
165          if (comparable1 != null)
166            result = comparable1.CompareTo(comparable2);
167          else {
168            string string1 = value1 != null ? value1.ToString() : string.Empty;
169            string string2 = value2 != null ? value2.ToString() : string.Empty;
170            result = string1.CompareTo(string2);
171          }
172          if (pair.Value == SortOrder.Descending)
173            result *= -1;
174          if (result != 0)
175            return result;
176        }
177        return result;
178      }
179    }
180  }
181}
Note: See TracBrowser for help on using the repository browser.