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