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