Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data.Views/3.3/StringConvertibleMatrixView.cs @ 3448

Last change on this file since 3448 was 3448, checked in by mkommend, 15 years ago

added reaction of color & visibility of IRuns in RunCollectionTabularView (ticket #970)

File size: 15.1 KB
RevLine 
[2669]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2669]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.ComponentModel;
[3314]24using System.Collections.Generic;
[2669]25using System.Drawing;
[3312]26using System.Linq;
[2669]27using System.Windows.Forms;
28using HeuristicLab.Common;
29using HeuristicLab.MainForm;
[2713]30using HeuristicLab.MainForm.WindowsForms;
[2669]31
32namespace HeuristicLab.Data.Views {
[3048]33  [View("StringConvertibleMatrix View")]
34  [Content(typeof(IStringConvertibleMatrix), true)]
[3228]35  public partial class StringConvertibleMatrixView : AsynchronousContentView {
[3448]36    protected int[] virtualRowIndizes;
[3430]37    private List<KeyValuePair<int, SortOrder>> sortedColumnIndizes;
38    private RowComparer rowComparer;
39
[3048]40    public new IStringConvertibleMatrix Content {
41      get { return (IStringConvertibleMatrix)base.Content; }
[2713]42      set { base.Content = value; }
[2669]43    }
44
[3430]45    public override bool ReadOnly {
46      get {
47        if ((Content != null) && Content.ReadOnly) return true;
48        return base.ReadOnly;
49      }
50      set { base.ReadOnly = value; }
51    }
[3314]52
[3048]53    public StringConvertibleMatrixView() {
[2669]54      InitializeComponent();
[3048]55      Caption = "StringConvertibleMatrix View";
[2677]56      errorProvider.SetIconAlignment(rowsTextBox, ErrorIconAlignment.MiddleLeft);
57      errorProvider.SetIconPadding(rowsTextBox, 2);
58      errorProvider.SetIconAlignment(columnsTextBox, ErrorIconAlignment.MiddleLeft);
59      errorProvider.SetIconPadding(columnsTextBox, 2);
[3314]60      sortedColumnIndizes = new List<KeyValuePair<int, SortOrder>>();
61      rowComparer = new RowComparer();
[2669]62    }
[3048]63    public StringConvertibleMatrixView(IStringConvertibleMatrix content)
[2669]64      : this() {
[2727]65      Content = content;
[2669]66    }
67
[2713]68    protected override void DeregisterContentEvents() {
69      Content.ItemChanged -= new EventHandler<EventArgs<int, int>>(Content_ItemChanged);
70      Content.Reset -= new EventHandler(Content_Reset);
[3320]71      Content.ColumnNamesChanged -= new EventHandler(Content_ColumnNamesChanged);
72      Content.RowNamesChanged -= new EventHandler(Content_RowNamesChanged);
[2713]73      base.DeregisterContentEvents();
[2669]74    }
[2713]75    protected override void RegisterContentEvents() {
76      base.RegisterContentEvents();
77      Content.ItemChanged += new EventHandler<EventArgs<int, int>>(Content_ItemChanged);
78      Content.Reset += new EventHandler(Content_Reset);
[3320]79      Content.ColumnNamesChanged += new EventHandler(Content_ColumnNamesChanged);
80      Content.RowNamesChanged += new EventHandler(Content_RowNamesChanged);
[2669]81    }
82
[2713]83    protected override void OnContentChanged() {
84      base.OnContentChanged();
85      if (Content == null) {
[3048]86        Caption = "StringConvertibleMatrix View";
[2677]87        rowsTextBox.Text = "";
88        columnsTextBox.Text = "";
[2669]89        dataGridView.Rows.Clear();
[2677]90        dataGridView.Columns.Clear();
[3324]91        virtualRowIndizes = new int[0];
[2669]92      } else {
[3048]93        Caption = "StringConvertibleMatrix (" + Content.GetType().Name + ")";
[2713]94        UpdateData();
[2669]95      }
[3362]96      SetEnabledStateOfControls();
[2669]97    }
[3350]98    protected override void OnReadOnlyChanged() {
99      base.OnReadOnlyChanged();
[3362]100      SetEnabledStateOfControls();
[3350]101    }
[3362]102    private void SetEnabledStateOfControls() {
[3350]103      if (Content == null) {
104        rowsTextBox.Enabled = false;
105        columnsTextBox.Enabled = false;
106        dataGridView.Enabled = false;
107      } else {
108        rowsTextBox.Enabled = true;
109        columnsTextBox.Enabled = true;
110        dataGridView.Enabled = true;
111        rowsTextBox.ReadOnly = ReadOnly;
112        columnsTextBox.ReadOnly = ReadOnly;
113        dataGridView.ReadOnly = ReadOnly;
114      }
115    }
[2669]116
[2713]117    private void UpdateData() {
[3328]118      sortedColumnIndizes.Clear();
119      Sort();
[2713]120      rowsTextBox.Text = Content.Rows.ToString();
[2973]121      rowsTextBox.Enabled = true;
[2713]122      columnsTextBox.Text = Content.Columns.ToString();
[2973]123      columnsTextBox.Enabled = true;
[3324]124      virtualRowIndizes = Enumerable.Range(0, Content.Rows).ToArray();
[3335]125      //DataGridViews with Rows but no columns are not allowed !
[3431]126      if (Content.Rows == 0 && dataGridView.RowCount != Content.Rows && !Content.ReadOnly)
[3335]127        Content.Rows = dataGridView.RowCount;
128      else
129        dataGridView.RowCount = Content.Rows;
[3431]130      if (Content.Columns == 0 && dataGridView.ColumnCount != Content.Columns && !Content.ReadOnly)
[3335]131        Content.Columns = dataGridView.ColumnCount;
132      else
133        dataGridView.ColumnCount = Content.Columns;
[3346]134
[3328]135      UpdateRowHeaders();
136      UpdateColumnHeaders();
[3346]137      dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
138      dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
[2669]139      dataGridView.Enabled = true;
140    }
141
[3312]142    private void UpdateColumnHeaders() {
143      for (int i = 0; i < Content.Columns; i++) {
144        if (Content.ColumnNames.Count() != 0)
145          dataGridView.Columns[i].HeaderText = Content.ColumnNames.ElementAt(i);
146        else
[3346]147          dataGridView.Columns[i].HeaderText = "Column " + (i + 1);
[3312]148      }
149      dataGridView.Invalidate();
150    }
151
152    private void UpdateRowHeaders() {
153      for (int i = dataGridView.FirstDisplayedScrollingRowIndex; i < dataGridView.FirstDisplayedScrollingRowIndex + dataGridView.DisplayedRowCount(true); i++) {
154        if (Content.RowNames.Count() != 0)
155          dataGridView.Rows[i].HeaderCell.Value = Content.RowNames.ElementAt(i);
[3314]156        else
[3346]157          dataGridView.Rows[i].HeaderCell.Value = "Row " + (i + 1);
[3312]158      }
159      dataGridView.Invalidate();
160    }
161
[3320]162    private void Content_RowNamesChanged(object sender, EventArgs e) {
[3328]163      if (InvokeRequired)
164        Invoke(new EventHandler(Content_RowNamesChanged), sender, e);
165      else
166        UpdateRowHeaders();
[3320]167    }
168    private void Content_ColumnNamesChanged(object sender, EventArgs e) {
[3328]169      if (InvokeRequired)
170        Invoke(new EventHandler(Content_ColumnNamesChanged), sender, e);
171      else
172        UpdateColumnHeaders();
[3320]173    }
[2713]174    private void Content_ItemChanged(object sender, EventArgs<int, int> e) {
[2669]175      if (InvokeRequired)
[2713]176        Invoke(new EventHandler<EventArgs<int, int>>(Content_ItemChanged), sender, e);
[3328]177      else
[3335]178        dataGridView.InvalidateCell(e.Value2, e.Value);
[2669]179    }
[2713]180    private void Content_Reset(object sender, EventArgs e) {
[2669]181      if (InvokeRequired)
[2713]182        Invoke(new EventHandler(Content_Reset), sender, e);
[2669]183      else
[2713]184        UpdateData();
[2669]185    }
186
[2677]187    #region TextBox Events
188    private void rowsTextBox_Validating(object sender, CancelEventArgs e) {
[2669]189      int i = 0;
[3335]190      if (!int.TryParse(rowsTextBox.Text, out i) || (i <= 0)) {
[2676]191        e.Cancel = true;
[3335]192        errorProvider.SetError(rowsTextBox, "Invalid Number of Rows (Valid values are positive integers larger than 0)");
[2677]193        rowsTextBox.SelectAll();
[2669]194      }
195    }
[2677]196    private void rowsTextBox_Validated(object sender, EventArgs e) {
[3430]197      if (!Content.ReadOnly) Content.Rows = int.Parse(rowsTextBox.Text);
[2677]198      errorProvider.SetError(rowsTextBox, string.Empty);
[2669]199    }
[2677]200    private void rowsTextBox_KeyDown(object sender, KeyEventArgs e) {
[2669]201      if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
[2677]202        rowsLabel.Focus();  // set focus on label to validate data
[2676]203      if (e.KeyCode == Keys.Escape) {
[2973]204        rowsTextBox.Text = Content.Rows.ToString();
[2677]205        rowsLabel.Focus();  // set focus on label to validate data
[2676]206      }
[2669]207    }
[2677]208    private void columnsTextBox_Validating(object sender, CancelEventArgs e) {
209      int i = 0;
[3335]210      if (!int.TryParse(columnsTextBox.Text, out i) || (i <= 0)) {
[2677]211        e.Cancel = true;
[3335]212        errorProvider.SetError(columnsTextBox, "Invalid Number of Columns (Valid values are positive integers larger than 0)");
[2677]213        columnsTextBox.SelectAll();
214      }
215    }
216    private void columnsTextBox_Validated(object sender, EventArgs e) {
[3430]217      if (!Content.ReadOnly) Content.Columns = int.Parse(columnsTextBox.Text);
[2677]218      errorProvider.SetError(columnsTextBox, string.Empty);
219    }
220    private void columnsTextBox_KeyDown(object sender, KeyEventArgs e) {
221      if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
222        columnsLabel.Focus();  // set focus on label to validate data
223      if (e.KeyCode == Keys.Escape) {
[2713]224        columnsTextBox.Text = Content.Columns.ToString();
[2677]225        columnsLabel.Focus();  // set focus on label to validate data
226      }
227    }
228    #endregion
229
230    #region DataGridView Events
[2669]231    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
[3328]232      if (!dataGridView.ReadOnly) {
233        string errorMessage;
234        if (!Content.Validate(e.FormattedValue.ToString(), out errorMessage)) {
235          e.Cancel = true;
236          dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
237        }
[2676]238      }
[2669]239    }
[2676]240    private void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
[3328]241      if (!dataGridView.ReadOnly) {
242        string value = e.Value.ToString();
243        int rowIndex = virtualRowIndizes[e.RowIndex];
244        e.ParsingApplied = Content.SetValue(value, rowIndex, e.ColumnIndex);
245        if (e.ParsingApplied) e.Value = Content.GetValue(rowIndex, e.ColumnIndex);
246      }
[2669]247    }
[2676]248    private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
249      dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
250    }
[3312]251    private void dataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) {
[3337]252      if (e.RowIndex < Content.Rows && e.ColumnIndex < Content.Columns) {
253        int rowIndex = virtualRowIndizes[e.RowIndex];
[3335]254        e.Value = Content.GetValue(rowIndex, e.ColumnIndex);
[3337]255      }
[3312]256    }
257    private void dataGridView_Scroll(object sender, ScrollEventArgs e) {
258      UpdateRowHeaders();
259    }
260    private void dataGridView_Resize(object sender, EventArgs e) {
261      UpdateRowHeaders();
262    }
[3314]263
264    private void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
[3320]265      if (Content != null) {
266        if (e.Button == MouseButtons.Left && Content.SortableView) {
267          bool addToSortedIndizes = (Control.ModifierKeys & Keys.Control) == Keys.Control;
268          SortOrder newSortOrder = SortOrder.Ascending;
269          if (sortedColumnIndizes.Any(x => x.Key == e.ColumnIndex)) {
270            SortOrder oldSortOrder = sortedColumnIndizes.Where(x => x.Key == e.ColumnIndex).First().Value;
271            int enumLength = Enum.GetValues(typeof(SortOrder)).Length;
272            newSortOrder = oldSortOrder = (SortOrder)Enum.Parse(typeof(SortOrder), ((((int)oldSortOrder) + 1) % enumLength).ToString());
273          }
[3314]274
[3320]275          if (!addToSortedIndizes)
276            sortedColumnIndizes.Clear();
[3314]277
[3320]278          if (sortedColumnIndizes.Any(x => x.Key == e.ColumnIndex)) {
279            int sortedIndex = sortedColumnIndizes.FindIndex(x => x.Key == e.ColumnIndex);
280            if (newSortOrder != SortOrder.None)
281              sortedColumnIndizes[sortedIndex] = new KeyValuePair<int, SortOrder>(e.ColumnIndex, newSortOrder);
282            else
283              sortedColumnIndizes.RemoveAt(sortedIndex);
284          } else
285            if (newSortOrder != SortOrder.None)
286              sortedColumnIndizes.Add(new KeyValuePair<int, SortOrder>(e.ColumnIndex, newSortOrder));
287          Sort();
288        } else if (e.Button == MouseButtons.Right) {
289          if (Content.ColumnNames.Count() != 0)
290            contextMenu.Show(MousePosition);
291        }
[3314]292      }
293    }
294
295    private void Sort() {
[3444]296      virtualRowIndizes = Sort(sortedColumnIndizes);
297      UpdateSortGlyph();
298      dataGridView.Invalidate();
299    }
300    protected virtual int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
[3328]301      int[] newSortedIndex = Enumerable.Range(0, Content.Rows).ToArray();
[3444]302      if (sortedColumns.Count() != 0) {
303        rowComparer.SortedIndizes = sortedColumns;
[3346]304        rowComparer.Matrix = Content;
[3314]305        Array.Sort(newSortedIndex, rowComparer);
306      }
[3444]307      return newSortedIndex;
[3328]308    }
309    private void UpdateSortGlyph() {
[3314]310      foreach (DataGridViewColumn col in this.dataGridView.Columns)
311        col.HeaderCell.SortGlyphDirection = SortOrder.None;
312      foreach (KeyValuePair<int, SortOrder> p in sortedColumnIndizes)
313        this.dataGridView.Columns[p.Key].HeaderCell.SortGlyphDirection = p.Value;
314    }
[2677]315    #endregion
[3312]316
[3346]317    public class RowComparer : IComparer<int> {
[3314]318      public RowComparer() {
319      }
[3312]320
[3346]321      private List<KeyValuePair<int, SortOrder>> sortedIndizes;
322      public IEnumerable<KeyValuePair<int, SortOrder>> SortedIndizes {
323        get { return this.sortedIndizes; }
324        set { sortedIndizes = new List<KeyValuePair<int, SortOrder>>(value); }
325      }
326      private IStringConvertibleMatrix matrix;
327      public IStringConvertibleMatrix Matrix {
328        get { return this.matrix; }
329        set { this.matrix = value; }
330      }
331
[3314]332      public int Compare(int x, int y) {
333        int result = 0;
334        double double1, double2;
335        DateTime dateTime1, dateTime2;
[3444]336        TimeSpan timeSpan1, timeSpan2;
[3314]337        string string1, string2;
338
[3346]339        if (matrix == null)
340          throw new InvalidOperationException("Could not sort IStringConvertibleMatrix if the matrix member is null.");
341        if (sortedIndizes == null)
342          return 0;
343
344        foreach (KeyValuePair<int, SortOrder> pair in sortedIndizes.Where(p => p.Value != SortOrder.None)) {
[3350]345          string1 = matrix.GetValue(x, pair.Key);
346          string2 = matrix.GetValue(y, pair.Key);
347          if (double.TryParse(string1, out double1) && double.TryParse(string2, out double2))
348            result = double1.CompareTo(double2);
349          else if (DateTime.TryParse(string1, out dateTime1) && DateTime.TryParse(string2, out dateTime2))
350            result = dateTime1.CompareTo(dateTime2);
[3444]351          else if (TimeSpan.TryParse(string1, out timeSpan1) && TimeSpan.TryParse(string2, out timeSpan2))
352            result = timeSpan1.CompareTo(timeSpan2);
[3350]353          else {
354            if (string1 != null)
355              result = string1.CompareTo(string2);
356            else if (string2 != null)
357              result = string2.CompareTo(string1) * -1;
358          }
359          if (pair.Value == SortOrder.Descending)
360            result *= -1;
361          if (result != 0)
362            return result;
[3314]363        }
364        return result;
365      }
366    }
[3316]367
368    private void ShowHideColumns_Click(object sender, EventArgs e) {
369      new ColumnsVisibilityDialog(this.dataGridView.Columns.Cast<DataGridViewColumn>()).ShowDialog();
370    }
[2669]371  }
372}
Note: See TracBrowser for help on using the repository browser.