1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Windows.Forms;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Data.Views;
|
---|
28 | using HeuristicLab.DataPreprocessing.Filter;
|
---|
29 | using HeuristicLab.MainForm;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.DataPreprocessing.Views {
|
---|
32 | [View("Data Grid Content View")]
|
---|
33 | [Content(typeof(IDataGridContent), true)]
|
---|
34 | public partial class DataGridContentView : StringConvertibleMatrixView {
|
---|
35 |
|
---|
36 | private bool isSearching = false;
|
---|
37 | private bool updateOnMouseUp = 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 IDictionary<int, IList<int>> _highlightedCellsBackground;
|
---|
50 | public IDictionary<int, IList<int>> HightlightedCellsBackground {
|
---|
51 | get { return _highlightedCellsBackground; }
|
---|
52 | set {
|
---|
53 | _highlightedCellsBackground = value;
|
---|
54 | Refresh();
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | public DataGridContentView() {
|
---|
59 | InitializeComponent();
|
---|
60 | dataGridView.CellMouseClick += dataGridView_CellMouseClick;
|
---|
61 | dataGridView.KeyDown += dataGridView_KeyDown;
|
---|
62 | dataGridView.MouseUp += dataGridView_MouseUp;
|
---|
63 | contextMenuCell.Items.Add(ShowHideColumns);
|
---|
64 | _highlightedCellsBackground = new Dictionary<int, IList<int>>();
|
---|
65 | currentCell = null;
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected override void OnContentChanged() {
|
---|
69 | List<KeyValuePair<int, SortOrder>> order = new List<KeyValuePair<int, SortOrder>>(base.sortedColumnIndices);
|
---|
70 | base.OnContentChanged();
|
---|
71 |
|
---|
72 | DataGridView.RowHeadersWidth = 70;
|
---|
73 |
|
---|
74 | if (Content == null && findAndReplaceDialog != null) {
|
---|
75 | findAndReplaceDialog.Close();
|
---|
76 | }
|
---|
77 |
|
---|
78 | if (Content != null) {
|
---|
79 | base.sortedColumnIndices = order;
|
---|
80 | base.Sort();
|
---|
81 | }
|
---|
82 |
|
---|
83 | }
|
---|
84 |
|
---|
85 | protected override void RegisterContentEvents() {
|
---|
86 | base.RegisterContentEvents();
|
---|
87 | Content.Changed += Content_Changed;
|
---|
88 | Content.FilterLogic.FilterChanged += FilterLogic_FilterChanged;
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected override void DeregisterContentEvents() {
|
---|
92 | base.DeregisterContentEvents();
|
---|
93 | Content.Changed -= Content_Changed;
|
---|
94 | Content.FilterLogic.FilterChanged -= FilterLogic_FilterChanged;
|
---|
95 | }
|
---|
96 |
|
---|
97 | private void FilterLogic_FilterChanged(object sender, EventArgs e) {
|
---|
98 | OnContentChanged();
|
---|
99 | searchIterator = null;
|
---|
100 | if (findAndReplaceDialog != null && !findAndReplaceDialog.IsDisposed) {
|
---|
101 | if (Content.FilterLogic.IsFiltered) {
|
---|
102 | findAndReplaceDialog.DisableReplace();
|
---|
103 | } else {
|
---|
104 | findAndReplaceDialog.EnableReplace();
|
---|
105 | }
|
---|
106 | }
|
---|
107 | btnReplace.Enabled = !Content.FilterLogic.IsFiltered;
|
---|
108 | }
|
---|
109 |
|
---|
110 | private void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
|
---|
111 | OnContentChanged();
|
---|
112 | searchIterator = null;
|
---|
113 | }
|
---|
114 |
|
---|
115 | protected override void dataGridView_SelectionChanged(object sender, EventArgs e) {
|
---|
116 | base.dataGridView_SelectionChanged(sender, e);
|
---|
117 | if (Content != null && dataGridView.RowCount != 0 && dataGridView.ColumnCount != 0)
|
---|
118 | Content.Selection = GetSelectedCells();
|
---|
119 | }
|
---|
120 |
|
---|
121 | //couldn't use base.dataGridView_CellValidating as the values have to be validated per column individually
|
---|
122 | protected override void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
|
---|
123 | if (dataGridView.ReadOnly) return;
|
---|
124 | if (Content == null) return;
|
---|
125 | if (Content.Rows == 0 || Content.Columns == 0) return;
|
---|
126 |
|
---|
127 | string errorMessage;
|
---|
128 | if (!String.IsNullOrEmpty(e.FormattedValue.ToString())) {
|
---|
129 | if (dataGridView.IsCurrentCellInEditMode && Content.FilterLogic.IsFiltered) {
|
---|
130 | errorMessage = "A filter is active, you cannot modify data. Press ESC to exit edit mode.";
|
---|
131 | } else {
|
---|
132 | Content.Validate(e.FormattedValue.ToString(), out errorMessage, e.ColumnIndex);
|
---|
133 | }
|
---|
134 |
|
---|
135 | if (!String.IsNullOrEmpty(errorMessage)) {
|
---|
136 | e.Cancel = true;
|
---|
137 | dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
|
---|
138 | }
|
---|
139 |
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | //protected override void PasteValuesToDataGridView() {
|
---|
145 | // base.PasteValuesToDataGridView();
|
---|
146 | // dataGridView.Refresh();
|
---|
147 | //}
|
---|
148 |
|
---|
149 | protected override void SetEnabledStateOfControls() {
|
---|
150 | base.SetEnabledStateOfControls();
|
---|
151 | rowsTextBox.ReadOnly = true;
|
---|
152 | columnsTextBox.ReadOnly = true;
|
---|
153 | }
|
---|
154 |
|
---|
155 | protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
|
---|
156 | btnApplySort.Enabled = sortedColumns.Any();
|
---|
157 | return base.Sort(sortedColumns);
|
---|
158 | }
|
---|
159 |
|
---|
160 | protected override void ClearSorting() {
|
---|
161 | btnApplySort.Enabled = false;
|
---|
162 | base.ClearSorting();
|
---|
163 | }
|
---|
164 |
|
---|
165 | protected override void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
166 | if (Content != null) {
|
---|
167 | if (e.Button == System.Windows.Forms.MouseButtons.Left) {
|
---|
168 | dataGridView.Focus();
|
---|
169 | dataGridView.ClearSelection();
|
---|
170 | dataGridView.SelectionChanged -= dataGridView_SelectionChanged;
|
---|
171 | for (int i = 0; i < dataGridView.RowCount; i++) {
|
---|
172 | if (i + 1 == dataGridView.RowCount)
|
---|
173 | dataGridView.SelectionChanged += dataGridView_SelectionChanged;
|
---|
174 | dataGridView[e.ColumnIndex, i].Selected = true;
|
---|
175 | }
|
---|
176 | } else if (Content.SortableView) {
|
---|
177 | SortColumn(e.ColumnIndex);
|
---|
178 | }
|
---|
179 | }
|
---|
180 | searchIterator = null;
|
---|
181 | }
|
---|
182 |
|
---|
183 | private void dataGridView_MouseUp(object sender, MouseEventArgs e) {
|
---|
184 | if (!updateOnMouseUp)
|
---|
185 | return;
|
---|
186 |
|
---|
187 | updateOnMouseUp = false;
|
---|
188 | dataGridView_SelectionChanged(sender, e);
|
---|
189 | }
|
---|
190 |
|
---|
191 | private void btnApplySort_Click(object sender, System.EventArgs e) {
|
---|
192 | Content.ManipulationLogic.ReOrderToIndices(virtualRowIndices);
|
---|
193 | OnContentChanged();
|
---|
194 | }
|
---|
195 |
|
---|
196 | #region FindAndReplaceDialog
|
---|
197 |
|
---|
198 | private void CreateFindAndReplaceDialog() {
|
---|
199 | if (findAndReplaceDialog == null || findAndReplaceDialog.IsDisposed) {
|
---|
200 | findAndReplaceDialog = new SearchAndReplaceDialog();
|
---|
201 | findAndReplaceDialog.Show(this);
|
---|
202 | if (AreMultipleCellsSelected()) {
|
---|
203 | ResetHighlightedCellsBackground();
|
---|
204 | HightlightedCellsBackground = GetSelectedCells();
|
---|
205 | dataGridView.ClearSelection();
|
---|
206 | }
|
---|
207 | findAndReplaceDialog.FindAllEvent += findAndReplaceDialog_FindAllEvent;
|
---|
208 | findAndReplaceDialog.FindNextEvent += findAndReplaceDialog_FindNextEvent;
|
---|
209 | findAndReplaceDialog.ReplaceAllEvent += findAndReplaceDialog_ReplaceAllEvent;
|
---|
210 | findAndReplaceDialog.ReplaceNextEvent += findAndReplaceDialog_ReplaceEvent;
|
---|
211 | findAndReplaceDialog.FormClosing += findAndReplaceDialog_FormClosing;
|
---|
212 | searchIterator = null;
|
---|
213 | DataGridView.SelectionChanged += DataGridView_SelectionChanged_FindAndReplace;
|
---|
214 | if (Content.FilterLogic.IsFiltered) {
|
---|
215 | findAndReplaceDialog.DisableReplace();
|
---|
216 | }
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | private void DataGridView_SelectionChanged_FindAndReplace(object sender, EventArgs e) {
|
---|
221 | if (Content != null) {
|
---|
222 | if (!isSearching && AreMultipleCellsSelected()) {
|
---|
223 | ResetHighlightedCellsBackground();
|
---|
224 | HightlightedCellsBackground = GetSelectedCells();
|
---|
225 | searchIterator = null;
|
---|
226 | }
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | void findAndReplaceDialog_FormClosing(object sender, FormClosingEventArgs e) {
|
---|
231 | ResetHighlightedCellsBackground();
|
---|
232 | searchIterator = null;
|
---|
233 | DataGridView.SelectionChanged -= DataGridView_SelectionChanged_FindAndReplace;
|
---|
234 | }
|
---|
235 |
|
---|
236 | void findAndReplaceDialog_ReplaceEvent(object sender, EventArgs e) {
|
---|
237 | if (searchIterator != null && searchIterator.GetCurrent() != null) {
|
---|
238 | Replace(TransformToDictionary(currentCell));
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | void findAndReplaceDialog_ReplaceAllEvent(object sender, EventArgs e) {
|
---|
243 | Replace(FindAll(findAndReplaceDialog.GetSearchText()));
|
---|
244 | }
|
---|
245 |
|
---|
246 | void findAndReplaceDialog_FindNextEvent(object sender, EventArgs e) {
|
---|
247 | if (searchIterator == null
|
---|
248 | || currentSearchText != findAndReplaceDialog.GetSearchText()
|
---|
249 | || currentComparisonOperation != findAndReplaceDialog.GetComparisonOperation()) {
|
---|
250 |
|
---|
251 | searchIterator = new FindPreprocessingItemsIterator(FindAll(findAndReplaceDialog.GetSearchText()));
|
---|
252 | currentSearchText = findAndReplaceDialog.GetSearchText();
|
---|
253 | currentComparisonOperation = findAndReplaceDialog.GetComparisonOperation();
|
---|
254 | }
|
---|
255 |
|
---|
256 | if (IsOneCellSelected()) {
|
---|
257 | var first = GetSelectedCells().First();
|
---|
258 | searchIterator.SetStartCell(first.Key, first.Value[0]);
|
---|
259 | }
|
---|
260 |
|
---|
261 | bool moreOccurences = false;
|
---|
262 | currentCell = searchIterator.GetCurrent();
|
---|
263 | moreOccurences = searchIterator.MoveNext();
|
---|
264 | if (IsOneCellSelected() && currentCell != null) {
|
---|
265 | var first = GetSelectedCells().First();
|
---|
266 | if (currentCell.Item1 == first.Key && currentCell.Item2 == first.Value[0]) {
|
---|
267 | if (!moreOccurences) {
|
---|
268 | searchIterator.Reset();
|
---|
269 | }
|
---|
270 | currentCell = searchIterator.GetCurrent();
|
---|
271 | moreOccurences = searchIterator.MoveNext();
|
---|
272 | if (!moreOccurences) {
|
---|
273 | searchIterator.Reset();
|
---|
274 | }
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | dataGridView.ClearSelection();
|
---|
279 |
|
---|
280 | if (currentCell != null) {
|
---|
281 | dataGridView[currentCell.Item1, currentCell.Item2].Selected = true;
|
---|
282 | dataGridView.CurrentCell = dataGridView[currentCell.Item1, currentCell.Item2];
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | private bool AreMultipleCellsSelected() {
|
---|
287 | return GetSelectedCellCount() > 1;
|
---|
288 | }
|
---|
289 |
|
---|
290 | private bool IsOneCellSelected() {
|
---|
291 | return GetSelectedCellCount() == 1;
|
---|
292 | }
|
---|
293 |
|
---|
294 | private int GetSelectedCellCount() {
|
---|
295 | int count = 0;
|
---|
296 | foreach (var column in GetSelectedCells()) {
|
---|
297 | count += column.Value.Count();
|
---|
298 | }
|
---|
299 | return count;
|
---|
300 | }
|
---|
301 |
|
---|
302 | void findAndReplaceDialog_FindAllEvent(object sender, EventArgs e) {
|
---|
303 | dataGridView.ClearSelection();
|
---|
304 | isSearching = true;
|
---|
305 | SuspendRepaint();
|
---|
306 | var selectedCells = FindAll(findAndReplaceDialog.GetSearchText());
|
---|
307 | foreach (var column in selectedCells) {
|
---|
308 | foreach (var cell in column.Value) {
|
---|
309 | dataGridView[column.Key, cell].Selected = true;
|
---|
310 | }
|
---|
311 | }
|
---|
312 | ResumeRepaint(true);
|
---|
313 | isSearching = false;
|
---|
314 | Content.Selection = selectedCells;
|
---|
315 | //update statistic in base
|
---|
316 | base.dataGridView_SelectionChanged(sender, e);
|
---|
317 | }
|
---|
318 |
|
---|
319 | private Core.ConstraintOperation GetConstraintOperation(ComparisonOperation comparisonOperation) {
|
---|
320 | Core.ConstraintOperation constraintOperation = Core.ConstraintOperation.Equal;
|
---|
321 | switch (comparisonOperation) {
|
---|
322 | case ComparisonOperation.Equal:
|
---|
323 | constraintOperation = Core.ConstraintOperation.Equal;
|
---|
324 | break;
|
---|
325 | case ComparisonOperation.Greater:
|
---|
326 | constraintOperation = Core.ConstraintOperation.Greater;
|
---|
327 | break;
|
---|
328 | case ComparisonOperation.GreaterOrEqual:
|
---|
329 | constraintOperation = Core.ConstraintOperation.GreaterOrEqual;
|
---|
330 | break;
|
---|
331 | case ComparisonOperation.Less:
|
---|
332 | constraintOperation = Core.ConstraintOperation.Less;
|
---|
333 | break;
|
---|
334 | case ComparisonOperation.LessOrEqual:
|
---|
335 | constraintOperation = Core.ConstraintOperation.LessOrEqual;
|
---|
336 | break;
|
---|
337 | case ComparisonOperation.NotEqual:
|
---|
338 | constraintOperation = Core.ConstraintOperation.NotEqual;
|
---|
339 | break;
|
---|
340 | }
|
---|
341 | return constraintOperation;
|
---|
342 | }
|
---|
343 |
|
---|
344 | private IDictionary<int, IList<int>> FindAll(string match) {
|
---|
345 | bool searchInSelection = HightlightedCellsBackground.Values.Sum(list => list.Count) > 1;
|
---|
346 | ComparisonOperation comparisonOperation = findAndReplaceDialog.GetComparisonOperation();
|
---|
347 | var foundCells = new Dictionary<int, IList<int>>();
|
---|
348 | for (int i = 0; i < Content.FilterLogic.PreprocessingData.Columns; i++) {
|
---|
349 | var filters = CreateFilters(match, comparisonOperation, i);
|
---|
350 |
|
---|
351 | bool[] filteredRows = Content.FilterLogic.GetFilterResult(filters, true);
|
---|
352 | var foundIndices = new List<int>();
|
---|
353 | for (int idx = 0; idx < filteredRows.Length; ++idx) {
|
---|
354 | var notFilteredThusFound = !filteredRows[idx];
|
---|
355 | if (notFilteredThusFound) {
|
---|
356 | foundIndices.Add(idx);
|
---|
357 | }
|
---|
358 | }
|
---|
359 | foundCells[i] = foundIndices;
|
---|
360 | IList<int> selectedList;
|
---|
361 | if (searchInSelection && HightlightedCellsBackground.TryGetValue(i, out selectedList)) {
|
---|
362 | foundCells[i] = foundCells[i].Intersect(selectedList).ToList<int>();
|
---|
363 | } else if (searchInSelection) {
|
---|
364 | foundCells[i].Clear();
|
---|
365 | }
|
---|
366 | }
|
---|
367 | return MapToSorting(foundCells);
|
---|
368 | }
|
---|
369 |
|
---|
370 | private List<IFilter> CreateFilters(string match, ComparisonOperation comparisonOperation, int columnIndex) {
|
---|
371 | IPreprocessingData preprocessingData = Content.FilterLogic.PreprocessingData;
|
---|
372 | IStringConvertibleValue value;
|
---|
373 | if (preprocessingData.VariableHasType<double>(columnIndex)) {
|
---|
374 | value = new DoubleValue();
|
---|
375 | } else if (preprocessingData.VariableHasType<String>(columnIndex)) {
|
---|
376 | value = new StringValue();
|
---|
377 | } else if (preprocessingData.VariableHasType<DateTime>(columnIndex)) {
|
---|
378 | value = new DateTimeValue();
|
---|
379 | } else {
|
---|
380 | throw new ArgumentException("unsupported type");
|
---|
381 | }
|
---|
382 | value.SetValue(match);
|
---|
383 | var comparisonFilter = new ComparisonFilter(preprocessingData, GetConstraintOperation(comparisonOperation), value, true);
|
---|
384 | comparisonFilter.ConstraintColumn = columnIndex;
|
---|
385 | return new List<Filter.IFilter>() { comparisonFilter };
|
---|
386 | }
|
---|
387 |
|
---|
388 | private IDictionary<int, IList<int>> MapToSorting(Dictionary<int, IList<int>> foundCells) {
|
---|
389 | if (sortedColumnIndices.Count == 0) {
|
---|
390 | return foundCells;
|
---|
391 | } else {
|
---|
392 | var sortedFoundCells = new Dictionary<int, IList<int>>();
|
---|
393 |
|
---|
394 | var indicesToVirtual = new Dictionary<int, int>();
|
---|
395 | for (int i = 0; i < virtualRowIndices.Length; ++i) {
|
---|
396 | indicesToVirtual.Add(virtualRowIndices[i], i);
|
---|
397 | }
|
---|
398 |
|
---|
399 | foreach (var entry in foundCells) {
|
---|
400 | var cells = new List<int>();
|
---|
401 | foreach (var cell in entry.Value) {
|
---|
402 | cells.Add(indicesToVirtual[cell]);
|
---|
403 | }
|
---|
404 | cells.Sort();
|
---|
405 | sortedFoundCells.Add(entry.Key, cells);
|
---|
406 | }
|
---|
407 | return sortedFoundCells;
|
---|
408 | }
|
---|
409 | }
|
---|
410 |
|
---|
411 | private void Replace(IDictionary<int, IList<int>> cells) {
|
---|
412 | if (findAndReplaceDialog != null) {
|
---|
413 | ReplaceTransaction(() => {
|
---|
414 | switch (findAndReplaceDialog.GetReplaceAction()) {
|
---|
415 | case ReplaceAction.Value:
|
---|
416 | Content.ManipulationLogic.ReplaceIndicesByValue(cells, findAndReplaceDialog.GetReplaceText());
|
---|
417 | break;
|
---|
418 | case ReplaceAction.Average:
|
---|
419 | Content.ManipulationLogic.ReplaceIndicesByAverageValue(cells, false);
|
---|
420 | break;
|
---|
421 | case ReplaceAction.Median:
|
---|
422 | Content.ManipulationLogic.ReplaceIndicesByMedianValue(cells, false);
|
---|
423 | break;
|
---|
424 | case ReplaceAction.Random:
|
---|
425 | Content.ManipulationLogic.ReplaceIndicesByRandomValue(cells, false);
|
---|
426 | break;
|
---|
427 | case ReplaceAction.MostCommon:
|
---|
428 | Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(cells, false);
|
---|
429 | break;
|
---|
430 | case ReplaceAction.Interpolation:
|
---|
431 | Content.ManipulationLogic.ReplaceIndicesByLinearInterpolationOfNeighbours(cells);
|
---|
432 | break;
|
---|
433 | }
|
---|
434 | });
|
---|
435 | }
|
---|
436 | }
|
---|
437 |
|
---|
438 | private IDictionary<int, IList<int>> TransformToDictionary(Tuple<int, int> tuple) {
|
---|
439 | var highlightCells = new Dictionary<int, IList<int>>();
|
---|
440 | highlightCells.Add(tuple.Item1, new List<int>() { tuple.Item2 });
|
---|
441 | return highlightCells;
|
---|
442 | }
|
---|
443 |
|
---|
444 | private void ResetHighlightedCellsBackground() {
|
---|
445 | HightlightedCellsBackground = new Dictionary<int, IList<int>>();
|
---|
446 | }
|
---|
447 |
|
---|
448 | #endregion FindAndReplaceDialog
|
---|
449 |
|
---|
450 | private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
451 | if (Content == null) return;
|
---|
452 | if (e.Button == System.Windows.Forms.MouseButtons.Right && !(e.ColumnIndex != -1 && e.RowIndex == -1)) {
|
---|
453 | if (e.ColumnIndex == -1 || e.RowIndex == -1) {
|
---|
454 | replaceValueOverColumnToolStripMenuItem.Visible = false;
|
---|
455 | contextMenuCell.Show(MousePosition);
|
---|
456 | } else {
|
---|
457 | if (!dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, e.RowIndex])) {
|
---|
458 | dataGridView.ClearSelection();
|
---|
459 | dataGridView[e.ColumnIndex, e.RowIndex].Selected = true;
|
---|
460 | }
|
---|
461 |
|
---|
462 | var columnIndices = new HashSet<int>();
|
---|
463 | for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
|
---|
464 | columnIndices.Add(dataGridView.SelectedCells[i].ColumnIndex);
|
---|
465 | }
|
---|
466 |
|
---|
467 | replaceValueOverSelectionToolStripMenuItem.Enabled = AreMultipleCellsSelected();
|
---|
468 |
|
---|
469 | averageToolStripMenuItem_Column.Enabled =
|
---|
470 | averageToolStripMenuItem_Selection.Enabled =
|
---|
471 | medianToolStripMenuItem_Column.Enabled =
|
---|
472 | medianToolStripMenuItem_Selection.Enabled =
|
---|
473 | randomToolStripMenuItem_Column.Enabled =
|
---|
474 | randomToolStripMenuItem_Selection.Enabled = !Content.PreProcessingData.AreAllStringColumns(columnIndices);
|
---|
475 |
|
---|
476 | smoothingToolStripMenuItem_Column.Enabled =
|
---|
477 | interpolationToolStripMenuItem_Column.Enabled = !dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, 0])
|
---|
478 | && !dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, Content.Rows - 1])
|
---|
479 | && !Content.PreProcessingData.AreAllStringColumns(columnIndices);
|
---|
480 |
|
---|
481 | replaceValueOverColumnToolStripMenuItem.Visible = true;
|
---|
482 | contextMenuCell.Show(MousePosition);
|
---|
483 | }
|
---|
484 | }
|
---|
485 | }
|
---|
486 |
|
---|
487 | private void dataGridView_KeyDown(object sender, KeyEventArgs e) {
|
---|
488 | var selectedRows = dataGridView.SelectedRows;
|
---|
489 | var selectedCells = dataGridView.SelectedCells;
|
---|
490 | if (!Content.FilterLogic.IsFiltered) { //data is in read only mode....
|
---|
491 | if (e.KeyCode == Keys.Delete && selectedCells.Count == Content.Rows && selectedCells.Count > 0) {
|
---|
492 | Content.DeleteColumn(selectedCells[0].ColumnIndex);
|
---|
493 | } else if (e.KeyCode == Keys.Delete && selectedRows.Count > 0) {
|
---|
494 | List<int> rows = new List<int>();
|
---|
495 | for (int i = 0; i < selectedRows.Count; ++i) {
|
---|
496 | int index = (sortedColumnIndices.Count != 0) ? (Convert.ToInt32(selectedRows[i].HeaderCell.Value) - 1) :
|
---|
497 | selectedRows[i].Index;
|
---|
498 | rows.Add(index);
|
---|
499 | }
|
---|
500 | Content.DeleteRows(rows);
|
---|
501 | } else if (e.Control && e.KeyCode == Keys.F) {
|
---|
502 | CreateFindAndReplaceDialog();
|
---|
503 | findAndReplaceDialog.ActivateSearch();
|
---|
504 | } else if (e.Control && e.KeyCode == Keys.R) {
|
---|
505 | CreateFindAndReplaceDialog();
|
---|
506 | findAndReplaceDialog.ActivateReplace();
|
---|
507 | }
|
---|
508 | }
|
---|
509 | }
|
---|
510 |
|
---|
511 | private IDictionary<int, IList<int>> GetSelectedCells() {
|
---|
512 | IDictionary<int, IList<int>> selectedCells = new Dictionary<int, IList<int>>();
|
---|
513 |
|
---|
514 | //special case if all cells are selected
|
---|
515 | if (dataGridView.AreAllCellsSelected(true)) {
|
---|
516 | for (int i = 0; i < Content.Columns; i++)
|
---|
517 | selectedCells[i] = Enumerable.Range(0, Content.Rows).ToList();
|
---|
518 | return selectedCells;
|
---|
519 | }
|
---|
520 |
|
---|
521 | foreach (var selectedCell in dataGridView.SelectedCells) {
|
---|
522 | var cell = (DataGridViewCell)selectedCell;
|
---|
523 | if (!selectedCells.ContainsKey(cell.ColumnIndex))
|
---|
524 | selectedCells.Add(cell.ColumnIndex, new List<int>(1024));
|
---|
525 | selectedCells[cell.ColumnIndex].Add(cell.RowIndex);
|
---|
526 | }
|
---|
527 |
|
---|
528 | return selectedCells;
|
---|
529 | }
|
---|
530 |
|
---|
531 | private void StartReplacing() {
|
---|
532 | SuspendRepaint();
|
---|
533 | }
|
---|
534 |
|
---|
535 | private void StopReplacing() {
|
---|
536 | ResumeRepaint(true);
|
---|
537 | }
|
---|
538 |
|
---|
539 | private void ReplaceTransaction(Action action) {
|
---|
540 | StartReplacing();
|
---|
541 | action();
|
---|
542 | StopReplacing();
|
---|
543 | }
|
---|
544 |
|
---|
545 | private void btnSearch_Click(object sender, EventArgs e) {
|
---|
546 | CreateFindAndReplaceDialog();
|
---|
547 | findAndReplaceDialog.ActivateSearch();
|
---|
548 | }
|
---|
549 |
|
---|
550 | private void btnReplace_Click(object sender, EventArgs e) {
|
---|
551 | CreateFindAndReplaceDialog();
|
---|
552 | findAndReplaceDialog.ActivateReplace();
|
---|
553 | }
|
---|
554 |
|
---|
555 | #region ContextMenu Events
|
---|
556 |
|
---|
557 | private void ReplaceWithAverage_Column_Click(object sender, EventArgs e) {
|
---|
558 | ReplaceTransaction(() => {
|
---|
559 | Content.ManipulationLogic.ReplaceIndicesByAverageValue(GetSelectedCells(), false);
|
---|
560 | });
|
---|
561 | }
|
---|
562 | private void ReplaceWithAverage_Selection_Click(object sender, EventArgs e) {
|
---|
563 | ReplaceTransaction(() => {
|
---|
564 | Content.ManipulationLogic.ReplaceIndicesByAverageValue(GetSelectedCells(), true);
|
---|
565 | });
|
---|
566 | }
|
---|
567 |
|
---|
568 | private void ReplaceWithMedian_Column_Click(object sender, EventArgs e) {
|
---|
569 | ReplaceTransaction(() => {
|
---|
570 | Content.ManipulationLogic.ReplaceIndicesByMedianValue(GetSelectedCells(), false);
|
---|
571 | });
|
---|
572 | }
|
---|
573 | private void ReplaceWithMedian_Selection_Click(object sender, EventArgs e) {
|
---|
574 | ReplaceTransaction(() => {
|
---|
575 | Content.ManipulationLogic.ReplaceIndicesByMedianValue(GetSelectedCells(), true);
|
---|
576 | });
|
---|
577 | }
|
---|
578 |
|
---|
579 | private void ReplaceWithRandom_Column_Click(object sender, EventArgs e) {
|
---|
580 | ReplaceTransaction(() => {
|
---|
581 | Content.ManipulationLogic.ReplaceIndicesByRandomValue(GetSelectedCells(), false);
|
---|
582 | });
|
---|
583 | }
|
---|
584 | private void ReplaceWithRandom_Selection_Click(object sender, EventArgs e) {
|
---|
585 | ReplaceTransaction(() => {
|
---|
586 | Content.ManipulationLogic.ReplaceIndicesByRandomValue(GetSelectedCells(), true);
|
---|
587 | });
|
---|
588 | }
|
---|
589 |
|
---|
590 | private void ReplaceWithMostCommon_Column_Click(object sender, EventArgs e) {
|
---|
591 | ReplaceTransaction(() => {
|
---|
592 | Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(GetSelectedCells(), false);
|
---|
593 | });
|
---|
594 | }
|
---|
595 | private void ReplaceWithMostCommon_Selection_Click(object sender, EventArgs e) {
|
---|
596 | ReplaceTransaction(() => {
|
---|
597 | Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(GetSelectedCells(), true);
|
---|
598 | });
|
---|
599 | }
|
---|
600 |
|
---|
601 | private void ReplaceWithInterpolation_Column_Click(object sender, EventArgs e) {
|
---|
602 | ReplaceTransaction(() => {
|
---|
603 | Content.ManipulationLogic.ReplaceIndicesByLinearInterpolationOfNeighbours(GetSelectedCells());
|
---|
604 | });
|
---|
605 | }
|
---|
606 |
|
---|
607 | private void ReplaceWithSmoothing_Selection_Click(object sender, EventArgs e) {
|
---|
608 | ReplaceTransaction(() => {
|
---|
609 | Content.ManipulationLogic.ReplaceIndicesBySmoothing(GetSelectedCells());
|
---|
610 | });
|
---|
611 | }
|
---|
612 | #endregion
|
---|
613 |
|
---|
614 | }
|
---|
615 | }
|
---|