[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();
|
---|
[10380] | 60 | dataGridView.CellMouseClick += dataGridView_CellMouseClick;
|
---|
[13252] | 61 | dataGridView.RowHeaderMouseClick += dataGridView_RowHeaderMouseClick;
|
---|
[10668] | 62 | dataGridView.KeyDown += dataGridView_KeyDown;
|
---|
[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 |
|
---|
[12676] | 206 | protected override void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
| 207 | if (Content != null) {
|
---|
[13252] | 208 | if (e.Button == MouseButtons.Left) {
|
---|
[12676] | 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 | }
|
---|
[13252] | 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) {
|
---|
[12676] | 221 | SortColumn(e.ColumnIndex);
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
[10930] | 224 | searchIterator = null;
|
---|
| 225 | }
|
---|
[13252] | 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 | }
|
---|
[10930] | 234 |
|
---|
[10964] | 235 | private void dataGridView_MouseUp(object sender, MouseEventArgs e) {
|
---|
| 236 | if (!updateOnMouseUp)
|
---|
| 237 | return;
|
---|
[10900] | 238 |
|
---|
[10964] | 239 | updateOnMouseUp = false;
|
---|
| 240 | dataGridView_SelectionChanged(sender, e);
|
---|
[10236] | 241 | }
|
---|
[10253] | 242 |
|
---|
| 243 | private void btnApplySort_Click(object sender, System.EventArgs e) {
|
---|
[11098] | 244 | Content.ManipulationLogic.ReOrderToIndices(virtualRowIndices);
|
---|
| 245 | OnContentChanged();
|
---|
[10253] | 246 | }
|
---|
[10345] | 247 |
|
---|
[10698] | 248 | #region FindAndReplaceDialog
|
---|
[10585] | 249 |
|
---|
[10636] | 250 | private void CreateFindAndReplaceDialog() {
|
---|
[10876] | 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;
|
---|
[10930] | 266 | if (Content.FilterLogic.IsFiltered) {
|
---|
| 267 | findAndReplaceDialog.DisableReplace();
|
---|
| 268 | }
|
---|
[10852] | 269 | }
|
---|
[10636] | 270 | }
|
---|
| 271 |
|
---|
[10916] | 272 | private void DataGridView_SelectionChanged_FindAndReplace(object sender, EventArgs e) {
|
---|
[10863] | 273 | if (Content != null) {
|
---|
[10870] | 274 | if (!isSearching && AreMultipleCellsSelected()) {
|
---|
[10863] | 275 | ResetHighlightedCellsBackground();
|
---|
| 276 | HightlightedCellsBackground = GetSelectedCells();
|
---|
| 277 | searchIterator = null;
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 | }
|
---|
| 281 |
|
---|
[10705] | 282 | void findAndReplaceDialog_FormClosing(object sender, FormClosingEventArgs e) {
|
---|
[10719] | 283 | ResetHighlightedCellsBackground();
|
---|
[10852] | 284 | searchIterator = null;
|
---|
[10863] | 285 | DataGridView.SelectionChanged -= DataGridView_SelectionChanged_FindAndReplace;
|
---|
[10705] | 286 | }
|
---|
| 287 |
|
---|
[10672] | 288 | void findAndReplaceDialog_ReplaceEvent(object sender, EventArgs e) {
|
---|
[10712] | 289 | if (searchIterator != null && searchIterator.GetCurrent() != null) {
|
---|
[10706] | 290 | Replace(TransformToDictionary(currentCell));
|
---|
[10672] | 291 | }
|
---|
[10636] | 292 | }
|
---|
| 293 |
|
---|
| 294 | void findAndReplaceDialog_ReplaceAllEvent(object sender, EventArgs e) {
|
---|
[10672] | 295 | Replace(FindAll(findAndReplaceDialog.GetSearchText()));
|
---|
[10636] | 296 | }
|
---|
| 297 |
|
---|
| 298 | void findAndReplaceDialog_FindNextEvent(object sender, EventArgs e) {
|
---|
[10947] | 299 | if (searchIterator == null
|
---|
| 300 | || currentSearchText != findAndReplaceDialog.GetSearchText()
|
---|
| 301 | || currentComparisonOperation != findAndReplaceDialog.GetComparisonOperation()) {
|
---|
| 302 |
|
---|
[10698] | 303 | searchIterator = new FindPreprocessingItemsIterator(FindAll(findAndReplaceDialog.GetSearchText()));
|
---|
| 304 | currentSearchText = findAndReplaceDialog.GetSearchText();
|
---|
[10870] | 305 | currentComparisonOperation = findAndReplaceDialog.GetComparisonOperation();
|
---|
[10698] | 306 | }
|
---|
[10870] | 307 |
|
---|
[10852] | 308 | if (IsOneCellSelected()) {
|
---|
| 309 | var first = GetSelectedCells().First();
|
---|
| 310 | searchIterator.SetStartCell(first.Key, first.Value[0]);
|
---|
| 311 | }
|
---|
[10712] | 312 |
|
---|
[10705] | 313 | bool moreOccurences = false;
|
---|
[10870] | 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();
|
---|
[10852] | 321 | }
|
---|
[10870] | 322 | currentCell = searchIterator.GetCurrent();
|
---|
| 323 | moreOccurences = searchIterator.MoveNext();
|
---|
| 324 | if (!moreOccurences) {
|
---|
| 325 | searchIterator.Reset();
|
---|
| 326 | }
|
---|
[10852] | 327 | }
|
---|
[10870] | 328 | }
|
---|
[10672] | 329 |
|
---|
[10870] | 330 | dataGridView.ClearSelection();
|
---|
| 331 |
|
---|
[10705] | 332 | if (currentCell != null) {
|
---|
[10852] | 333 | dataGridView[currentCell.Item1, currentCell.Item2].Selected = true;
|
---|
[10946] | 334 | dataGridView.CurrentCell = dataGridView[currentCell.Item1, currentCell.Item2];
|
---|
[10698] | 335 | }
|
---|
[10852] | 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();
|
---|
[10706] | 350 | }
|
---|
[10852] | 351 | return count;
|
---|
[10636] | 352 | }
|
---|
| 353 |
|
---|
| 354 | void findAndReplaceDialog_FindAllEvent(object sender, EventArgs e) {
|
---|
[10870] | 355 | dataGridView.ClearSelection();
|
---|
| 356 | isSearching = true;
|
---|
[10901] | 357 | SuspendRepaint();
|
---|
[10916] | 358 | var selectedCells = FindAll(findAndReplaceDialog.GetSearchText());
|
---|
| 359 | foreach (var column in selectedCells) {
|
---|
[10870] | 360 | foreach (var cell in column.Value) {
|
---|
| 361 | dataGridView[column.Key, cell].Selected = true;
|
---|
| 362 | }
|
---|
| 363 | }
|
---|
[10901] | 364 | ResumeRepaint(true);
|
---|
[10870] | 365 | isSearching = false;
|
---|
[11002] | 366 | Content.Selection = selectedCells;
|
---|
[10916] | 367 | //update statistic in base
|
---|
| 368 | base.dataGridView_SelectionChanged(sender, e);
|
---|
[10636] | 369 | }
|
---|
| 370 |
|
---|
[10870] | 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 |
|
---|
[10672] | 396 | private IDictionary<int, IList<int>> FindAll(string match) {
|
---|
[10719] | 397 | bool searchInSelection = HightlightedCellsBackground.Values.Sum(list => list.Count) > 1;
|
---|
[10870] | 398 | ComparisonOperation comparisonOperation = findAndReplaceDialog.GetComparisonOperation();
|
---|
[10672] | 399 | var foundCells = new Dictionary<int, IList<int>>();
|
---|
| 400 | for (int i = 0; i < Content.FilterLogic.PreprocessingData.Columns; i++) {
|
---|
[10947] | 401 | var filters = CreateFilters(match, comparisonOperation, i);
|
---|
| 402 |
|
---|
[10844] | 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;
|
---|
[10719] | 412 | IList<int> selectedList;
|
---|
| 413 | if (searchInSelection && HightlightedCellsBackground.TryGetValue(i, out selectedList)) {
|
---|
[10739] | 414 | foundCells[i] = foundCells[i].Intersect(selectedList).ToList<int>();
|
---|
| 415 | } else if (searchInSelection) {
|
---|
[10719] | 416 | foundCells[i].Clear();
|
---|
| 417 | }
|
---|
[10672] | 418 | }
|
---|
[10947] | 419 | return MapToSorting(foundCells);
|
---|
[10672] | 420 | }
|
---|
| 421 |
|
---|
[10947] | 422 | private List<IFilter> CreateFilters(string match, ComparisonOperation comparisonOperation, int columnIndex) {
|
---|
| 423 | IPreprocessingData preprocessingData = Content.FilterLogic.PreprocessingData;
|
---|
| 424 | IStringConvertibleValue value;
|
---|
[11156] | 425 | if (preprocessingData.VariableHasType<double>(columnIndex)) {
|
---|
[10947] | 426 | value = new DoubleValue();
|
---|
[11156] | 427 | } else if (preprocessingData.VariableHasType<String>(columnIndex)) {
|
---|
[10947] | 428 | value = new StringValue();
|
---|
[11156] | 429 | } else if (preprocessingData.VariableHasType<DateTime>(columnIndex)) {
|
---|
[10947] | 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 |
|
---|
[10672] | 463 | private void Replace(IDictionary<int, IList<int>> cells) {
|
---|
| 464 | if (findAndReplaceDialog != null) {
|
---|
[10938] | 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 | });
|
---|
[10672] | 487 | }
|
---|
| 488 | }
|
---|
| 489 |
|
---|
[10698] | 490 | private IDictionary<int, IList<int>> TransformToDictionary(Tuple<int, int> tuple) {
|
---|
[10672] | 491 | var highlightCells = new Dictionary<int, IList<int>>();
|
---|
[10698] | 492 | highlightCells.Add(tuple.Item1, new List<int>() { tuple.Item2 });
|
---|
[10672] | 493 | return highlightCells;
|
---|
| 494 | }
|
---|
| 495 |
|
---|
[10719] | 496 | private void ResetHighlightedCellsBackground() {
|
---|
| 497 | HightlightedCellsBackground = new Dictionary<int, IList<int>>();
|
---|
| 498 | }
|
---|
| 499 |
|
---|
[10698] | 500 | #endregion FindAndReplaceDialog
|
---|
| 501 |
|
---|
[10380] | 502 | private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
| 503 | if (Content == null) return;
|
---|
[13252] | 504 | if (e.Button == MouseButtons.Right && !(e.ColumnIndex != -1 && e.RowIndex == -1)) {
|
---|
[10380] | 505 | if (e.ColumnIndex == -1 || e.RowIndex == -1) {
|
---|
[10769] | 506 | replaceValueOverColumnToolStripMenuItem.Visible = false;
|
---|
[10627] | 507 | contextMenuCell.Show(MousePosition);
|
---|
[10380] | 508 | } else {
|
---|
[10590] | 509 | if (!dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, e.RowIndex])) {
|
---|
| 510 | dataGridView.ClearSelection();
|
---|
| 511 | dataGridView[e.ColumnIndex, e.RowIndex].Selected = true;
|
---|
| 512 | }
|
---|
[10812] | 513 |
|
---|
[10621] | 514 | var columnIndices = new HashSet<int>();
|
---|
| 515 | for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
|
---|
| 516 | columnIndices.Add(dataGridView.SelectedCells[i].ColumnIndex);
|
---|
| 517 | }
|
---|
[10875] | 518 |
|
---|
| 519 | replaceValueOverSelectionToolStripMenuItem.Enabled = AreMultipleCellsSelected();
|
---|
| 520 |
|
---|
[10812] | 521 | averageToolStripMenuItem_Column.Enabled =
|
---|
| 522 | averageToolStripMenuItem_Selection.Enabled =
|
---|
| 523 | medianToolStripMenuItem_Column.Enabled =
|
---|
| 524 | medianToolStripMenuItem_Selection.Enabled =
|
---|
| 525 | randomToolStripMenuItem_Column.Enabled =
|
---|
[11002] | 526 | randomToolStripMenuItem_Selection.Enabled = !Content.PreProcessingData.AreAllStringColumns(columnIndices);
|
---|
[10812] | 527 |
|
---|
| 528 | smoothingToolStripMenuItem_Column.Enabled =
|
---|
[10820] | 529 | interpolationToolStripMenuItem_Column.Enabled = !dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, 0])
|
---|
| 530 | && !dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, Content.Rows - 1])
|
---|
[11002] | 531 | && !Content.PreProcessingData.AreAllStringColumns(columnIndices);
|
---|
[10812] | 532 |
|
---|
[10769] | 533 | replaceValueOverColumnToolStripMenuItem.Visible = true;
|
---|
[10380] | 534 | contextMenuCell.Show(MousePosition);
|
---|
| 535 | }
|
---|
| 536 | }
|
---|
| 537 | }
|
---|
| 538 |
|
---|
[14140] | 539 | protected override void dataGridView_KeyDown(object sender, KeyEventArgs e) {
|
---|
[10668] | 540 | var selectedRows = dataGridView.SelectedRows;
|
---|
[12676] | 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();
|
---|
[10668] | 559 | }
|
---|
| 560 | }
|
---|
| 561 | }
|
---|
| 562 |
|
---|
[10672] | 563 | private IDictionary<int, IList<int>> GetSelectedCells() {
|
---|
| 564 | IDictionary<int, IList<int>> selectedCells = new Dictionary<int, IList<int>>();
|
---|
[10993] | 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;
|
---|
[10590] | 571 | }
|
---|
[10993] | 572 |
|
---|
| 573 | foreach (var selectedCell in dataGridView.SelectedCells) {
|
---|
[11037] | 574 | var cell = (DataGridViewCell)selectedCell;
|
---|
| 575 | if (!selectedCells.ContainsKey(cell.ColumnIndex))
|
---|
[10993] | 576 | selectedCells.Add(cell.ColumnIndex, new List<int>(1024));
|
---|
| 577 | selectedCells[cell.ColumnIndex].Add(cell.RowIndex);
|
---|
| 578 | }
|
---|
| 579 |
|
---|
[10590] | 580 | return selectedCells;
|
---|
| 581 | }
|
---|
| 582 |
|
---|
[10938] | 583 | private void StartReplacing() {
|
---|
| 584 | SuspendRepaint();
|
---|
| 585 | }
|
---|
| 586 |
|
---|
| 587 | private void StopReplacing() {
|
---|
[10947] | 588 | ResumeRepaint(true);
|
---|
[10938] | 589 | }
|
---|
| 590 |
|
---|
| 591 | private void ReplaceTransaction(Action action) {
|
---|
| 592 | StartReplacing();
|
---|
| 593 | action();
|
---|
| 594 | StopReplacing();
|
---|
| 595 | }
|
---|
| 596 |
|
---|
[10964] | 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 |
|
---|
[10769] | 609 | private void ReplaceWithAverage_Column_Click(object sender, EventArgs e) {
|
---|
[10938] | 610 | ReplaceTransaction(() => {
|
---|
| 611 | Content.ManipulationLogic.ReplaceIndicesByAverageValue(GetSelectedCells(), false);
|
---|
| 612 | });
|
---|
[10380] | 613 | }
|
---|
[10809] | 614 | private void ReplaceWithAverage_Selection_Click(object sender, EventArgs e) {
|
---|
[10938] | 615 | ReplaceTransaction(() => {
|
---|
| 616 | Content.ManipulationLogic.ReplaceIndicesByAverageValue(GetSelectedCells(), true);
|
---|
| 617 | });
|
---|
[10809] | 618 | }
|
---|
[10380] | 619 |
|
---|
[10769] | 620 | private void ReplaceWithMedian_Column_Click(object sender, EventArgs e) {
|
---|
[10938] | 621 | ReplaceTransaction(() => {
|
---|
| 622 | Content.ManipulationLogic.ReplaceIndicesByMedianValue(GetSelectedCells(), false);
|
---|
| 623 | });
|
---|
[10380] | 624 | }
|
---|
[10809] | 625 | private void ReplaceWithMedian_Selection_Click(object sender, EventArgs e) {
|
---|
[10938] | 626 | ReplaceTransaction(() => {
|
---|
| 627 | Content.ManipulationLogic.ReplaceIndicesByMedianValue(GetSelectedCells(), true);
|
---|
| 628 | });
|
---|
[10809] | 629 | }
|
---|
[10380] | 630 |
|
---|
[10769] | 631 | private void ReplaceWithRandom_Column_Click(object sender, EventArgs e) {
|
---|
[10938] | 632 | ReplaceTransaction(() => {
|
---|
| 633 | Content.ManipulationLogic.ReplaceIndicesByRandomValue(GetSelectedCells(), false);
|
---|
| 634 | });
|
---|
[10380] | 635 | }
|
---|
[10809] | 636 | private void ReplaceWithRandom_Selection_Click(object sender, EventArgs e) {
|
---|
[10938] | 637 | ReplaceTransaction(() => {
|
---|
| 638 | Content.ManipulationLogic.ReplaceIndicesByRandomValue(GetSelectedCells(), true);
|
---|
| 639 | });
|
---|
[10809] | 640 | }
|
---|
[10380] | 641 |
|
---|
[10769] | 642 | private void ReplaceWithMostCommon_Column_Click(object sender, EventArgs e) {
|
---|
[10938] | 643 | ReplaceTransaction(() => {
|
---|
| 644 | Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(GetSelectedCells(), false);
|
---|
| 645 | });
|
---|
[10380] | 646 | }
|
---|
[10809] | 647 | private void ReplaceWithMostCommon_Selection_Click(object sender, EventArgs e) {
|
---|
[10938] | 648 | ReplaceTransaction(() => {
|
---|
| 649 | Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(GetSelectedCells(), true);
|
---|
| 650 | });
|
---|
[10809] | 651 | }
|
---|
[10380] | 652 |
|
---|
[10769] | 653 | private void ReplaceWithInterpolation_Column_Click(object sender, EventArgs e) {
|
---|
[10938] | 654 | ReplaceTransaction(() => {
|
---|
| 655 | Content.ManipulationLogic.ReplaceIndicesByLinearInterpolationOfNeighbours(GetSelectedCells());
|
---|
| 656 | });
|
---|
[10380] | 657 | }
|
---|
[10769] | 658 |
|
---|
| 659 | private void ReplaceWithSmoothing_Selection_Click(object sender, EventArgs e) {
|
---|
[10938] | 660 | ReplaceTransaction(() => {
|
---|
| 661 | Content.ManipulationLogic.ReplaceIndicesBySmoothing(GetSelectedCells());
|
---|
| 662 | });
|
---|
[10769] | 663 | }
|
---|
[10964] | 664 | #endregion
|
---|
[10769] | 665 |
|
---|
[13252] | 666 | private void addRowButton_Click(object sender, EventArgs e) {
|
---|
[12986] | 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 |
|
---|
[13252] | 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 | }
|
---|
[14546] | 681 |
|
---|
| 682 | private void checkInputsTargetButton_Click(object sender, EventArgs e) {
|
---|
| 683 | foreach (DataGridViewColumn column in DataGridView.Columns) {
|
---|
| 684 | var variable = column.HeaderText;
|
---|
| 685 | bool isInputTarget = Content.PreProcessingData.InputVariables.Contains(variable)
|
---|
| 686 | || Content.PreProcessingData.TargetVariable == variable;
|
---|
| 687 | column.Visible = isInputTarget;
|
---|
| 688 | }
|
---|
| 689 | }
|
---|
| 690 |
|
---|
| 691 | private void checkAllButton_Click(object sender, EventArgs e) {
|
---|
| 692 | foreach (DataGridViewColumn column in DataGridView.Columns) {
|
---|
| 693 | column.Visible = true;
|
---|
| 694 | }
|
---|
| 695 | }
|
---|
| 696 |
|
---|
| 697 | private void uncheckAllButton_Click(object sender, EventArgs e) {
|
---|
| 698 | foreach (DataGridViewColumn column in DataGridView.Columns) {
|
---|
| 699 | column.Visible = false;
|
---|
| 700 | }
|
---|
| 701 | }
|
---|
[10236] | 702 | }
|
---|
| 703 | }
|
---|