Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10998 was 10998, checked in by psteiner, 10 years ago

Formatting Sourcecode Views

File size: 25.0 KB
RevLine 
[10539]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
[10534]22using System;
23using System.Collections.Generic;
[10622]24using System.Drawing;
[10534]25using System.Linq;
[10236]26using System.Windows.Forms;
[10672]27using HeuristicLab.Data;
[10719]28using HeuristicLab.DataPreprocessing.Filter;
[10236]29using HeuristicLab.MainForm;
30
[10558]31namespace HeuristicLab.DataPreprocessing.Views {
[10236]32  [View("Data Grid Content View")]
[10311]33  [Content(typeof(IDataGridContent), true)]
[10950]34  public partial class DataGridContentView : PreprocessingStringConvertibleMatrixView {
[10236]35
[10583]36    private bool notOwnEvent = true;
[10870]37    private bool isSearching = false;
[10938]38    private bool isReplacing = false;
[10934]39    private bool updateOnMouseUp = false;
[10762]40    private SearchAndReplaceDialog findAndReplaceDialog;
[10698]41    private IFindPreprocessingItemsIterator searchIterator;
[10672]42    private string currentSearchText;
[10870]43    private ComparisonOperation currentComparisonOperation;
[10706]44    private Tuple<int, int> currentCell;
[10583]45
[10236]46    public new IDataGridContent Content {
47      get { return (IDataGridContent)base.Content; }
48      set { base.Content = value; }
49    }
50
[10574]51    private IList<int> _highlightedRowIndices;
[10623]52    public IList<int> HighlightedRowIndices {
[10585]53      get { return _highlightedRowIndices; }
54      set {
55        _highlightedRowIndices = value;
56        Refresh();
57      }
[10574]58    }
59
[10719]60    private IDictionary<int, IList<int>> _highlightedCellsBackground;
61    public IDictionary<int, IList<int>> HightlightedCellsBackground {
62      get { return _highlightedCellsBackground; }
63      set {
64        _highlightedCellsBackground = value;
65        Refresh();
66      }
67    }
68
[10236]69    public DataGridContentView() {
70      InitializeComponent();
[10380]71      dataGridView.CellMouseClick += dataGridView_CellMouseClick;
[10585]72      dataGridView.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(dataGridView_CellPainting);
[10668]73      dataGridView.KeyDown += dataGridView_KeyDown;
[10934]74      dataGridView.MouseUp += dataGridView_MouseUp;
[10947]75      dataGridView.ColumnHeaderMouseClick += dataGridView_ColumnHeaderMouseClick;
[10380]76      contextMenuCell.Items.Add(ShowHideColumns);
[10623]77      _highlightedRowIndices = new List<int>();
[10719]78      _highlightedCellsBackground = new Dictionary<int, IList<int>>();
[10706]79      currentCell = null;
[10236]80    }
81
[10965]82    protected override void OnContentChanged() {
83      List<KeyValuePair<int, SortOrder>> order = new List<KeyValuePair<int, SortOrder>>(base.sortedColumnIndices);
84      base.OnContentChanged();
85
86      DataGridView.RowHeadersWidth = 70;
87
88      if (Content == null && findAndReplaceDialog != null) {
89        findAndReplaceDialog.Close();
90      }
91
92      if (Content != null) {
93        base.sortedColumnIndices = order;
94        base.Sort();
95      }
96
97    }
98
99    protected override void RegisterContentEvents() {
100      base.RegisterContentEvents();
101      Content.Changed += Content_Changed;
102      Content.FilterLogic.FilterChanged += FilterLogic_FilterChanged;
103    }
104
105    protected override void DeregisterContentEvents() {
106      base.DeregisterContentEvents();
107      Content.Changed -= Content_Changed;
108      Content.FilterLogic.FilterChanged -= FilterLogic_FilterChanged;
109    }
110
111    private void FilterLogic_FilterChanged(object sender, EventArgs e) {
112      OnContentChanged();
[10947]113      searchIterator = null;
[10965]114      if (findAndReplaceDialog != null && !findAndReplaceDialog.IsDisposed) {
115        if (Content.FilterLogic.IsFiltered) {
116          findAndReplaceDialog.DisableReplace();
117        } else {
118          findAndReplaceDialog.EnableReplace();
119        }
120      }
121      btnReplace.Enabled = !Content.FilterLogic.IsFiltered;
[10947]122    }
[10965]123
124    private void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
125      if (notOwnEvent) {
126        switch (e.Type) {
127          case DataPreprocessingChangedEventType.ChangeColumn:
128          case DataPreprocessingChangedEventType.ChangeItem:
129            dataGridView.Refresh();
130            break;
131          default:
132            OnContentChanged();
133            break;
134        }
135      }
136      searchIterator = null;
137    }
138
[10964]139    #region methods which override/modify methods from PreprocessingStringConvertibleMatrixView
[10947]140
[10916]141    protected override void dataGridView_SelectionChanged(object sender, EventArgs e) {
[10993]142      base.dataGridView_SelectionChanged(sender, e);
143      Content.DataGridLogic.SetSelection(GetSelectedCells());
[10804]144    }
145
[10964]146    protected override void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
147      if (!dataGridView.ReadOnly) {
148        string errorMessage;
149        if (Content != null) {
150          if (dataGridView.IsCurrentCellInEditMode && Content.FilterLogic.IsFiltered) {
151            errorMessage = "A filter is active, you cannot modify data. Press ESC to exit edit mode.";
152          } else {
153            Content.DataGridLogic.Validate(e.FormattedValue.ToString(), out errorMessage, e.ColumnIndex);
154          }
[10934]155
[10964]156          if (!String.IsNullOrEmpty(errorMessage)) {
157            e.Cancel = true;
158            dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
159          }
160        }
161      }
[10947]162    }
[10934]163
[10964]164    protected override void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
165      triggersOwnEvent(() => base.dataGridView_CellParsing(sender, e));
166    }
167
168    protected override void PasteValuesToDataGridView() {
169      triggersOwnEvent(() => base.PasteValuesToDataGridView());
[10997]170      dataGridView.Refresh();
[10964]171    }
172
173    protected override void SetEnabledStateOfControls() {
174      base.SetEnabledStateOfControls();
175      rowsTextBox.ReadOnly = true;
176      columnsTextBox.ReadOnly = true;
177    }
178
179    protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
180      btnApplySort.Enabled = sortedColumns.Any();
181      return base.Sort(sortedColumns);
182    }
183
184    protected override void ClearSorting() {
185      btnApplySort.Enabled = false;
186      base.ClearSorting();
187    }
188
189    #endregion
190
[10965]191    private void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
[10930]192      searchIterator = null;
193    }
194
[10964]195    private void dataGridView_MouseUp(object sender, MouseEventArgs e) {
196      if (!updateOnMouseUp)
197        return;
[10900]198
[10964]199      updateOnMouseUp = false;
200      dataGridView_SelectionChanged(sender, e);
[10236]201    }
[10253]202
203    private void btnApplySort_Click(object sender, System.EventArgs e) {
[10585]204      triggersOwnEvent(() => {
[10809]205        Content.ManipulationLogic.ReOrderToIndices(virtualRowIndices);
[10585]206        OnContentChanged();
207      });
[10253]208    }
[10345]209
[10585]210    private void triggersOwnEvent(Action action) {
211      notOwnEvent = false;
212      action();
213      notOwnEvent = true;
214    }
215
[10698]216    #region FindAndReplaceDialog
[10585]217
[10636]218    private void CreateFindAndReplaceDialog() {
[10876]219      if (findAndReplaceDialog == null || findAndReplaceDialog.IsDisposed) {
220        findAndReplaceDialog = new SearchAndReplaceDialog();
221        findAndReplaceDialog.Show(this);
222        if (AreMultipleCellsSelected()) {
223          ResetHighlightedCellsBackground();
224          HightlightedCellsBackground = GetSelectedCells();
225          dataGridView.ClearSelection();
226        }
227        findAndReplaceDialog.FindAllEvent += findAndReplaceDialog_FindAllEvent;
228        findAndReplaceDialog.FindNextEvent += findAndReplaceDialog_FindNextEvent;
229        findAndReplaceDialog.ReplaceAllEvent += findAndReplaceDialog_ReplaceAllEvent;
230        findAndReplaceDialog.ReplaceNextEvent += findAndReplaceDialog_ReplaceEvent;
231        findAndReplaceDialog.FormClosing += findAndReplaceDialog_FormClosing;
232        searchIterator = null;
233        DataGridView.SelectionChanged += DataGridView_SelectionChanged_FindAndReplace;
[10930]234        if (Content.FilterLogic.IsFiltered) {
235          findAndReplaceDialog.DisableReplace();
236        }
[10852]237      }
[10636]238    }
239
[10916]240    private void DataGridView_SelectionChanged_FindAndReplace(object sender, EventArgs e) {
[10863]241      if (Content != null) {
[10870]242        if (!isSearching && AreMultipleCellsSelected()) {
[10863]243          ResetHighlightedCellsBackground();
244          HightlightedCellsBackground = GetSelectedCells();
245          searchIterator = null;
246        }
247      }
248    }
249
[10705]250    void findAndReplaceDialog_FormClosing(object sender, FormClosingEventArgs e) {
[10719]251      ResetHighlightedCellsBackground();
[10852]252      searchIterator = null;
[10863]253      DataGridView.SelectionChanged -= DataGridView_SelectionChanged_FindAndReplace;
[10705]254    }
255
[10672]256    void findAndReplaceDialog_ReplaceEvent(object sender, EventArgs e) {
[10712]257      if (searchIterator != null && searchIterator.GetCurrent() != null) {
[10706]258        Replace(TransformToDictionary(currentCell));
[10672]259      }
[10636]260    }
261
262    void findAndReplaceDialog_ReplaceAllEvent(object sender, EventArgs e) {
[10672]263      Replace(FindAll(findAndReplaceDialog.GetSearchText()));
[10636]264    }
265
266    void findAndReplaceDialog_FindNextEvent(object sender, EventArgs e) {
[10947]267      if (searchIterator == null
268        || currentSearchText != findAndReplaceDialog.GetSearchText()
269        || currentComparisonOperation != findAndReplaceDialog.GetComparisonOperation()) {
270
[10698]271        searchIterator = new FindPreprocessingItemsIterator(FindAll(findAndReplaceDialog.GetSearchText()));
272        currentSearchText = findAndReplaceDialog.GetSearchText();
[10870]273        currentComparisonOperation = findAndReplaceDialog.GetComparisonOperation();
[10698]274      }
[10870]275
[10852]276      if (IsOneCellSelected()) {
277        var first = GetSelectedCells().First();
278        searchIterator.SetStartCell(first.Key, first.Value[0]);
279      }
[10712]280
[10705]281      bool moreOccurences = false;
[10870]282      currentCell = searchIterator.GetCurrent();
283      moreOccurences = searchIterator.MoveNext();
284      if (IsOneCellSelected() && currentCell != null) {
285        var first = GetSelectedCells().First();
286        if (currentCell.Item1 == first.Key && currentCell.Item2 == first.Value[0]) {
287          if (!moreOccurences) {
288            searchIterator.Reset();
[10852]289          }
[10870]290          currentCell = searchIterator.GetCurrent();
291          moreOccurences = searchIterator.MoveNext();
292          if (!moreOccurences) {
293            searchIterator.Reset();
294          }
[10852]295        }
[10870]296      }
[10672]297
[10870]298      dataGridView.ClearSelection();
299
[10705]300      if (currentCell != null) {
[10852]301        dataGridView[currentCell.Item1, currentCell.Item2].Selected = true;
[10946]302        dataGridView.CurrentCell = dataGridView[currentCell.Item1, currentCell.Item2];
[10698]303      }
[10852]304    }
305
306    private bool AreMultipleCellsSelected() {
307      return GetSelectedCellCount() > 1;
308    }
309
310    private bool IsOneCellSelected() {
311      return GetSelectedCellCount() == 1;
312    }
313
314    private int GetSelectedCellCount() {
315      int count = 0;
316      foreach (var column in GetSelectedCells()) {
317        count += column.Value.Count();
[10706]318      }
[10852]319      return count;
[10636]320    }
321
322    void findAndReplaceDialog_FindAllEvent(object sender, EventArgs e) {
[10870]323      dataGridView.ClearSelection();
324      isSearching = true;
[10901]325      SuspendRepaint();
[10916]326      var selectedCells = FindAll(findAndReplaceDialog.GetSearchText());
327      foreach (var column in selectedCells) {
[10870]328        foreach (var cell in column.Value) {
329          dataGridView[column.Key, cell].Selected = true;
330        }
331      }
[10901]332      ResumeRepaint(true);
[10870]333      isSearching = false;
[10916]334      Content.DataGridLogic.SetSelection(selectedCells);
335      //update statistic in base
336      base.dataGridView_SelectionChanged(sender, e);
[10636]337    }
338
[10870]339    private Core.ConstraintOperation GetConstraintOperation(ComparisonOperation comparisonOperation) {
340      Core.ConstraintOperation constraintOperation = Core.ConstraintOperation.Equal;
341      switch (comparisonOperation) {
342        case ComparisonOperation.Equal:
343          constraintOperation = Core.ConstraintOperation.Equal;
344          break;
345        case ComparisonOperation.Greater:
346          constraintOperation = Core.ConstraintOperation.Greater;
347          break;
348        case ComparisonOperation.GreaterOrEqual:
349          constraintOperation = Core.ConstraintOperation.GreaterOrEqual;
350          break;
351        case ComparisonOperation.Less:
352          constraintOperation = Core.ConstraintOperation.Less;
353          break;
354        case ComparisonOperation.LessOrEqual:
355          constraintOperation = Core.ConstraintOperation.LessOrEqual;
356          break;
357        case ComparisonOperation.NotEqual:
358          constraintOperation = Core.ConstraintOperation.NotEqual;
359          break;
360      }
361      return constraintOperation;
362    }
363
[10672]364    private IDictionary<int, IList<int>> FindAll(string match) {
[10719]365      bool searchInSelection = HightlightedCellsBackground.Values.Sum(list => list.Count) > 1;
[10870]366      ComparisonOperation comparisonOperation = findAndReplaceDialog.GetComparisonOperation();
[10672]367      var foundCells = new Dictionary<int, IList<int>>();
368      for (int i = 0; i < Content.FilterLogic.PreprocessingData.Columns; i++) {
[10947]369        var filters = CreateFilters(match, comparisonOperation, i);
370
[10844]371        bool[] filteredRows = Content.FilterLogic.GetFilterResult(filters, true);
372        var foundIndices = new List<int>();
373        for (int idx = 0; idx < filteredRows.Length; ++idx) {
374          var notFilteredThusFound = !filteredRows[idx];
375          if (notFilteredThusFound) {
376            foundIndices.Add(idx);
377          }
378        }
379        foundCells[i] = foundIndices;
[10719]380        IList<int> selectedList;
381        if (searchInSelection && HightlightedCellsBackground.TryGetValue(i, out selectedList)) {
[10739]382          foundCells[i] = foundCells[i].Intersect(selectedList).ToList<int>();
383        } else if (searchInSelection) {
[10719]384          foundCells[i].Clear();
385        }
[10672]386      }
[10947]387      return MapToSorting(foundCells);
[10672]388    }
389
[10947]390    private List<IFilter> CreateFilters(string match, ComparisonOperation comparisonOperation, int columnIndex) {
391      IPreprocessingData preprocessingData = Content.FilterLogic.PreprocessingData;
392      IStringConvertibleValue value;
393      if (preprocessingData.IsType<double>(columnIndex)) {
394        value = new DoubleValue();
395      } else if (preprocessingData.IsType<String>(columnIndex)) {
396        value = new StringValue();
397      } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
398        value = new DateTimeValue();
399      } else {
400        throw new ArgumentException("unsupported type");
401      }
402      value.SetValue(match);
403      var comparisonFilter = new ComparisonFilter(preprocessingData, GetConstraintOperation(comparisonOperation), value, true);
404      comparisonFilter.ConstraintColumn = columnIndex;
405      return new List<Filter.IFilter>() { comparisonFilter };
406    }
407
408    private IDictionary<int, IList<int>> MapToSorting(Dictionary<int, IList<int>> foundCells) {
409      if (sortedColumnIndices.Count == 0) {
410        return foundCells;
411      } else {
412        var sortedFoundCells = new Dictionary<int, IList<int>>();
413
414        var indicesToVirtual = new Dictionary<int, int>();
415        for (int i = 0; i < virtualRowIndices.Length; ++i) {
416          indicesToVirtual.Add(virtualRowIndices[i], i);
417        }
418
419        foreach (var entry in foundCells) {
420          var cells = new List<int>();
421          foreach (var cell in entry.Value) {
422            cells.Add(indicesToVirtual[cell]);
423          }
424          cells.Sort();
425          sortedFoundCells.Add(entry.Key, cells);
426        }
427        return sortedFoundCells;
428      }
429    }
430
[10672]431    private void Replace(IDictionary<int, IList<int>> cells) {
432      if (findAndReplaceDialog != null) {
[10938]433        ReplaceTransaction(() => {
434          switch (findAndReplaceDialog.GetReplaceAction()) {
435            case ReplaceAction.Value:
436              Content.ManipulationLogic.ReplaceIndicesByValue(cells, findAndReplaceDialog.GetReplaceText());
437              break;
438            case ReplaceAction.Average:
439              Content.ManipulationLogic.ReplaceIndicesByAverageValue(cells, false);
440              break;
441            case ReplaceAction.Median:
442              Content.ManipulationLogic.ReplaceIndicesByMedianValue(cells, false);
443              break;
444            case ReplaceAction.Random:
445              Content.ManipulationLogic.ReplaceIndicesByRandomValue(cells, false);
446              break;
447            case ReplaceAction.MostCommon:
448              Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(cells, false);
449              break;
450            case ReplaceAction.Interpolation:
451              Content.ManipulationLogic.ReplaceIndicesByLinearInterpolationOfNeighbours(cells);
452              break;
453          }
454        });
[10672]455      }
456    }
457
[10698]458    private IDictionary<int, IList<int>> TransformToDictionary(Tuple<int, int> tuple) {
[10672]459      var highlightCells = new Dictionary<int, IList<int>>();
[10698]460      highlightCells.Add(tuple.Item1, new List<int>() { tuple.Item2 });
[10672]461      return highlightCells;
462    }
463
[10719]464    private void ResetHighlightedCellsBackground() {
465      HightlightedCellsBackground = new Dictionary<int, IList<int>>();
466    }
467
[10698]468    #endregion FindAndReplaceDialog
469
[10380]470    private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
471      if (Content == null) return;
472      if (e.Button == System.Windows.Forms.MouseButtons.Right) {
473        if (e.ColumnIndex == -1 || e.RowIndex == -1) {
[10769]474          replaceValueOverColumnToolStripMenuItem.Visible = false;
[10627]475          contextMenuCell.Show(MousePosition);
[10380]476        } else {
[10590]477          if (!dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, e.RowIndex])) {
478            dataGridView.ClearSelection();
479            dataGridView[e.ColumnIndex, e.RowIndex].Selected = true;
480          }
[10812]481
[10621]482          var columnIndices = new HashSet<int>();
483          for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
484            columnIndices.Add(dataGridView.SelectedCells[i].ColumnIndex);
485          }
[10875]486
487          replaceValueOverSelectionToolStripMenuItem.Enabled = AreMultipleCellsSelected();
488
[10812]489          averageToolStripMenuItem_Column.Enabled =
490            averageToolStripMenuItem_Selection.Enabled =
491            medianToolStripMenuItem_Column.Enabled =
492            medianToolStripMenuItem_Selection.Enabled =
493            randomToolStripMenuItem_Column.Enabled =
494            randomToolStripMenuItem_Selection.Enabled = !Content.DataGridLogic.AreAllStringColumns(columnIndices);
495
496          smoothingToolStripMenuItem_Column.Enabled =
[10820]497            interpolationToolStripMenuItem_Column.Enabled = !dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, 0])
498            && !dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, Content.Rows - 1])
[10812]499            && !Content.DataGridLogic.AreAllStringColumns(columnIndices);
500
[10769]501          replaceValueOverColumnToolStripMenuItem.Visible = true;
[10380]502          contextMenuCell.Show(MousePosition);
503        }
504      }
505    }
506
[10585]507    protected void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
508      if (Content == null) return;
509      if (e.RowIndex < 0) return;
510      if (e.ColumnIndex < 0) return;
511      if (e.State.HasFlag(DataGridViewElementStates.Selected)) return;
512      if (!e.PaintParts.HasFlag(DataGridViewPaintParts.Background)) return;
[10870]513      if (HighlightedRowIndices == null) return;
[10574]514
[10585]515      int rowIndex = virtualRowIndices[e.RowIndex];
[10574]516
[10585]517      Color backColor = e.CellStyle.BackColor;
[10574]518
[10719]519      if (HightlightedCellsBackground.ContainsKey(e.ColumnIndex) && HightlightedCellsBackground[e.ColumnIndex].Contains(e.RowIndex)) {
520        backColor = Color.LightGray;
521      }
[10574]522
[10585]523      using (Brush backColorBrush = new SolidBrush(backColor)) {
524        Rectangle bounds = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height);
525        e.Graphics.FillRectangle(backColorBrush, bounds);
526      }
[10712]527
[10585]528      using (Brush gridBrush = new SolidBrush(Color.LightGray)) {
529        Pen gridLinePen = new Pen(gridBrush);
530        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
531               e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
532               e.CellBounds.Bottom - 1);
533        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
534            e.CellBounds.Top, e.CellBounds.Right - 1,
535            e.CellBounds.Bottom);
536      }
537      e.PaintContent(e.CellBounds);
538      e.Handled = true;
[10574]539    }
540
[10964]541    private void dataGridView_KeyDown(object sender, KeyEventArgs e) {
[10668]542      var selectedRows = dataGridView.SelectedRows;
543      if (e.KeyCode == Keys.Delete && selectedRows.Count > 0) {
544        List<int> rows = new List<int>();
545        for (int i = 0; i < selectedRows.Count; ++i) {
546          rows.Add(selectedRows[i].Index);
547        }
548        triggersOwnEvent(() => {
549          Content.DataGridLogic.DeleteRow(rows);
550          OnContentChanged();
551        });
552      } else if (e.Control && e.KeyCode == Keys.F) {
553        CreateFindAndReplaceDialog();
554        findAndReplaceDialog.ActivateSearch();
555      } else if (e.Control && e.KeyCode == Keys.R) {
556        CreateFindAndReplaceDialog();
557        findAndReplaceDialog.ActivateReplace();
558      }
559    }
560
[10672]561    private IDictionary<int, IList<int>> GetSelectedCells() {
562      IDictionary<int, IList<int>> selectedCells = new Dictionary<int, IList<int>>();
[10993]563
564      //special case if all cells are selected
565      if (dataGridView.AreAllCellsSelected(true)) {
566        for (int i = 0; i < Content.Columns; i++)
567          selectedCells[i] = Enumerable.Range(0, Content.Rows).ToList();
568        return selectedCells;
[10590]569      }
[10993]570
571      foreach (var selectedCell in dataGridView.SelectedCells) {
572        var cell = (DataGridViewCell) selectedCell;
573        if (!selectedCells.ContainsKey(cell.ColumnIndex))
574          selectedCells.Add(cell.ColumnIndex, new List<int>(1024));
575        selectedCells[cell.ColumnIndex].Add(cell.RowIndex);
576      }
577
[10590]578      return selectedCells;
579    }
580
[10938]581    private void StartReplacing() {
582      isReplacing = true;
583      SuspendRepaint();
584    }
585
586    private void StopReplacing() {
587      isReplacing = false;
[10947]588      ResumeRepaint(true);
[10938]589    }
590
591    private void ReplaceTransaction(Action action) {
592      StartReplacing();
593      action();
594      StopReplacing();
595    }
596
[10964]597    private void btnSearch_Click(object sender, EventArgs e) {
598      CreateFindAndReplaceDialog();
599      findAndReplaceDialog.ActivateSearch();
600    }
601
602    private void btnReplace_Click(object sender, EventArgs e) {
603      CreateFindAndReplaceDialog();
604      findAndReplaceDialog.ActivateReplace();
605    }
606
607    #region ContextMenu Events
608
[10769]609    private void ReplaceWithAverage_Column_Click(object sender, EventArgs e) {
[10938]610      ReplaceTransaction(() => {
611        Content.ManipulationLogic.ReplaceIndicesByAverageValue(GetSelectedCells(), false);
612      });
[10380]613    }
[10809]614    private void ReplaceWithAverage_Selection_Click(object sender, EventArgs e) {
[10938]615      ReplaceTransaction(() => {
616        Content.ManipulationLogic.ReplaceIndicesByAverageValue(GetSelectedCells(), true);
617      });
[10809]618    }
[10380]619
[10769]620    private void ReplaceWithMedian_Column_Click(object sender, EventArgs e) {
[10938]621      ReplaceTransaction(() => {
622        Content.ManipulationLogic.ReplaceIndicesByMedianValue(GetSelectedCells(), false);
623      });
[10380]624    }
[10809]625    private void ReplaceWithMedian_Selection_Click(object sender, EventArgs e) {
[10938]626      ReplaceTransaction(() => {
627        Content.ManipulationLogic.ReplaceIndicesByMedianValue(GetSelectedCells(), true);
628      });
[10809]629    }
[10380]630
[10769]631    private void ReplaceWithRandom_Column_Click(object sender, EventArgs e) {
[10938]632      ReplaceTransaction(() => {
633        Content.ManipulationLogic.ReplaceIndicesByRandomValue(GetSelectedCells(), false);
634      });
[10380]635    }
[10809]636    private void ReplaceWithRandom_Selection_Click(object sender, EventArgs e) {
[10938]637      ReplaceTransaction(() => {
638        Content.ManipulationLogic.ReplaceIndicesByRandomValue(GetSelectedCells(), true);
639      });
[10809]640    }
[10380]641
[10769]642    private void ReplaceWithMostCommon_Column_Click(object sender, EventArgs e) {
[10938]643      ReplaceTransaction(() => {
644        Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(GetSelectedCells(), false);
645      });
[10380]646    }
[10809]647    private void ReplaceWithMostCommon_Selection_Click(object sender, EventArgs e) {
[10938]648      ReplaceTransaction(() => {
649        Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(GetSelectedCells(), true);
650      });
[10809]651    }
[10380]652
[10769]653    private void ReplaceWithInterpolation_Column_Click(object sender, EventArgs e) {
[10938]654      ReplaceTransaction(() => {
655        Content.ManipulationLogic.ReplaceIndicesByLinearInterpolationOfNeighbours(GetSelectedCells());
656      });
[10380]657    }
[10769]658
659    private void ReplaceWithSmoothing_Selection_Click(object sender, EventArgs e) {
[10938]660      ReplaceTransaction(() => {
661        Content.ManipulationLogic.ReplaceIndicesBySmoothing(GetSelectedCells());
662      });
[10769]663    }
[10964]664    #endregion
[10769]665
[10236]666  }
667}
Note: See TracBrowser for help on using the repository browser.