Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.DataPreprocessing.Views/3.4/DataGridContentView.cs @ 11178

Last change on this file since 11178 was 11170, checked in by ascheibe, 10 years ago

#2115 updated copyright year in stable branch

File size: 22.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Data;
27using HeuristicLab.Data.Views;
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 : StringConvertibleMatrixView {
35
36    private bool isSearching = false;
37    private bool updateOnMouseUp = 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 IDictionary<int, IList<int>> _highlightedCellsBackground;
50    public IDictionary<int, IList<int>> HightlightedCellsBackground {
51      get { return _highlightedCellsBackground; }
52      set {
53        _highlightedCellsBackground = value;
54        Refresh();
55      }
56    }
57
58    public DataGridContentView() {
59      InitializeComponent();
60      dataGridView.CellMouseClick += dataGridView_CellMouseClick;
61      dataGridView.KeyDown += dataGridView_KeyDown;
62      dataGridView.MouseUp += dataGridView_MouseUp;
63      dataGridView.ColumnHeaderMouseClick += dataGridView_ColumnHeaderMouseClick;
64      contextMenuCell.Items.Add(ShowHideColumns);
65      _highlightedCellsBackground = new Dictionary<int, IList<int>>();
66      currentCell = null;
67    }
68
69    protected override void OnContentChanged() {
70      List<KeyValuePair<int, SortOrder>> order = new List<KeyValuePair<int, SortOrder>>(base.sortedColumnIndices);
71      base.OnContentChanged();
72
73      DataGridView.RowHeadersWidth = 70;
74
75      if (Content == null && findAndReplaceDialog != null) {
76        findAndReplaceDialog.Close();
77      }
78
79      if (Content != null) {
80        base.sortedColumnIndices = order;
81        base.Sort();
82      }
83
84    }
85
86    protected override void RegisterContentEvents() {
87      base.RegisterContentEvents();
88      Content.Changed += Content_Changed;
89      Content.FilterLogic.FilterChanged += FilterLogic_FilterChanged;
90    }
91
92    protected override void DeregisterContentEvents() {
93      base.DeregisterContentEvents();
94      Content.Changed -= Content_Changed;
95      Content.FilterLogic.FilterChanged -= FilterLogic_FilterChanged;
96    }
97
98    private void FilterLogic_FilterChanged(object sender, EventArgs e) {
99      OnContentChanged();
100      searchIterator = null;
101      if (findAndReplaceDialog != null && !findAndReplaceDialog.IsDisposed) {
102        if (Content.FilterLogic.IsFiltered) {
103          findAndReplaceDialog.DisableReplace();
104        } else {
105          findAndReplaceDialog.EnableReplace();
106        }
107      }
108      btnReplace.Enabled = !Content.FilterLogic.IsFiltered;
109    }
110
111    private void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
112      OnContentChanged();
113      searchIterator = null;
114    }
115
116    protected override void dataGridView_SelectionChanged(object sender, EventArgs e) {
117      base.dataGridView_SelectionChanged(sender, e);
118      if (Content != null && dataGridView.RowCount != 0 && dataGridView.ColumnCount != 0)
119        Content.Selection = GetSelectedCells();
120    }
121
122    //couldn't use base.dataGridView_CellValidating as the values have to be validated per column individually
123    protected override void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
124      if (dataGridView.ReadOnly) return;
125      if (Content == null) return;
126      if (Content.Rows == 0 || Content.Columns == 0) return;
127
128      string errorMessage;
129      if (Content != null) {
130        if (dataGridView.IsCurrentCellInEditMode && Content.FilterLogic.IsFiltered) {
131          errorMessage = "A filter is active, you cannot modify data. Press ESC to exit edit mode.";
132        } else {
133          Content.Validate(e.FormattedValue.ToString(), out errorMessage, e.ColumnIndex);
134        }
135
136        if (!String.IsNullOrEmpty(errorMessage)) {
137          e.Cancel = true;
138          dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
139        }
140
141      }
142    }
143
144
145    //protected override void PasteValuesToDataGridView() {
146    //  base.PasteValuesToDataGridView();
147    //  dataGridView.Refresh();
148    //}
149
150    protected override void SetEnabledStateOfControls() {
151      base.SetEnabledStateOfControls();
152      rowsTextBox.ReadOnly = true;
153      columnsTextBox.ReadOnly = true;
154    }
155
156    protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
157      btnApplySort.Enabled = sortedColumns.Any();
158      return base.Sort(sortedColumns);
159    }
160
161    protected override void ClearSorting() {
162      btnApplySort.Enabled = false;
163      base.ClearSorting();
164    }
165
166    private void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
167      searchIterator = null;
168    }
169
170    private void dataGridView_MouseUp(object sender, MouseEventArgs e) {
171      if (!updateOnMouseUp)
172        return;
173
174      updateOnMouseUp = false;
175      dataGridView_SelectionChanged(sender, e);
176    }
177
178    private void btnApplySort_Click(object sender, System.EventArgs e) {
179      Content.ManipulationLogic.ReOrderToIndices(virtualRowIndices);
180      OnContentChanged();
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
238        searchIterator = new FindPreprocessingItemsIterator(FindAll(findAndReplaceDialog.GetSearchText()));
239        currentSearchText = findAndReplaceDialog.GetSearchText();
240        currentComparisonOperation = findAndReplaceDialog.GetComparisonOperation();
241      }
242
243      if (IsOneCellSelected()) {
244        var first = GetSelectedCells().First();
245        searchIterator.SetStartCell(first.Key, first.Value[0]);
246      }
247
248      bool moreOccurences = false;
249      currentCell = searchIterator.GetCurrent();
250      moreOccurences = searchIterator.MoveNext();
251      if (IsOneCellSelected() && currentCell != null) {
252        var first = GetSelectedCells().First();
253        if (currentCell.Item1 == first.Key && currentCell.Item2 == first.Value[0]) {
254          if (!moreOccurences) {
255            searchIterator.Reset();
256          }
257          currentCell = searchIterator.GetCurrent();
258          moreOccurences = searchIterator.MoveNext();
259          if (!moreOccurences) {
260            searchIterator.Reset();
261          }
262        }
263      }
264
265      dataGridView.ClearSelection();
266
267      if (currentCell != null) {
268        dataGridView[currentCell.Item1, currentCell.Item2].Selected = true;
269        dataGridView.CurrentCell = dataGridView[currentCell.Item1, currentCell.Item2];
270      }
271    }
272
273    private bool AreMultipleCellsSelected() {
274      return GetSelectedCellCount() > 1;
275    }
276
277    private bool IsOneCellSelected() {
278      return GetSelectedCellCount() == 1;
279    }
280
281    private int GetSelectedCellCount() {
282      int count = 0;
283      foreach (var column in GetSelectedCells()) {
284        count += column.Value.Count();
285      }
286      return count;
287    }
288
289    void findAndReplaceDialog_FindAllEvent(object sender, EventArgs e) {
290      dataGridView.ClearSelection();
291      isSearching = true;
292      SuspendRepaint();
293      var selectedCells = FindAll(findAndReplaceDialog.GetSearchText());
294      foreach (var column in selectedCells) {
295        foreach (var cell in column.Value) {
296          dataGridView[column.Key, cell].Selected = true;
297        }
298      }
299      ResumeRepaint(true);
300      isSearching = false;
301      Content.Selection = selectedCells;
302      //update statistic in base
303      base.dataGridView_SelectionChanged(sender, e);
304    }
305
306    private Core.ConstraintOperation GetConstraintOperation(ComparisonOperation comparisonOperation) {
307      Core.ConstraintOperation constraintOperation = Core.ConstraintOperation.Equal;
308      switch (comparisonOperation) {
309        case ComparisonOperation.Equal:
310          constraintOperation = Core.ConstraintOperation.Equal;
311          break;
312        case ComparisonOperation.Greater:
313          constraintOperation = Core.ConstraintOperation.Greater;
314          break;
315        case ComparisonOperation.GreaterOrEqual:
316          constraintOperation = Core.ConstraintOperation.GreaterOrEqual;
317          break;
318        case ComparisonOperation.Less:
319          constraintOperation = Core.ConstraintOperation.Less;
320          break;
321        case ComparisonOperation.LessOrEqual:
322          constraintOperation = Core.ConstraintOperation.LessOrEqual;
323          break;
324        case ComparisonOperation.NotEqual:
325          constraintOperation = Core.ConstraintOperation.NotEqual;
326          break;
327      }
328      return constraintOperation;
329    }
330
331    private IDictionary<int, IList<int>> FindAll(string match) {
332      bool searchInSelection = HightlightedCellsBackground.Values.Sum(list => list.Count) > 1;
333      ComparisonOperation comparisonOperation = findAndReplaceDialog.GetComparisonOperation();
334      var foundCells = new Dictionary<int, IList<int>>();
335      for (int i = 0; i < Content.FilterLogic.PreprocessingData.Columns; i++) {
336        var filters = CreateFilters(match, comparisonOperation, i);
337
338        bool[] filteredRows = Content.FilterLogic.GetFilterResult(filters, true);
339        var foundIndices = new List<int>();
340        for (int idx = 0; idx < filteredRows.Length; ++idx) {
341          var notFilteredThusFound = !filteredRows[idx];
342          if (notFilteredThusFound) {
343            foundIndices.Add(idx);
344          }
345        }
346        foundCells[i] = foundIndices;
347        IList<int> selectedList;
348        if (searchInSelection && HightlightedCellsBackground.TryGetValue(i, out selectedList)) {
349          foundCells[i] = foundCells[i].Intersect(selectedList).ToList<int>();
350        } else if (searchInSelection) {
351          foundCells[i].Clear();
352        }
353      }
354      return MapToSorting(foundCells);
355    }
356
357    private List<IFilter> CreateFilters(string match, ComparisonOperation comparisonOperation, int columnIndex) {
358      IPreprocessingData preprocessingData = Content.FilterLogic.PreprocessingData;
359      IStringConvertibleValue value;
360      if (preprocessingData.VariableHasType<double>(columnIndex)) {
361        value = new DoubleValue();
362      } else if (preprocessingData.VariableHasType<String>(columnIndex)) {
363        value = new StringValue();
364      } else if (preprocessingData.VariableHasType<DateTime>(columnIndex)) {
365        value = new DateTimeValue();
366      } else {
367        throw new ArgumentException("unsupported type");
368      }
369      value.SetValue(match);
370      var comparisonFilter = new ComparisonFilter(preprocessingData, GetConstraintOperation(comparisonOperation), value, true);
371      comparisonFilter.ConstraintColumn = columnIndex;
372      return new List<Filter.IFilter>() { comparisonFilter };
373    }
374
375    private IDictionary<int, IList<int>> MapToSorting(Dictionary<int, IList<int>> foundCells) {
376      if (sortedColumnIndices.Count == 0) {
377        return foundCells;
378      } else {
379        var sortedFoundCells = new Dictionary<int, IList<int>>();
380
381        var indicesToVirtual = new Dictionary<int, int>();
382        for (int i = 0; i < virtualRowIndices.Length; ++i) {
383          indicesToVirtual.Add(virtualRowIndices[i], i);
384        }
385
386        foreach (var entry in foundCells) {
387          var cells = new List<int>();
388          foreach (var cell in entry.Value) {
389            cells.Add(indicesToVirtual[cell]);
390          }
391          cells.Sort();
392          sortedFoundCells.Add(entry.Key, cells);
393        }
394        return sortedFoundCells;
395      }
396    }
397
398    private void Replace(IDictionary<int, IList<int>> cells) {
399      if (findAndReplaceDialog != null) {
400        ReplaceTransaction(() => {
401          switch (findAndReplaceDialog.GetReplaceAction()) {
402            case ReplaceAction.Value:
403              Content.ManipulationLogic.ReplaceIndicesByValue(cells, findAndReplaceDialog.GetReplaceText());
404              break;
405            case ReplaceAction.Average:
406              Content.ManipulationLogic.ReplaceIndicesByAverageValue(cells, false);
407              break;
408            case ReplaceAction.Median:
409              Content.ManipulationLogic.ReplaceIndicesByMedianValue(cells, false);
410              break;
411            case ReplaceAction.Random:
412              Content.ManipulationLogic.ReplaceIndicesByRandomValue(cells, false);
413              break;
414            case ReplaceAction.MostCommon:
415              Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(cells, false);
416              break;
417            case ReplaceAction.Interpolation:
418              Content.ManipulationLogic.ReplaceIndicesByLinearInterpolationOfNeighbours(cells);
419              break;
420          }
421        });
422      }
423    }
424
425    private IDictionary<int, IList<int>> TransformToDictionary(Tuple<int, int> tuple) {
426      var highlightCells = new Dictionary<int, IList<int>>();
427      highlightCells.Add(tuple.Item1, new List<int>() { tuple.Item2 });
428      return highlightCells;
429    }
430
431    private void ResetHighlightedCellsBackground() {
432      HightlightedCellsBackground = new Dictionary<int, IList<int>>();
433    }
434
435    #endregion FindAndReplaceDialog
436
437    private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
438      if (Content == null) return;
439      if (e.Button == System.Windows.Forms.MouseButtons.Right) {
440        if (e.ColumnIndex == -1 || e.RowIndex == -1) {
441          replaceValueOverColumnToolStripMenuItem.Visible = false;
442          contextMenuCell.Show(MousePosition);
443        } else {
444          if (!dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, e.RowIndex])) {
445            dataGridView.ClearSelection();
446            dataGridView[e.ColumnIndex, e.RowIndex].Selected = true;
447          }
448
449          var columnIndices = new HashSet<int>();
450          for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
451            columnIndices.Add(dataGridView.SelectedCells[i].ColumnIndex);
452          }
453
454          replaceValueOverSelectionToolStripMenuItem.Enabled = AreMultipleCellsSelected();
455
456          averageToolStripMenuItem_Column.Enabled =
457            averageToolStripMenuItem_Selection.Enabled =
458            medianToolStripMenuItem_Column.Enabled =
459            medianToolStripMenuItem_Selection.Enabled =
460            randomToolStripMenuItem_Column.Enabled =
461            randomToolStripMenuItem_Selection.Enabled = !Content.PreProcessingData.AreAllStringColumns(columnIndices);
462
463          smoothingToolStripMenuItem_Column.Enabled =
464            interpolationToolStripMenuItem_Column.Enabled = !dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, 0])
465            && !dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, Content.Rows - 1])
466            && !Content.PreProcessingData.AreAllStringColumns(columnIndices);
467
468          replaceValueOverColumnToolStripMenuItem.Visible = true;
469          contextMenuCell.Show(MousePosition);
470        }
471      }
472    }
473
474    private void dataGridView_KeyDown(object sender, KeyEventArgs e) {
475      var selectedRows = dataGridView.SelectedRows;
476      if (e.KeyCode == Keys.Delete && selectedRows.Count > 0) {
477        List<int> rows = new List<int>();
478        for (int i = 0; i < selectedRows.Count; ++i) {
479          rows.Add(selectedRows[i].Index);
480        }
481        Content.DeleteRow(rows);
482      } else if (e.Control && e.KeyCode == Keys.F) {
483        CreateFindAndReplaceDialog();
484        findAndReplaceDialog.ActivateSearch();
485      } else if (e.Control && e.KeyCode == Keys.R) {
486        CreateFindAndReplaceDialog();
487        findAndReplaceDialog.ActivateReplace();
488      }
489    }
490
491    private IDictionary<int, IList<int>> GetSelectedCells() {
492      IDictionary<int, IList<int>> selectedCells = new Dictionary<int, IList<int>>();
493
494      //special case if all cells are selected
495      if (dataGridView.AreAllCellsSelected(true)) {
496        for (int i = 0; i < Content.Columns; i++)
497          selectedCells[i] = Enumerable.Range(0, Content.Rows).ToList();
498        return selectedCells;
499      }
500
501      foreach (var selectedCell in dataGridView.SelectedCells) {
502        var cell = (DataGridViewCell)selectedCell;
503        if (!selectedCells.ContainsKey(cell.ColumnIndex))
504          selectedCells.Add(cell.ColumnIndex, new List<int>(1024));
505        selectedCells[cell.ColumnIndex].Add(cell.RowIndex);
506      }
507
508      return selectedCells;
509    }
510
511    private void StartReplacing() {
512      SuspendRepaint();
513    }
514
515    private void StopReplacing() {
516      ResumeRepaint(true);
517    }
518
519    private void ReplaceTransaction(Action action) {
520      StartReplacing();
521      action();
522      StopReplacing();
523    }
524
525    private void btnSearch_Click(object sender, EventArgs e) {
526      CreateFindAndReplaceDialog();
527      findAndReplaceDialog.ActivateSearch();
528    }
529
530    private void btnReplace_Click(object sender, EventArgs e) {
531      CreateFindAndReplaceDialog();
532      findAndReplaceDialog.ActivateReplace();
533    }
534
535    #region ContextMenu Events
536
537    private void ReplaceWithAverage_Column_Click(object sender, EventArgs e) {
538      ReplaceTransaction(() => {
539        Content.ManipulationLogic.ReplaceIndicesByAverageValue(GetSelectedCells(), false);
540      });
541    }
542    private void ReplaceWithAverage_Selection_Click(object sender, EventArgs e) {
543      ReplaceTransaction(() => {
544        Content.ManipulationLogic.ReplaceIndicesByAverageValue(GetSelectedCells(), true);
545      });
546    }
547
548    private void ReplaceWithMedian_Column_Click(object sender, EventArgs e) {
549      ReplaceTransaction(() => {
550        Content.ManipulationLogic.ReplaceIndicesByMedianValue(GetSelectedCells(), false);
551      });
552    }
553    private void ReplaceWithMedian_Selection_Click(object sender, EventArgs e) {
554      ReplaceTransaction(() => {
555        Content.ManipulationLogic.ReplaceIndicesByMedianValue(GetSelectedCells(), true);
556      });
557    }
558
559    private void ReplaceWithRandom_Column_Click(object sender, EventArgs e) {
560      ReplaceTransaction(() => {
561        Content.ManipulationLogic.ReplaceIndicesByRandomValue(GetSelectedCells(), false);
562      });
563    }
564    private void ReplaceWithRandom_Selection_Click(object sender, EventArgs e) {
565      ReplaceTransaction(() => {
566        Content.ManipulationLogic.ReplaceIndicesByRandomValue(GetSelectedCells(), true);
567      });
568    }
569
570    private void ReplaceWithMostCommon_Column_Click(object sender, EventArgs e) {
571      ReplaceTransaction(() => {
572        Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(GetSelectedCells(), false);
573      });
574    }
575    private void ReplaceWithMostCommon_Selection_Click(object sender, EventArgs e) {
576      ReplaceTransaction(() => {
577        Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(GetSelectedCells(), true);
578      });
579    }
580
581    private void ReplaceWithInterpolation_Column_Click(object sender, EventArgs e) {
582      ReplaceTransaction(() => {
583        Content.ManipulationLogic.ReplaceIndicesByLinearInterpolationOfNeighbours(GetSelectedCells());
584      });
585    }
586
587    private void ReplaceWithSmoothing_Selection_Click(object sender, EventArgs e) {
588      ReplaceTransaction(() => {
589        Content.ManipulationLogic.ReplaceIndicesBySmoothing(GetSelectedCells());
590      });
591    }
592    #endregion
593
594  }
595}
Note: See TracBrowser for help on using the repository browser.