Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4102 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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