Free cookie consent management tool by TermsFeed Policy Generator

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

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