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