Free cookie consent management tool by TermsFeed Policy Generator

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

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