#region License Information
/* HeuristicLab
* Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using HeuristicLab.DataImporter.DbExplorer.Interfaces;
namespace HeuristicLab.DataImporter.DataProcessor {
public partial class DbTablesView : Form {
private enum GridColumnsIndex {
Selected = 2,
Filtered = 3,
MinValue = 4,
MaxValue = 5,
LikeValue = 6
}
protected IEnumerable Tables {
get {
return (IEnumerable)this.listTables.DataSource;
}
}
public IEnumerable SelectedTables {
get {
return Tables.Where(x => x.IsSelected);
}
}
private IDbExplorer dbExplorer;
public IDbExplorer DbExplorer {
get { return this.dbExplorer; }
set { this.dbExplorer = value; }
}
private DataGridViewCellStyle inactiveStyle;
public DbTablesView() {
InitializeComponent();
inactiveStyle = new DataGridViewCellStyle();
inactiveStyle.BackColor = Color.DarkGray;
this.listTables.DrawMode = DrawMode.OwnerDrawFixed;
this.listTables.DrawItem += new DrawItemEventHandler(this.listTables_DrawItem);
this.gridColumns.CellContentClick += new DataGridViewCellEventHandler(gridColumns_CellContentClick);
this.gridColumns.CellContentDoubleClick += new DataGridViewCellEventHandler(gridColumns_CellContentClick);
this.gridColumns.CellEndEdit += new DataGridViewCellEventHandler(gridColumns_CellEndEdit);
this.gridColumns.CellValidating += new DataGridViewCellValidatingEventHandler(gridColumns_CellValidating);
}
public void SetTables(IEnumerable tables) {
this.listTables.DataSource = tables;
}
private void btnClearSelection_Click(object sender, EventArgs e) {
foreach (DbTable table in this.Tables) {
if (table.IsSelected)
table.ClearSelection();
}
listTables.Invalidate();
gridColumns.Invalidate();
}
private void selectAllColumnsToolStripMenuItem_Click(object sender, EventArgs e) {
DbTable table = (DbTable)listTables.SelectedItem;
gridColumns.ClearSelection();
table.SelectAllColumns();
//needed for correct update of GUI
gridColumns.DataSource = null;
gridColumns.DataSource = table.Columns;
for (int i = 0; i < gridColumns.RowCount; i++) {
if (dbExplorer.IsFilterableDataType(table.Columns.ElementAt(i).SqlDataType)) {
ChangeAppearanceOfRow(i);
}
}
dbExplorer.FillAffectedRowsForTable(table);
this.lblAffectedRows.Text = "Affected rows: " + table.AffectedRows;
listTables.Refresh();
gridColumns.Refresh();
}
private void deselectAllColumnsToolStripMenuItem_Click(object sender, EventArgs e) {
DbTable table = (DbTable)listTables.SelectedItem;
gridColumns.ClearSelection();
table.ClearSelection();
//needed for correct update of GUI
gridColumns.DataSource = null;
gridColumns.DataSource = table.Columns;
for (int i = 0; i < gridColumns.RowCount; i++)
ChangeAppearanceOfRow(i);
dbExplorer.FillAffectedRowsForTable(table);
this.lblAffectedRows.Text = "Affected rows: " + table.AffectedRows;
listTables.Refresh();
gridColumns.Refresh();
}
private void DataTablesForm_Shown(object sender, EventArgs e) {
//needed because otherwise the table is not painted correctly
UpdateGridColumns();
}
private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e) {
this.listTables.Invalidate();
}
#region ListView events
private void listTables_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Right) {
listTables.SelectedIndex = listTables.IndexFromPoint(e.Location);
}
}
private void listTables_DrawItem(object sender, DrawItemEventArgs e) {
e.DrawBackground();
Brush brush = Brushes.Black;
DbTable actTable = (DbTable)listTables.Items[e.Index];
if (actTable.IsSelected)
brush = Brushes.Red;
if (((e.State & DrawItemState.Focus) == DrawItemState.Focus) ||
((e.State & DrawItemState.Selected) == DrawItemState.Selected))
brush = Brushes.White;
e.Graphics.DrawString(actTable.ToString(), e.Font, brush, e.Bounds, StringFormat.GenericDefault);
e.DrawFocusRectangle();
}
private void listTables_SelectedIndexChanged(object sender, EventArgs e) {
UpdateGridColumns();
}
#endregion
#region DataGridView events
private void gridColumns_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
if (e.RowIndex > -1) {
if (e.ColumnIndex > 2 && this.gridColumns[e.ColumnIndex, e.RowIndex].ReadOnly) {
e.PaintBackground(e.CellBounds, false);
if (e.ColumnIndex == 3) {
Rectangle r = e.CellBounds;
r.Width = 13;
r.Height = 13;
r.X += e.CellBounds.Width / 2 - 7;
r.Y += e.CellBounds.Height / 2 - 7;
ControlPaint.DrawCheckBox(e.Graphics, r, ButtonState.Inactive);
}
e.Handled = true;
}
}
}
private void gridColumns_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
if (e.ColumnIndex < 4 || !gridColumns.IsCurrentCellDirty)
return;
DbTable table = ((DbTable)this.listTables.SelectedItem);
if (e.RowIndex >= table.Columns.Count())
return;
DbColumn col = table.Columns.ElementAt(e.RowIndex);
double doubleVal;
DateTime dateVal;
bool cancel = false;
if (dbExplorer.IsNumericDataType(col.SqlDataType)) {
if (!Double.TryParse(e.FormattedValue.ToString(), out doubleVal))
cancel = true;
} else if (dbExplorer.IsDateDataType(col.SqlDataType)) {
if (!DateTime.TryParse(e.FormattedValue.ToString(), out dateVal))
cancel = true;
}
if (cancel) {
e.Cancel = true;
MessageBox.Show("Please enter a " + col.SqlDataType + " value!");
}
}
private void gridColumns_CellEndEdit(object cender, DataGridViewCellEventArgs e) {
if (e.ColumnIndex < 4)
return;
DbTable table = ((DbTable)this.listTables.SelectedItem);
if (e.RowIndex >= table.Columns.Count())
return;
DbColumn col = table.Columns.ElementAt(e.RowIndex);
if (e.ColumnIndex == 4)
col.MinValue = gridColumns.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
else if (e.ColumnIndex == 5)
col.MaxValue = gridColumns.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
else if (e.ColumnIndex == 6)
col.LikeValue = gridColumns.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null ? "" :
gridColumns.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
dbExplorer.FillAffectedRowsForTable(table);
lblAffectedRows.Text = "Affected rows: " + table.AffectedRows;
}
private void gridColumns_CellContentClick(object sender, DataGridViewCellEventArgs e) {
if (e.ColumnIndex == 2 || e.ColumnIndex == 3) {
gridColumns.EndEdit();
gridColumns.ClearSelection();
DbTable table = (DbTable)this.listTables.SelectedItem;
DbColumn col = table.Columns.ElementAt(e.RowIndex);
if (e.ColumnIndex == 3 || col.FilterColumn) {
this.Cursor = Cursors.WaitCursor;
if ((col.MinValue == null || col.MaxValue == null) &&
dbExplorer.IsFilterableDataType(col.SqlDataType) && !dbExplorer.IsStringDataType(col.SqlDataType))
dbExplorer.FillMinMaxValuesForTable(table);
dbExplorer.FillAffectedRowsForTable(table);
this.Cursor = Cursors.Default;
}
ChangeAppearanceOfRow(e.RowIndex);
this.listTables.Invalidate();
this.gridColumns.Invalidate();
lblAffectedRows.Text = "Affected rows: " + table.AffectedRows;
}
}
private void ChangeAppearanceOfRow(int rowIndex) {
DbTable table = (DbTable)this.listTables.SelectedItem;
DbColumn col = table.Columns.ElementAt(rowIndex);
if (!col.Selected) {
col.FilterColumn = false;
foreach (int i in Enum.GetValues(typeof(GridColumnsIndex)))
ChangeAppearanceOfCell(i, rowIndex, i == (int)GridColumnsIndex.Selected);
} else {
if (dbExplorer.IsFilterableDataType(col.SqlDataType)) {
ChangeAppearanceOfCell((int)GridColumnsIndex.Filtered, rowIndex, true);
if (col.FilterColumn) {
if (dbExplorer.IsStringDataType(col.SqlDataType)) {
ChangeAppearanceOfCell((int)GridColumnsIndex.MinValue, rowIndex, false);
ChangeAppearanceOfCell((int)GridColumnsIndex.MaxValue, rowIndex, false);
ChangeAppearanceOfCell((int)GridColumnsIndex.LikeValue, rowIndex, true);
} else {
ChangeAppearanceOfCell((int)GridColumnsIndex.MinValue, rowIndex, true);
ChangeAppearanceOfCell((int)GridColumnsIndex.MaxValue, rowIndex, true);
ChangeAppearanceOfCell((int)GridColumnsIndex.LikeValue, rowIndex, false);
}
} else {
ChangeAppearanceOfCell((int)GridColumnsIndex.MinValue, rowIndex, false);
ChangeAppearanceOfCell((int)GridColumnsIndex.MaxValue, rowIndex, false);
ChangeAppearanceOfCell((int)GridColumnsIndex.LikeValue, rowIndex, false);
}
}
}
}
private void ChangeAppearanceOfCell(int columnIndex, int rowIndex, bool active) {
if (active) {
gridColumns[columnIndex, rowIndex].ReadOnly = false;
gridColumns[columnIndex, rowIndex].Style = null;
} else {
gridColumns[columnIndex, rowIndex].ReadOnly = true;
gridColumns[columnIndex, rowIndex].Style = this.inactiveStyle;
}
}
private void UpdateGridColumns() {
DbTable actTable = (DbTable)this.listTables.SelectedItem;
if (actTable != null) {
gridColumns.EndEdit();
gridColumns.DataSource = actTable.Columns;
this.Cursor = Cursors.WaitCursor;
try {
dbExplorer.FillMinMaxValuesForTable(actTable);
dbExplorer.FillAffectedRowsForTable(actTable);
this.lblRows.Text = "Total rows: " + actTable.TotalRows;
this.lblAffectedRows.Text = "Affected rows: " + actTable.AffectedRows;
for (int i = 0; i < actTable.Columns.Count(); i++)
ChangeAppearanceOfRow(i);
this.gridColumns.Invalidate();
}
finally {
this.Cursor = Cursors.Default;
}
}
}
#endregion
}
}