#region GNU notice
// GridProcessor - Search and replace abilities for the .NET DataGridView
// Copyright (C) 2007, by Evan Stein
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion GNU notice
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace GridProcessor
{
public partial class FormLoadSplitExpression : Form
{
private Split mSplit = new Split();
private int mSplitID = -1;
private Dictionary<string, DataGridViewColumn> mGridColumns;
private Settings mSettings;
/// <summary>
/// Loads a Split object into the workspace
/// </summary>
/// <param name="gridColumns">Column reference for converting heading text to indices</param>
/// <param name="settings">Database connection information</param>
public FormLoadSplitExpression(Dictionary<string, DataGridViewColumn> gridColumns, Settings settings)
{
InitializeComponent();
mSettings = settings;
mGridColumns = gridColumns;
}
private void FormLoadSplitExpression_Load(object sender, EventArgs e)
{
oleDbConnection1.ConnectionString = mSettings.ConnectionString;
try
{
oleDbDataAdapter_gridProcess.Fill(this.dataSetGridProcess1, "GridProcess");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Data error", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.DialogResult = DialogResult.Cancel;
this.Hide();
return;
}
SetEditButtons(bindingSource_gridProcess.Count > 0);
}
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
Util.GridMouseDown(dataGridView1, sender, e);
}
private void button_ok_Click(object sender, EventArgs e)
{
mSplit = Util.SplitFactory(mSplitID, mGridColumns, mSettings);
this.DialogResult = DialogResult.OK;
this.Hide();
}
private void button_rename_Click(object sender, EventArgs e)
{
RenameSplit();
}
private void button_delete_Click(object sender, EventArgs e)
{
DeleteSplit();
}
private void button_cancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Hide();
}
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0)
return;
mSplit = Util.SplitFactory(mSplitID, mGridColumns, mSettings);
this.DialogResult = DialogResult.OK;
this.Hide();
}
/// <summary>
/// Disable items associated with editing if there's no data
/// </summary>
/// <param name="hasRows"></param>
private void SetEditButtons(bool hasRows)
{
button_delete.Enabled = hasRows;
button_rename.Enabled = hasRows;
}
private bool RenameSplit()
{
DataRowView drv;
int foundIndex;
string currentName, newName = string.Empty;
// currentFilter,
FormNewName f;
DialogResult result;
drv = (DataRowView)bindingSource_gridProcess.Current;
currentName = drv["Name"].ToString();
// Ask the user what replacement name they want
f = new FormNewName(currentName);
result = f.ShowDialog(this);
if (result == DialogResult.OK)
newName = f.NewName;
f.Close();
if (result != DialogResult.OK)
return false;
// A bit of validation
if (newName.Length == 0)
{
MessageBox.Show(this, "The new name can't be blank.",
"Rename Expression", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return false;
}
if (currentName == newName)
{
MessageBox.Show(this, "The new name is the same as the old one.",
"Rename Expression", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return false;
}
// Attempt to find an existing item with the same name
foundIndex = bindingSource_gridProcess.Find("Name", newName);
if (foundIndex >= 0)
{
MessageBox.Show(this, "The new name already exists.",
"Rename Replacement", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return false;
}
// Finally, if all goes well ... replace name
// update the underlying row, rather than the view
drv.Row["Name"] = newName;
// Update the database
try
{
oleDbDataAdapter_gridProcess.Update(dataSetGridProcess1, "GridProcess");
}
catch (Exception ex)
{
MessageBox.Show(this, "Update error: " + ex.Message,
"Rename Replacement", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return false;
}
// mReplacement.Name = newName;
dataGridView1.Invalidate();
return true;
}
private void DeleteSplit()
{
DataRowView drv;
DialogResult result;
int processID;
result = MessageBox.Show(this, "Delete this split?",
"Delete Split", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result != DialogResult.Yes)
return;
drv = (DataRowView)bindingSource_gridProcess.Current;
processID = Convert.ToInt32(drv["GridProcessID"]);
if (Util.DeleteGridProcess(processID, mSettings))
drv.Delete();
}
/// <summary>
/// The chosen Split object
/// </summary>
public Split Split
{
get { return mSplit; }
}
private void bindingSource_gridProcess_CurrentChanged(object sender, EventArgs e)
{
DataRowView drv;
drv = (DataRowView)bindingSource_gridProcess.Current;
mSplitID = Convert.ToInt32(drv["GridProcessID"]);
}
private void renameToolStripMenuItem_Click(object sender, EventArgs e)
{
RenameSplit();
}
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
DeleteSplit();
}
}
}