Free cookie consent management tool by TermsFeed Policy Generator

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

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

corrected tabular view and bubble chart view (ticket #970)

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