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