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