Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.DataPreprocessing.Views/3.4/DataGridContentView.cs

Last change on this file was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 27.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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(DataGridContent), true)]
34  public partial class DataGridContentView : StringConvertibleMatrixView {
35    private bool isSearching = false;
36    private bool updateOnMouseUp = false;
37    private SearchAndReplaceDialog findAndReplaceDialog;
38    private IFindPreprocessingItemsIterator searchIterator;
39    private string currentSearchText;
40    private ComparisonOperation currentComparisonOperation;
41    private Tuple<int, int> currentCell;
42
43    public new DataGridContent Content {
44      get { return (DataGridContent)base.Content; }
45      set { base.Content = value; }
46    }
47
48    private IDictionary<int, IList<int>> _highlightedCellsBackground;
49    public IDictionary<int, IList<int>> HightlightedCellsBackground {
50      get { return _highlightedCellsBackground; }
51      set {
52        _highlightedCellsBackground = value;
53        Refresh();
54      }
55    }
56
57    public DataGridContentView() {
58      InitializeComponent();
59      dataGridView.MouseDown += dataGridView_MouseDown;
60      dataGridView.CellMouseClick += dataGridView_CellMouseClick;
61      dataGridView.RowHeaderMouseClick += dataGridView_RowHeaderMouseClick;
62      dataGridView.MouseUp += dataGridView_MouseUp;
63      contextMenuCell.Items.Add(ShowHideColumns);
64      _highlightedCellsBackground = new Dictionary<int, IList<int>>();
65      currentCell = null;
66    }
67
68    protected override void OnContentChanged() {
69      List<KeyValuePair<int, SortOrder>> order = new List<KeyValuePair<int, SortOrder>>(base.sortedColumnIndices);
70      base.OnContentChanged();
71
72      DataGridView.RowHeadersWidth = 70;
73
74      if (Content == null && findAndReplaceDialog != null) {
75        findAndReplaceDialog.Close();
76      }
77
78      if (Content != null) {
79        base.sortedColumnIndices = order;
80        base.Sort();
81      }
82    }
83
84    protected override void RegisterContentEvents() {
85      base.RegisterContentEvents();
86      Content.Changed += Content_Changed;
87      Content.PreprocessingData.FilterChanged += FilterLogic_FilterChanged;
88    }
89
90    protected override void DeregisterContentEvents() {
91      base.DeregisterContentEvents();
92      Content.Changed -= Content_Changed;
93      Content.PreprocessingData.FilterChanged -= FilterLogic_FilterChanged;
94    }
95
96    private void FilterLogic_FilterChanged(object sender, EventArgs e) {
97      OnContentChanged();
98      searchIterator = null;
99      if (findAndReplaceDialog != null && !findAndReplaceDialog.IsDisposed) {
100        if (Content.PreprocessingData.IsFiltered) {
101          findAndReplaceDialog.DisableReplace();
102        } else {
103          findAndReplaceDialog.EnableReplace();
104        }
105      }
106      btnReplace.Enabled = !Content.PreprocessingData.IsFiltered;
107    }
108
109    private void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
110      OnContentChanged();
111      searchIterator = null;
112    }
113
114    protected override void dataGridView_SelectionChanged(object sender, EventArgs e) {
115      base.dataGridView_SelectionChanged(sender, e);
116      if (Content != null && dataGridView.RowCount != 0 && dataGridView.ColumnCount != 0)
117        Content.Selection = GetSelectedCells();
118    }
119
120    //couldn't use base.dataGridView_CellValidating as the values have to be validated per column individually
121    protected override void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
122      if (dataGridView.ReadOnly) return;
123      if (Content == null) return;
124      if (Content.Rows == 0 || Content.Columns == 0) return;
125
126      string errorMessage;
127      if (!String.IsNullOrEmpty(e.FormattedValue.ToString())) {
128        if (dataGridView.IsCurrentCellInEditMode && Content.PreprocessingData.IsFiltered) {
129          errorMessage = "A filter is active, you cannot modify data. Press ESC to exit edit mode.";
130        } else {
131          Content.Validate(e.FormattedValue.ToString(), out errorMessage, e.ColumnIndex);
132        }
133
134        if (!String.IsNullOrEmpty(errorMessage)) {
135          e.Cancel = true;
136          dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
137        }
138
139      }
140    }
141
142    protected override void PasteValuesToDataGridView() {
143      string[,] values = SplitClipboardString(Clipboard.GetText());
144      if (values.Length == 0) return;
145      int rowIndex = 0;
146      int columnIndex = 0;
147      if (dataGridView.CurrentCell != null) {
148        rowIndex = dataGridView.CurrentCell.RowIndex;
149        columnIndex = dataGridView.CurrentCell.ColumnIndex;
150      }
151
152      bool containsHeader = false;
153      var firstRow = Enumerable.Range(0, values.GetLength(0)).Select(col => values[col, 0]).ToList();
154      if ((Content.Selection.Values.Sum(s => s.Count) == Content.Rows * Content.Columns)
155          || (rowIndex == 0 && columnIndex == 0 && Content.Rows <= values.GetLength(1) && Content.Columns <= values.GetLength(0))) {
156        // All is selected -or- top left selected and everything will be replaced
157        double temp;
158        containsHeader = !firstRow.All(string.IsNullOrEmpty) && firstRow.Any(r => !double.TryParse(r, out temp));
159        if (containsHeader)
160          rowIndex = columnIndex = 0;
161      }
162
163      int newRows = values.GetLength(1) + rowIndex - (containsHeader ? 1 : 0);
164      int newColumns = values.GetLength(0) + columnIndex;
165      if (Content.Rows < newRows) Content.Rows = newRows;
166      if (Content.Columns < newColumns) Content.Columns = newColumns;
167
168      ReplaceTransaction(() => {
169        Content.PreprocessingData.InTransaction(() => {
170          for (int row = containsHeader ? 1 : 0; row < values.GetLength(1); row++) {
171            for (int col = 0; col < values.GetLength(0); col++) {
172              Content.SetValue(values[col, row], row + rowIndex - (containsHeader ? 1 : 0), col + columnIndex);
173            }
174          }
175          if (containsHeader) {
176            for (int i = 0; i < firstRow.Count; i++)
177              if (string.IsNullOrWhiteSpace(firstRow[i]))
178                firstRow[i] = string.Format("<{0}>", i);
179            Content.PreprocessingData.RenameColumns(firstRow);
180          }
181        });
182      });
183
184      ClearSorting();
185    }
186
187    protected override void SetEnabledStateOfControls() {
188      base.SetEnabledStateOfControls();
189      rowsTextBox.ReadOnly = true;
190      columnsTextBox.ReadOnly = true;
191    }
192
193    protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
194      btnApplySort.Enabled = sortedColumns.Any();
195      return base.Sort(sortedColumns);
196    }
197
198    protected override void ClearSorting() {
199      btnApplySort.Enabled = false;
200      base.ClearSorting();
201    }
202
203    //Necessary so that dataGridView.SelectedRows and SelectedColumns are populated correctly
204    //further information: https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectedcolumns.aspx
205    private void dataGridView_MouseDown(object sender, MouseEventArgs e) {
206      var hitTestInfo = dataGridView.HitTest(e.X, e.Y);
207      // row header click
208      if (hitTestInfo.ColumnIndex == -1 && hitTestInfo.RowIndex >= 0) {
209        if (dataGridView.SelectionMode != DataGridViewSelectionMode.RowHeaderSelect) {
210          dataGridView.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
211        }
212      }
213      // column header click
214      if (hitTestInfo.RowIndex == -1 && hitTestInfo.ColumnIndex >= 0) {
215        if (dataGridView.SelectionMode != DataGridViewSelectionMode.ColumnHeaderSelect) {
216          dataGridView.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
217        }
218      }
219    }
220
221    protected override void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
222      if (Content == null) return;
223
224      if (e.Button == MouseButtons.Middle) {
225        int newIndex = e.ColumnIndex >= 0 ? e.ColumnIndex : 0;
226        Content.PreprocessingData.InsertColumn<double>(newIndex.ToString(), newIndex);
227      } else if (e.Button == MouseButtons.Right && Content.SortableView) {
228        SortColumn(e.ColumnIndex);
229      }
230
231      searchIterator = null;
232    }
233    private void dataGridView_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
234      if (Content != null) {
235        if (e.Button == MouseButtons.Middle) {
236          int newIndex = e.RowIndex >= 0 ? e.RowIndex : 0;
237          Content.PreprocessingData.InsertRow(newIndex);
238        }
239      }
240    }
241
242    private void dataGridView_MouseUp(object sender, MouseEventArgs e) {
243      if (!updateOnMouseUp)
244        return;
245
246      updateOnMouseUp = false;
247      dataGridView_SelectionChanged(sender, e);
248    }
249
250    private void btnApplySort_Click(object sender, EventArgs e) {
251      Content.ReOrderToIndices(virtualRowIndices);
252      OnContentChanged();
253    }
254
255    #region FindAndReplaceDialog
256
257    private void CreateFindAndReplaceDialog() {
258      if (findAndReplaceDialog == null || findAndReplaceDialog.IsDisposed) {
259        findAndReplaceDialog = new SearchAndReplaceDialog();
260        findAndReplaceDialog.Show(this);
261        if (AreMultipleCellsSelected()) {
262          ResetHighlightedCellsBackground();
263          HightlightedCellsBackground = GetSelectedCells();
264          dataGridView.ClearSelection();
265        }
266        findAndReplaceDialog.FindAllEvent += findAndReplaceDialog_FindAllEvent;
267        findAndReplaceDialog.FindNextEvent += findAndReplaceDialog_FindNextEvent;
268        findAndReplaceDialog.ReplaceAllEvent += findAndReplaceDialog_ReplaceAllEvent;
269        findAndReplaceDialog.ReplaceNextEvent += findAndReplaceDialog_ReplaceEvent;
270        findAndReplaceDialog.FormClosing += findAndReplaceDialog_FormClosing;
271        searchIterator = null;
272        DataGridView.SelectionChanged += DataGridView_SelectionChanged_FindAndReplace;
273        if (Content.PreprocessingData.IsFiltered) {
274          findAndReplaceDialog.DisableReplace();
275        }
276      }
277    }
278
279    private void DataGridView_SelectionChanged_FindAndReplace(object sender, EventArgs e) {
280      if (Content != null) {
281        if (!isSearching && AreMultipleCellsSelected()) {
282          ResetHighlightedCellsBackground();
283          HightlightedCellsBackground = GetSelectedCells();
284          searchIterator = null;
285        }
286      }
287    }
288
289    void findAndReplaceDialog_FormClosing(object sender, FormClosingEventArgs e) {
290      ResetHighlightedCellsBackground();
291      searchIterator = null;
292      DataGridView.SelectionChanged -= DataGridView_SelectionChanged_FindAndReplace;
293    }
294
295    void findAndReplaceDialog_ReplaceEvent(object sender, EventArgs e) {
296      if (searchIterator != null && searchIterator.GetCurrent() != null) {
297        Replace(TransformToDictionary(currentCell));
298      }
299    }
300
301    void findAndReplaceDialog_ReplaceAllEvent(object sender, EventArgs e) {
302      Replace(FindAll(findAndReplaceDialog.GetSearchText()));
303    }
304
305    void findAndReplaceDialog_FindNextEvent(object sender, EventArgs e) {
306      if (searchIterator == null
307        || currentSearchText != findAndReplaceDialog.GetSearchText()
308        || currentComparisonOperation != findAndReplaceDialog.GetComparisonOperation()) {
309
310        searchIterator = new FindPreprocessingItemsIterator(FindAll(findAndReplaceDialog.GetSearchText()));
311        currentSearchText = findAndReplaceDialog.GetSearchText();
312        currentComparisonOperation = findAndReplaceDialog.GetComparisonOperation();
313      }
314
315      if (IsOneCellSelected()) {
316        var first = GetSelectedCells().First();
317        searchIterator.SetStartCell(first.Key, first.Value[0]);
318      }
319
320      bool moreOccurences = false;
321      currentCell = searchIterator.GetCurrent();
322      moreOccurences = searchIterator.MoveNext();
323      if (IsOneCellSelected() && currentCell != null) {
324        var first = GetSelectedCells().First();
325        if (currentCell.Item1 == first.Key && currentCell.Item2 == first.Value[0]) {
326          if (!moreOccurences) {
327            searchIterator.Reset();
328          }
329          currentCell = searchIterator.GetCurrent();
330          moreOccurences = searchIterator.MoveNext();
331          if (!moreOccurences) {
332            searchIterator.Reset();
333          }
334        }
335      }
336
337      dataGridView.ClearSelection();
338
339      if (currentCell != null) {
340        dataGridView[currentCell.Item1, currentCell.Item2].Selected = true;
341        dataGridView.CurrentCell = dataGridView[currentCell.Item1, currentCell.Item2];
342      }
343    }
344
345    private bool AreMultipleCellsSelected() {
346      return GetSelectedCellCount() > 1;
347    }
348
349    private bool IsOneCellSelected() {
350      return GetSelectedCellCount() == 1;
351    }
352
353    private int GetSelectedCellCount() {
354      int count = 0;
355      foreach (var column in GetSelectedCells()) {
356        count += column.Value.Count();
357      }
358      return count;
359    }
360
361    void findAndReplaceDialog_FindAllEvent(object sender, EventArgs e) {
362      dataGridView.ClearSelection();
363      isSearching = true;
364      SuspendRepaint();
365      var selectedCells = FindAll(findAndReplaceDialog.GetSearchText());
366      foreach (var column in selectedCells) {
367        foreach (var cell in column.Value) {
368          dataGridView[column.Key, cell].Selected = true;
369        }
370      }
371      ResumeRepaint(true);
372      isSearching = false;
373      Content.Selection = selectedCells;
374      //update statistic in base
375      base.dataGridView_SelectionChanged(sender, e);
376    }
377
378    private Core.ConstraintOperation GetConstraintOperation(ComparisonOperation comparisonOperation) {
379      Core.ConstraintOperation constraintOperation = Core.ConstraintOperation.Equal;
380      switch (comparisonOperation) {
381        case ComparisonOperation.Equal:
382          constraintOperation = Core.ConstraintOperation.Equal;
383          break;
384        case ComparisonOperation.Greater:
385          constraintOperation = Core.ConstraintOperation.Greater;
386          break;
387        case ComparisonOperation.GreaterOrEqual:
388          constraintOperation = Core.ConstraintOperation.GreaterOrEqual;
389          break;
390        case ComparisonOperation.Less:
391          constraintOperation = Core.ConstraintOperation.Less;
392          break;
393        case ComparisonOperation.LessOrEqual:
394          constraintOperation = Core.ConstraintOperation.LessOrEqual;
395          break;
396        case ComparisonOperation.NotEqual:
397          constraintOperation = Core.ConstraintOperation.NotEqual;
398          break;
399      }
400      return constraintOperation;
401    }
402
403    private IDictionary<int, IList<int>> FindAll(string match) {
404      bool searchInSelection = HightlightedCellsBackground.Values.Sum(list => list.Count) > 1;
405      ComparisonOperation comparisonOperation = findAndReplaceDialog.GetComparisonOperation();
406      var foundCells = new Dictionary<int, IList<int>>();
407      for (int i = 0; i < Content.PreprocessingData.Columns; i++) {
408        var filters = CreateFilters(match, comparisonOperation, i);
409
410        bool[] filteredRows = GetFilterResult(filters, true);
411        var foundIndices = new List<int>();
412        for (int idx = 0; idx < filteredRows.Length; ++idx) {
413          var notFilteredThusFound = !filteredRows[idx];
414          if (notFilteredThusFound) {
415            foundIndices.Add(idx);
416          }
417        }
418        foundCells[i] = foundIndices;
419        IList<int> selectedList;
420        if (searchInSelection && HightlightedCellsBackground.TryGetValue(i, out selectedList)) {
421          foundCells[i] = foundCells[i].Intersect(selectedList).ToList<int>();
422        } else if (searchInSelection) {
423          foundCells[i].Clear();
424        }
425      }
426      return MapToSorting(foundCells);
427    }
428
429    private bool[] GetFilterResult(IList<IFilter> filters, bool isAndCombination) {
430      IList<IFilter> activeFilters = filters.Where(f => f.Active && f.ConstraintData != null).ToList();
431
432      if (activeFilters.Count == 0) {
433        return Enumerable.Repeat(false, Content.PreprocessingData.Rows).ToArray(); ;
434      }
435
436      var result = Enumerable.Repeat(!isAndCombination, Content.PreprocessingData.Rows).ToArray();
437      foreach (IFilter filter in activeFilters) {
438        bool[] filterResult = filter.Check();
439        for (int row = 0; row < result.Length; ++row) {
440          result[row] = isAndCombination ? result[row] || filterResult[row] : result[row] && filterResult[row];
441        }
442      }
443      return result;
444    }
445
446    private List<IFilter> CreateFilters(string match, ComparisonOperation comparisonOperation, int columnIndex) {
447      IPreprocessingData preprocessingData = Content.PreprocessingData;
448      IStringConvertibleValue value;
449      if (preprocessingData.VariableHasType<double>(columnIndex)) {
450        value = new DoubleValue();
451      } else if (preprocessingData.VariableHasType<String>(columnIndex)) {
452        value = new StringValue();
453      } else if (preprocessingData.VariableHasType<DateTime>(columnIndex)) {
454        value = new DateTimeValue();
455      } else {
456        throw new ArgumentException("unsupported type");
457      }
458      value.SetValue(match);
459      var comparisonFilter = new ComparisonFilter(preprocessingData, GetConstraintOperation(comparisonOperation), value, true);
460      comparisonFilter.ConstraintColumn = columnIndex;
461      return new List<Filter.IFilter>() { comparisonFilter };
462    }
463
464    private IDictionary<int, IList<int>> MapToSorting(Dictionary<int, IList<int>> foundCells) {
465      if (sortedColumnIndices.Count == 0) {
466        return foundCells;
467      } else {
468        var sortedFoundCells = new Dictionary<int, IList<int>>();
469
470        var indicesToVirtual = new Dictionary<int, int>();
471        for (int i = 0; i < virtualRowIndices.Length; ++i) {
472          indicesToVirtual.Add(virtualRowIndices[i], i);
473        }
474
475        foreach (var entry in foundCells) {
476          var cells = new List<int>();
477          foreach (var cell in entry.Value) {
478            cells.Add(indicesToVirtual[cell]);
479          }
480          cells.Sort();
481          sortedFoundCells.Add(entry.Key, cells);
482        }
483        return sortedFoundCells;
484      }
485    }
486
487    private void Replace(IDictionary<int, IList<int>> cells) {
488      if (findAndReplaceDialog != null) {
489        ReplaceTransaction(() => {
490          switch (findAndReplaceDialog.GetReplaceAction()) {
491            case ReplaceAction.Value:
492              Content.ReplaceIndicesByString(cells, findAndReplaceDialog.GetReplaceText());
493              break;
494            case ReplaceAction.Average:
495              Content.ReplaceIndicesByMean(cells, false);
496              break;
497            case ReplaceAction.Median:
498              Content.ReplaceIndicesByMedianValue(cells, false);
499              break;
500            case ReplaceAction.Random:
501              Content.ReplaceIndicesByRandomValue(cells, false);
502              break;
503            case ReplaceAction.MostCommon:
504              Content.ReplaceIndicesByMode(cells, false);
505              break;
506            case ReplaceAction.Interpolation:
507              Content.ReplaceIndicesByLinearInterpolationOfNeighbours(cells);
508              break;
509          }
510        });
511      }
512    }
513
514    private IDictionary<int, IList<int>> TransformToDictionary(Tuple<int, int> tuple) {
515      var highlightCells = new Dictionary<int, IList<int>>();
516      highlightCells.Add(tuple.Item1, new List<int>() { tuple.Item2 });
517      return highlightCells;
518    }
519
520    private void ResetHighlightedCellsBackground() {
521      HightlightedCellsBackground = new Dictionary<int, IList<int>>();
522    }
523
524    #endregion FindAndReplaceDialog
525    private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
526      if (Content == null) return;
527      if (e.Button == MouseButtons.Right && !(e.ColumnIndex != -1 && e.RowIndex == -1)) {
528        if (e.ColumnIndex == -1 || e.RowIndex == -1) {
529          replaceValueOverColumnToolStripMenuItem.Visible = false;
530          contextMenuCell.Show(MousePosition);
531        } else {
532          if (!dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, e.RowIndex])) {
533            dataGridView.ClearSelection();
534            dataGridView[e.ColumnIndex, e.RowIndex].Selected = true;
535          }
536
537          var columnIndices = new HashSet<int>();
538          for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
539            columnIndices.Add(dataGridView.SelectedCells[i].ColumnIndex);
540          }
541
542          replaceValueOverSelectionToolStripMenuItem.Enabled = AreMultipleCellsSelected();
543
544          averageToolStripMenuItem_Column.Enabled =
545            averageToolStripMenuItem_Selection.Enabled =
546            medianToolStripMenuItem_Column.Enabled =
547            medianToolStripMenuItem_Selection.Enabled =
548            randomToolStripMenuItem_Column.Enabled =
549            randomToolStripMenuItem_Selection.Enabled = !Content.PreprocessingData.AreAllStringColumns(columnIndices);
550
551          replaceValueOverColumnToolStripMenuItem.Visible = true;
552          contextMenuCell.Show(MousePosition);
553        }
554      }
555    }
556
557    protected override void dataGridView_KeyDown(object sender, KeyEventArgs e) {
558      base.dataGridView_KeyDown(sender, e);
559      //data is in read only mode....
560      if (Content.PreprocessingData.IsFiltered) return;
561
562      if (e.KeyCode == Keys.Delete) {
563        //Delete column
564        if (dataGridView.SelectedColumns.Count > 0) {
565          var columnsToDelete = dataGridView.SelectedColumns.Cast<DataGridViewColumn>().OrderByDescending(col => col.Index).ToList();
566          foreach (var col in columnsToDelete)
567            Content.DeleteColumn(col.Index);
568        }
569        //Delete row
570        if (dataGridView.SelectedRows.Count > 0) {
571          //necessary if columns are sorted to translate the selected row index
572          var rowIndexes = dataGridView.SelectedRows.Cast<DataGridViewRow>().Select(row => GetRowIndex(row.Index)).ToList();
573          Content.DeleteRows(rowIndexes);
574        }
575      } else if (e.Control && e.KeyCode == Keys.F) {
576        CreateFindAndReplaceDialog();
577        findAndReplaceDialog.ActivateSearch();
578      } else if (e.Control && e.KeyCode == Keys.R) {
579        CreateFindAndReplaceDialog();
580        findAndReplaceDialog.ActivateReplace();
581      }
582    }
583
584    private IDictionary<int, IList<int>> GetSelectedCells() {
585      IDictionary<int, IList<int>> selectedCells = new Dictionary<int, IList<int>>();
586
587      //special case if all cells are selected
588      if (dataGridView.AreAllCellsSelected(true)) {
589        for (int i = 0; i < Content.Columns; i++)
590          selectedCells[i] = Enumerable.Range(0, Content.Rows).ToList();
591        return selectedCells;
592      }
593
594      foreach (DataGridViewCell cell in dataGridView.SelectedCells) {
595        if (!selectedCells.ContainsKey(cell.ColumnIndex))
596          selectedCells.Add(cell.ColumnIndex, new List<int>());
597        selectedCells[cell.ColumnIndex].Add(cell.RowIndex);
598      }
599
600      return selectedCells;
601    }
602
603    private void ReplaceTransaction(Action action) {
604      SuspendRepaint();
605      action();
606      ResumeRepaint(true);
607    }
608
609    private void btnSearch_Click(object sender, EventArgs e) {
610      CreateFindAndReplaceDialog();
611      findAndReplaceDialog.ActivateSearch();
612    }
613
614    private void btnReplace_Click(object sender, EventArgs e) {
615      CreateFindAndReplaceDialog();
616      findAndReplaceDialog.ActivateReplace();
617    }
618
619    #region ContextMenu Events
620    private void ReplaceWithAverage_Column_Click(object sender, EventArgs e) {
621      ReplaceTransaction(() => {
622        Content.ReplaceIndicesByMean(GetSelectedCells(), false);
623      });
624    }
625    private void ReplaceWithAverage_Selection_Click(object sender, EventArgs e) {
626      ReplaceTransaction(() => {
627        Content.ReplaceIndicesByMean(GetSelectedCells(), true);
628      });
629    }
630
631    private void ReplaceWithMedian_Column_Click(object sender, EventArgs e) {
632      ReplaceTransaction(() => {
633        Content.ReplaceIndicesByMedianValue(GetSelectedCells(), false);
634      });
635    }
636    private void ReplaceWithMedian_Selection_Click(object sender, EventArgs e) {
637      ReplaceTransaction(() => {
638        Content.ReplaceIndicesByMedianValue(GetSelectedCells(), true);
639      });
640    }
641
642    private void ReplaceWithRandom_Column_Click(object sender, EventArgs e) {
643      ReplaceTransaction(() => {
644        Content.ReplaceIndicesByRandomValue(GetSelectedCells(), false);
645      });
646    }
647    private void ReplaceWithRandom_Selection_Click(object sender, EventArgs e) {
648      ReplaceTransaction(() => {
649        Content.ReplaceIndicesByRandomValue(GetSelectedCells(), true);
650      });
651    }
652
653    private void ReplaceWithMostCommon_Column_Click(object sender, EventArgs e) {
654      ReplaceTransaction(() => {
655        Content.ReplaceIndicesByMode(GetSelectedCells(), false);
656      });
657    }
658    private void ReplaceWithMostCommon_Selection_Click(object sender, EventArgs e) {
659      ReplaceTransaction(() => {
660        Content.ReplaceIndicesByMode(GetSelectedCells(), true);
661      });
662    }
663
664    private void ReplaceWithInterpolation_Column_Click(object sender, EventArgs e) {
665      ReplaceTransaction(() => {
666        Content.ReplaceIndicesByLinearInterpolationOfNeighbours(GetSelectedCells());
667      });
668    }
669    #endregion
670
671    private void addRowButton_Click(object sender, EventArgs e) {
672      Content.PreprocessingData.InsertRow(Content.Rows);
673    }
674
675    private void addColumnButton_Click(object sender, EventArgs e) {
676      Content.PreprocessingData.InsertColumn<double>(Content.Columns.ToString(), Content.Columns);
677    }
678
679    private void renameColumnsButton_Click(object sender, EventArgs e) {
680      var renameDialog = new RenameColumnsDialog(Content.ColumnNames);
681
682      if (renameDialog.ShowDialog(this) == DialogResult.OK) {
683        Content.PreprocessingData.RenameColumns(renameDialog.ColumnNames);
684      }
685    }
686
687    private void checkInputsTargetButton_Click(object sender, EventArgs e) {
688      foreach (DataGridViewColumn column in DataGridView.Columns) {
689        var variable = column.HeaderText;
690        bool isInputTarget = Content.PreprocessingData.InputVariables.Contains(variable)
691          || Content.PreprocessingData.TargetVariable == variable;
692        column.Visible = isInputTarget;
693      }
694    }
695
696    private void checkAllButton_Click(object sender, EventArgs e) {
697      foreach (DataGridViewColumn column in DataGridView.Columns) {
698        column.Visible = true;
699      }
700    }
701
702    private void uncheckAllButton_Click(object sender, EventArgs e) {
703      foreach (DataGridViewColumn column in DataGridView.Columns) {
704        column.Visible = false;
705      }
706    }
707
708    private void shuffleAllButton_Click(object sender, EventArgs e) {
709      Content.Shuffle(shuffleWithinPartitionsCheckBox.Checked);
710    }
711  }
712}
Note: See TracBrowser for help on using the repository browser.