[10539] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17097] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10539] | 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 |
|
---|
[10534] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[10236] | 25 | using System.Windows.Forms;
|
---|
[10672] | 26 | using HeuristicLab.Data;
|
---|
[11000] | 27 | using HeuristicLab.Data.Views;
|
---|
[10719] | 28 | using HeuristicLab.DataPreprocessing.Filter;
|
---|
[10236] | 29 | using HeuristicLab.MainForm;
|
---|
| 30 |
|
---|
[10558] | 31 | namespace HeuristicLab.DataPreprocessing.Views {
|
---|
[10236] | 32 | [View("Data Grid Content View")]
|
---|
[14075] | 33 | [Content(typeof(DataGridContent), true)]
|
---|
[11000] | 34 | public partial class DataGridContentView : StringConvertibleMatrixView {
|
---|
[10870] | 35 | private bool isSearching = false;
|
---|
[10934] | 36 | private bool updateOnMouseUp = false;
|
---|
[10762] | 37 | private SearchAndReplaceDialog findAndReplaceDialog;
|
---|
[10698] | 38 | private IFindPreprocessingItemsIterator searchIterator;
|
---|
[10672] | 39 | private string currentSearchText;
|
---|
[10870] | 40 | private ComparisonOperation currentComparisonOperation;
|
---|
[10706] | 41 | private Tuple<int, int> currentCell;
|
---|
[10583] | 42 |
|
---|
[14075] | 43 | public new DataGridContent Content {
|
---|
| 44 | get { return (DataGridContent)base.Content; }
|
---|
[10236] | 45 | set { base.Content = value; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[10719] | 48 | private IDictionary<int, IList<int>> _highlightedCellsBackground;
|
---|
| 49 | public IDictionary<int, IList<int>> HightlightedCellsBackground {
|
---|
| 50 | get { return _highlightedCellsBackground; }
|
---|
| 51 | set {
|
---|
| 52 | _highlightedCellsBackground = value;
|
---|
| 53 | Refresh();
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[10236] | 57 | public DataGridContentView() {
|
---|
| 58 | InitializeComponent();
|
---|
[14797] | 59 | dataGridView.MouseDown += dataGridView_MouseDown;
|
---|
[10380] | 60 | dataGridView.CellMouseClick += dataGridView_CellMouseClick;
|
---|
[13289] | 61 | dataGridView.RowHeaderMouseClick += dataGridView_RowHeaderMouseClick;
|
---|
[10934] | 62 | dataGridView.MouseUp += dataGridView_MouseUp;
|
---|
[10380] | 63 | contextMenuCell.Items.Add(ShowHideColumns);
|
---|
[10719] | 64 | _highlightedCellsBackground = new Dictionary<int, IList<int>>();
|
---|
[10706] | 65 | currentCell = null;
|
---|
[10236] | 66 | }
|
---|
| 67 |
|
---|
[10965] | 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 | protected override void RegisterContentEvents() {
|
---|
| 85 | base.RegisterContentEvents();
|
---|
| 86 | Content.Changed += Content_Changed;
|
---|
[15535] | 87 | Content.PreprocessingData.FilterChanged += FilterLogic_FilterChanged;
|
---|
[10965] | 88 | }
|
---|
| 89 |
|
---|
| 90 | protected override void DeregisterContentEvents() {
|
---|
| 91 | base.DeregisterContentEvents();
|
---|
| 92 | Content.Changed -= Content_Changed;
|
---|
[15535] | 93 | Content.PreprocessingData.FilterChanged -= FilterLogic_FilterChanged;
|
---|
[10965] | 94 | }
|
---|
| 95 |
|
---|
| 96 | private void FilterLogic_FilterChanged(object sender, EventArgs e) {
|
---|
| 97 | OnContentChanged();
|
---|
[10947] | 98 | searchIterator = null;
|
---|
[10965] | 99 | if (findAndReplaceDialog != null && !findAndReplaceDialog.IsDisposed) {
|
---|
[15535] | 100 | if (Content.PreprocessingData.IsFiltered) {
|
---|
[10965] | 101 | findAndReplaceDialog.DisableReplace();
|
---|
| 102 | } else {
|
---|
| 103 | findAndReplaceDialog.EnableReplace();
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
[15535] | 106 | btnReplace.Enabled = !Content.PreprocessingData.IsFiltered;
|
---|
[10947] | 107 | }
|
---|
[10965] | 108 |
|
---|
| 109 | private void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
|
---|
[11098] | 110 | OnContentChanged();
|
---|
[10965] | 111 | searchIterator = null;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[10916] | 114 | protected override void dataGridView_SelectionChanged(object sender, EventArgs e) {
|
---|
[10993] | 115 | base.dataGridView_SelectionChanged(sender, e);
|
---|
[11098] | 116 | if (Content != null && dataGridView.RowCount != 0 && dataGridView.ColumnCount != 0)
|
---|
[11037] | 117 | Content.Selection = GetSelectedCells();
|
---|
[10804] | 118 | }
|
---|
| 119 |
|
---|
[11098] | 120 | //couldn't use base.dataGridView_CellValidating as the values have to be validated per column individually
|
---|
[10964] | 121 | protected override void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
|
---|
[11098] | 122 | if (dataGridView.ReadOnly) return;
|
---|
| 123 | if (Content == null) return;
|
---|
| 124 | if (Content.Rows == 0 || Content.Columns == 0) return;
|
---|
[10934] | 125 |
|
---|
[11098] | 126 | string errorMessage;
|
---|
[12718] | 127 | if (!String.IsNullOrEmpty(e.FormattedValue.ToString())) {
|
---|
[15535] | 128 | if (dataGridView.IsCurrentCellInEditMode && Content.PreprocessingData.IsFiltered) {
|
---|
[11098] | 129 | errorMessage = "A filter is active, you cannot modify data. Press ESC to exit edit mode.";
|
---|
| 130 | } else {
|
---|
| 131 | Content.Validate(e.FormattedValue.ToString(), out errorMessage, e.ColumnIndex);
|
---|
[10964] | 132 | }
|
---|
[11098] | 133 |
|
---|
| 134 | if (!String.IsNullOrEmpty(errorMessage)) {
|
---|
| 135 | e.Cancel = true;
|
---|
| 136 | dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[10964] | 139 | }
|
---|
[10947] | 140 | }
|
---|
[10934] | 141 |
|
---|
[13289] | 142 | protected override void PasteValuesToDataGridView() {
|
---|
| 143 | string[,] values = SplitClipboardString(Clipboard.GetText());
|
---|
[14075] | 144 | if (values.Length == 0) return;
|
---|
[13289] | 145 | int rowIndex = 0;
|
---|
| 146 | int columnIndex = 0;
|
---|
| 147 | if (dataGridView.CurrentCell != null) {
|
---|
| 148 | rowIndex = dataGridView.CurrentCell.RowIndex;
|
---|
| 149 | columnIndex = dataGridView.CurrentCell.ColumnIndex;
|
---|
| 150 | }
|
---|
[10964] | 151 |
|
---|
[14075] | 152 | bool containsHeader = false;
|
---|
| 153 | var firstRow = Enumerable.Range(0, values.GetLength(0)).Select(col => values[col, 0]).ToList();
|
---|
| 154 | if ((Content.Selection.Values.Sum(s => s.Count) == Content.Rows * Content.Columns)
|
---|
| 155 | || (rowIndex == 0 && columnIndex == 0 && Content.Rows <= values.GetLength(1) && Content.Columns <= values.GetLength(0))) {
|
---|
| 156 | // All is selected -or- top left selected and everything will be replaced
|
---|
| 157 | double temp;
|
---|
| 158 | containsHeader = !firstRow.All(string.IsNullOrEmpty) && firstRow.Any(r => !double.TryParse(r, out temp));
|
---|
| 159 | if (containsHeader)
|
---|
| 160 | rowIndex = columnIndex = 0;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | int newRows = values.GetLength(1) + rowIndex - (containsHeader ? 1 : 0);
|
---|
| 164 | int newColumns = values.GetLength(0) + columnIndex;
|
---|
| 165 | if (Content.Rows < newRows) Content.Rows = newRows;
|
---|
| 166 | if (Content.Columns < newColumns) Content.Columns = newColumns;
|
---|
| 167 |
|
---|
[13289] | 168 | ReplaceTransaction(() => {
|
---|
[15535] | 169 | Content.PreprocessingData.InTransaction(() => {
|
---|
[14075] | 170 | for (int row = containsHeader ? 1 : 0; row < values.GetLength(1); row++) {
|
---|
[13289] | 171 | for (int col = 0; col < values.GetLength(0); col++) {
|
---|
[14075] | 172 | Content.SetValue(values[col, row], row + rowIndex - (containsHeader ? 1 : 0), col + columnIndex);
|
---|
[13289] | 173 | }
|
---|
| 174 | }
|
---|
[14075] | 175 | if (containsHeader) {
|
---|
| 176 | for (int i = 0; i < firstRow.Count; i++)
|
---|
| 177 | if (string.IsNullOrWhiteSpace(firstRow[i]))
|
---|
| 178 | firstRow[i] = string.Format("<{0}>", i);
|
---|
[15535] | 179 | Content.PreprocessingData.RenameColumns(firstRow);
|
---|
[14075] | 180 | }
|
---|
[13289] | 181 | });
|
---|
| 182 | });
|
---|
| 183 |
|
---|
| 184 | ClearSorting();
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[10964] | 187 | protected override void SetEnabledStateOfControls() {
|
---|
| 188 | base.SetEnabledStateOfControls();
|
---|
| 189 | rowsTextBox.ReadOnly = true;
|
---|
| 190 | columnsTextBox.ReadOnly = true;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
|
---|
| 194 | btnApplySort.Enabled = sortedColumns.Any();
|
---|
| 195 | return base.Sort(sortedColumns);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | protected override void ClearSorting() {
|
---|
| 199 | btnApplySort.Enabled = false;
|
---|
| 200 | base.ClearSorting();
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[14797] | 203 | //Necessary so that dataGridView.SelectedRows and SelectedColumns are populated correctly
|
---|
| 204 | //further information: https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectedcolumns.aspx
|
---|
| 205 | private void dataGridView_MouseDown(object sender, MouseEventArgs e) {
|
---|
| 206 | var hitTestInfo = dataGridView.HitTest(e.X, e.Y);
|
---|
| 207 | // row header click
|
---|
| 208 | if (hitTestInfo.ColumnIndex == -1 && hitTestInfo.RowIndex >= 0) {
|
---|
| 209 | if (dataGridView.SelectionMode != DataGridViewSelectionMode.RowHeaderSelect) {
|
---|
| 210 | dataGridView.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
|
---|
[12718] | 211 | }
|
---|
| 212 | }
|
---|
[14797] | 213 | // column header click
|
---|
| 214 | if (hitTestInfo.RowIndex == -1 && hitTestInfo.ColumnIndex >= 0) {
|
---|
| 215 | if (dataGridView.SelectionMode != DataGridViewSelectionMode.ColumnHeaderSelect) {
|
---|
| 216 | dataGridView.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | protected override void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
| 222 | if (Content == null) return;
|
---|
| 223 |
|
---|
| 224 | if (e.Button == MouseButtons.Middle) {
|
---|
| 225 | int newIndex = e.ColumnIndex >= 0 ? e.ColumnIndex : 0;
|
---|
[15535] | 226 | Content.PreprocessingData.InsertColumn<double>(newIndex.ToString(), newIndex);
|
---|
[14797] | 227 | } else if (e.Button == MouseButtons.Right && Content.SortableView) {
|
---|
| 228 | SortColumn(e.ColumnIndex);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[10930] | 231 | searchIterator = null;
|
---|
| 232 | }
|
---|
[13289] | 233 | private void dataGridView_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
| 234 | if (Content != null) {
|
---|
| 235 | if (e.Button == MouseButtons.Middle) {
|
---|
| 236 | int newIndex = e.RowIndex >= 0 ? e.RowIndex : 0;
|
---|
[15535] | 237 | Content.PreprocessingData.InsertRow(newIndex);
|
---|
[13289] | 238 | }
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
[10930] | 241 |
|
---|
[10964] | 242 | private void dataGridView_MouseUp(object sender, MouseEventArgs e) {
|
---|
| 243 | if (!updateOnMouseUp)
|
---|
| 244 | return;
|
---|
[10900] | 245 |
|
---|
[10964] | 246 | updateOnMouseUp = false;
|
---|
| 247 | dataGridView_SelectionChanged(sender, e);
|
---|
[10236] | 248 | }
|
---|
[10253] | 249 |
|
---|
[15535] | 250 | private void btnApplySort_Click(object sender, EventArgs e) {
|
---|
| 251 | Content.ReOrderToIndices(virtualRowIndices);
|
---|
[11098] | 252 | OnContentChanged();
|
---|
[10253] | 253 | }
|
---|
[10345] | 254 |
|
---|
[10698] | 255 | #region FindAndReplaceDialog
|
---|
[10585] | 256 |
|
---|
[10636] | 257 | private void CreateFindAndReplaceDialog() {
|
---|
[10876] | 258 | if (findAndReplaceDialog == null || findAndReplaceDialog.IsDisposed) {
|
---|
| 259 | findAndReplaceDialog = new SearchAndReplaceDialog();
|
---|
| 260 | findAndReplaceDialog.Show(this);
|
---|
| 261 | if (AreMultipleCellsSelected()) {
|
---|
| 262 | ResetHighlightedCellsBackground();
|
---|
| 263 | HightlightedCellsBackground = GetSelectedCells();
|
---|
| 264 | dataGridView.ClearSelection();
|
---|
| 265 | }
|
---|
| 266 | findAndReplaceDialog.FindAllEvent += findAndReplaceDialog_FindAllEvent;
|
---|
| 267 | findAndReplaceDialog.FindNextEvent += findAndReplaceDialog_FindNextEvent;
|
---|
| 268 | findAndReplaceDialog.ReplaceAllEvent += findAndReplaceDialog_ReplaceAllEvent;
|
---|
| 269 | findAndReplaceDialog.ReplaceNextEvent += findAndReplaceDialog_ReplaceEvent;
|
---|
| 270 | findAndReplaceDialog.FormClosing += findAndReplaceDialog_FormClosing;
|
---|
| 271 | searchIterator = null;
|
---|
| 272 | DataGridView.SelectionChanged += DataGridView_SelectionChanged_FindAndReplace;
|
---|
[15535] | 273 | if (Content.PreprocessingData.IsFiltered) {
|
---|
[10930] | 274 | findAndReplaceDialog.DisableReplace();
|
---|
| 275 | }
|
---|
[10852] | 276 | }
|
---|
[10636] | 277 | }
|
---|
| 278 |
|
---|
[10916] | 279 | private void DataGridView_SelectionChanged_FindAndReplace(object sender, EventArgs e) {
|
---|
[10863] | 280 | if (Content != null) {
|
---|
[10870] | 281 | if (!isSearching && AreMultipleCellsSelected()) {
|
---|
[10863] | 282 | ResetHighlightedCellsBackground();
|
---|
| 283 | HightlightedCellsBackground = GetSelectedCells();
|
---|
| 284 | searchIterator = null;
|
---|
| 285 | }
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[10705] | 289 | void findAndReplaceDialog_FormClosing(object sender, FormClosingEventArgs e) {
|
---|
[10719] | 290 | ResetHighlightedCellsBackground();
|
---|
[10852] | 291 | searchIterator = null;
|
---|
[10863] | 292 | DataGridView.SelectionChanged -= DataGridView_SelectionChanged_FindAndReplace;
|
---|
[10705] | 293 | }
|
---|
| 294 |
|
---|
[10672] | 295 | void findAndReplaceDialog_ReplaceEvent(object sender, EventArgs e) {
|
---|
[10712] | 296 | if (searchIterator != null && searchIterator.GetCurrent() != null) {
|
---|
[10706] | 297 | Replace(TransformToDictionary(currentCell));
|
---|
[10672] | 298 | }
|
---|
[10636] | 299 | }
|
---|
| 300 |
|
---|
| 301 | void findAndReplaceDialog_ReplaceAllEvent(object sender, EventArgs e) {
|
---|
[10672] | 302 | Replace(FindAll(findAndReplaceDialog.GetSearchText()));
|
---|
[10636] | 303 | }
|
---|
| 304 |
|
---|
| 305 | void findAndReplaceDialog_FindNextEvent(object sender, EventArgs e) {
|
---|
[10947] | 306 | if (searchIterator == null
|
---|
| 307 | || currentSearchText != findAndReplaceDialog.GetSearchText()
|
---|
| 308 | || currentComparisonOperation != findAndReplaceDialog.GetComparisonOperation()) {
|
---|
| 309 |
|
---|
[10698] | 310 | searchIterator = new FindPreprocessingItemsIterator(FindAll(findAndReplaceDialog.GetSearchText()));
|
---|
| 311 | currentSearchText = findAndReplaceDialog.GetSearchText();
|
---|
[10870] | 312 | currentComparisonOperation = findAndReplaceDialog.GetComparisonOperation();
|
---|
[10698] | 313 | }
|
---|
[10870] | 314 |
|
---|
[10852] | 315 | if (IsOneCellSelected()) {
|
---|
| 316 | var first = GetSelectedCells().First();
|
---|
| 317 | searchIterator.SetStartCell(first.Key, first.Value[0]);
|
---|
| 318 | }
|
---|
[10712] | 319 |
|
---|
[10705] | 320 | bool moreOccurences = false;
|
---|
[10870] | 321 | currentCell = searchIterator.GetCurrent();
|
---|
| 322 | moreOccurences = searchIterator.MoveNext();
|
---|
| 323 | if (IsOneCellSelected() && currentCell != null) {
|
---|
| 324 | var first = GetSelectedCells().First();
|
---|
| 325 | if (currentCell.Item1 == first.Key && currentCell.Item2 == first.Value[0]) {
|
---|
| 326 | if (!moreOccurences) {
|
---|
| 327 | searchIterator.Reset();
|
---|
[10852] | 328 | }
|
---|
[10870] | 329 | currentCell = searchIterator.GetCurrent();
|
---|
| 330 | moreOccurences = searchIterator.MoveNext();
|
---|
| 331 | if (!moreOccurences) {
|
---|
| 332 | searchIterator.Reset();
|
---|
| 333 | }
|
---|
[10852] | 334 | }
|
---|
[10870] | 335 | }
|
---|
[10672] | 336 |
|
---|
[10870] | 337 | dataGridView.ClearSelection();
|
---|
| 338 |
|
---|
[10705] | 339 | if (currentCell != null) {
|
---|
[10852] | 340 | dataGridView[currentCell.Item1, currentCell.Item2].Selected = true;
|
---|
[10946] | 341 | dataGridView.CurrentCell = dataGridView[currentCell.Item1, currentCell.Item2];
|
---|
[10698] | 342 | }
|
---|
[10852] | 343 | }
|
---|
| 344 |
|
---|
| 345 | private bool AreMultipleCellsSelected() {
|
---|
| 346 | return GetSelectedCellCount() > 1;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | private bool IsOneCellSelected() {
|
---|
| 350 | return GetSelectedCellCount() == 1;
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | private int GetSelectedCellCount() {
|
---|
| 354 | int count = 0;
|
---|
| 355 | foreach (var column in GetSelectedCells()) {
|
---|
| 356 | count += column.Value.Count();
|
---|
[10706] | 357 | }
|
---|
[10852] | 358 | return count;
|
---|
[10636] | 359 | }
|
---|
| 360 |
|
---|
| 361 | void findAndReplaceDialog_FindAllEvent(object sender, EventArgs e) {
|
---|
[10870] | 362 | dataGridView.ClearSelection();
|
---|
| 363 | isSearching = true;
|
---|
[10901] | 364 | SuspendRepaint();
|
---|
[10916] | 365 | var selectedCells = FindAll(findAndReplaceDialog.GetSearchText());
|
---|
| 366 | foreach (var column in selectedCells) {
|
---|
[10870] | 367 | foreach (var cell in column.Value) {
|
---|
| 368 | dataGridView[column.Key, cell].Selected = true;
|
---|
| 369 | }
|
---|
| 370 | }
|
---|
[10901] | 371 | ResumeRepaint(true);
|
---|
[10870] | 372 | isSearching = false;
|
---|
[11002] | 373 | Content.Selection = selectedCells;
|
---|
[10916] | 374 | //update statistic in base
|
---|
| 375 | base.dataGridView_SelectionChanged(sender, e);
|
---|
[10636] | 376 | }
|
---|
| 377 |
|
---|
[10870] | 378 | private Core.ConstraintOperation GetConstraintOperation(ComparisonOperation comparisonOperation) {
|
---|
| 379 | Core.ConstraintOperation constraintOperation = Core.ConstraintOperation.Equal;
|
---|
| 380 | switch (comparisonOperation) {
|
---|
| 381 | case ComparisonOperation.Equal:
|
---|
| 382 | constraintOperation = Core.ConstraintOperation.Equal;
|
---|
| 383 | break;
|
---|
| 384 | case ComparisonOperation.Greater:
|
---|
| 385 | constraintOperation = Core.ConstraintOperation.Greater;
|
---|
| 386 | break;
|
---|
| 387 | case ComparisonOperation.GreaterOrEqual:
|
---|
| 388 | constraintOperation = Core.ConstraintOperation.GreaterOrEqual;
|
---|
| 389 | break;
|
---|
| 390 | case ComparisonOperation.Less:
|
---|
| 391 | constraintOperation = Core.ConstraintOperation.Less;
|
---|
| 392 | break;
|
---|
| 393 | case ComparisonOperation.LessOrEqual:
|
---|
| 394 | constraintOperation = Core.ConstraintOperation.LessOrEqual;
|
---|
| 395 | break;
|
---|
| 396 | case ComparisonOperation.NotEqual:
|
---|
| 397 | constraintOperation = Core.ConstraintOperation.NotEqual;
|
---|
| 398 | break;
|
---|
| 399 | }
|
---|
| 400 | return constraintOperation;
|
---|
| 401 | }
|
---|
| 402 |
|
---|
[10672] | 403 | private IDictionary<int, IList<int>> FindAll(string match) {
|
---|
[10719] | 404 | bool searchInSelection = HightlightedCellsBackground.Values.Sum(list => list.Count) > 1;
|
---|
[10870] | 405 | ComparisonOperation comparisonOperation = findAndReplaceDialog.GetComparisonOperation();
|
---|
[10672] | 406 | var foundCells = new Dictionary<int, IList<int>>();
|
---|
[15535] | 407 | for (int i = 0; i < Content.PreprocessingData.Columns; i++) {
|
---|
[10947] | 408 | var filters = CreateFilters(match, comparisonOperation, i);
|
---|
| 409 |
|
---|
[15535] | 410 | bool[] filteredRows = GetFilterResult(filters, true);
|
---|
[10844] | 411 | var foundIndices = new List<int>();
|
---|
| 412 | for (int idx = 0; idx < filteredRows.Length; ++idx) {
|
---|
[17018] | 413 | if (filteredRows[idx]) {
|
---|
[10844] | 414 | foundIndices.Add(idx);
|
---|
| 415 | }
|
---|
| 416 | }
|
---|
| 417 | foundCells[i] = foundIndices;
|
---|
[10719] | 418 | IList<int> selectedList;
|
---|
| 419 | if (searchInSelection && HightlightedCellsBackground.TryGetValue(i, out selectedList)) {
|
---|
[10739] | 420 | foundCells[i] = foundCells[i].Intersect(selectedList).ToList<int>();
|
---|
| 421 | } else if (searchInSelection) {
|
---|
[10719] | 422 | foundCells[i].Clear();
|
---|
| 423 | }
|
---|
[10672] | 424 | }
|
---|
[10947] | 425 | return MapToSorting(foundCells);
|
---|
[10672] | 426 | }
|
---|
| 427 |
|
---|
[15535] | 428 | private bool[] GetFilterResult(IList<IFilter> filters, bool isAndCombination) {
|
---|
| 429 | IList<IFilter> activeFilters = filters.Where(f => f.Active && f.ConstraintData != null).ToList();
|
---|
| 430 |
|
---|
| 431 | if (activeFilters.Count == 0) {
|
---|
| 432 | return Enumerable.Repeat(false, Content.PreprocessingData.Rows).ToArray(); ;
|
---|
| 433 | }
|
---|
| 434 |
|
---|
| 435 | var result = Enumerable.Repeat(!isAndCombination, Content.PreprocessingData.Rows).ToArray();
|
---|
| 436 | foreach (IFilter filter in activeFilters) {
|
---|
| 437 | bool[] filterResult = filter.Check();
|
---|
| 438 | for (int row = 0; row < result.Length; ++row) {
|
---|
| 439 | result[row] = isAndCombination ? result[row] || filterResult[row] : result[row] && filterResult[row];
|
---|
| 440 | }
|
---|
| 441 | }
|
---|
| 442 | return result;
|
---|
| 443 | }
|
---|
| 444 |
|
---|
[10947] | 445 | private List<IFilter> CreateFilters(string match, ComparisonOperation comparisonOperation, int columnIndex) {
|
---|
[15535] | 446 | IPreprocessingData preprocessingData = Content.PreprocessingData;
|
---|
[10947] | 447 | IStringConvertibleValue value;
|
---|
[11159] | 448 | if (preprocessingData.VariableHasType<double>(columnIndex)) {
|
---|
[10947] | 449 | value = new DoubleValue();
|
---|
[11159] | 450 | } else if (preprocessingData.VariableHasType<String>(columnIndex)) {
|
---|
[10947] | 451 | value = new StringValue();
|
---|
[11159] | 452 | } else if (preprocessingData.VariableHasType<DateTime>(columnIndex)) {
|
---|
[10947] | 453 | value = new DateTimeValue();
|
---|
| 454 | } else {
|
---|
| 455 | throw new ArgumentException("unsupported type");
|
---|
| 456 | }
|
---|
| 457 | value.SetValue(match);
|
---|
| 458 | var comparisonFilter = new ComparisonFilter(preprocessingData, GetConstraintOperation(comparisonOperation), value, true);
|
---|
| 459 | comparisonFilter.ConstraintColumn = columnIndex;
|
---|
| 460 | return new List<Filter.IFilter>() { comparisonFilter };
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | private IDictionary<int, IList<int>> MapToSorting(Dictionary<int, IList<int>> foundCells) {
|
---|
| 464 | if (sortedColumnIndices.Count == 0) {
|
---|
| 465 | return foundCells;
|
---|
| 466 | } else {
|
---|
| 467 | var sortedFoundCells = new Dictionary<int, IList<int>>();
|
---|
| 468 |
|
---|
| 469 | var indicesToVirtual = new Dictionary<int, int>();
|
---|
| 470 | for (int i = 0; i < virtualRowIndices.Length; ++i) {
|
---|
| 471 | indicesToVirtual.Add(virtualRowIndices[i], i);
|
---|
| 472 | }
|
---|
| 473 |
|
---|
| 474 | foreach (var entry in foundCells) {
|
---|
| 475 | var cells = new List<int>();
|
---|
| 476 | foreach (var cell in entry.Value) {
|
---|
| 477 | cells.Add(indicesToVirtual[cell]);
|
---|
| 478 | }
|
---|
| 479 | cells.Sort();
|
---|
| 480 | sortedFoundCells.Add(entry.Key, cells);
|
---|
| 481 | }
|
---|
| 482 | return sortedFoundCells;
|
---|
| 483 | }
|
---|
| 484 | }
|
---|
| 485 |
|
---|
[10672] | 486 | private void Replace(IDictionary<int, IList<int>> cells) {
|
---|
| 487 | if (findAndReplaceDialog != null) {
|
---|
[10938] | 488 | ReplaceTransaction(() => {
|
---|
| 489 | switch (findAndReplaceDialog.GetReplaceAction()) {
|
---|
| 490 | case ReplaceAction.Value:
|
---|
[15535] | 491 | Content.ReplaceIndicesByString(cells, findAndReplaceDialog.GetReplaceText());
|
---|
[10938] | 492 | break;
|
---|
| 493 | case ReplaceAction.Average:
|
---|
[15535] | 494 | Content.ReplaceIndicesByMean(cells, false);
|
---|
[10938] | 495 | break;
|
---|
| 496 | case ReplaceAction.Median:
|
---|
[15535] | 497 | Content.ReplaceIndicesByMedianValue(cells, false);
|
---|
[10938] | 498 | break;
|
---|
| 499 | case ReplaceAction.Random:
|
---|
[15535] | 500 | Content.ReplaceIndicesByRandomValue(cells, false);
|
---|
[10938] | 501 | break;
|
---|
| 502 | case ReplaceAction.MostCommon:
|
---|
[15535] | 503 | Content.ReplaceIndicesByMode(cells, false);
|
---|
[10938] | 504 | break;
|
---|
| 505 | case ReplaceAction.Interpolation:
|
---|
[15535] | 506 | Content.ReplaceIndicesByLinearInterpolationOfNeighbours(cells);
|
---|
[10938] | 507 | break;
|
---|
| 508 | }
|
---|
| 509 | });
|
---|
[10672] | 510 | }
|
---|
| 511 | }
|
---|
| 512 |
|
---|
[10698] | 513 | private IDictionary<int, IList<int>> TransformToDictionary(Tuple<int, int> tuple) {
|
---|
[10672] | 514 | var highlightCells = new Dictionary<int, IList<int>>();
|
---|
[10698] | 515 | highlightCells.Add(tuple.Item1, new List<int>() { tuple.Item2 });
|
---|
[10672] | 516 | return highlightCells;
|
---|
| 517 | }
|
---|
| 518 |
|
---|
[10719] | 519 | private void ResetHighlightedCellsBackground() {
|
---|
| 520 | HightlightedCellsBackground = new Dictionary<int, IList<int>>();
|
---|
| 521 | }
|
---|
| 522 |
|
---|
[10698] | 523 | #endregion FindAndReplaceDialog
|
---|
[10380] | 524 | private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
| 525 | if (Content == null) return;
|
---|
[13289] | 526 | if (e.Button == MouseButtons.Right && !(e.ColumnIndex != -1 && e.RowIndex == -1)) {
|
---|
[10380] | 527 | if (e.ColumnIndex == -1 || e.RowIndex == -1) {
|
---|
[10769] | 528 | replaceValueOverColumnToolStripMenuItem.Visible = false;
|
---|
[10627] | 529 | contextMenuCell.Show(MousePosition);
|
---|
[10380] | 530 | } else {
|
---|
[10590] | 531 | if (!dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, e.RowIndex])) {
|
---|
| 532 | dataGridView.ClearSelection();
|
---|
| 533 | dataGridView[e.ColumnIndex, e.RowIndex].Selected = true;
|
---|
| 534 | }
|
---|
[10812] | 535 |
|
---|
[10621] | 536 | var columnIndices = new HashSet<int>();
|
---|
| 537 | for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
|
---|
| 538 | columnIndices.Add(dataGridView.SelectedCells[i].ColumnIndex);
|
---|
| 539 | }
|
---|
[10875] | 540 |
|
---|
| 541 | replaceValueOverSelectionToolStripMenuItem.Enabled = AreMultipleCellsSelected();
|
---|
| 542 |
|
---|
[10812] | 543 | averageToolStripMenuItem_Column.Enabled =
|
---|
| 544 | averageToolStripMenuItem_Selection.Enabled =
|
---|
| 545 | medianToolStripMenuItem_Column.Enabled =
|
---|
| 546 | medianToolStripMenuItem_Selection.Enabled =
|
---|
| 547 | randomToolStripMenuItem_Column.Enabled =
|
---|
[15535] | 548 | randomToolStripMenuItem_Selection.Enabled = !Content.PreprocessingData.AreAllStringColumns(columnIndices);
|
---|
[10812] | 549 |
|
---|
[10769] | 550 | replaceValueOverColumnToolStripMenuItem.Visible = true;
|
---|
[10380] | 551 | contextMenuCell.Show(MousePosition);
|
---|
| 552 | }
|
---|
| 553 | }
|
---|
| 554 | }
|
---|
| 555 |
|
---|
[14141] | 556 | protected override void dataGridView_KeyDown(object sender, KeyEventArgs e) {
|
---|
[14797] | 557 | base.dataGridView_KeyDown(sender, e);
|
---|
| 558 | //data is in read only mode....
|
---|
[15535] | 559 | if (Content.PreprocessingData.IsFiltered) return;
|
---|
[14797] | 560 |
|
---|
| 561 | if (e.KeyCode == Keys.Delete) {
|
---|
| 562 | //Delete column
|
---|
| 563 | if (dataGridView.SelectedColumns.Count > 0) {
|
---|
| 564 | var columnsToDelete = dataGridView.SelectedColumns.Cast<DataGridViewColumn>().OrderByDescending(col => col.Index).ToList();
|
---|
| 565 | foreach (var col in columnsToDelete)
|
---|
| 566 | Content.DeleteColumn(col.Index);
|
---|
[10668] | 567 | }
|
---|
[14797] | 568 | //Delete row
|
---|
| 569 | if (dataGridView.SelectedRows.Count > 0) {
|
---|
| 570 | //necessary if columns are sorted to translate the selected row index
|
---|
| 571 | var rowIndexes = dataGridView.SelectedRows.Cast<DataGridViewRow>().Select(row => GetRowIndex(row.Index)).ToList();
|
---|
| 572 | Content.DeleteRows(rowIndexes);
|
---|
| 573 | }
|
---|
| 574 | } else if (e.Control && e.KeyCode == Keys.F) {
|
---|
| 575 | CreateFindAndReplaceDialog();
|
---|
| 576 | findAndReplaceDialog.ActivateSearch();
|
---|
| 577 | } else if (e.Control && e.KeyCode == Keys.R) {
|
---|
| 578 | CreateFindAndReplaceDialog();
|
---|
| 579 | findAndReplaceDialog.ActivateReplace();
|
---|
[10668] | 580 | }
|
---|
| 581 | }
|
---|
| 582 |
|
---|
[10672] | 583 | private IDictionary<int, IList<int>> GetSelectedCells() {
|
---|
| 584 | IDictionary<int, IList<int>> selectedCells = new Dictionary<int, IList<int>>();
|
---|
[10993] | 585 |
|
---|
| 586 | //special case if all cells are selected
|
---|
| 587 | if (dataGridView.AreAllCellsSelected(true)) {
|
---|
| 588 | for (int i = 0; i < Content.Columns; i++)
|
---|
| 589 | selectedCells[i] = Enumerable.Range(0, Content.Rows).ToList();
|
---|
| 590 | return selectedCells;
|
---|
[10590] | 591 | }
|
---|
[10993] | 592 |
|
---|
[15535] | 593 | foreach (DataGridViewCell cell in dataGridView.SelectedCells) {
|
---|
[11037] | 594 | if (!selectedCells.ContainsKey(cell.ColumnIndex))
|
---|
[15535] | 595 | selectedCells.Add(cell.ColumnIndex, new List<int>());
|
---|
[10993] | 596 | selectedCells[cell.ColumnIndex].Add(cell.RowIndex);
|
---|
| 597 | }
|
---|
| 598 |
|
---|
[10590] | 599 | return selectedCells;
|
---|
| 600 | }
|
---|
| 601 |
|
---|
[15535] | 602 | private void ReplaceTransaction(Action action) {
|
---|
[10938] | 603 | SuspendRepaint();
|
---|
[15535] | 604 | action();
|
---|
[10947] | 605 | ResumeRepaint(true);
|
---|
[10938] | 606 | }
|
---|
| 607 |
|
---|
[10964] | 608 | private void btnSearch_Click(object sender, EventArgs e) {
|
---|
| 609 | CreateFindAndReplaceDialog();
|
---|
| 610 | findAndReplaceDialog.ActivateSearch();
|
---|
| 611 | }
|
---|
| 612 |
|
---|
| 613 | private void btnReplace_Click(object sender, EventArgs e) {
|
---|
| 614 | CreateFindAndReplaceDialog();
|
---|
| 615 | findAndReplaceDialog.ActivateReplace();
|
---|
| 616 | }
|
---|
| 617 |
|
---|
| 618 | #region ContextMenu Events
|
---|
[10769] | 619 | private void ReplaceWithAverage_Column_Click(object sender, EventArgs e) {
|
---|
[10938] | 620 | ReplaceTransaction(() => {
|
---|
[15535] | 621 | Content.ReplaceIndicesByMean(GetSelectedCells(), false);
|
---|
[10938] | 622 | });
|
---|
[10380] | 623 | }
|
---|
[10809] | 624 | private void ReplaceWithAverage_Selection_Click(object sender, EventArgs e) {
|
---|
[10938] | 625 | ReplaceTransaction(() => {
|
---|
[15535] | 626 | Content.ReplaceIndicesByMean(GetSelectedCells(), true);
|
---|
[10938] | 627 | });
|
---|
[10809] | 628 | }
|
---|
[10380] | 629 |
|
---|
[10769] | 630 | private void ReplaceWithMedian_Column_Click(object sender, EventArgs e) {
|
---|
[10938] | 631 | ReplaceTransaction(() => {
|
---|
[15535] | 632 | Content.ReplaceIndicesByMedianValue(GetSelectedCells(), false);
|
---|
[10938] | 633 | });
|
---|
[10380] | 634 | }
|
---|
[10809] | 635 | private void ReplaceWithMedian_Selection_Click(object sender, EventArgs e) {
|
---|
[10938] | 636 | ReplaceTransaction(() => {
|
---|
[15535] | 637 | Content.ReplaceIndicesByMedianValue(GetSelectedCells(), true);
|
---|
[10938] | 638 | });
|
---|
[10809] | 639 | }
|
---|
[10380] | 640 |
|
---|
[10769] | 641 | private void ReplaceWithRandom_Column_Click(object sender, EventArgs e) {
|
---|
[10938] | 642 | ReplaceTransaction(() => {
|
---|
[15535] | 643 | Content.ReplaceIndicesByRandomValue(GetSelectedCells(), false);
|
---|
[10938] | 644 | });
|
---|
[10380] | 645 | }
|
---|
[10809] | 646 | private void ReplaceWithRandom_Selection_Click(object sender, EventArgs e) {
|
---|
[10938] | 647 | ReplaceTransaction(() => {
|
---|
[15535] | 648 | Content.ReplaceIndicesByRandomValue(GetSelectedCells(), true);
|
---|
[10938] | 649 | });
|
---|
[10809] | 650 | }
|
---|
[10380] | 651 |
|
---|
[10769] | 652 | private void ReplaceWithMostCommon_Column_Click(object sender, EventArgs e) {
|
---|
[10938] | 653 | ReplaceTransaction(() => {
|
---|
[15535] | 654 | Content.ReplaceIndicesByMode(GetSelectedCells(), false);
|
---|
[10938] | 655 | });
|
---|
[10380] | 656 | }
|
---|
[10809] | 657 | private void ReplaceWithMostCommon_Selection_Click(object sender, EventArgs e) {
|
---|
[10938] | 658 | ReplaceTransaction(() => {
|
---|
[15535] | 659 | Content.ReplaceIndicesByMode(GetSelectedCells(), true);
|
---|
[10938] | 660 | });
|
---|
[10809] | 661 | }
|
---|
[10380] | 662 |
|
---|
[10769] | 663 | private void ReplaceWithInterpolation_Column_Click(object sender, EventArgs e) {
|
---|
[10938] | 664 | ReplaceTransaction(() => {
|
---|
[15535] | 665 | Content.ReplaceIndicesByLinearInterpolationOfNeighbours(GetSelectedCells());
|
---|
[10938] | 666 | });
|
---|
[10380] | 667 | }
|
---|
[10964] | 668 | #endregion
|
---|
[10769] | 669 |
|
---|
[13289] | 670 | private void addRowButton_Click(object sender, EventArgs e) {
|
---|
[15535] | 671 | Content.PreprocessingData.InsertRow(Content.Rows);
|
---|
[13289] | 672 | }
|
---|
| 673 |
|
---|
| 674 | private void addColumnButton_Click(object sender, EventArgs e) {
|
---|
[15535] | 675 | Content.PreprocessingData.InsertColumn<double>(Content.Columns.ToString(), Content.Columns);
|
---|
[13289] | 676 | }
|
---|
| 677 |
|
---|
| 678 | private void renameColumnsButton_Click(object sender, EventArgs e) {
|
---|
| 679 | var renameDialog = new RenameColumnsDialog(Content.ColumnNames);
|
---|
| 680 |
|
---|
| 681 | if (renameDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
[15535] | 682 | Content.PreprocessingData.RenameColumns(renameDialog.ColumnNames);
|
---|
[13289] | 683 | }
|
---|
| 684 | }
|
---|
[15242] | 685 |
|
---|
| 686 | private void checkInputsTargetButton_Click(object sender, EventArgs e) {
|
---|
| 687 | foreach (DataGridViewColumn column in DataGridView.Columns) {
|
---|
| 688 | var variable = column.HeaderText;
|
---|
[15535] | 689 | bool isInputTarget = Content.PreprocessingData.InputVariables.Contains(variable)
|
---|
| 690 | || Content.PreprocessingData.TargetVariable == variable;
|
---|
[15242] | 691 | column.Visible = isInputTarget;
|
---|
| 692 | }
|
---|
| 693 | }
|
---|
| 694 |
|
---|
| 695 | private void checkAllButton_Click(object sender, EventArgs e) {
|
---|
| 696 | foreach (DataGridViewColumn column in DataGridView.Columns) {
|
---|
| 697 | column.Visible = true;
|
---|
| 698 | }
|
---|
| 699 | }
|
---|
| 700 |
|
---|
| 701 | private void uncheckAllButton_Click(object sender, EventArgs e) {
|
---|
| 702 | foreach (DataGridViewColumn column in DataGridView.Columns) {
|
---|
| 703 | column.Visible = false;
|
---|
| 704 | }
|
---|
| 705 | }
|
---|
[15535] | 706 |
|
---|
| 707 | private void shuffleAllButton_Click(object sender, EventArgs e) {
|
---|
| 708 | Content.Shuffle(shuffleWithinPartitionsCheckBox.Checked);
|
---|
| 709 | }
|
---|
[10236] | 710 | }
|
---|
| 711 | }
|
---|