Free cookie consent management tool by TermsFeed Policy Generator

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

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