Free cookie consent management tool by TermsFeed Policy Generator

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

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