using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Mesnac.Basic.Service
{
///
/// UndoRedo服务类
///
public class UndoRedoService
{
#region 字段定义
private static Dictionary _dicControlEventHandler = new Dictionary();
#endregion
#region 属性定义
///
/// Undo操作栈
///
public static Stack UndoStack = new Stack();
///
/// Redo操作栈
///
public static Stack RedoStack = new Stack();
///
/// 控件事件处理绑定集合
///
public static Dictionary DicControlEventHandler
{
get
{
return _dicControlEventHandler;
}
}
#endregion
#region Undo Redo服务初始化
///
/// Undo Redo服务初始化
///
/// 要注册UndoRedo服务的控件集合
public static void Init(List controls)
{
Clear();
EventHandlerProcessor ehp = null;
foreach (Control ctl in controls)
{
if (ctl is TextBox)
{
ehp = new EventHandlerProcessor((ctl as TextBox).Text);
_dicControlEventHandler.Add(ctl, ehp);
(ctl as TextBox).LostFocus -= ehp.TextBox_LostFocus;
(ctl as TextBox).LostFocus += ehp.TextBox_LostFocus;
}
if (ctl is CheckBox)
{
ehp = new EventHandlerProcessor((ctl as CheckBox).Checked);
_dicControlEventHandler.Add(ctl, ehp);
(ctl as CheckBox).CheckedChanged -= ehp.CheckBox_CheckedChanged;
(ctl as CheckBox).CheckedChanged += ehp.CheckBox_CheckedChanged;
}
if (ctl is ComboBox)
{
ehp = new EventHandlerProcessor((ctl as ComboBox).SelectedItem);
_dicControlEventHandler.Add(ctl, ehp);
(ctl as ComboBox).SelectedIndexChanged -= ehp.ComboBox_SelectedIndexChanged;
(ctl as ComboBox).SelectedIndexChanged += ehp.ComboBox_SelectedIndexChanged;
}
if (ctl is DateTimePicker)
{
ehp = new EventHandlerProcessor((ctl as DateTimePicker).Value);
_dicControlEventHandler.Add(ctl, ehp);
(ctl as DateTimePicker).ValueChanged -= ehp.DateTimePicker_ValueChanged;
(ctl as DateTimePicker).ValueChanged += ehp.DateTimePicker_ValueChanged;
}
if (ctl is DataGridView)
{
DataTable dtOld = Mesnac.Basic.DataProcessor.GetDataTableFromGridView((ctl as DataGridView));
ehp = new EventHandlerProcessor(dtOld);
_dicControlEventHandler.Add(ctl, ehp);
(ctl as DataGridView).CellValueChanged -= ehp.DataGridView_CellValueChanged;
(ctl as DataGridView).CellValueChanged += ehp.DataGridView_CellValueChanged;
}
}
}
#endregion
#region 清除操作栈
///
/// 清除操作栈
///
public static void Clear()
{
foreach(Control ctl in _dicControlEventHandler.Keys)
{
if (ctl is TextBox)
{
(ctl as TextBox).LostFocus -= _dicControlEventHandler[ctl].TextBox_LostFocus;
}
if (ctl is CheckBox)
{
(ctl as CheckBox).CheckedChanged -= _dicControlEventHandler[ctl].CheckBox_CheckedChanged;
}
if (ctl is ComboBox)
{
(ctl as ComboBox).SelectedIndexChanged -= _dicControlEventHandler[ctl].ComboBox_SelectedIndexChanged;
}
if (ctl is DateTimePicker)
{
(ctl as DateTimePicker).ValueChanged -= _dicControlEventHandler[ctl].DateTimePicker_ValueChanged;
}
if (ctl is DataGridView)
{
(ctl as DataGridView).CellValueChanged -= _dicControlEventHandler[ctl].DataGridView_CellValueChanged;
}
}
_dicControlEventHandler.Clear();
UndoStack.Clear();
RedoStack.Clear();
}
#endregion
}
}