Free cookie consent management tool by TermsFeed Policy Generator

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

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

Adjusted the SetEnabledStateOfControls method in all views, added the Enabled property into the IView interface and adapted the ViewHost, View and ContentView class (ticket #1155).

File size: 7.7 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 sealed partial class RunCollectionTabularView : StringConvertibleMatrixView {
34    private int[] runToRowMapping;
35    public RunCollectionTabularView() {
36      InitializeComponent();
37    }
38
39    public override bool ReadOnly {
40      get { return true; }
41      set { /*not needed because results are always readonly */}
42    }
43
44    public new RunCollection Content {
45      get { return (RunCollection)base.Content; }
46      set { base.Content = value; }
47    }
48
49    protected override void OnContentChanged() {
50      base.OnContentChanged();
51      if (Content != null) {
52        runToRowMapping = Enumerable.Range(0, Content.Count).ToArray();
53        UpdateRowAttributes();
54      }
55    }
56
57    #region events
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    private 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    private 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      OnContentChanged();
84    }
85    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
86      DeregisterRunEvents(e.Items);
87    }
88    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
89      RegisterRunEvents(e.Items);
90    }
91    private void run_Changed(object sender, EventArgs e) {
92      if (InvokeRequired)
93        this.Invoke(new EventHandler(run_Changed), sender, e);
94      else {
95        IRun run = (IRun)sender;
96        UpdateRun(run);
97      }
98    }
99    #endregion
100
101    private void UpdateRun(IRun run) {
102      int runIndex = GetIndexOfRun(run);
103      int rowIndex = runToRowMapping[runIndex];
104      this.dataGridView.Rows[rowIndex].Visible = run.Visible;
105      this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
106      this.UpdateRowHeaders();
107    }
108
109    private int GetIndexOfRun(IRun run) {
110      int i = 0;
111      foreach (IRun actualRun in Content) {
112        if (actualRun == run)
113          return i;
114        i++;
115      }
116      throw new ArgumentException("Run " + run.Name + "could not be found in the RunCollection.");
117    }
118
119    private void dataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
120      if (e.RowIndex >= 0) {
121        IRun run = Content.ElementAt(runToRowMapping.ToList().IndexOf(e.RowIndex));
122        IContentView view = MainFormManager.MainForm.ShowContent(run);
123        if (view != null) {
124          view.ReadOnly = this.ReadOnly;
125          view.Locked = this.Locked;
126        }
127      }
128    }
129
130    protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
131      int[] newSortedIndex = Enumerable.Range(0, Content.Count).ToArray();
132      RunCollectionRowComparer rowComparer = new RunCollectionRowComparer();
133      if (sortedColumns.Count() != 0) {
134        rowComparer.SortedIndizes = sortedColumns;
135        rowComparer.Matrix = Content;
136        Array.Sort(newSortedIndex, rowComparer);
137      }
138
139      runToRowMapping = new int[newSortedIndex.Length];
140      int i = 0;
141      foreach (int runIndex in newSortedIndex) {
142        runToRowMapping[runIndex] = i;
143        i++;
144      }
145      UpdateRowAttributes();
146      return newSortedIndex;
147    }
148
149    private void UpdateRowAttributes() {
150      int runIndex = 0;
151      foreach (IRun run in Content) {
152        int rowIndex = this.runToRowMapping[runIndex];
153        this.dataGridView.Rows[rowIndex].Visible = run.Visible;
154        this.dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = run.Color;
155        runIndex++;
156      }
157    }
158
159    public class RunCollectionRowComparer : IComparer<int> {
160      public RunCollectionRowComparer() {
161      }
162
163      private List<KeyValuePair<int, SortOrder>> sortedIndizes;
164      public IEnumerable<KeyValuePair<int, SortOrder>> SortedIndizes {
165        get { return this.sortedIndizes; }
166        set { sortedIndizes = new List<KeyValuePair<int, SortOrder>>(value); }
167      }
168      private RunCollection matrix;
169      public RunCollection Matrix {
170        get { return this.matrix; }
171        set { this.matrix = value; }
172      }
173
174      public int Compare(int x, int y) {
175        int result = 0;
176        IItem value1, value2;
177        IComparable comparable1, comparable2;
178
179        if (matrix == null)
180          throw new InvalidOperationException("Could not sort IStringConvertibleMatrix if the matrix member is null.");
181        if (sortedIndizes == null)
182          return 0;
183
184        foreach (KeyValuePair<int, SortOrder> pair in sortedIndizes.Where(p => p.Value != SortOrder.None)) {
185          value1 = matrix.GetValue(x, pair.Key);
186          value2 = matrix.GetValue(y, pair.Key);
187          comparable1 = value1 as IComparable;
188          comparable2 = value2 as IComparable;
189          if (comparable1 != null)
190            result = comparable1.CompareTo(comparable2);
191          else {
192            string string1 = value1 != null ? value1.ToString() : string.Empty;
193            string string2 = value2 != null ? value2.ToString() : string.Empty;
194            result = string1.CompareTo(string2);
195          }
196          if (pair.Value == SortOrder.Descending)
197            result *= -1;
198          if (result != 0)
199            return result;
200        }
201        return result;
202      }
203    }
204  }
205}
Note: See TracBrowser for help on using the repository browser.