Free cookie consent management tool by TermsFeed Policy Generator

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

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

adapted view captions (ticket #893)

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