1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.ComponentModel;
|
---|
25 | using System.Data;
|
---|
26 | using System.Drawing;
|
---|
27 | using System.Linq;
|
---|
28 | using System.Text;
|
---|
29 | using System.Windows.Forms;
|
---|
30 | using HeuristicLab.DataImporter.DbExplorer.Interfaces;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.DataImporter.DataProcessor {
|
---|
33 | public partial class DbTablesView : Form {
|
---|
34 | private enum GridColumnsIndex {
|
---|
35 | Selected = 2,
|
---|
36 | Filtered = 3,
|
---|
37 | MinValue = 4,
|
---|
38 | MaxValue = 5,
|
---|
39 | LikeValue = 6
|
---|
40 | }
|
---|
41 |
|
---|
42 | protected IEnumerable<DbTable> Tables {
|
---|
43 | get {
|
---|
44 | return (IEnumerable<DbTable>)this.listTables.DataSource;
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public IEnumerable<DbTable> SelectedTables {
|
---|
49 | get {
|
---|
50 | return Tables.Where(x => x.IsSelected);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | private IDbExplorer dbExplorer;
|
---|
55 | public IDbExplorer DbExplorer {
|
---|
56 | get { return this.dbExplorer; }
|
---|
57 | set { this.dbExplorer = value; }
|
---|
58 | }
|
---|
59 |
|
---|
60 | private DataGridViewCellStyle inactiveStyle;
|
---|
61 |
|
---|
62 | public DbTablesView() {
|
---|
63 | InitializeComponent();
|
---|
64 | inactiveStyle = new DataGridViewCellStyle();
|
---|
65 | inactiveStyle.BackColor = Color.DarkGray;
|
---|
66 | this.listTables.DrawMode = DrawMode.OwnerDrawFixed;
|
---|
67 | this.listTables.DrawItem += new DrawItemEventHandler(this.listTables_DrawItem);
|
---|
68 | this.gridColumns.CellContentClick += new DataGridViewCellEventHandler(gridColumns_CellContentClick);
|
---|
69 | this.gridColumns.CellContentDoubleClick += new DataGridViewCellEventHandler(gridColumns_CellContentClick);
|
---|
70 | this.gridColumns.CellEndEdit += new DataGridViewCellEventHandler(gridColumns_CellEndEdit);
|
---|
71 | this.gridColumns.CellValidating += new DataGridViewCellValidatingEventHandler(gridColumns_CellValidating);
|
---|
72 |
|
---|
73 | }
|
---|
74 |
|
---|
75 | public void SetTables(IEnumerable<DbTable> tables) {
|
---|
76 | this.listTables.DataSource = tables;
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | private void btnClearSelection_Click(object sender, EventArgs e) {
|
---|
81 | foreach (DbTable table in this.Tables) {
|
---|
82 | if (table.IsSelected)
|
---|
83 | table.ClearSelection();
|
---|
84 | }
|
---|
85 | listTables.Invalidate();
|
---|
86 | gridColumns.Invalidate();
|
---|
87 | }
|
---|
88 |
|
---|
89 | private void selectAllColumnsToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
90 | DbTable table = (DbTable)listTables.SelectedItem;
|
---|
91 | gridColumns.ClearSelection();
|
---|
92 | table.SelectAllColumns();
|
---|
93 | //needed for correct update of GUI
|
---|
94 | gridColumns.DataSource = null;
|
---|
95 | gridColumns.DataSource = table.Columns;
|
---|
96 | for (int i = 0; i < gridColumns.RowCount; i++) {
|
---|
97 | if (dbExplorer.IsFilterableDataType(table.Columns.ElementAt(i).SqlDataType)) {
|
---|
98 | ChangeAppearanceOfRow(i);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | dbExplorer.FillAffectedRowsForTable(table);
|
---|
102 | this.lblAffectedRows.Text = "Affected rows: " + table.AffectedRows;
|
---|
103 | listTables.Refresh();
|
---|
104 | gridColumns.Refresh();
|
---|
105 | }
|
---|
106 |
|
---|
107 | private void deselectAllColumnsToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
108 | DbTable table = (DbTable)listTables.SelectedItem;
|
---|
109 | gridColumns.ClearSelection();
|
---|
110 | table.ClearSelection();
|
---|
111 | //needed for correct update of GUI
|
---|
112 | gridColumns.DataSource = null;
|
---|
113 | gridColumns.DataSource = table.Columns;
|
---|
114 | for (int i = 0; i < gridColumns.RowCount; i++)
|
---|
115 | ChangeAppearanceOfRow(i);
|
---|
116 |
|
---|
117 | dbExplorer.FillAffectedRowsForTable(table);
|
---|
118 | this.lblAffectedRows.Text = "Affected rows: " + table.AffectedRows;
|
---|
119 | listTables.Refresh();
|
---|
120 | gridColumns.Refresh();
|
---|
121 | }
|
---|
122 |
|
---|
123 | private void DataTablesForm_Shown(object sender, EventArgs e) {
|
---|
124 | //needed because otherwise the table is not painted correctly
|
---|
125 | UpdateGridColumns();
|
---|
126 | }
|
---|
127 |
|
---|
128 | private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e) {
|
---|
129 | this.listTables.Invalidate();
|
---|
130 | }
|
---|
131 |
|
---|
132 | #region ListView events
|
---|
133 | private void listTables_MouseDown(object sender, MouseEventArgs e) {
|
---|
134 | if (e.Button == MouseButtons.Right) {
|
---|
135 | listTables.SelectedIndex = listTables.IndexFromPoint(e.Location);
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | private void listTables_DrawItem(object sender, DrawItemEventArgs e) {
|
---|
140 | e.DrawBackground();
|
---|
141 | Brush brush = Brushes.Black;
|
---|
142 | DbTable actTable = (DbTable)listTables.Items[e.Index];
|
---|
143 | if (actTable.IsSelected)
|
---|
144 | brush = Brushes.Red;
|
---|
145 | if (((e.State & DrawItemState.Focus) == DrawItemState.Focus) ||
|
---|
146 | ((e.State & DrawItemState.Selected) == DrawItemState.Selected))
|
---|
147 | brush = Brushes.White;
|
---|
148 |
|
---|
149 | e.Graphics.DrawString(actTable.ToString(), e.Font, brush, e.Bounds, StringFormat.GenericDefault);
|
---|
150 | e.DrawFocusRectangle();
|
---|
151 | }
|
---|
152 |
|
---|
153 | private void listTables_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
154 | UpdateGridColumns();
|
---|
155 | }
|
---|
156 | #endregion
|
---|
157 |
|
---|
158 | #region DataGridView events
|
---|
159 | private void gridColumns_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
|
---|
160 | if (e.RowIndex > -1) {
|
---|
161 | if (e.ColumnIndex > 2 && this.gridColumns[e.ColumnIndex, e.RowIndex].ReadOnly) {
|
---|
162 | e.PaintBackground(e.CellBounds, false);
|
---|
163 | if (e.ColumnIndex == 3) {
|
---|
164 | Rectangle r = e.CellBounds;
|
---|
165 | r.Width = 13;
|
---|
166 | r.Height = 13;
|
---|
167 | r.X += e.CellBounds.Width / 2 - 7;
|
---|
168 | r.Y += e.CellBounds.Height / 2 - 7;
|
---|
169 | ControlPaint.DrawCheckBox(e.Graphics, r, ButtonState.Inactive);
|
---|
170 | }
|
---|
171 | e.Handled = true;
|
---|
172 | }
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | private void gridColumns_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
|
---|
177 | if (e.ColumnIndex < 4 || !gridColumns.IsCurrentCellDirty)
|
---|
178 | return;
|
---|
179 | DbTable table = ((DbTable)this.listTables.SelectedItem);
|
---|
180 | if (e.RowIndex >= table.Columns.Count())
|
---|
181 | return;
|
---|
182 |
|
---|
183 | DbColumn col = table.Columns.ElementAt(e.RowIndex);
|
---|
184 | double doubleVal;
|
---|
185 | DateTime dateVal;
|
---|
186 | bool cancel = false;
|
---|
187 | if (dbExplorer.IsNumericDataType(col.SqlDataType)) {
|
---|
188 | if (!Double.TryParse(e.FormattedValue.ToString(), out doubleVal))
|
---|
189 | cancel = true;
|
---|
190 | } else if (dbExplorer.IsDateDataType(col.SqlDataType)) {
|
---|
191 | if (!DateTime.TryParse(e.FormattedValue.ToString(), out dateVal))
|
---|
192 | cancel = true;
|
---|
193 | }
|
---|
194 |
|
---|
195 | if (cancel) {
|
---|
196 | e.Cancel = true;
|
---|
197 | MessageBox.Show("Please enter a " + col.SqlDataType + " value!");
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | private void gridColumns_CellEndEdit(object cender, DataGridViewCellEventArgs e) {
|
---|
202 | if (e.ColumnIndex < 4)
|
---|
203 | return;
|
---|
204 |
|
---|
205 | DbTable table = ((DbTable)this.listTables.SelectedItem);
|
---|
206 | if (e.RowIndex >= table.Columns.Count())
|
---|
207 | return;
|
---|
208 |
|
---|
209 | DbColumn col = table.Columns.ElementAt(e.RowIndex);
|
---|
210 | if (e.ColumnIndex == 4)
|
---|
211 | col.MinValue = gridColumns.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
|
---|
212 | else if (e.ColumnIndex == 5)
|
---|
213 | col.MaxValue = gridColumns.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
|
---|
214 | else if (e.ColumnIndex == 6)
|
---|
215 | col.LikeValue = gridColumns.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null ? "" :
|
---|
216 | gridColumns.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
|
---|
217 | dbExplorer.FillAffectedRowsForTable(table);
|
---|
218 | lblAffectedRows.Text = "Affected rows: " + table.AffectedRows;
|
---|
219 | }
|
---|
220 |
|
---|
221 | private void gridColumns_CellContentClick(object sender, DataGridViewCellEventArgs e) {
|
---|
222 | if (e.ColumnIndex == 2 || e.ColumnIndex == 3) {
|
---|
223 | gridColumns.EndEdit();
|
---|
224 | gridColumns.ClearSelection();
|
---|
225 | DbTable table = (DbTable)this.listTables.SelectedItem;
|
---|
226 | DbColumn col = table.Columns.ElementAt(e.RowIndex);
|
---|
227 |
|
---|
228 | if (e.ColumnIndex == 3 || col.FilterColumn) {
|
---|
229 | this.Cursor = Cursors.WaitCursor;
|
---|
230 | if ((col.MinValue == null || col.MaxValue == null) &&
|
---|
231 | dbExplorer.IsFilterableDataType(col.SqlDataType) && !dbExplorer.IsStringDataType(col.SqlDataType))
|
---|
232 | dbExplorer.FillMinMaxValuesForTable(table);
|
---|
233 | dbExplorer.FillAffectedRowsForTable(table);
|
---|
234 | this.Cursor = Cursors.Default;
|
---|
235 | }
|
---|
236 |
|
---|
237 | ChangeAppearanceOfRow(e.RowIndex);
|
---|
238 | this.listTables.Invalidate();
|
---|
239 | this.gridColumns.Invalidate();
|
---|
240 | lblAffectedRows.Text = "Affected rows: " + table.AffectedRows;
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | private void ChangeAppearanceOfRow(int rowIndex) {
|
---|
245 | DbTable table = (DbTable)this.listTables.SelectedItem;
|
---|
246 | DbColumn col = table.Columns.ElementAt(rowIndex);
|
---|
247 |
|
---|
248 | if (!col.Selected) {
|
---|
249 | col.FilterColumn = false;
|
---|
250 | foreach (int i in Enum.GetValues(typeof(GridColumnsIndex)))
|
---|
251 | ChangeAppearanceOfCell(i, rowIndex, i == (int)GridColumnsIndex.Selected);
|
---|
252 | } else {
|
---|
253 | if (dbExplorer.IsFilterableDataType(col.SqlDataType)) {
|
---|
254 | ChangeAppearanceOfCell((int)GridColumnsIndex.Filtered, rowIndex, true);
|
---|
255 | if (col.FilterColumn) {
|
---|
256 | if (dbExplorer.IsStringDataType(col.SqlDataType)) {
|
---|
257 | ChangeAppearanceOfCell((int)GridColumnsIndex.MinValue, rowIndex, false);
|
---|
258 | ChangeAppearanceOfCell((int)GridColumnsIndex.MaxValue, rowIndex, false);
|
---|
259 | ChangeAppearanceOfCell((int)GridColumnsIndex.LikeValue, rowIndex, true);
|
---|
260 | } else {
|
---|
261 | ChangeAppearanceOfCell((int)GridColumnsIndex.MinValue, rowIndex, true);
|
---|
262 | ChangeAppearanceOfCell((int)GridColumnsIndex.MaxValue, rowIndex, true);
|
---|
263 | ChangeAppearanceOfCell((int)GridColumnsIndex.LikeValue, rowIndex, false);
|
---|
264 | }
|
---|
265 | } else {
|
---|
266 | ChangeAppearanceOfCell((int)GridColumnsIndex.MinValue, rowIndex, false);
|
---|
267 | ChangeAppearanceOfCell((int)GridColumnsIndex.MaxValue, rowIndex, false);
|
---|
268 | ChangeAppearanceOfCell((int)GridColumnsIndex.LikeValue, rowIndex, false);
|
---|
269 | }
|
---|
270 | }
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | private void ChangeAppearanceOfCell(int columnIndex, int rowIndex, bool active) {
|
---|
275 | if (active) {
|
---|
276 | gridColumns[columnIndex, rowIndex].ReadOnly = false;
|
---|
277 | gridColumns[columnIndex, rowIndex].Style = null;
|
---|
278 | } else {
|
---|
279 | gridColumns[columnIndex, rowIndex].ReadOnly = true;
|
---|
280 | gridColumns[columnIndex, rowIndex].Style = this.inactiveStyle;
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | private void UpdateGridColumns() {
|
---|
285 | DbTable actTable = (DbTable)this.listTables.SelectedItem;
|
---|
286 | if (actTable != null) {
|
---|
287 | gridColumns.EndEdit();
|
---|
288 | gridColumns.DataSource = actTable.Columns;
|
---|
289 | this.Cursor = Cursors.WaitCursor;
|
---|
290 |
|
---|
291 | try {
|
---|
292 | dbExplorer.FillMinMaxValuesForTable(actTable);
|
---|
293 | dbExplorer.FillAffectedRowsForTable(actTable);
|
---|
294 |
|
---|
295 | this.lblRows.Text = "Total rows: " + actTable.TotalRows;
|
---|
296 | this.lblAffectedRows.Text = "Affected rows: " + actTable.AffectedRows;
|
---|
297 |
|
---|
298 | for (int i = 0; i < actTable.Columns.Count(); i++)
|
---|
299 | ChangeAppearanceOfRow(i);
|
---|
300 |
|
---|
301 | this.gridColumns.Invalidate();
|
---|
302 | }
|
---|
303 | finally {
|
---|
304 | this.Cursor = Cursors.Default;
|
---|
305 | }
|
---|
306 | }
|
---|
307 | }
|
---|
308 | #endregion
|
---|
309 | }
|
---|
310 | }
|
---|