[10539] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12009] | 3 | * Copyright (C) 2002-2015 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")]
|
---|
[10311] | 33 | [Content(typeof(IDataGridContent), 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 |
|
---|
[10236] | 44 | public new IDataGridContent Content {
|
---|
| 45 | get { return (IDataGridContent)base.Content; }
|
---|
| 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;
|
---|
[10668] | 61 | dataGridView.KeyDown += dataGridView_KeyDown;
|
---|
[10934] | 62 | dataGridView.MouseUp += dataGridView_MouseUp;
|
---|
[10947] | 63 | dataGridView.ColumnHeaderMouseClick += dataGridView_ColumnHeaderMouseClick;
|
---|
[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;
|
---|
| 129 | if (Content != null) {
|
---|
| 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 |
|
---|
[11098] | 145 | //protected override void PasteValuesToDataGridView() {
|
---|
| 146 | // base.PasteValuesToDataGridView();
|
---|
| 147 | // dataGridView.Refresh();
|
---|
| 148 | //}
|
---|
[10964] | 149 |
|
---|
| 150 | protected override void SetEnabledStateOfControls() {
|
---|
| 151 | base.SetEnabledStateOfControls();
|
---|
| 152 | rowsTextBox.ReadOnly = true;
|
---|
| 153 | columnsTextBox.ReadOnly = true;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
|
---|
| 157 | btnApplySort.Enabled = sortedColumns.Any();
|
---|
| 158 | return base.Sort(sortedColumns);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | protected override void ClearSorting() {
|
---|
| 162 | btnApplySort.Enabled = false;
|
---|
| 163 | base.ClearSorting();
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[10965] | 166 | private void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
[10930] | 167 | searchIterator = null;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[10964] | 170 | private void dataGridView_MouseUp(object sender, MouseEventArgs e) {
|
---|
| 171 | if (!updateOnMouseUp)
|
---|
| 172 | return;
|
---|
[10900] | 173 |
|
---|
[10964] | 174 | updateOnMouseUp = false;
|
---|
| 175 | dataGridView_SelectionChanged(sender, e);
|
---|
[10236] | 176 | }
|
---|
[10253] | 177 |
|
---|
| 178 | private void btnApplySort_Click(object sender, System.EventArgs e) {
|
---|
[11098] | 179 | Content.ManipulationLogic.ReOrderToIndices(virtualRowIndices);
|
---|
| 180 | OnContentChanged();
|
---|
[10253] | 181 | }
|
---|
[10345] | 182 |
|
---|
[10698] | 183 | #region FindAndReplaceDialog
|
---|
[10585] | 184 |
|
---|
[10636] | 185 | private void CreateFindAndReplaceDialog() {
|
---|
[10876] | 186 | if (findAndReplaceDialog == null || findAndReplaceDialog.IsDisposed) {
|
---|
| 187 | findAndReplaceDialog = new SearchAndReplaceDialog();
|
---|
| 188 | findAndReplaceDialog.Show(this);
|
---|
| 189 | if (AreMultipleCellsSelected()) {
|
---|
| 190 | ResetHighlightedCellsBackground();
|
---|
| 191 | HightlightedCellsBackground = GetSelectedCells();
|
---|
| 192 | dataGridView.ClearSelection();
|
---|
| 193 | }
|
---|
| 194 | findAndReplaceDialog.FindAllEvent += findAndReplaceDialog_FindAllEvent;
|
---|
| 195 | findAndReplaceDialog.FindNextEvent += findAndReplaceDialog_FindNextEvent;
|
---|
| 196 | findAndReplaceDialog.ReplaceAllEvent += findAndReplaceDialog_ReplaceAllEvent;
|
---|
| 197 | findAndReplaceDialog.ReplaceNextEvent += findAndReplaceDialog_ReplaceEvent;
|
---|
| 198 | findAndReplaceDialog.FormClosing += findAndReplaceDialog_FormClosing;
|
---|
| 199 | searchIterator = null;
|
---|
| 200 | DataGridView.SelectionChanged += DataGridView_SelectionChanged_FindAndReplace;
|
---|
[10930] | 201 | if (Content.FilterLogic.IsFiltered) {
|
---|
| 202 | findAndReplaceDialog.DisableReplace();
|
---|
| 203 | }
|
---|
[10852] | 204 | }
|
---|
[10636] | 205 | }
|
---|
| 206 |
|
---|
[10916] | 207 | private void DataGridView_SelectionChanged_FindAndReplace(object sender, EventArgs e) {
|
---|
[10863] | 208 | if (Content != null) {
|
---|
[10870] | 209 | if (!isSearching && AreMultipleCellsSelected()) {
|
---|
[10863] | 210 | ResetHighlightedCellsBackground();
|
---|
| 211 | HightlightedCellsBackground = GetSelectedCells();
|
---|
| 212 | searchIterator = null;
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[10705] | 217 | void findAndReplaceDialog_FormClosing(object sender, FormClosingEventArgs e) {
|
---|
[10719] | 218 | ResetHighlightedCellsBackground();
|
---|
[10852] | 219 | searchIterator = null;
|
---|
[10863] | 220 | DataGridView.SelectionChanged -= DataGridView_SelectionChanged_FindAndReplace;
|
---|
[10705] | 221 | }
|
---|
| 222 |
|
---|
[10672] | 223 | void findAndReplaceDialog_ReplaceEvent(object sender, EventArgs e) {
|
---|
[10712] | 224 | if (searchIterator != null && searchIterator.GetCurrent() != null) {
|
---|
[10706] | 225 | Replace(TransformToDictionary(currentCell));
|
---|
[10672] | 226 | }
|
---|
[10636] | 227 | }
|
---|
| 228 |
|
---|
| 229 | void findAndReplaceDialog_ReplaceAllEvent(object sender, EventArgs e) {
|
---|
[10672] | 230 | Replace(FindAll(findAndReplaceDialog.GetSearchText()));
|
---|
[10636] | 231 | }
|
---|
| 232 |
|
---|
| 233 | void findAndReplaceDialog_FindNextEvent(object sender, EventArgs e) {
|
---|
[10947] | 234 | if (searchIterator == null
|
---|
| 235 | || currentSearchText != findAndReplaceDialog.GetSearchText()
|
---|
| 236 | || currentComparisonOperation != findAndReplaceDialog.GetComparisonOperation()) {
|
---|
| 237 |
|
---|
[10698] | 238 | searchIterator = new FindPreprocessingItemsIterator(FindAll(findAndReplaceDialog.GetSearchText()));
|
---|
| 239 | currentSearchText = findAndReplaceDialog.GetSearchText();
|
---|
[10870] | 240 | currentComparisonOperation = findAndReplaceDialog.GetComparisonOperation();
|
---|
[10698] | 241 | }
|
---|
[10870] | 242 |
|
---|
[10852] | 243 | if (IsOneCellSelected()) {
|
---|
| 244 | var first = GetSelectedCells().First();
|
---|
| 245 | searchIterator.SetStartCell(first.Key, first.Value[0]);
|
---|
| 246 | }
|
---|
[10712] | 247 |
|
---|
[10705] | 248 | bool moreOccurences = false;
|
---|
[10870] | 249 | currentCell = searchIterator.GetCurrent();
|
---|
| 250 | moreOccurences = searchIterator.MoveNext();
|
---|
| 251 | if (IsOneCellSelected() && currentCell != null) {
|
---|
| 252 | var first = GetSelectedCells().First();
|
---|
| 253 | if (currentCell.Item1 == first.Key && currentCell.Item2 == first.Value[0]) {
|
---|
| 254 | if (!moreOccurences) {
|
---|
| 255 | searchIterator.Reset();
|
---|
[10852] | 256 | }
|
---|
[10870] | 257 | currentCell = searchIterator.GetCurrent();
|
---|
| 258 | moreOccurences = searchIterator.MoveNext();
|
---|
| 259 | if (!moreOccurences) {
|
---|
| 260 | searchIterator.Reset();
|
---|
| 261 | }
|
---|
[10852] | 262 | }
|
---|
[10870] | 263 | }
|
---|
[10672] | 264 |
|
---|
[10870] | 265 | dataGridView.ClearSelection();
|
---|
| 266 |
|
---|
[10705] | 267 | if (currentCell != null) {
|
---|
[10852] | 268 | dataGridView[currentCell.Item1, currentCell.Item2].Selected = true;
|
---|
[10946] | 269 | dataGridView.CurrentCell = dataGridView[currentCell.Item1, currentCell.Item2];
|
---|
[10698] | 270 | }
|
---|
[10852] | 271 | }
|
---|
| 272 |
|
---|
| 273 | private bool AreMultipleCellsSelected() {
|
---|
| 274 | return GetSelectedCellCount() > 1;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | private bool IsOneCellSelected() {
|
---|
| 278 | return GetSelectedCellCount() == 1;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | private int GetSelectedCellCount() {
|
---|
| 282 | int count = 0;
|
---|
| 283 | foreach (var column in GetSelectedCells()) {
|
---|
| 284 | count += column.Value.Count();
|
---|
[10706] | 285 | }
|
---|
[10852] | 286 | return count;
|
---|
[10636] | 287 | }
|
---|
| 288 |
|
---|
| 289 | void findAndReplaceDialog_FindAllEvent(object sender, EventArgs e) {
|
---|
[10870] | 290 | dataGridView.ClearSelection();
|
---|
| 291 | isSearching = true;
|
---|
[10901] | 292 | SuspendRepaint();
|
---|
[10916] | 293 | var selectedCells = FindAll(findAndReplaceDialog.GetSearchText());
|
---|
| 294 | foreach (var column in selectedCells) {
|
---|
[10870] | 295 | foreach (var cell in column.Value) {
|
---|
| 296 | dataGridView[column.Key, cell].Selected = true;
|
---|
| 297 | }
|
---|
| 298 | }
|
---|
[10901] | 299 | ResumeRepaint(true);
|
---|
[10870] | 300 | isSearching = false;
|
---|
[11002] | 301 | Content.Selection = selectedCells;
|
---|
[10916] | 302 | //update statistic in base
|
---|
| 303 | base.dataGridView_SelectionChanged(sender, e);
|
---|
[10636] | 304 | }
|
---|
| 305 |
|
---|
[10870] | 306 | private Core.ConstraintOperation GetConstraintOperation(ComparisonOperation comparisonOperation) {
|
---|
| 307 | Core.ConstraintOperation constraintOperation = Core.ConstraintOperation.Equal;
|
---|
| 308 | switch (comparisonOperation) {
|
---|
| 309 | case ComparisonOperation.Equal:
|
---|
| 310 | constraintOperation = Core.ConstraintOperation.Equal;
|
---|
| 311 | break;
|
---|
| 312 | case ComparisonOperation.Greater:
|
---|
| 313 | constraintOperation = Core.ConstraintOperation.Greater;
|
---|
| 314 | break;
|
---|
| 315 | case ComparisonOperation.GreaterOrEqual:
|
---|
| 316 | constraintOperation = Core.ConstraintOperation.GreaterOrEqual;
|
---|
| 317 | break;
|
---|
| 318 | case ComparisonOperation.Less:
|
---|
| 319 | constraintOperation = Core.ConstraintOperation.Less;
|
---|
| 320 | break;
|
---|
| 321 | case ComparisonOperation.LessOrEqual:
|
---|
| 322 | constraintOperation = Core.ConstraintOperation.LessOrEqual;
|
---|
| 323 | break;
|
---|
| 324 | case ComparisonOperation.NotEqual:
|
---|
| 325 | constraintOperation = Core.ConstraintOperation.NotEqual;
|
---|
| 326 | break;
|
---|
| 327 | }
|
---|
| 328 | return constraintOperation;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
[10672] | 331 | private IDictionary<int, IList<int>> FindAll(string match) {
|
---|
[10719] | 332 | bool searchInSelection = HightlightedCellsBackground.Values.Sum(list => list.Count) > 1;
|
---|
[10870] | 333 | ComparisonOperation comparisonOperation = findAndReplaceDialog.GetComparisonOperation();
|
---|
[10672] | 334 | var foundCells = new Dictionary<int, IList<int>>();
|
---|
| 335 | for (int i = 0; i < Content.FilterLogic.PreprocessingData.Columns; i++) {
|
---|
[10947] | 336 | var filters = CreateFilters(match, comparisonOperation, i);
|
---|
| 337 |
|
---|
[10844] | 338 | bool[] filteredRows = Content.FilterLogic.GetFilterResult(filters, true);
|
---|
| 339 | var foundIndices = new List<int>();
|
---|
| 340 | for (int idx = 0; idx < filteredRows.Length; ++idx) {
|
---|
| 341 | var notFilteredThusFound = !filteredRows[idx];
|
---|
| 342 | if (notFilteredThusFound) {
|
---|
| 343 | foundIndices.Add(idx);
|
---|
| 344 | }
|
---|
| 345 | }
|
---|
| 346 | foundCells[i] = foundIndices;
|
---|
[10719] | 347 | IList<int> selectedList;
|
---|
| 348 | if (searchInSelection && HightlightedCellsBackground.TryGetValue(i, out selectedList)) {
|
---|
[10739] | 349 | foundCells[i] = foundCells[i].Intersect(selectedList).ToList<int>();
|
---|
| 350 | } else if (searchInSelection) {
|
---|
[10719] | 351 | foundCells[i].Clear();
|
---|
| 352 | }
|
---|
[10672] | 353 | }
|
---|
[10947] | 354 | return MapToSorting(foundCells);
|
---|
[10672] | 355 | }
|
---|
| 356 |
|
---|
[10947] | 357 | private List<IFilter> CreateFilters(string match, ComparisonOperation comparisonOperation, int columnIndex) {
|
---|
| 358 | IPreprocessingData preprocessingData = Content.FilterLogic.PreprocessingData;
|
---|
| 359 | IStringConvertibleValue value;
|
---|
[11159] | 360 | if (preprocessingData.VariableHasType<double>(columnIndex)) {
|
---|
[10947] | 361 | value = new DoubleValue();
|
---|
[11159] | 362 | } else if (preprocessingData.VariableHasType<String>(columnIndex)) {
|
---|
[10947] | 363 | value = new StringValue();
|
---|
[11159] | 364 | } else if (preprocessingData.VariableHasType<DateTime>(columnIndex)) {
|
---|
[10947] | 365 | value = new DateTimeValue();
|
---|
| 366 | } else {
|
---|
| 367 | throw new ArgumentException("unsupported type");
|
---|
| 368 | }
|
---|
| 369 | value.SetValue(match);
|
---|
| 370 | var comparisonFilter = new ComparisonFilter(preprocessingData, GetConstraintOperation(comparisonOperation), value, true);
|
---|
| 371 | comparisonFilter.ConstraintColumn = columnIndex;
|
---|
| 372 | return new List<Filter.IFilter>() { comparisonFilter };
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | private IDictionary<int, IList<int>> MapToSorting(Dictionary<int, IList<int>> foundCells) {
|
---|
| 376 | if (sortedColumnIndices.Count == 0) {
|
---|
| 377 | return foundCells;
|
---|
| 378 | } else {
|
---|
| 379 | var sortedFoundCells = new Dictionary<int, IList<int>>();
|
---|
| 380 |
|
---|
| 381 | var indicesToVirtual = new Dictionary<int, int>();
|
---|
| 382 | for (int i = 0; i < virtualRowIndices.Length; ++i) {
|
---|
| 383 | indicesToVirtual.Add(virtualRowIndices[i], i);
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | foreach (var entry in foundCells) {
|
---|
| 387 | var cells = new List<int>();
|
---|
| 388 | foreach (var cell in entry.Value) {
|
---|
| 389 | cells.Add(indicesToVirtual[cell]);
|
---|
| 390 | }
|
---|
| 391 | cells.Sort();
|
---|
| 392 | sortedFoundCells.Add(entry.Key, cells);
|
---|
| 393 | }
|
---|
| 394 | return sortedFoundCells;
|
---|
| 395 | }
|
---|
| 396 | }
|
---|
| 397 |
|
---|
[10672] | 398 | private void Replace(IDictionary<int, IList<int>> cells) {
|
---|
| 399 | if (findAndReplaceDialog != null) {
|
---|
[10938] | 400 | ReplaceTransaction(() => {
|
---|
| 401 | switch (findAndReplaceDialog.GetReplaceAction()) {
|
---|
| 402 | case ReplaceAction.Value:
|
---|
| 403 | Content.ManipulationLogic.ReplaceIndicesByValue(cells, findAndReplaceDialog.GetReplaceText());
|
---|
| 404 | break;
|
---|
| 405 | case ReplaceAction.Average:
|
---|
| 406 | Content.ManipulationLogic.ReplaceIndicesByAverageValue(cells, false);
|
---|
| 407 | break;
|
---|
| 408 | case ReplaceAction.Median:
|
---|
| 409 | Content.ManipulationLogic.ReplaceIndicesByMedianValue(cells, false);
|
---|
| 410 | break;
|
---|
| 411 | case ReplaceAction.Random:
|
---|
| 412 | Content.ManipulationLogic.ReplaceIndicesByRandomValue(cells, false);
|
---|
| 413 | break;
|
---|
| 414 | case ReplaceAction.MostCommon:
|
---|
| 415 | Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(cells, false);
|
---|
| 416 | break;
|
---|
| 417 | case ReplaceAction.Interpolation:
|
---|
| 418 | Content.ManipulationLogic.ReplaceIndicesByLinearInterpolationOfNeighbours(cells);
|
---|
| 419 | break;
|
---|
| 420 | }
|
---|
| 421 | });
|
---|
[10672] | 422 | }
|
---|
| 423 | }
|
---|
| 424 |
|
---|
[10698] | 425 | private IDictionary<int, IList<int>> TransformToDictionary(Tuple<int, int> tuple) {
|
---|
[10672] | 426 | var highlightCells = new Dictionary<int, IList<int>>();
|
---|
[10698] | 427 | highlightCells.Add(tuple.Item1, new List<int>() { tuple.Item2 });
|
---|
[10672] | 428 | return highlightCells;
|
---|
| 429 | }
|
---|
| 430 |
|
---|
[10719] | 431 | private void ResetHighlightedCellsBackground() {
|
---|
| 432 | HightlightedCellsBackground = new Dictionary<int, IList<int>>();
|
---|
| 433 | }
|
---|
| 434 |
|
---|
[10698] | 435 | #endregion FindAndReplaceDialog
|
---|
| 436 |
|
---|
[10380] | 437 | private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
| 438 | if (Content == null) return;
|
---|
| 439 | if (e.Button == System.Windows.Forms.MouseButtons.Right) {
|
---|
| 440 | if (e.ColumnIndex == -1 || e.RowIndex == -1) {
|
---|
[10769] | 441 | replaceValueOverColumnToolStripMenuItem.Visible = false;
|
---|
[10627] | 442 | contextMenuCell.Show(MousePosition);
|
---|
[10380] | 443 | } else {
|
---|
[10590] | 444 | if (!dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, e.RowIndex])) {
|
---|
| 445 | dataGridView.ClearSelection();
|
---|
| 446 | dataGridView[e.ColumnIndex, e.RowIndex].Selected = true;
|
---|
| 447 | }
|
---|
[10812] | 448 |
|
---|
[10621] | 449 | var columnIndices = new HashSet<int>();
|
---|
| 450 | for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
|
---|
| 451 | columnIndices.Add(dataGridView.SelectedCells[i].ColumnIndex);
|
---|
| 452 | }
|
---|
[10875] | 453 |
|
---|
| 454 | replaceValueOverSelectionToolStripMenuItem.Enabled = AreMultipleCellsSelected();
|
---|
| 455 |
|
---|
[10812] | 456 | averageToolStripMenuItem_Column.Enabled =
|
---|
| 457 | averageToolStripMenuItem_Selection.Enabled =
|
---|
| 458 | medianToolStripMenuItem_Column.Enabled =
|
---|
| 459 | medianToolStripMenuItem_Selection.Enabled =
|
---|
| 460 | randomToolStripMenuItem_Column.Enabled =
|
---|
[11002] | 461 | randomToolStripMenuItem_Selection.Enabled = !Content.PreProcessingData.AreAllStringColumns(columnIndices);
|
---|
[10812] | 462 |
|
---|
| 463 | smoothingToolStripMenuItem_Column.Enabled =
|
---|
[10820] | 464 | interpolationToolStripMenuItem_Column.Enabled = !dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, 0])
|
---|
| 465 | && !dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, Content.Rows - 1])
|
---|
[11002] | 466 | && !Content.PreProcessingData.AreAllStringColumns(columnIndices);
|
---|
[10812] | 467 |
|
---|
[10769] | 468 | replaceValueOverColumnToolStripMenuItem.Visible = true;
|
---|
[10380] | 469 | contextMenuCell.Show(MousePosition);
|
---|
| 470 | }
|
---|
| 471 | }
|
---|
| 472 | }
|
---|
| 473 |
|
---|
[10964] | 474 | private void dataGridView_KeyDown(object sender, KeyEventArgs e) {
|
---|
[10668] | 475 | var selectedRows = dataGridView.SelectedRows;
|
---|
| 476 | if (e.KeyCode == Keys.Delete && selectedRows.Count > 0) {
|
---|
| 477 | List<int> rows = new List<int>();
|
---|
| 478 | for (int i = 0; i < selectedRows.Count; ++i) {
|
---|
| 479 | rows.Add(selectedRows[i].Index);
|
---|
| 480 | }
|
---|
[11098] | 481 | Content.DeleteRow(rows);
|
---|
[10668] | 482 | } else if (e.Control && e.KeyCode == Keys.F) {
|
---|
| 483 | CreateFindAndReplaceDialog();
|
---|
| 484 | findAndReplaceDialog.ActivateSearch();
|
---|
| 485 | } else if (e.Control && e.KeyCode == Keys.R) {
|
---|
| 486 | CreateFindAndReplaceDialog();
|
---|
| 487 | findAndReplaceDialog.ActivateReplace();
|
---|
| 488 | }
|
---|
| 489 | }
|
---|
| 490 |
|
---|
[10672] | 491 | private IDictionary<int, IList<int>> GetSelectedCells() {
|
---|
| 492 | IDictionary<int, IList<int>> selectedCells = new Dictionary<int, IList<int>>();
|
---|
[10993] | 493 |
|
---|
| 494 | //special case if all cells are selected
|
---|
| 495 | if (dataGridView.AreAllCellsSelected(true)) {
|
---|
| 496 | for (int i = 0; i < Content.Columns; i++)
|
---|
| 497 | selectedCells[i] = Enumerable.Range(0, Content.Rows).ToList();
|
---|
| 498 | return selectedCells;
|
---|
[10590] | 499 | }
|
---|
[10993] | 500 |
|
---|
| 501 | foreach (var selectedCell in dataGridView.SelectedCells) {
|
---|
[11037] | 502 | var cell = (DataGridViewCell)selectedCell;
|
---|
| 503 | if (!selectedCells.ContainsKey(cell.ColumnIndex))
|
---|
[10993] | 504 | selectedCells.Add(cell.ColumnIndex, new List<int>(1024));
|
---|
| 505 | selectedCells[cell.ColumnIndex].Add(cell.RowIndex);
|
---|
| 506 | }
|
---|
| 507 |
|
---|
[10590] | 508 | return selectedCells;
|
---|
| 509 | }
|
---|
| 510 |
|
---|
[10938] | 511 | private void StartReplacing() {
|
---|
| 512 | SuspendRepaint();
|
---|
| 513 | }
|
---|
| 514 |
|
---|
| 515 | private void StopReplacing() {
|
---|
[10947] | 516 | ResumeRepaint(true);
|
---|
[10938] | 517 | }
|
---|
| 518 |
|
---|
| 519 | private void ReplaceTransaction(Action action) {
|
---|
| 520 | StartReplacing();
|
---|
| 521 | action();
|
---|
| 522 | StopReplacing();
|
---|
| 523 | }
|
---|
| 524 |
|
---|
[10964] | 525 | private void btnSearch_Click(object sender, EventArgs e) {
|
---|
| 526 | CreateFindAndReplaceDialog();
|
---|
| 527 | findAndReplaceDialog.ActivateSearch();
|
---|
| 528 | }
|
---|
| 529 |
|
---|
| 530 | private void btnReplace_Click(object sender, EventArgs e) {
|
---|
| 531 | CreateFindAndReplaceDialog();
|
---|
| 532 | findAndReplaceDialog.ActivateReplace();
|
---|
| 533 | }
|
---|
| 534 |
|
---|
| 535 | #region ContextMenu Events
|
---|
| 536 |
|
---|
[10769] | 537 | private void ReplaceWithAverage_Column_Click(object sender, EventArgs e) {
|
---|
[10938] | 538 | ReplaceTransaction(() => {
|
---|
| 539 | Content.ManipulationLogic.ReplaceIndicesByAverageValue(GetSelectedCells(), false);
|
---|
| 540 | });
|
---|
[10380] | 541 | }
|
---|
[10809] | 542 | private void ReplaceWithAverage_Selection_Click(object sender, EventArgs e) {
|
---|
[10938] | 543 | ReplaceTransaction(() => {
|
---|
| 544 | Content.ManipulationLogic.ReplaceIndicesByAverageValue(GetSelectedCells(), true);
|
---|
| 545 | });
|
---|
[10809] | 546 | }
|
---|
[10380] | 547 |
|
---|
[10769] | 548 | private void ReplaceWithMedian_Column_Click(object sender, EventArgs e) {
|
---|
[10938] | 549 | ReplaceTransaction(() => {
|
---|
| 550 | Content.ManipulationLogic.ReplaceIndicesByMedianValue(GetSelectedCells(), false);
|
---|
| 551 | });
|
---|
[10380] | 552 | }
|
---|
[10809] | 553 | private void ReplaceWithMedian_Selection_Click(object sender, EventArgs e) {
|
---|
[10938] | 554 | ReplaceTransaction(() => {
|
---|
| 555 | Content.ManipulationLogic.ReplaceIndicesByMedianValue(GetSelectedCells(), true);
|
---|
| 556 | });
|
---|
[10809] | 557 | }
|
---|
[10380] | 558 |
|
---|
[10769] | 559 | private void ReplaceWithRandom_Column_Click(object sender, EventArgs e) {
|
---|
[10938] | 560 | ReplaceTransaction(() => {
|
---|
| 561 | Content.ManipulationLogic.ReplaceIndicesByRandomValue(GetSelectedCells(), false);
|
---|
| 562 | });
|
---|
[10380] | 563 | }
|
---|
[10809] | 564 | private void ReplaceWithRandom_Selection_Click(object sender, EventArgs e) {
|
---|
[10938] | 565 | ReplaceTransaction(() => {
|
---|
| 566 | Content.ManipulationLogic.ReplaceIndicesByRandomValue(GetSelectedCells(), true);
|
---|
| 567 | });
|
---|
[10809] | 568 | }
|
---|
[10380] | 569 |
|
---|
[10769] | 570 | private void ReplaceWithMostCommon_Column_Click(object sender, EventArgs e) {
|
---|
[10938] | 571 | ReplaceTransaction(() => {
|
---|
| 572 | Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(GetSelectedCells(), false);
|
---|
| 573 | });
|
---|
[10380] | 574 | }
|
---|
[10809] | 575 | private void ReplaceWithMostCommon_Selection_Click(object sender, EventArgs e) {
|
---|
[10938] | 576 | ReplaceTransaction(() => {
|
---|
| 577 | Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(GetSelectedCells(), true);
|
---|
| 578 | });
|
---|
[10809] | 579 | }
|
---|
[10380] | 580 |
|
---|
[10769] | 581 | private void ReplaceWithInterpolation_Column_Click(object sender, EventArgs e) {
|
---|
[10938] | 582 | ReplaceTransaction(() => {
|
---|
| 583 | Content.ManipulationLogic.ReplaceIndicesByLinearInterpolationOfNeighbours(GetSelectedCells());
|
---|
| 584 | });
|
---|
[10380] | 585 | }
|
---|
[10769] | 586 |
|
---|
| 587 | private void ReplaceWithSmoothing_Selection_Click(object sender, EventArgs e) {
|
---|
[10938] | 588 | ReplaceTransaction(() => {
|
---|
| 589 | Content.ManipulationLogic.ReplaceIndicesBySmoothing(GetSelectedCells());
|
---|
| 590 | });
|
---|
[10769] | 591 | }
|
---|
[10964] | 592 | #endregion
|
---|
[10769] | 593 |
|
---|
[10236] | 594 | }
|
---|
| 595 | }
|
---|