[10539] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 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;
|
---|
[10622] | 24 | using System.Drawing;
|
---|
[10534] | 25 | using System.Linq;
|
---|
[10236] | 26 | using System.Windows.Forms;
|
---|
[10672] | 27 | using HeuristicLab.Data;
|
---|
[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)]
|
---|
[10583] | 34 | public partial class DataGridContentView : CopyOfStringConvertibleMatrixView {
|
---|
[10236] | 35 |
|
---|
[10583] | 36 | private bool notOwnEvent = true;
|
---|
[10870] | 37 | private bool isSearching = 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 |
|
---|
[10574] | 49 | private IList<int> _highlightedRowIndices;
|
---|
[10623] | 50 | public IList<int> HighlightedRowIndices {
|
---|
[10585] | 51 | get { return _highlightedRowIndices; }
|
---|
| 52 | set {
|
---|
| 53 | _highlightedRowIndices = value;
|
---|
| 54 | Refresh();
|
---|
| 55 | }
|
---|
[10574] | 56 | }
|
---|
| 57 |
|
---|
[10719] | 58 | private IDictionary<int, IList<int>> _highlightedCellsBackground;
|
---|
| 59 | public IDictionary<int, IList<int>> HightlightedCellsBackground {
|
---|
| 60 | get { return _highlightedCellsBackground; }
|
---|
| 61 | set {
|
---|
| 62 | _highlightedCellsBackground = value;
|
---|
| 63 | Refresh();
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[10236] | 67 | public DataGridContentView() {
|
---|
| 68 | InitializeComponent();
|
---|
[10380] | 69 | dataGridView.CellMouseClick += dataGridView_CellMouseClick;
|
---|
[10585] | 70 | dataGridView.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(dataGridView_CellPainting);
|
---|
[10668] | 71 | dataGridView.KeyDown += dataGridView_KeyDown;
|
---|
[10380] | 72 | contextMenuCell.Items.Add(ShowHideColumns);
|
---|
[10623] | 73 | _highlightedRowIndices = new List<int>();
|
---|
[10719] | 74 | _highlightedCellsBackground = new Dictionary<int, IList<int>>();
|
---|
[10706] | 75 | currentCell = null;
|
---|
[10236] | 76 | }
|
---|
| 77 |
|
---|
[10916] | 78 | protected override void dataGridView_SelectionChanged(object sender, EventArgs e) {
|
---|
| 79 | if (Content != null) {
|
---|
| 80 | if (!isSearching) {
|
---|
| 81 | base.dataGridView_SelectionChanged(sender, e);
|
---|
| 82 | Content.DataGridLogic.SetSelection(GetSelectedCells());
|
---|
| 83 | }
|
---|
[10870] | 84 | }
|
---|
[10804] | 85 | }
|
---|
| 86 |
|
---|
[10236] | 87 | protected override void OnContentChanged() {
|
---|
| 88 | base.OnContentChanged();
|
---|
[10898] | 89 | // ToDo: Temporarily disabled because needs to much performance
|
---|
| 90 | //DataGridView.AutoResizeColumns();
|
---|
| 91 | //DataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
|
---|
[10911] | 92 | DataGridView.RowHeadersWidth = 70;
|
---|
[10739] | 93 | if (Content == null && findAndReplaceDialog != null) {
|
---|
| 94 | findAndReplaceDialog.Close();
|
---|
| 95 | }
|
---|
[10236] | 96 | }
|
---|
| 97 |
|
---|
[10571] | 98 | protected override void RegisterContentEvents() {
|
---|
| 99 | base.RegisterContentEvents();
|
---|
| 100 | Content.Changed += Content_Changed;
|
---|
[10900] | 101 | Content.FilterLogic.FilterChanged += FilterLogic_FilterChanged;
|
---|
[10571] | 102 | }
|
---|
| 103 |
|
---|
[10900] | 104 | void FilterLogic_FilterChanged(object sender, EventArgs e) {
|
---|
| 105 | OnContentChanged();
|
---|
| 106 | searchIterator = null;
|
---|
[10916] | 107 | }
|
---|
[10900] | 108 |
|
---|
[10571] | 109 | protected override void DeregisterContentEvents() {
|
---|
| 110 | base.DeregisterContentEvents();
|
---|
| 111 | Content.Changed -= Content_Changed;
|
---|
[10900] | 112 | Content.FilterLogic.FilterChanged -= FilterLogic_FilterChanged;
|
---|
[10571] | 113 | }
|
---|
| 114 |
|
---|
| 115 | void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
|
---|
[10583] | 116 | if (notOwnEvent) {
|
---|
[10622] | 117 | switch (e.Type) {
|
---|
| 118 | case DataPreprocessingChangedEventType.ChangeColumn:
|
---|
| 119 | case DataPreprocessingChangedEventType.ChangeItem:
|
---|
| 120 | dataGridView.Refresh();
|
---|
| 121 | break;
|
---|
[10817] | 122 | default:
|
---|
| 123 | OnContentChanged();
|
---|
| 124 | break;
|
---|
[10622] | 125 | }
|
---|
[10583] | 126 | }
|
---|
[10852] | 127 | searchIterator = null;
|
---|
[10571] | 128 | }
|
---|
| 129 |
|
---|
[10583] | 130 | protected override void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
|
---|
[10236] | 131 | if (!dataGridView.ReadOnly) {
|
---|
| 132 | string errorMessage;
|
---|
[10916] | 133 | if (Content != null) {
|
---|
[10900] | 134 | if (dataGridView.IsCurrentCellInEditMode && Content.FilterLogic.IsFiltered()) {
|
---|
| 135 | errorMessage = "A filter is active, you cannot modify data. Press ESC to exit edit mode.";
|
---|
| 136 | } else {
|
---|
| 137 | Content.DataGridLogic.Validate(e.FormattedValue.ToString(), out errorMessage, e.ColumnIndex);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | if (!String.IsNullOrEmpty(errorMessage)) {
|
---|
| 141 | e.Cancel = true;
|
---|
| 142 | dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
|
---|
| 143 | }
|
---|
[10236] | 144 | }
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
[10253] | 147 |
|
---|
[10583] | 148 | protected override void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
|
---|
[10585] | 149 | triggersOwnEvent(() => base.dataGridView_CellParsing(sender, e));
|
---|
[10583] | 150 | }
|
---|
| 151 |
|
---|
| 152 | protected override void PasteValuesToDataGridView() {
|
---|
[10585] | 153 | triggersOwnEvent(() => base.PasteValuesToDataGridView());
|
---|
[10583] | 154 | }
|
---|
| 155 |
|
---|
[10317] | 156 | protected override void SetEnabledStateOfControls() {
|
---|
| 157 | base.SetEnabledStateOfControls();
|
---|
| 158 | rowsTextBox.ReadOnly = true;
|
---|
| 159 | columnsTextBox.ReadOnly = true;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[10253] | 162 | private void btnApplySort_Click(object sender, System.EventArgs e) {
|
---|
[10585] | 163 | triggersOwnEvent(() => {
|
---|
[10809] | 164 | Content.ManipulationLogic.ReOrderToIndices(virtualRowIndices);
|
---|
[10585] | 165 | OnContentChanged();
|
---|
| 166 | });
|
---|
[10253] | 167 | }
|
---|
[10345] | 168 |
|
---|
[10585] | 169 | private void triggersOwnEvent(Action action) {
|
---|
| 170 | notOwnEvent = false;
|
---|
| 171 | action();
|
---|
| 172 | notOwnEvent = true;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[10698] | 175 | #region FindAndReplaceDialog
|
---|
[10585] | 176 |
|
---|
[10636] | 177 | private void CreateFindAndReplaceDialog() {
|
---|
[10876] | 178 | if (findAndReplaceDialog == null || findAndReplaceDialog.IsDisposed) {
|
---|
| 179 | findAndReplaceDialog = new SearchAndReplaceDialog();
|
---|
| 180 | findAndReplaceDialog.Show(this);
|
---|
| 181 | if (AreMultipleCellsSelected()) {
|
---|
| 182 | ResetHighlightedCellsBackground();
|
---|
| 183 | HightlightedCellsBackground = GetSelectedCells();
|
---|
| 184 | dataGridView.ClearSelection();
|
---|
| 185 | }
|
---|
| 186 | findAndReplaceDialog.FindAllEvent += findAndReplaceDialog_FindAllEvent;
|
---|
| 187 | findAndReplaceDialog.FindNextEvent += findAndReplaceDialog_FindNextEvent;
|
---|
| 188 | findAndReplaceDialog.ReplaceAllEvent += findAndReplaceDialog_ReplaceAllEvent;
|
---|
| 189 | findAndReplaceDialog.ReplaceNextEvent += findAndReplaceDialog_ReplaceEvent;
|
---|
| 190 | findAndReplaceDialog.FormClosing += findAndReplaceDialog_FormClosing;
|
---|
| 191 | searchIterator = null;
|
---|
| 192 | DataGridView.SelectionChanged += DataGridView_SelectionChanged_FindAndReplace;
|
---|
[10852] | 193 | }
|
---|
[10636] | 194 | }
|
---|
| 195 |
|
---|
[10916] | 196 | private void DataGridView_SelectionChanged_FindAndReplace(object sender, EventArgs e) {
|
---|
[10863] | 197 | if (Content != null) {
|
---|
[10870] | 198 | if (!isSearching && AreMultipleCellsSelected()) {
|
---|
[10863] | 199 | ResetHighlightedCellsBackground();
|
---|
| 200 | HightlightedCellsBackground = GetSelectedCells();
|
---|
| 201 | searchIterator = null;
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[10705] | 206 | void findAndReplaceDialog_FormClosing(object sender, FormClosingEventArgs e) {
|
---|
[10719] | 207 | ResetHighlightedCellsBackground();
|
---|
[10852] | 208 | searchIterator = null;
|
---|
[10863] | 209 | DataGridView.SelectionChanged -= DataGridView_SelectionChanged_FindAndReplace;
|
---|
[10705] | 210 | }
|
---|
| 211 |
|
---|
[10672] | 212 | void findAndReplaceDialog_ReplaceEvent(object sender, EventArgs e) {
|
---|
[10712] | 213 | if (searchIterator != null && searchIterator.GetCurrent() != null) {
|
---|
[10706] | 214 | Replace(TransformToDictionary(currentCell));
|
---|
[10672] | 215 | }
|
---|
[10636] | 216 | }
|
---|
| 217 |
|
---|
| 218 | void findAndReplaceDialog_ReplaceAllEvent(object sender, EventArgs e) {
|
---|
[10672] | 219 | Replace(FindAll(findAndReplaceDialog.GetSearchText()));
|
---|
[10636] | 220 | }
|
---|
| 221 |
|
---|
| 222 | void findAndReplaceDialog_FindNextEvent(object sender, EventArgs e) {
|
---|
[10876] | 223 | if (searchIterator == null ||
|
---|
| 224 | currentSearchText != findAndReplaceDialog.GetSearchText() ||
|
---|
[10870] | 225 | currentComparisonOperation != findAndReplaceDialog.GetComparisonOperation()) {
|
---|
[10698] | 226 | searchIterator = new FindPreprocessingItemsIterator(FindAll(findAndReplaceDialog.GetSearchText()));
|
---|
| 227 | currentSearchText = findAndReplaceDialog.GetSearchText();
|
---|
[10870] | 228 | currentComparisonOperation = findAndReplaceDialog.GetComparisonOperation();
|
---|
[10698] | 229 | }
|
---|
[10870] | 230 |
|
---|
[10852] | 231 | if (IsOneCellSelected()) {
|
---|
| 232 | var first = GetSelectedCells().First();
|
---|
| 233 | searchIterator.SetStartCell(first.Key, first.Value[0]);
|
---|
| 234 | }
|
---|
[10712] | 235 |
|
---|
[10705] | 236 | bool moreOccurences = false;
|
---|
[10870] | 237 | currentCell = searchIterator.GetCurrent();
|
---|
| 238 | moreOccurences = searchIterator.MoveNext();
|
---|
| 239 | if (IsOneCellSelected() && currentCell != null) {
|
---|
| 240 | var first = GetSelectedCells().First();
|
---|
| 241 | if (currentCell.Item1 == first.Key && currentCell.Item2 == first.Value[0]) {
|
---|
| 242 | if (!moreOccurences) {
|
---|
| 243 | searchIterator.Reset();
|
---|
[10852] | 244 | }
|
---|
[10870] | 245 | currentCell = searchIterator.GetCurrent();
|
---|
| 246 | moreOccurences = searchIterator.MoveNext();
|
---|
| 247 | if (!moreOccurences) {
|
---|
| 248 | searchIterator.Reset();
|
---|
| 249 | }
|
---|
[10852] | 250 | }
|
---|
[10870] | 251 | }
|
---|
[10672] | 252 |
|
---|
[10870] | 253 | dataGridView.ClearSelection();
|
---|
| 254 |
|
---|
[10705] | 255 | if (currentCell != null) {
|
---|
[10852] | 256 | dataGridView[currentCell.Item1, currentCell.Item2].Selected = true;
|
---|
[10698] | 257 | }
|
---|
[10852] | 258 | }
|
---|
| 259 |
|
---|
| 260 | private bool AreMultipleCellsSelected() {
|
---|
| 261 | return GetSelectedCellCount() > 1;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | private bool IsOneCellSelected() {
|
---|
| 265 | return GetSelectedCellCount() == 1;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | private int GetSelectedCellCount() {
|
---|
| 269 | int count = 0;
|
---|
| 270 | foreach (var column in GetSelectedCells()) {
|
---|
| 271 | count += column.Value.Count();
|
---|
[10706] | 272 | }
|
---|
[10852] | 273 | return count;
|
---|
[10636] | 274 | }
|
---|
| 275 |
|
---|
| 276 | void findAndReplaceDialog_FindAllEvent(object sender, EventArgs e) {
|
---|
[10870] | 277 | dataGridView.ClearSelection();
|
---|
| 278 | isSearching = true;
|
---|
[10901] | 279 | SuspendRepaint();
|
---|
[10916] | 280 | var selectedCells = FindAll(findAndReplaceDialog.GetSearchText());
|
---|
| 281 | foreach (var column in selectedCells) {
|
---|
[10870] | 282 | foreach (var cell in column.Value) {
|
---|
| 283 | dataGridView[column.Key, cell].Selected = true;
|
---|
| 284 | }
|
---|
| 285 | }
|
---|
[10901] | 286 | ResumeRepaint(true);
|
---|
[10870] | 287 | isSearching = false;
|
---|
[10916] | 288 | Content.DataGridLogic.SetSelection(selectedCells);
|
---|
| 289 | //update statistic in base
|
---|
| 290 | base.dataGridView_SelectionChanged(sender, e);
|
---|
[10636] | 291 | }
|
---|
| 292 |
|
---|
[10870] | 293 | private Core.ConstraintOperation GetConstraintOperation(ComparisonOperation comparisonOperation) {
|
---|
| 294 | Core.ConstraintOperation constraintOperation = Core.ConstraintOperation.Equal;
|
---|
| 295 | switch (comparisonOperation) {
|
---|
| 296 | case ComparisonOperation.Equal:
|
---|
| 297 | constraintOperation = Core.ConstraintOperation.Equal;
|
---|
| 298 | break;
|
---|
| 299 | case ComparisonOperation.Greater:
|
---|
| 300 | constraintOperation = Core.ConstraintOperation.Greater;
|
---|
| 301 | break;
|
---|
| 302 | case ComparisonOperation.GreaterOrEqual:
|
---|
| 303 | constraintOperation = Core.ConstraintOperation.GreaterOrEqual;
|
---|
| 304 | break;
|
---|
| 305 | case ComparisonOperation.Less:
|
---|
| 306 | constraintOperation = Core.ConstraintOperation.Less;
|
---|
| 307 | break;
|
---|
| 308 | case ComparisonOperation.LessOrEqual:
|
---|
| 309 | constraintOperation = Core.ConstraintOperation.LessOrEqual;
|
---|
| 310 | break;
|
---|
| 311 | case ComparisonOperation.NotEqual:
|
---|
| 312 | constraintOperation = Core.ConstraintOperation.NotEqual;
|
---|
| 313 | break;
|
---|
| 314 | }
|
---|
| 315 | return constraintOperation;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
[10672] | 318 | private IDictionary<int, IList<int>> FindAll(string match) {
|
---|
[10719] | 319 | bool searchInSelection = HightlightedCellsBackground.Values.Sum(list => list.Count) > 1;
|
---|
[10870] | 320 | ComparisonOperation comparisonOperation = findAndReplaceDialog.GetComparisonOperation();
|
---|
| 321 | var comparisonFilter = new ComparisonFilter(Content.FilterLogic.PreprocessingData, GetConstraintOperation(comparisonOperation), new StringValue(match), true);
|
---|
[10672] | 322 | var filters = new List<Filter.IFilter>() { comparisonFilter };
|
---|
| 323 | var foundCells = new Dictionary<int, IList<int>>();
|
---|
| 324 | for (int i = 0; i < Content.FilterLogic.PreprocessingData.Columns; i++) {
|
---|
| 325 | comparisonFilter.ConstraintColumn = i;
|
---|
[10844] | 326 | bool[] filteredRows = Content.FilterLogic.GetFilterResult(filters, true);
|
---|
| 327 | var foundIndices = new List<int>();
|
---|
| 328 | for (int idx = 0; idx < filteredRows.Length; ++idx) {
|
---|
| 329 | var notFilteredThusFound = !filteredRows[idx];
|
---|
| 330 | if (notFilteredThusFound) {
|
---|
| 331 | foundIndices.Add(idx);
|
---|
| 332 | }
|
---|
| 333 | }
|
---|
| 334 | foundCells[i] = foundIndices;
|
---|
[10719] | 335 | IList<int> selectedList;
|
---|
| 336 | if (searchInSelection && HightlightedCellsBackground.TryGetValue(i, out selectedList)) {
|
---|
[10739] | 337 | foundCells[i] = foundCells[i].Intersect(selectedList).ToList<int>();
|
---|
| 338 | } else if (searchInSelection) {
|
---|
[10719] | 339 | foundCells[i].Clear();
|
---|
| 340 | }
|
---|
[10672] | 341 | }
|
---|
| 342 | return foundCells;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | private void Replace(IDictionary<int, IList<int>> cells) {
|
---|
| 346 | if (findAndReplaceDialog != null) {
|
---|
| 347 | switch (findAndReplaceDialog.GetReplaceAction()) {
|
---|
| 348 | case ReplaceAction.Value:
|
---|
[10809] | 349 | Content.ManipulationLogic.ReplaceIndicesByValue(cells, findAndReplaceDialog.GetReplaceText());
|
---|
[10672] | 350 | break;
|
---|
| 351 | case ReplaceAction.Average:
|
---|
[10809] | 352 | Content.ManipulationLogic.ReplaceIndicesByAverageValue(cells, false);
|
---|
[10672] | 353 | break;
|
---|
| 354 | case ReplaceAction.Median:
|
---|
[10809] | 355 | Content.ManipulationLogic.ReplaceIndicesByMedianValue(cells, false);
|
---|
[10672] | 356 | break;
|
---|
| 357 | case ReplaceAction.Random:
|
---|
[10809] | 358 | Content.ManipulationLogic.ReplaceIndicesByRandomValue(cells, false);
|
---|
[10672] | 359 | break;
|
---|
| 360 | case ReplaceAction.MostCommon:
|
---|
[10809] | 361 | Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(cells, false);
|
---|
[10672] | 362 | break;
|
---|
| 363 | case ReplaceAction.Interpolation:
|
---|
[10809] | 364 | Content.ManipulationLogic.ReplaceIndicesByLinearInterpolationOfNeighbours(cells);
|
---|
[10672] | 365 | break;
|
---|
| 366 | }
|
---|
| 367 | }
|
---|
| 368 | }
|
---|
| 369 |
|
---|
[10698] | 370 | private IDictionary<int, IList<int>> TransformToDictionary(Tuple<int, int> tuple) {
|
---|
[10672] | 371 | var highlightCells = new Dictionary<int, IList<int>>();
|
---|
[10698] | 372 | highlightCells.Add(tuple.Item1, new List<int>() { tuple.Item2 });
|
---|
[10672] | 373 | return highlightCells;
|
---|
| 374 | }
|
---|
| 375 |
|
---|
[10719] | 376 | private void ResetHighlightedCellsBackground() {
|
---|
| 377 | HightlightedCellsBackground = new Dictionary<int, IList<int>>();
|
---|
| 378 | }
|
---|
| 379 |
|
---|
[10698] | 380 | #endregion FindAndReplaceDialog
|
---|
| 381 |
|
---|
[10380] | 382 | private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
| 383 | if (Content == null) return;
|
---|
| 384 | if (e.Button == System.Windows.Forms.MouseButtons.Right) {
|
---|
| 385 | if (e.ColumnIndex == -1 || e.RowIndex == -1) {
|
---|
[10769] | 386 | replaceValueOverColumnToolStripMenuItem.Visible = false;
|
---|
[10627] | 387 | contextMenuCell.Show(MousePosition);
|
---|
[10380] | 388 | } else {
|
---|
[10590] | 389 | if (!dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, e.RowIndex])) {
|
---|
| 390 | dataGridView.ClearSelection();
|
---|
| 391 | dataGridView[e.ColumnIndex, e.RowIndex].Selected = true;
|
---|
| 392 | }
|
---|
[10812] | 393 |
|
---|
[10621] | 394 | var columnIndices = new HashSet<int>();
|
---|
| 395 | for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
|
---|
| 396 | columnIndices.Add(dataGridView.SelectedCells[i].ColumnIndex);
|
---|
| 397 | }
|
---|
[10875] | 398 |
|
---|
| 399 | replaceValueOverSelectionToolStripMenuItem.Enabled = AreMultipleCellsSelected();
|
---|
| 400 |
|
---|
[10812] | 401 | averageToolStripMenuItem_Column.Enabled =
|
---|
| 402 | averageToolStripMenuItem_Selection.Enabled =
|
---|
| 403 | medianToolStripMenuItem_Column.Enabled =
|
---|
| 404 | medianToolStripMenuItem_Selection.Enabled =
|
---|
| 405 | randomToolStripMenuItem_Column.Enabled =
|
---|
| 406 | randomToolStripMenuItem_Selection.Enabled = !Content.DataGridLogic.AreAllStringColumns(columnIndices);
|
---|
| 407 |
|
---|
| 408 | smoothingToolStripMenuItem_Column.Enabled =
|
---|
[10820] | 409 | interpolationToolStripMenuItem_Column.Enabled = !dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, 0])
|
---|
| 410 | && !dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, Content.Rows - 1])
|
---|
[10812] | 411 | && !Content.DataGridLogic.AreAllStringColumns(columnIndices);
|
---|
| 412 |
|
---|
[10769] | 413 | replaceValueOverColumnToolStripMenuItem.Visible = true;
|
---|
[10380] | 414 | contextMenuCell.Show(MousePosition);
|
---|
| 415 | }
|
---|
| 416 | }
|
---|
| 417 | }
|
---|
| 418 |
|
---|
[10585] | 419 | protected void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
|
---|
| 420 | if (Content == null) return;
|
---|
| 421 | if (e.RowIndex < 0) return;
|
---|
| 422 | if (e.ColumnIndex < 0) return;
|
---|
| 423 | if (e.State.HasFlag(DataGridViewElementStates.Selected)) return;
|
---|
| 424 | if (!e.PaintParts.HasFlag(DataGridViewPaintParts.Background)) return;
|
---|
[10870] | 425 | if (HighlightedRowIndices == null) return;
|
---|
[10574] | 426 |
|
---|
[10585] | 427 | int rowIndex = virtualRowIndices[e.RowIndex];
|
---|
[10574] | 428 |
|
---|
[10585] | 429 | Color backColor = e.CellStyle.BackColor;
|
---|
[10574] | 430 |
|
---|
[10719] | 431 | if (HightlightedCellsBackground.ContainsKey(e.ColumnIndex) && HightlightedCellsBackground[e.ColumnIndex].Contains(e.RowIndex)) {
|
---|
| 432 | backColor = Color.LightGray;
|
---|
| 433 | }
|
---|
[10574] | 434 |
|
---|
[10585] | 435 | using (Brush backColorBrush = new SolidBrush(backColor)) {
|
---|
| 436 | Rectangle bounds = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height);
|
---|
| 437 | e.Graphics.FillRectangle(backColorBrush, bounds);
|
---|
| 438 | }
|
---|
[10712] | 439 |
|
---|
[10585] | 440 | using (Brush gridBrush = new SolidBrush(Color.LightGray)) {
|
---|
| 441 | Pen gridLinePen = new Pen(gridBrush);
|
---|
| 442 | e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
|
---|
| 443 | e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
|
---|
| 444 | e.CellBounds.Bottom - 1);
|
---|
| 445 | e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
|
---|
| 446 | e.CellBounds.Top, e.CellBounds.Right - 1,
|
---|
| 447 | e.CellBounds.Bottom);
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 | e.PaintContent(e.CellBounds);
|
---|
| 451 | e.Handled = true;
|
---|
[10574] | 452 | }
|
---|
| 453 |
|
---|
[10668] | 454 | void dataGridView_KeyDown(object sender, KeyEventArgs e) {
|
---|
| 455 | var selectedRows = dataGridView.SelectedRows;
|
---|
| 456 | if (e.KeyCode == Keys.Delete && selectedRows.Count > 0) {
|
---|
| 457 | List<int> rows = new List<int>();
|
---|
| 458 | for (int i = 0; i < selectedRows.Count; ++i) {
|
---|
| 459 | rows.Add(selectedRows[i].Index);
|
---|
| 460 | }
|
---|
| 461 | triggersOwnEvent(() => {
|
---|
| 462 | Content.DataGridLogic.DeleteRow(rows);
|
---|
| 463 | OnContentChanged();
|
---|
| 464 | });
|
---|
| 465 | } else if (e.Control && e.KeyCode == Keys.F) {
|
---|
| 466 | CreateFindAndReplaceDialog();
|
---|
| 467 | findAndReplaceDialog.ActivateSearch();
|
---|
| 468 | } else if (e.Control && e.KeyCode == Keys.R) {
|
---|
| 469 | CreateFindAndReplaceDialog();
|
---|
| 470 | findAndReplaceDialog.ActivateReplace();
|
---|
| 471 | }
|
---|
| 472 | }
|
---|
| 473 |
|
---|
[10345] | 474 | protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
|
---|
| 475 | btnApplySort.Enabled = sortedColumns.Any();
|
---|
| 476 | return base.Sort(sortedColumns);
|
---|
| 477 | }
|
---|
| 478 |
|
---|
| 479 | protected override void ClearSorting() {
|
---|
| 480 | btnApplySort.Enabled = false;
|
---|
| 481 | base.ClearSorting();
|
---|
| 482 | }
|
---|
[10380] | 483 |
|
---|
[10672] | 484 | private IDictionary<int, IList<int>> GetSelectedCells() {
|
---|
| 485 | IDictionary<int, IList<int>> selectedCells = new Dictionary<int, IList<int>>();
|
---|
[10590] | 486 | for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
|
---|
[10622] | 487 | var columnIndex = dataGridView.SelectedCells[i].ColumnIndex;
|
---|
| 488 | if (!selectedCells.ContainsKey(columnIndex)) {
|
---|
| 489 | selectedCells.Add(columnIndex, new List<int>());
|
---|
[10590] | 490 | }
|
---|
| 491 | selectedCells[columnIndex].Add(dataGridView.SelectedCells[i].RowIndex);
|
---|
| 492 | }
|
---|
| 493 | return selectedCells;
|
---|
| 494 | }
|
---|
| 495 |
|
---|
[10769] | 496 | private void ReplaceWithAverage_Column_Click(object sender, EventArgs e) {
|
---|
[10812] | 497 | Content.ManipulationLogic.ReplaceIndicesByAverageValue(GetSelectedCells(), false);
|
---|
[10380] | 498 | }
|
---|
[10809] | 499 | private void ReplaceWithAverage_Selection_Click(object sender, EventArgs e) {
|
---|
| 500 | Content.ManipulationLogic.ReplaceIndicesByAverageValue(GetSelectedCells(), true);
|
---|
| 501 | }
|
---|
[10380] | 502 |
|
---|
[10769] | 503 | private void ReplaceWithMedian_Column_Click(object sender, EventArgs e) {
|
---|
[10809] | 504 | Content.ManipulationLogic.ReplaceIndicesByMedianValue(GetSelectedCells(), false);
|
---|
[10380] | 505 | }
|
---|
[10809] | 506 | private void ReplaceWithMedian_Selection_Click(object sender, EventArgs e) {
|
---|
| 507 | Content.ManipulationLogic.ReplaceIndicesByMedianValue(GetSelectedCells(), true);
|
---|
| 508 | }
|
---|
[10380] | 509 |
|
---|
[10769] | 510 | private void ReplaceWithRandom_Column_Click(object sender, EventArgs e) {
|
---|
[10809] | 511 | Content.ManipulationLogic.ReplaceIndicesByRandomValue(GetSelectedCells(), false);
|
---|
[10380] | 512 | }
|
---|
[10809] | 513 | private void ReplaceWithRandom_Selection_Click(object sender, EventArgs e) {
|
---|
| 514 | Content.ManipulationLogic.ReplaceIndicesByRandomValue(GetSelectedCells(), true);
|
---|
| 515 | }
|
---|
[10380] | 516 |
|
---|
[10769] | 517 | private void ReplaceWithMostCommon_Column_Click(object sender, EventArgs e) {
|
---|
[10809] | 518 | Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(GetSelectedCells(), false);
|
---|
[10380] | 519 | }
|
---|
[10809] | 520 | private void ReplaceWithMostCommon_Selection_Click(object sender, EventArgs e) {
|
---|
| 521 | Content.ManipulationLogic.ReplaceIndicesByMostCommonValue(GetSelectedCells(), true);
|
---|
| 522 | }
|
---|
[10380] | 523 |
|
---|
[10769] | 524 | private void ReplaceWithInterpolation_Column_Click(object sender, EventArgs e) {
|
---|
[10809] | 525 | Content.ManipulationLogic.ReplaceIndicesByLinearInterpolationOfNeighbours(GetSelectedCells());
|
---|
[10380] | 526 | }
|
---|
[10769] | 527 |
|
---|
| 528 | private void ReplaceWithSmoothing_Selection_Click(object sender, EventArgs e) {
|
---|
[10820] | 529 | Content.ManipulationLogic.ReplaceIndicesBySmoothing(GetSelectedCells());
|
---|
[10769] | 530 | }
|
---|
| 531 |
|
---|
[10764] | 532 | private void btnSearch_Click(object sender, EventArgs e) {
|
---|
[10762] | 533 | CreateFindAndReplaceDialog();
|
---|
| 534 | findAndReplaceDialog.ActivateSearch();
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | private void btnReplace_Click(object sender, EventArgs e) {
|
---|
| 538 | CreateFindAndReplaceDialog();
|
---|
| 539 | findAndReplaceDialog.ActivateReplace();
|
---|
| 540 | }
|
---|
[10236] | 541 | }
|
---|
| 542 | }
|
---|