Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10719 was 10719, checked in by sbreuer, 10 years ago
  • highlight selected cells when search an replace opens
File size: 15.8 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 FindAndReplaceDialog findAndReplaceDialog;
38    private IFindPreprocessingItemsIterator searchIterator;
39    private string currentSearchText;
40    private Tuple<int, int> currentCell;
41
42    public new IDataGridContent Content {
43      get { return (IDataGridContent)base.Content; }
44      set { base.Content = value; }
45    }
46
47    private IList<int> _highlightedRowIndices;
48    public IList<int> HighlightedRowIndices {
49      get { return _highlightedRowIndices; }
50      set {
51        _highlightedRowIndices = value;
52        Refresh();
53      }
54    }
55
56    private IDictionary<int, IList<int>> _highlightedCells;
57    public IDictionary<int, IList<int>> HightlightedCells {
58      get { return _highlightedCells; }
59      set {
60        _highlightedCells = value;
61        Refresh();
62      }
63    }
64
65    private IDictionary<int, IList<int>> _highlightedCellsBackground;
66    public IDictionary<int, IList<int>> HightlightedCellsBackground {
67      get { return _highlightedCellsBackground; }
68      set {
69        _highlightedCellsBackground = value;
70        Refresh();
71      }
72    }
73
74    public DataGridContentView() {
75      InitializeComponent();
76      dataGridView.CellMouseClick += dataGridView_CellMouseClick;
77      dataGridView.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(dataGridView_CellPainting);
78      dataGridView.KeyDown += dataGridView_KeyDown;
79      contextMenuCell.Items.Add(ShowHideColumns);
80      _highlightedCells = new Dictionary<int, IList<int>>();
81      _highlightedRowIndices = new List<int>();
82      _highlightedCellsBackground = new Dictionary<int, IList<int>>();
83      currentCell = null;
84    }
85
86    protected override void OnContentChanged() {
87      base.OnContentChanged();
88    }
89
90    protected override void RegisterContentEvents() {
91      base.RegisterContentEvents();
92      Content.Changed += Content_Changed;
93    }
94
95    protected override void DeregisterContentEvents() {
96      base.DeregisterContentEvents();
97      Content.Changed -= Content_Changed;
98    }
99
100    void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
101      if (notOwnEvent) {
102        switch (e.Type) {
103          case DataPreprocessingChangedEventType.AddColumn:
104          case DataPreprocessingChangedEventType.AddRow:
105          case DataPreprocessingChangedEventType.DeleteColumn:
106          case DataPreprocessingChangedEventType.DeleteRow:
107          case DataPreprocessingChangedEventType.Any:
108            OnContentChanged();
109            break;
110          case DataPreprocessingChangedEventType.ChangeColumn:
111          case DataPreprocessingChangedEventType.ChangeItem:
112            dataGridView.Refresh();
113            break;
114        }
115      }
116    }
117
118    protected override void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
119      if (!dataGridView.ReadOnly) {
120        string errorMessage;
121        if (Content != null && !Content.DataGridLogic.Validate(e.FormattedValue.ToString(), out errorMessage, e.ColumnIndex)) {
122          e.Cancel = true;
123          dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
124        }
125      }
126    }
127
128    protected override void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
129      triggersOwnEvent(() => base.dataGridView_CellParsing(sender, e));
130    }
131
132    protected override void PasteValuesToDataGridView() {
133      triggersOwnEvent(() => base.PasteValuesToDataGridView());
134    }
135
136    protected override void SetEnabledStateOfControls() {
137      base.SetEnabledStateOfControls();
138      rowsTextBox.ReadOnly = true;
139      columnsTextBox.ReadOnly = true;
140    }
141
142    private void btnApplySort_Click(object sender, System.EventArgs e) {
143      triggersOwnEvent(() => {
144        Content.PreprocessingDataManipulation.ReOrderToIndices(virtualRowIndices);
145        OnContentChanged();
146      });
147    }
148
149    private void triggersOwnEvent(Action action) {
150      notOwnEvent = false;
151      action();
152      notOwnEvent = true;
153    }
154
155    #region FindAndReplaceDialog
156
157    private void CreateFindAndReplaceDialog() {
158      findAndReplaceDialog = new FindAndReplaceDialog();
159      findAndReplaceDialog.Show(this);
160      HightlightedCellsBackground = GetSelectedCells();
161      dataGridView.ClearSelection();
162      findAndReplaceDialog.FindAllEvent += findAndReplaceDialog_FindAllEvent;
163      findAndReplaceDialog.FindNextEvent += findAndReplaceDialog_FindNextEvent;
164      findAndReplaceDialog.ReplaceAllEvent += findAndReplaceDialog_ReplaceAllEvent;
165      findAndReplaceDialog.ReplaceNextEvent += findAndReplaceDialog_ReplaceEvent;
166      findAndReplaceDialog.FormClosing += findAndReplaceDialog_FormClosing;
167    }
168
169    void findAndReplaceDialog_FormClosing(object sender, FormClosingEventArgs e) {
170      ResetHighlightedCells();
171      ResetHighlightedCellsBackground();
172    }
173
174    void findAndReplaceDialog_ReplaceEvent(object sender, EventArgs e) {
175      if (searchIterator != null && searchIterator.GetCurrent() != null) {
176        Replace(TransformToDictionary(currentCell));
177      }
178    }
179
180    void findAndReplaceDialog_ReplaceAllEvent(object sender, EventArgs e) {
181      Replace(FindAll(findAndReplaceDialog.GetSearchText()));
182    }
183
184    void findAndReplaceDialog_FindNextEvent(object sender, EventArgs e) {
185      if (searchIterator == null || currentSearchText != findAndReplaceDialog.GetSearchText()) {
186        searchIterator = new FindPreprocessingItemsIterator(FindAll(findAndReplaceDialog.GetSearchText()));
187        currentSearchText = findAndReplaceDialog.GetSearchText();
188      }
189
190      bool moreOccurences = false;
191      do {
192        currentCell = searchIterator.GetCurrent();
193        moreOccurences = searchIterator.MoveNext();
194      } while (moreOccurences && (currentCell == null || !Content.GetValue(currentCell.Item2, currentCell.Item1).Equals(currentSearchText)));
195
196      if (currentCell != null) {
197        HightlightedCells = TransformToDictionary(currentCell);
198      } else {
199        ResetHighlightedCells();
200      }
201
202      if (!moreOccurences) {
203        searchIterator.Reset();
204      }
205    }
206
207    void findAndReplaceDialog_FindAllEvent(object sender, EventArgs e) {
208      HightlightedCells = FindAll(findAndReplaceDialog.GetSearchText());
209      searchIterator.Reset();
210    }
211
212    private IDictionary<int, IList<int>> FindAll(string match) {
213      bool searchInSelection = HightlightedCellsBackground.Values.Sum(list => list.Count) > 1;
214      var comparisonFilter = new ComparisonFilter(Content.FilterLogic.PreprocessingData, Core.ConstraintOperation.Equal, new StringValue(match), true);
215      var filters = new List<Filter.IFilter>() { comparisonFilter };
216      var foundCells = new Dictionary<int, IList<int>>();
217      for (int i = 0; i < Content.FilterLogic.PreprocessingData.Columns; i++) {
218        comparisonFilter.ConstraintColumn = i;
219        bool[] filteredRows = Content.FilterLogic.Preview(filters);
220        foundCells[i] = filteredRows.Select((value, index) => new { Index = index, Value = value })
221          .Where(pair => pair.Value)
222          .Select(pair => pair.Index)
223          .ToList();
224        IList<int> selectedList;
225        if (searchInSelection && HightlightedCellsBackground.TryGetValue(i, out selectedList)) {
226          foundCells[i].Intersect(selectedList);
227        } else {
228          foundCells[i].Clear();
229        }
230      }
231      return foundCells;
232    }
233
234    private void Replace(IDictionary<int, IList<int>> cells) {
235      if (findAndReplaceDialog != null) {
236        switch (findAndReplaceDialog.GetReplaceAction()) {
237          case ReplaceAction.Value:
238            Content.PreprocessingDataManipulation.ReplaceIndicesByValue(cells, findAndReplaceDialog.GetReplaceText());
239            break;
240          case ReplaceAction.Average:
241            Content.PreprocessingDataManipulation.ReplaceIndicesByAverageValue(cells);
242            break;
243          case ReplaceAction.Median:
244            Content.PreprocessingDataManipulation.ReplaceIndicesByMedianValue(cells);
245            break;
246          case ReplaceAction.Random:
247            Content.PreprocessingDataManipulation.ReplaceIndicesByRandomValue(cells);
248            break;
249          case ReplaceAction.MostCommon:
250            Content.PreprocessingDataManipulation.ReplaceIndicesByMostCommonValue(cells);
251            break;
252          case ReplaceAction.Interpolation:
253            Content.PreprocessingDataManipulation.ReplaceIndicesByLinearInterpolationOfNeighbours(cells);
254            break;
255        }
256      }
257    }
258
259    private IDictionary<int, IList<int>> TransformToDictionary(Tuple<int, int> tuple) {
260      var highlightCells = new Dictionary<int, IList<int>>();
261      highlightCells.Add(tuple.Item1, new List<int>() { tuple.Item2 });
262      return highlightCells;
263    }
264
265    private void ResetHighlightedCells() {
266      HightlightedCells = new Dictionary<int, IList<int>>();
267    }
268
269    private void ResetHighlightedCellsBackground() {
270      HightlightedCellsBackground = new Dictionary<int, IList<int>>();
271    }
272
273    #endregion FindAndReplaceDialog
274
275    private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
276      if (Content == null) return;
277      if (e.Button == System.Windows.Forms.MouseButtons.Right) {
278        if (e.ColumnIndex == -1 || e.RowIndex == -1) {
279          replaceValueToolStripMenuItem.Visible = false;
280          contextMenuCell.Show(MousePosition);
281        } else {
282          if (!dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, e.RowIndex])) {
283            dataGridView.ClearSelection();
284            dataGridView[e.ColumnIndex, e.RowIndex].Selected = true;
285          }
286          interpolationToolStripMenuItem.Enabled = !(e.RowIndex == 0 || e.RowIndex == Content.Rows);
287          var columnIndices = new HashSet<int>();
288          for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
289            columnIndices.Add(dataGridView.SelectedCells[i].ColumnIndex);
290          }
291          averageToolStripMenuItem.Enabled = medianToolStripMenuItem.Enabled = randomToolStripMenuItem.Enabled = !Content.DataGridLogic.AreAllStringColumns(columnIndices);
292          interpolationToolStripMenuItem.Enabled = interpolationToolStripMenuItem.Enabled && !Content.DataGridLogic.AreAllStringColumns(columnIndices);
293          replaceValueToolStripMenuItem.Visible = true;
294          contextMenuCell.Show(MousePosition);
295        }
296      }
297    }
298
299    protected void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
300      if (Content == null) return;
301      if (e.RowIndex < 0) return;
302      if (e.ColumnIndex < 0) return;
303      if (e.State.HasFlag(DataGridViewElementStates.Selected)) return;
304      if (!e.PaintParts.HasFlag(DataGridViewPaintParts.Background)) return;
305      if (HighlightedRowIndices == null && HightlightedCells == null) return;
306
307      int rowIndex = virtualRowIndices[e.RowIndex];
308
309      Color backColor = e.CellStyle.BackColor;
310
311      if (HightlightedCellsBackground.ContainsKey(e.ColumnIndex) && HightlightedCellsBackground[e.ColumnIndex].Contains(e.RowIndex)) {
312        backColor = Color.LightGray;
313      }
314      if (HighlightedRowIndices.Contains(rowIndex) || HightlightedCells.ContainsKey(e.ColumnIndex) && HightlightedCells[e.ColumnIndex].Contains(e.RowIndex)) {
315        backColor = Color.LightGreen;
316      }
317
318      using (Brush backColorBrush = new SolidBrush(backColor)) {
319        Rectangle bounds = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height);
320        e.Graphics.FillRectangle(backColorBrush, bounds);
321      }
322
323      using (Brush gridBrush = new SolidBrush(Color.LightGray)) {
324        Pen gridLinePen = new Pen(gridBrush);
325        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
326               e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
327               e.CellBounds.Bottom - 1);
328        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
329            e.CellBounds.Top, e.CellBounds.Right - 1,
330            e.CellBounds.Bottom);
331      }
332
333      e.PaintContent(e.CellBounds);
334      e.Handled = true;
335    }
336
337    void dataGridView_KeyDown(object sender, KeyEventArgs e) {
338      var selectedRows = dataGridView.SelectedRows;
339      if (e.KeyCode == Keys.Delete && selectedRows.Count > 0) {
340        List<int> rows = new List<int>();
341        for (int i = 0; i < selectedRows.Count; ++i) {
342          rows.Add(selectedRows[i].Index);
343        }
344        triggersOwnEvent(() => {
345          Content.DataGridLogic.DeleteRow(rows);
346          OnContentChanged();
347        });
348      } else if (e.Control && e.KeyCode == Keys.F) {
349        CreateFindAndReplaceDialog();
350        findAndReplaceDialog.ActivateSearch();
351      } else if (e.Control && e.KeyCode == Keys.R) {
352        CreateFindAndReplaceDialog();
353        findAndReplaceDialog.ActivateReplace();
354      }
355    }
356
357    protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
358      btnApplySort.Enabled = sortedColumns.Any();
359      return base.Sort(sortedColumns);
360    }
361
362    protected override void ClearSorting() {
363      btnApplySort.Enabled = false;
364      base.ClearSorting();
365    }
366
367    private IDictionary<int, IList<int>> GetSelectedCells() {
368      IDictionary<int, IList<int>> selectedCells = new Dictionary<int, IList<int>>();
369      for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
370        var columnIndex = dataGridView.SelectedCells[i].ColumnIndex;
371        if (!selectedCells.ContainsKey(columnIndex)) {
372          selectedCells.Add(columnIndex, new List<int>());
373        }
374        selectedCells[columnIndex].Add(dataGridView.SelectedCells[i].RowIndex);
375      }
376      return selectedCells;
377    }
378
379    private void ReplaceWithAverage_Click(object sender, EventArgs e) {
380      Content.PreprocessingDataManipulation.ReplaceIndicesByAverageValue(GetSelectedCells());
381    }
382
383    private void ReplaceWithMedian_Click(object sender, EventArgs e) {
384      Content.PreprocessingDataManipulation.ReplaceIndicesByMedianValue(GetSelectedCells());
385    }
386
387    private void ReplaceWithRandom_Click(object sender, EventArgs e) {
388      Content.PreprocessingDataManipulation.ReplaceIndicesByRandomValue(GetSelectedCells());
389    }
390
391    private void ReplaceWithMostCommon_Click(object sender, EventArgs e) {
392      Content.PreprocessingDataManipulation.ReplaceIndicesByMostCommonValue(GetSelectedCells());
393    }
394
395    private void ReplaceWithInterpolation_Click(object sender, EventArgs e) {
396      Content.PreprocessingDataManipulation.ReplaceIndicesByLinearInterpolationOfNeighbours(GetSelectedCells());
397    }
398  }
399}
Note: See TracBrowser for help on using the repository browser.