Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.3/DataGridContentView.cs @ 10881

Last change on this file since 10881 was 10881, checked in by sbreuer, 9 years ago
  • resize column width on first opening of data grid view
File size: 20.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Data;
28using HeuristicLab.DataPreprocessing.Filter;
29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.DataPreprocessing.Views {
32  [View("Data Grid Content View")]
33  [Content(typeof(IDataGridContent), true)]
34  public partial class DataGridContentView : CopyOfStringConvertibleMatrixView {
35
36    private bool notOwnEvent = true;
37    private bool isSearching = false;
38    private SearchAndReplaceDialog findAndReplaceDialog;
39    private IFindPreprocessingItemsIterator searchIterator;
40    private string currentSearchText;
41    private ComparisonOperation currentComparisonOperation;
42    private Tuple<int, int> currentCell;
43
44    public new IDataGridContent Content {
45      get { return (IDataGridContent)base.Content; }
46      set { base.Content = value; }
47    }
48
49    private IList<int> _highlightedRowIndices;
50    public IList<int> HighlightedRowIndices {
51      get { return _highlightedRowIndices; }
52      set {
53        _highlightedRowIndices = value;
54        Refresh();
55      }
56    }
57
58    private IDictionary<int, IList<int>> _highlightedCellsBackground;
59    public IDictionary<int, IList<int>> HightlightedCellsBackground {
60      get { return _highlightedCellsBackground; }
61      set {
62        _highlightedCellsBackground = value;
63        Refresh();
64      }
65    }
66
67    public DataGridContentView() {
68      InitializeComponent();
69      dataGridView.CellMouseClick += dataGridView_CellMouseClick;
70      dataGridView.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(dataGridView_CellPainting);
71      dataGridView.KeyDown += dataGridView_KeyDown;
72      contextMenuCell.Items.Add(ShowHideColumns);
73      _highlightedRowIndices = new List<int>();
74      _highlightedCellsBackground = new Dictionary<int, IList<int>>();
75      currentCell = null;
76      DataGridView.SelectionChanged += DataGridView_SelectionChanged;
77    }
78
79    void DataGridView_SelectionChanged(object sender, EventArgs e) {
80      if (!isSearching && Content != null) {
81        Content.DataGridLogic.SetSelection(GetSelectedCells());
82      }
83    }
84
85    protected override void OnContentChanged() {
86      base.OnContentChanged();
87      DataGridView.AutoResizeColumns();
88      DataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
89      if (Content == null && findAndReplaceDialog != null) {
90        findAndReplaceDialog.Close();
91      }
92    }
93
94    protected override void RegisterContentEvents() {
95      base.RegisterContentEvents();
96      Content.Changed += Content_Changed;
97    }
98
99    protected override void DeregisterContentEvents() {
100      base.DeregisterContentEvents();
101      Content.Changed -= Content_Changed;
102    }
103
104    void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
105      if (notOwnEvent) {
106        switch (e.Type) {
107          case DataPreprocessingChangedEventType.ChangeColumn:
108          case DataPreprocessingChangedEventType.ChangeItem:
109            dataGridView.Refresh();
110            break;
111          default:
112            OnContentChanged();
113            break;
114        }
115      }
116      searchIterator = null;
117    }
118
119    protected override void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
120      if (!dataGridView.ReadOnly) {
121        string errorMessage;
122        if (Content != null && !Content.DataGridLogic.Validate(e.FormattedValue.ToString(), out errorMessage, e.ColumnIndex)) {
123          e.Cancel = true;
124          dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
125        }
126      }
127    }
128
129    protected override void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
130      triggersOwnEvent(() => base.dataGridView_CellParsing(sender, e));
131    }
132
133    protected override void PasteValuesToDataGridView() {
134      triggersOwnEvent(() => base.PasteValuesToDataGridView());
135    }
136
137    protected override void SetEnabledStateOfControls() {
138      base.SetEnabledStateOfControls();
139      rowsTextBox.ReadOnly = true;
140      columnsTextBox.ReadOnly = true;
141    }
142
143    private void btnApplySort_Click(object sender, System.EventArgs e) {
144      triggersOwnEvent(() => {
145        Content.ManipulationLogic.ReOrderToIndices(virtualRowIndices);
146        OnContentChanged();
147      });
148    }
149
150    private void triggersOwnEvent(Action action) {
151      notOwnEvent = false;
152      action();
153      notOwnEvent = true;
154    }
155
156    #region FindAndReplaceDialog
157
158    private void CreateFindAndReplaceDialog() {
159      if (findAndReplaceDialog == null || findAndReplaceDialog.IsDisposed) {
160        findAndReplaceDialog = new SearchAndReplaceDialog();
161        findAndReplaceDialog.Show(this);
162        if (AreMultipleCellsSelected()) {
163          ResetHighlightedCellsBackground();
164          HightlightedCellsBackground = GetSelectedCells();
165          dataGridView.ClearSelection();
166        }
167        findAndReplaceDialog.FindAllEvent += findAndReplaceDialog_FindAllEvent;
168        findAndReplaceDialog.FindNextEvent += findAndReplaceDialog_FindNextEvent;
169        findAndReplaceDialog.ReplaceAllEvent += findAndReplaceDialog_ReplaceAllEvent;
170        findAndReplaceDialog.ReplaceNextEvent += findAndReplaceDialog_ReplaceEvent;
171        findAndReplaceDialog.FormClosing += findAndReplaceDialog_FormClosing;
172        searchIterator = null;
173        DataGridView.SelectionChanged += DataGridView_SelectionChanged_FindAndReplace;
174      }
175    }
176
177    void DataGridView_SelectionChanged_FindAndReplace(object sender, EventArgs e) {
178      if (Content != null) {
179        if (!isSearching && AreMultipleCellsSelected()) {
180          ResetHighlightedCellsBackground();
181          HightlightedCellsBackground = GetSelectedCells();
182          searchIterator = null;
183        }
184      }
185    }
186
187    void findAndReplaceDialog_FormClosing(object sender, FormClosingEventArgs e) {
188      ResetHighlightedCellsBackground();
189      searchIterator = null;
190      DataGridView.SelectionChanged -= DataGridView_SelectionChanged_FindAndReplace;
191    }
192
193    void findAndReplaceDialog_ReplaceEvent(object sender, EventArgs e) {
194      if (searchIterator != null && searchIterator.GetCurrent() != null) {
195        Replace(TransformToDictionary(currentCell));
196      }
197    }
198
199    void findAndReplaceDialog_ReplaceAllEvent(object sender, EventArgs e) {
200      Replace(FindAll(findAndReplaceDialog.GetSearchText()));
201    }
202
203    void findAndReplaceDialog_FindNextEvent(object sender, EventArgs e) {
204      if (searchIterator == null ||
205        currentSearchText != findAndReplaceDialog.GetSearchText() ||
206        currentComparisonOperation != findAndReplaceDialog.GetComparisonOperation()) {
207        searchIterator = new FindPreprocessingItemsIterator(FindAll(findAndReplaceDialog.GetSearchText()));
208        currentSearchText = findAndReplaceDialog.GetSearchText();
209        currentComparisonOperation = findAndReplaceDialog.GetComparisonOperation();
210      }
211
212      if (IsOneCellSelected()) {
213        var first = GetSelectedCells().First();
214        searchIterator.SetStartCell(first.Key, first.Value[0]);
215      }
216
217      bool moreOccurences = false;
218      currentCell = searchIterator.GetCurrent();
219      moreOccurences = searchIterator.MoveNext();
220      if (IsOneCellSelected() && currentCell != null) {
221        var first = GetSelectedCells().First();
222        if (currentCell.Item1 == first.Key && currentCell.Item2 == first.Value[0]) {
223          if (!moreOccurences) {
224            searchIterator.Reset();
225          }
226          currentCell = searchIterator.GetCurrent();
227          moreOccurences = searchIterator.MoveNext();
228          if (!moreOccurences) {
229            searchIterator.Reset();
230          }
231        }
232      }
233
234      dataGridView.ClearSelection();
235
236      if (currentCell != null) {
237        dataGridView[currentCell.Item1, currentCell.Item2].Selected = true;
238      }
239    }
240
241    private bool AreMultipleCellsSelected() {
242      return GetSelectedCellCount() > 1;
243    }
244
245    private bool IsOneCellSelected() {
246      return GetSelectedCellCount() == 1;
247    }
248
249    private int GetSelectedCellCount() {
250      int count = 0;
251      foreach (var column in GetSelectedCells()) {
252        count += column.Value.Count();
253      }
254      return count;
255    }
256
257    void findAndReplaceDialog_FindAllEvent(object sender, EventArgs e) {
258      dataGridView.ClearSelection();
259      isSearching = true;
260      foreach (var column in FindAll(findAndReplaceDialog.GetSearchText())) {
261        foreach (var cell in column.Value) {
262          dataGridView[column.Key, cell].Selected = true;
263        }
264      }
265      isSearching = false;
266      DataGridView_SelectionChanged(null, null);
267    }
268
269    private Core.ConstraintOperation GetConstraintOperation(ComparisonOperation comparisonOperation) {
270      Core.ConstraintOperation constraintOperation = Core.ConstraintOperation.Equal;
271      switch (comparisonOperation) {
272        case ComparisonOperation.Equal:
273          constraintOperation = Core.ConstraintOperation.Equal;
274          break;
275        case ComparisonOperation.Greater:
276          constraintOperation = Core.ConstraintOperation.Greater;
277          break;
278        case ComparisonOperation.GreaterOrEqual:
279          constraintOperation = Core.ConstraintOperation.GreaterOrEqual;
280          break;
281        case ComparisonOperation.Less:
282          constraintOperation = Core.ConstraintOperation.Less;
283          break;
284        case ComparisonOperation.LessOrEqual:
285          constraintOperation = Core.ConstraintOperation.LessOrEqual;
286          break;
287        case ComparisonOperation.NotEqual:
288          constraintOperation = Core.ConstraintOperation.NotEqual;
289          break;
290      }
291      return constraintOperation;
292    }
293
294    private IDictionary<int, IList<int>> FindAll(string match) {
295      bool searchInSelection = HightlightedCellsBackground.Values.Sum(list => list.Count) > 1;
296      ComparisonOperation comparisonOperation = findAndReplaceDialog.GetComparisonOperation();
297      var comparisonFilter = new ComparisonFilter(Content.FilterLogic.PreprocessingData, GetConstraintOperation(comparisonOperation), new StringValue(match), true);
298      var filters = new List<Filter.IFilter>() { comparisonFilter };
299      var foundCells = new Dictionary<int, IList<int>>();
300      for (int i = 0; i < Content.FilterLogic.PreprocessingData.Columns; i++) {
301        comparisonFilter.ConstraintColumn = i;
302        bool[] filteredRows = Content.FilterLogic.GetFilterResult(filters, true);
303        var foundIndices = new List<int>();
304        for (int idx = 0; idx < filteredRows.Length; ++idx) {
305          var notFilteredThusFound = !filteredRows[idx];
306          if (notFilteredThusFound) {
307            foundIndices.Add(idx);
308          }
309        }
310        foundCells[i] = foundIndices;
311        IList<int> selectedList;
312        if (searchInSelection && HightlightedCellsBackground.TryGetValue(i, out selectedList)) {
313          foundCells[i] = foundCells[i].Intersect(selectedList).ToList<int>();
314        } else if (searchInSelection) {
315          foundCells[i].Clear();
316        }
317      }
318      return foundCells;
319    }
320
321    private void Replace(IDictionary<int, IList<int>> cells) {
322      if (findAndReplaceDialog != null) {
323        switch (findAndReplaceDialog.GetReplaceAction()) {
324          case ReplaceAction.Value:
325            Content.ManipulationLogic.ReplaceIndicesByValue(cells, findAndReplaceDialog.GetReplaceText());
326            break;
327          case ReplaceAction.Average:
328            Content.ManipulationLogic.ReplaceIndicesByAverageValue(cells, false);
329            break;
330          case ReplaceAction.Median:
331            Content.ManipulationLogic.ReplaceIndicesByMedianValue(cells, false);
332            break;
333          case ReplaceAction.Random:
334            Content.ManipulationLogic.ReplaceIndicesByRandomValue(cells, false);
335            break;
336          case ReplaceAction.MostCommon:
337            Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(cells, false);
338            break;
339          case ReplaceAction.Interpolation:
340            Content.ManipulationLogic.ReplaceIndicesByLinearInterpolationOfNeighbours(cells);
341            break;
342        }
343      }
344    }
345
346    private IDictionary<int, IList<int>> TransformToDictionary(Tuple<int, int> tuple) {
347      var highlightCells = new Dictionary<int, IList<int>>();
348      highlightCells.Add(tuple.Item1, new List<int>() { tuple.Item2 });
349      return highlightCells;
350    }
351
352    private void ResetHighlightedCellsBackground() {
353      HightlightedCellsBackground = new Dictionary<int, IList<int>>();
354    }
355
356    #endregion FindAndReplaceDialog
357
358    private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
359      if (Content == null) return;
360      if (e.Button == System.Windows.Forms.MouseButtons.Right) {
361        if (e.ColumnIndex == -1 || e.RowIndex == -1) {
362          replaceValueOverColumnToolStripMenuItem.Visible = false;
363          contextMenuCell.Show(MousePosition);
364        } else {
365          if (!dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, e.RowIndex])) {
366            dataGridView.ClearSelection();
367            dataGridView[e.ColumnIndex, e.RowIndex].Selected = true;
368          }
369
370          var columnIndices = new HashSet<int>();
371          for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
372            columnIndices.Add(dataGridView.SelectedCells[i].ColumnIndex);
373          }
374
375          replaceValueOverSelectionToolStripMenuItem.Enabled = AreMultipleCellsSelected();
376
377          averageToolStripMenuItem_Column.Enabled =
378            averageToolStripMenuItem_Selection.Enabled =
379            medianToolStripMenuItem_Column.Enabled =
380            medianToolStripMenuItem_Selection.Enabled =
381            randomToolStripMenuItem_Column.Enabled =
382            randomToolStripMenuItem_Selection.Enabled = !Content.DataGridLogic.AreAllStringColumns(columnIndices);
383
384          smoothingToolStripMenuItem_Column.Enabled =
385            interpolationToolStripMenuItem_Column.Enabled = !dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, 0])
386            && !dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, Content.Rows - 1])
387            && !Content.DataGridLogic.AreAllStringColumns(columnIndices);
388
389          replaceValueOverColumnToolStripMenuItem.Visible = true;
390          contextMenuCell.Show(MousePosition);
391        }
392      }
393    }
394
395    protected void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
396      if (Content == null) return;
397      if (e.RowIndex < 0) return;
398      if (e.ColumnIndex < 0) return;
399      if (e.State.HasFlag(DataGridViewElementStates.Selected)) return;
400      if (!e.PaintParts.HasFlag(DataGridViewPaintParts.Background)) return;
401      if (HighlightedRowIndices == null) return;
402
403      int rowIndex = virtualRowIndices[e.RowIndex];
404
405      Color backColor = e.CellStyle.BackColor;
406
407      if (HightlightedCellsBackground.ContainsKey(e.ColumnIndex) && HightlightedCellsBackground[e.ColumnIndex].Contains(e.RowIndex)) {
408        backColor = Color.LightGray;
409      }
410
411      using (Brush backColorBrush = new SolidBrush(backColor)) {
412        Rectangle bounds = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height);
413        e.Graphics.FillRectangle(backColorBrush, bounds);
414      }
415
416      using (Brush gridBrush = new SolidBrush(Color.LightGray)) {
417        Pen gridLinePen = new Pen(gridBrush);
418        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
419               e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
420               e.CellBounds.Bottom - 1);
421        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
422            e.CellBounds.Top, e.CellBounds.Right - 1,
423            e.CellBounds.Bottom);
424      }
425
426      e.PaintContent(e.CellBounds);
427      e.Handled = true;
428    }
429
430    void dataGridView_KeyDown(object sender, KeyEventArgs e) {
431      var selectedRows = dataGridView.SelectedRows;
432      if (e.KeyCode == Keys.Delete && selectedRows.Count > 0) {
433        List<int> rows = new List<int>();
434        for (int i = 0; i < selectedRows.Count; ++i) {
435          rows.Add(selectedRows[i].Index);
436        }
437        triggersOwnEvent(() => {
438          Content.DataGridLogic.DeleteRow(rows);
439          OnContentChanged();
440        });
441      } else if (e.Control && e.KeyCode == Keys.F) {
442        CreateFindAndReplaceDialog();
443        findAndReplaceDialog.ActivateSearch();
444      } else if (e.Control && e.KeyCode == Keys.R) {
445        CreateFindAndReplaceDialog();
446        findAndReplaceDialog.ActivateReplace();
447      }
448    }
449
450    protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
451      btnApplySort.Enabled = sortedColumns.Any();
452      return base.Sort(sortedColumns);
453    }
454
455    protected override void ClearSorting() {
456      btnApplySort.Enabled = false;
457      base.ClearSorting();
458    }
459
460    private IDictionary<int, IList<int>> GetSelectedCells() {
461      IDictionary<int, IList<int>> selectedCells = new Dictionary<int, IList<int>>();
462      for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
463        var columnIndex = dataGridView.SelectedCells[i].ColumnIndex;
464        if (!selectedCells.ContainsKey(columnIndex)) {
465          selectedCells.Add(columnIndex, new List<int>());
466        }
467        selectedCells[columnIndex].Add(dataGridView.SelectedCells[i].RowIndex);
468      }
469      return selectedCells;
470    }
471
472    private void ReplaceWithAverage_Column_Click(object sender, EventArgs e) {
473      Content.ManipulationLogic.ReplaceIndicesByAverageValue(GetSelectedCells(), false);
474    }
475    private void ReplaceWithAverage_Selection_Click(object sender, EventArgs e) {
476      Content.ManipulationLogic.ReplaceIndicesByAverageValue(GetSelectedCells(), true);
477    }
478
479    private void ReplaceWithMedian_Column_Click(object sender, EventArgs e) {
480      Content.ManipulationLogic.ReplaceIndicesByMedianValue(GetSelectedCells(), false);
481    }
482    private void ReplaceWithMedian_Selection_Click(object sender, EventArgs e) {
483      Content.ManipulationLogic.ReplaceIndicesByMedianValue(GetSelectedCells(), true);
484    }
485
486    private void ReplaceWithRandom_Column_Click(object sender, EventArgs e) {
487      Content.ManipulationLogic.ReplaceIndicesByRandomValue(GetSelectedCells(), false);
488    }
489    private void ReplaceWithRandom_Selection_Click(object sender, EventArgs e) {
490      Content.ManipulationLogic.ReplaceIndicesByRandomValue(GetSelectedCells(), true);
491    }
492
493    private void ReplaceWithMostCommon_Column_Click(object sender, EventArgs e) {
494      Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(GetSelectedCells(), false);
495    }
496    private void ReplaceWithMostCommon_Selection_Click(object sender, EventArgs e) {
497      Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(GetSelectedCells(), true);
498    }
499
500    private void ReplaceWithInterpolation_Column_Click(object sender, EventArgs e) {
501      Content.ManipulationLogic.ReplaceIndicesByLinearInterpolationOfNeighbours(GetSelectedCells());
502    }
503
504    private void ReplaceWithSmoothing_Selection_Click(object sender, EventArgs e) {
505      Content.ManipulationLogic.ReplaceIndicesBySmoothing(GetSelectedCells());
506    }
507
508    private void btnSearch_Click(object sender, EventArgs e) {
509      CreateFindAndReplaceDialog();
510      findAndReplaceDialog.ActivateSearch();
511    }
512
513    private void btnReplace_Click(object sender, EventArgs e) {
514      CreateFindAndReplaceDialog();
515      findAndReplaceDialog.ActivateReplace();
516    }
517  }
518}
Note: See TracBrowser for help on using the repository browser.