source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.4/DataGridContentView.cs @ 10930

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