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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Windows.Forms;
|
---|
27 | using HeuristicLab.MainForm;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.DataPreprocessing.Views {
|
---|
30 | [View("Data Grid Content View")]
|
---|
31 | [Content(typeof(IDataGridContent), true)]
|
---|
32 | public partial class DataGridContentView : CopyOfStringConvertibleMatrixView {
|
---|
33 |
|
---|
34 | private bool notOwnEvent = true;
|
---|
35 | private FindAndReplaceDialog findAndReplaceDialog;
|
---|
36 |
|
---|
37 | public new IDataGridContent Content {
|
---|
38 | get { return (IDataGridContent)base.Content; }
|
---|
39 | set { base.Content = value; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | private IList<int> _highlightedRowIndices;
|
---|
43 | public IList<int> HighlightedRowIndices {
|
---|
44 | get { return _highlightedRowIndices; }
|
---|
45 | set {
|
---|
46 | _highlightedRowIndices = value;
|
---|
47 | Refresh();
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | private IDictionary<int, IList<int>> _highlightedCells;
|
---|
52 | public IDictionary<int, IList<int>> HightlightedCells {
|
---|
53 | get { return _highlightedCells; }
|
---|
54 | set {
|
---|
55 | _highlightedCells = value;
|
---|
56 | Refresh();
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | public DataGridContentView() {
|
---|
61 | InitializeComponent();
|
---|
62 | dataGridView.CellMouseClick += dataGridView_CellMouseClick;
|
---|
63 | dataGridView.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(dataGridView_CellPainting);
|
---|
64 | dataGridView.KeyUp += dataGridView_KeyUp;
|
---|
65 | contextMenuCell.Items.Add(ShowHideColumns);
|
---|
66 | _highlightedCells = new Dictionary<int, IList<int>>();
|
---|
67 | _highlightedRowIndices = new List<int>();
|
---|
68 | }
|
---|
69 |
|
---|
70 | protected override void OnContentChanged() {
|
---|
71 | base.OnContentChanged();
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected override void RegisterContentEvents() {
|
---|
75 | base.RegisterContentEvents();
|
---|
76 | Content.Changed += Content_Changed;
|
---|
77 | }
|
---|
78 |
|
---|
79 | protected override void DeregisterContentEvents() {
|
---|
80 | base.DeregisterContentEvents();
|
---|
81 | Content.Changed -= Content_Changed;
|
---|
82 | }
|
---|
83 |
|
---|
84 | void Content_Changed(object sender, DataPreprocessingChangedEventArgs e) {
|
---|
85 | if (notOwnEvent) {
|
---|
86 | switch (e.Type) {
|
---|
87 | case DataPreprocessingChangedEventType.AddColumn:
|
---|
88 | case DataPreprocessingChangedEventType.AddRow:
|
---|
89 | case DataPreprocessingChangedEventType.DeleteColumn:
|
---|
90 | case DataPreprocessingChangedEventType.DeleteRow:
|
---|
91 | case DataPreprocessingChangedEventType.Any:
|
---|
92 | OnContentChanged();
|
---|
93 | break;
|
---|
94 | case DataPreprocessingChangedEventType.ChangeColumn:
|
---|
95 | case DataPreprocessingChangedEventType.ChangeItem:
|
---|
96 | dataGridView.Refresh();
|
---|
97 | break;
|
---|
98 | }
|
---|
99 |
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | protected override void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
|
---|
104 | if (!dataGridView.ReadOnly) {
|
---|
105 | string errorMessage;
|
---|
106 | if (Content != null && !Content.DataGridLogic.Validate(e.FormattedValue.ToString(), out errorMessage, e.ColumnIndex)) {
|
---|
107 | e.Cancel = true;
|
---|
108 | dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | protected override void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
|
---|
114 | triggersOwnEvent(() => base.dataGridView_CellParsing(sender, e));
|
---|
115 | }
|
---|
116 |
|
---|
117 | protected override void PasteValuesToDataGridView() {
|
---|
118 | triggersOwnEvent(() => base.PasteValuesToDataGridView());
|
---|
119 | }
|
---|
120 |
|
---|
121 | protected override void SetEnabledStateOfControls() {
|
---|
122 | base.SetEnabledStateOfControls();
|
---|
123 | rowsTextBox.ReadOnly = true;
|
---|
124 | columnsTextBox.ReadOnly = true;
|
---|
125 | }
|
---|
126 |
|
---|
127 | private void btnApplySort_Click(object sender, System.EventArgs e) {
|
---|
128 | triggersOwnEvent(() => {
|
---|
129 | Content.PreprocessingDataManipulation.ReOrderToIndices(virtualRowIndices);
|
---|
130 | OnContentChanged();
|
---|
131 | });
|
---|
132 | }
|
---|
133 |
|
---|
134 | private void triggersOwnEvent(Action action) {
|
---|
135 | notOwnEvent = false;
|
---|
136 | action();
|
---|
137 | notOwnEvent = true;
|
---|
138 | }
|
---|
139 |
|
---|
140 | private void dataGridView_KeyUp(object sender, KeyEventArgs e) {
|
---|
141 | var selectedRows = dataGridView.SelectedRows;
|
---|
142 | if (e.KeyCode == Keys.Delete && selectedRows.Count > 0) {
|
---|
143 | List<int> rows = new List<int>();
|
---|
144 | for (int i = 0; i < selectedRows.Count; ++i) {
|
---|
145 | rows.Add(selectedRows[i].Index);
|
---|
146 | }
|
---|
147 | triggersOwnEvent(() => {
|
---|
148 | Content.DataGridLogic.DeleteRow(rows);
|
---|
149 | OnContentChanged();
|
---|
150 | });
|
---|
151 | } else if (e.Control && e.KeyCode == Keys.F) {
|
---|
152 | CreateFindAndReplaceDialog();
|
---|
153 | findAndReplaceDialog.ActivateSearch();
|
---|
154 | } else if (e.Control && e.KeyCode == Keys.R) {
|
---|
155 | CreateFindAndReplaceDialog();
|
---|
156 | findAndReplaceDialog.ActivateReplace();
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | private void CreateFindAndReplaceDialog() {
|
---|
161 | findAndReplaceDialog = new FindAndReplaceDialog();
|
---|
162 | findAndReplaceDialog.Show();
|
---|
163 | findAndReplaceDialog.FindAllEvent += findAndReplaceDialog_FindAllEvent;
|
---|
164 | findAndReplaceDialog.FindNextEvent += findAndReplaceDialog_FindNextEvent;
|
---|
165 | findAndReplaceDialog.ReplaceAllEvent += findAndReplaceDialog_ReplaceAllEvent;
|
---|
166 | findAndReplaceDialog.ReplaceNextEvent += findAndReplaceDialog_ReplaceNextEvent;
|
---|
167 | }
|
---|
168 |
|
---|
169 | void findAndReplaceDialog_ReplaceNextEvent(object sender, EventArgs e) {
|
---|
170 | throw new NotImplementedException();
|
---|
171 | }
|
---|
172 |
|
---|
173 | void findAndReplaceDialog_ReplaceAllEvent(object sender, EventArgs e) {
|
---|
174 | throw new NotImplementedException();
|
---|
175 | }
|
---|
176 |
|
---|
177 | void findAndReplaceDialog_FindNextEvent(object sender, EventArgs e) {
|
---|
178 | throw new NotImplementedException();
|
---|
179 | }
|
---|
180 |
|
---|
181 | void findAndReplaceDialog_FindAllEvent(object sender, EventArgs e) {
|
---|
182 | throw new NotImplementedException();
|
---|
183 | }
|
---|
184 |
|
---|
185 | private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
186 | if (Content == null) return;
|
---|
187 | if (e.Button == System.Windows.Forms.MouseButtons.Right) {
|
---|
188 | if (e.ColumnIndex == -1 || e.RowIndex == -1) {
|
---|
189 | replaceValueToolStripMenuItem.Visible = false;
|
---|
190 | contextMenuCell.Show(MousePosition);
|
---|
191 | } else {
|
---|
192 | if (!dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, e.RowIndex])) {
|
---|
193 | dataGridView.ClearSelection();
|
---|
194 | dataGridView[e.ColumnIndex, e.RowIndex].Selected = true;
|
---|
195 | }
|
---|
196 | interpolationToolStripMenuItem.Enabled = !(e.RowIndex == 0 || e.RowIndex == Content.Rows);
|
---|
197 | var columnIndices = new HashSet<int>();
|
---|
198 | for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
|
---|
199 | columnIndices.Add(dataGridView.SelectedCells[i].ColumnIndex);
|
---|
200 | }
|
---|
201 | averageToolStripMenuItem.Enabled = medianToolStripMenuItem.Enabled = randomToolStripMenuItem.Enabled = !Content.DataGridLogic.AreAllStringColumns(columnIndices);
|
---|
202 | interpolationToolStripMenuItem.Enabled = interpolationToolStripMenuItem.Enabled && !Content.DataGridLogic.AreAllStringColumns(columnIndices);
|
---|
203 | replaceValueToolStripMenuItem.Visible = true;
|
---|
204 | contextMenuCell.Show(MousePosition);
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | protected void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
|
---|
210 | if (Content == null) return;
|
---|
211 | if (e.RowIndex < 0) return;
|
---|
212 | if (e.ColumnIndex < 0) return;
|
---|
213 | if (e.State.HasFlag(DataGridViewElementStates.Selected)) return;
|
---|
214 | if (!e.PaintParts.HasFlag(DataGridViewPaintParts.Background)) return;
|
---|
215 |
|
---|
216 | int rowIndex = virtualRowIndices[e.RowIndex];
|
---|
217 |
|
---|
218 | Color backColor = e.CellStyle.BackColor;
|
---|
219 |
|
---|
220 | if (HighlightedRowIndices != null && HighlightedRowIndices.Contains(rowIndex)
|
---|
221 | || HightlightedCells != null && HightlightedCells.ContainsKey(e.ColumnIndex) && HightlightedCells[e.ColumnIndex].Contains(e.RowIndex)) {
|
---|
222 | backColor = Color.Pink;
|
---|
223 | }
|
---|
224 |
|
---|
225 | using (Brush backColorBrush = new SolidBrush(backColor)) {
|
---|
226 | Rectangle bounds = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height);
|
---|
227 | e.Graphics.FillRectangle(backColorBrush, bounds);
|
---|
228 | }
|
---|
229 |
|
---|
230 | using (Brush gridBrush = new SolidBrush(Color.LightGray)) {
|
---|
231 | Pen gridLinePen = new Pen(gridBrush);
|
---|
232 | e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
|
---|
233 | e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
|
---|
234 | e.CellBounds.Bottom - 1);
|
---|
235 | e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
|
---|
236 | e.CellBounds.Top, e.CellBounds.Right - 1,
|
---|
237 | e.CellBounds.Bottom);
|
---|
238 | }
|
---|
239 |
|
---|
240 | e.PaintContent(e.CellBounds);
|
---|
241 | e.Handled = true;
|
---|
242 | }
|
---|
243 |
|
---|
244 | protected override int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
|
---|
245 | btnApplySort.Enabled = sortedColumns.Any();
|
---|
246 | return base.Sort(sortedColumns);
|
---|
247 | }
|
---|
248 |
|
---|
249 | protected override void ClearSorting() {
|
---|
250 | btnApplySort.Enabled = false;
|
---|
251 | base.ClearSorting();
|
---|
252 | }
|
---|
253 |
|
---|
254 | private Dictionary<int, List<int>> GetSelectedCells() {
|
---|
255 | var selectedCells = new Dictionary<int, List<int>>();
|
---|
256 | for (int i = 0; i < dataGridView.SelectedCells.Count; i++) {
|
---|
257 | var columnIndex = dataGridView.SelectedCells[i].ColumnIndex;
|
---|
258 | if (!selectedCells.ContainsKey(columnIndex)) {
|
---|
259 | selectedCells.Add(columnIndex, new List<int>());
|
---|
260 | }
|
---|
261 | selectedCells[columnIndex].Add(dataGridView.SelectedCells[i].RowIndex);
|
---|
262 | }
|
---|
263 | return selectedCells;
|
---|
264 | }
|
---|
265 |
|
---|
266 | private void ReplaceWithAverage_Click(object sender, EventArgs e) {
|
---|
267 | Content.PreprocessingDataManipulation.ReplaceIndicesByAverageValue(GetSelectedCells());
|
---|
268 | }
|
---|
269 |
|
---|
270 | private void ReplaceWithMedian_Click(object sender, EventArgs e) {
|
---|
271 | Content.PreprocessingDataManipulation.ReplaceIndicesByMedianValue(GetSelectedCells());
|
---|
272 | }
|
---|
273 |
|
---|
274 | private void ReplaceWithRandom_Click(object sender, EventArgs e) {
|
---|
275 | Content.PreprocessingDataManipulation.ReplaceIndicesByRandomValue(GetSelectedCells());
|
---|
276 | }
|
---|
277 |
|
---|
278 | private void ReplaceWithMostCommon_Click(object sender, EventArgs e) {
|
---|
279 | Content.PreprocessingDataManipulation.ReplaceIndicesByMostCommonValue(GetSelectedCells());
|
---|
280 | }
|
---|
281 |
|
---|
282 | private void ReplaceWithInterpolation_Click(object sender, EventArgs e) {
|
---|
283 | Content.PreprocessingDataManipulation.ReplaceIndicesByLinearInterpolationOfNeighbours(GetSelectedCells());
|
---|
284 | }
|
---|
285 | }
|
---|
286 | }
|
---|