Free cookie consent management tool by TermsFeed Policy Generator

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

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